Is there a way to automatically generate meshes within an area on a landscape?

Hi, I’m looking for a way to automatically generate meshes within a specified area on the landscape. By “automatically”, I mean generating the meshes via code based on provided vectors representing the area. Is this even possible?
For example, I would like to generate trees within an area by providing the FVectors representing the corners of the polygon which is the area. Imagine the foliage tool but automatically via code.

You did not explain it well enough for me to know what it is you really want to achieve, but assuming that you simply want to spawn an existing mesh asset onto the landscape then you can do that quite easily in run time.

Make a blueprint that will take your vectors (whatever you want them to define) and then (on begin play, since you can’t really add actors in construction script (well theoretically you can, as components of the said blueprint but that would be tricky solution)):

  1. Pick a point you want the actor to spawn (X and Y, skip the height)
  2. Trace vertically down for the landscape
  3. Get the impact location
  4. Spawn your actor there. If the actor’s pivot does is not at the mesh ground (feet) adjust accordingly
  5. Loop to point 1 till all the meshes are placed.

If that’s not what you wanted then explain more, please.

Hi, I’m so sorry for the lack of details. I have edited my question.
Unfortunately, I do not think the solution you provided works for my purpose because, instead of just generating a mesh onto a point on the landscape, the meshes have to fill up a specified area which can be quite big.

I’m more than certain that this is doable in code. Not that I’m able to do it, though. We’ve once did a blueprint that had a volume component that defined the area we wanted to do something similar with.

Can’t help you with the code, but if I were to do this in a blueprint, I’d just use the volume (you could go with 4 vectors but volume is nice because it visualizes the area you selected), then choose X,Y points within the volume and do what I typed earlier - so the tree falls onto landscape, no matter how it is sculpted. The tree will be the spawned actor. Then you check for density (trees / uu^2) or any other parameter you wish (which you have to calculate yourself of course) and that’s it - the loop condition. Each loop iteration gives 1 tree.

If you wish to do it on a bigger area, just increase the size of a volume. You can also use several bp’s with volumes to cover several areas. And if you’re worried that the volume may fall out of the landscape, just make sure you handle the trace error well.

I am quite positive that this is something achievable in BP’s. Unfortunately it’s gonna be too big to just do it here for you to visualize. Can’t help much more I’m afraid.

What do you mean by volume? Is that a class in UE4? Sorry, I’m really new to UE4. How should I choose the XY points? Also, how do I actually spawn an actor? I know how to do it manually (drag and drop), but how do I spawn one?

I’ll give you a quick explanation, but if you’re really new then check the documentation and wiki. Explaining the same things over and over again gets boring after some time.

Volume is a box, defined by 8 points by default (the shape can be more complex than a box but let’s keep it simple). It is a class that you can place in the world or use as a component for a blueprint that you’re creating. It defines an area in 3D world.

How should you choose the XY points? However you wish, depending on what outcome you want to achieve. If you want the trees to be perfectly placed in exact same distances from one another, then create an algorithm that checks how many trees should be in the given area (a public variable so you can tweak it in the editor and see the changes), calculate how many rows with how many trees there should be, divide the area that your volume defines by that number and you have the points.

I’d go for density (trees per surface that the volume actually defines) instead the actual tree number, so it’s easier to maintain it consistent between several volumes. But first things first, don’t do it all at once.

Or you could simply make it random from lower X coord of the volume to the higher X, same for Y. Then add a check, whether the currently randomed position is not too close to any existing one.

How do you spawn an actor during run-time? You simply drag a spawn actor node to the blueprint and choose the class you want it to be.

As I previously said, you may also want to do this all in the construction script so you can see the results without clicking ‘Play’ in the editor. You would need to use ‘Add Component’ node though.

If you’re really new to this, I highly advise against starting with something as complex. It’s not tough once you grasp the basics, but the basics is what you should start with to avoid nasty issues later on that come up from lack of understanding on how this stuff really works.

Good luck either way.

Thanks for the help! I’ll check out this box component soon.
I do understand that I should start off with the basics but I’m really short on time and it would definitely help if there are more tutorial and better documentation for UE4 in C++ code. The documentation for most of the classes I’m using barely has any description at all.
Oh well, enough whining, thanks so much for the help!