Demo
Created using blueprints (no coding needed)
Uses:
Blueprint Event Dispatchers
Blueprint Interface
Blueprint Custom Events
Custom Structure
Introduction:
Before jumping into explanation how to create a forest generator using blueprints we need to figure out how to randomly generate forest. First thing that comes to mind is just randomly pick location from spawn area and place tree there. But this solution creates lot of problems. All trees can spawn in one spot or create clumps, that means won’t be evenly distributed across area we want to populate. Trees can also spawn on another trees and we definitely want to avoid that behavior in our generator. To achieve this we can give a tree that we want to spawn it’s own area. To create that we divided our foliage area into a grid. In one grid cell we can only spawn one tree. By doing this, our forest from being created fully random now is fixed and dull and definitely does not look like a natural one. But on the other hand there is no longer a problem with colliding models. To put more life into the woods and to make them more natural we can use a simple trick: when tree is created it’s location is randomly chosen. But instead choosing the location from whole spawn area we use the range of a grid cell the tree was spawned. After we know the general idea how to create the forest lets jump to blueprints.
General plan how the job should be done:
Place Start Worker Blueprint into scene ⇒ Activate it ⇒ Start Worker generates grid and spawns first Worker ⇒ Start Forest activates Worker ⇒ Worker creates fixed number of trees ⇒ Worker notifies Start Worker that the job is done but forest is not finished yet ⇒ Start Worker creates another worker and activates it ⇒ repeat until Worker notify Start Worker that whole forest is created
By doing it this way whole forest won’t be created during one application tick, so if area to populate is large it won’t freeze the game.
Blueprints:
Custom Structure:
Custom structure was used to represent grid cell. It consists of three vectors representing top left corner, center, and bottom right corner of grid cell and a boolean value that will be helpful during spawning.
Start Worker Blueprint:
Variables to control the forest generation:
- Number of Trees: the amount of trees to spawn
- Tree Density: number from 0 to 1 controlling tree density (default 1 means blueprint will attempt to spawn amount of trees equal to Number of Trees variable)
- Radius: radius of foliage area that will be populated with trees
- Grid Cell Size: size of grid cell
- Tree Scale Min: minimum scale that can be applied to spawned tree
- Collision Range Trace: Range of collision trace
- Max Slope angle: max angle of slope that tree will spawn on
- Max Object for Worker: amount of trees that one worker can create during its lifetime
- Trees to Spawn: Static Mesh array holding model of trees to spawn.
- Tree Radius: array holding radius of trunk for tree that index from Trees to Spawn is corresponding to index from this array (sadly there is no map container in blueprints thats why we need two separate arrays to hold trees and radius of trunks)
Event graph:
The heart of the system. This is where all the calls are dispatched and workers are created.
Custom Event: Generate Grid:
This function is a starting function for foliage creator. For speed and overall performance this blueprint is using Static Mesh Instances, but the worker can spawn anything (actor, blueprint etc.). It creates as many Instanced Static Mesh Components as there are elements in Tree to Spawn array and the sets Static Mesh for them. When the component is created then it’s added to Instanced Trees array.
After this process next function is called:
Max Trees:
The purpose of this function is to determine the max amount of trees that can be created based on Radius and Grid Cell size array. It calculates how many rows / columns the grids will have and what is the amount of trees that the grid can hold. After that Number of Trees passed by user is its clamped between 0 and max and then multiplied by Tree Density.
Next call:
Create Grid:
It creates two arrays in loop fired up in another loop. It allows for easy grid creation. To calculate X coordinate it takes index from the first loop and multiplies it by a Grid Cell size Variable and then subtract a sum between Start Worker X coordinate and spawn area radius. Same with Y but index is taken from second loop. Z coordinate is not important in this process so we can skip it. This produces Top Left corner for Grid Cell. To get other two vectors X and Y coordinates subtract half of the Grid Cell Size. After the grid cell is prepared it is added to the Grid array. During this process Rand Grid array is also filled with values of indexes that Grid Cell were added. It will be needed later.
Next call:
Generate Forest:
This function calculates in which grid cell tress will be randomly planted. In a loop of range from 0 to Number of Trees an item from Rand Grid is randomly picked. Based on its value (representing index of a grid cell in Grid array) it changes the boolean value in Grid Cell to true. To avoid drawing this grid cell again, item from Rand Grid representing this grid cell is removed.
Next call:
Spawn Worker Custom Event:
This Event has an input node, that means when it’s called it will pass this value.
Passed value represents last index in a Grid array that was used to create a tree, so next worker knows where to start. When the event is called it spawns actor from passed class. Worker needs info about the work so additional variables are passed during it’s creation (how to achieve this will be explained later in worker section). After the spawn is completed events are binded to dispatchers in worker (also explained in worker section).
After creation is completed the call chain ends. To start a Worker, in every tick (Event Tick) the Start Forest gets all Actors that implements BPI_WorkerDispacher interface and calls Start Worker function. It will tell the Worker to start spawning trees.
Worker Blueprint:
Interface:
This will help to keep communication between Start Forest and Worker simple. Start Forest can simply take all blueprints that implements BPI_WorkerDispacher and call needed function on them.
More info: Blueprint interfaces
Event Dispatchers:
If event is binded to Event Dispatcher it will be executed when the Event Dispatcher is called. This allows for smooth communication and code execution between multiple blueprints. When Worker is created by Start Forest some of it’s custom events are binded to Event dispatchers in Worker blueprint. That means when the Worker will call an Event Dispatcher a event binded to this dispatcher will execute in Start Forest blueprint.
More info: Event Dispatchers
Expose Variables on spawn:
In variable details: editable and expose on spawn must be selected. This operation allows variables to be initiated when actor is spawned. It is used to tell the Worker where to start in Grid array, what trees can be spawned etc.
Functions:
Trace:
Simple function tracing against Landscape. Range of trace is calculated from the randomized spawn position and Collision Range Trace variable defined by user.
Check Slope Angle:
This Function does two things: calculates slope angle and check if tree can spawned (checks angles between a slope and angle defined by user)
Add Trees:
Function responsible for adding instances of trees to the map. It draws random tree and takes its radius. Based on this radius, function calculates safe distance that is used to find random location for spawn inside a grid cell.
After getting location to spawn Trace is called to get the Z coordinate. If Trace hits landscape, the slope is checked. Next, based on tree radius, slope angle, and Z coordinate of Start Forest actor (the owner of Instanced Static Mesh Component) Add Trees calculates how deep tree needs to be located on the landscape so it doesn’t partially float in the air. The rotation and scale is set randomly to add more life to created forest.
Event Graph:
When Start Worker is called the main loop in Worker (starting from the last Grid Cell used by previous Worker) it spawns trees using Add Trees and increases objects created counter. When the loop ends or if Worker creates its defined number of objects, Event Dispatchers are fired up, calling binded events from Start Worker like Spawn Worker or notification that whole forest was created and job is done.
Instructions:
- Place downloaded blueprints and structure in Game/Blueprints/ folder (/[Your_Game_Project]../Content/Blueprints)
- Add Start Worker to your map
- location where you put this actor is a center of foliage area that will be populated with trees
- trees will only spawn on landscape
- Initialize Control variables in Start Worker
- setting radius of foliage area too big and Grid Cell size too small may result in Runaway loop
- setting Max Objects for Worker too high might freeze application
- fill up Trees to Spawn array
- fill up Tree Radius array so first radius corresponds to first tree in Trees to Spawn array
- Prepare Level Blueprint
- Lunch the game and fire up event set up in Level Blueprint
Final thoughts:
With this base you can develop more complicated system. I manage to add 3 more “layers” to the forest, first one was trees, second bushes, third small props (small rocks etc.) and the last one was decals. Every layer creation worked almost the same like trees layer with some changes. For example props were rotated to match orientation of surface, and all objects had more randomization added to their transformation matrix.
Downloads:
Blueprints
Hey, just wanted to drop in and say thank you for providing such content! 🙂
Models and tutorials are really good, I’m sad to see there is 0 comments so far so I want to break that 🙂
Hope you stick with it,
eXtreme
We’re eXtremely glad to have first comment 🙂 Hope it’s just the beginning!
Cheers
Congratulations, man… Greatiful work, lovely! Saludo desde sao paulo, Brasil.
Hi there, this looks fantastic, yet the engine tells me Grid and Grid Cell are invalid types. I’m on UE 4.4
Thanks for your great work it is really helping me in learning unreal, your blog is now bookmarked 🙂 keep up the excellent work.
Thank you Moe!
This Blueprint is Great! Thanks for providing the assets as a download too.
thanks for this, ive been looking everywhere
Hi got a question, if for example I already have some meshes set up in the level then run this, would it take into consideration those meshes already in the map and spawn trees around it and not overlap?
This BP works only with landscape but it’s not a problem to check against objects collision (with box/sphere trace).
i have a problem many of tree spawn but are in the air and not snaped with the landscape
did you know a ways to snap it ?
also where the size of the tree coz now my tree is really small with the use of the blueprint
thanks for future answer
i found out the problem ^^
the actor was reversed and bad placed
and now all work fine
was my bad
thanks for your awesome work
Hello Darknightmare,
sorry I didn’t reply earlier. Glad you solved this out!
Cheers,
Michal
When I try to drag starterworker Bp actor on the level with newly created landscape I get this error
The following member variables in blueprint 'Blueprint /Game/StarterContent/Blueprints/StartWorker.StartWorker’ have invalid type. Would you like to remove them?
Grid
Hey Michal.
Thank you for your great Tutorial.
There are issues for using these BP in new version of UE.
for instance:
engine tells us Grid and Grid Cell are invalid types.
and when trying to open those BP we got this error:
The following member variables in blueprint ‚Blueprint /Game/StarterContent/Blueprints/StartWorker.StartWorker’ have invalid type. Would you like to remove them?
Grid
GridCell
Do you have any idea about these errors.
Thank you in advance.
Hello H.M,
I haven’t tested this BP in new version (4.6). Originally it was done in 4.2. However I tested this on 4.5 and worked fine. Anyway, I’m going to check this today/tomorrow and I’ll let you know.
Michal
Ok, I found out what’s the problem. You have to place all BPs into [YOUR_GAME_PROJECT]\Content\Blueprints\ folder (you need to create this folder either in UE Editor or in Explorer). As far as I can see you placed it to: [YOUR_GAME_PROJECT]\Content\StarterContent\Blueprints\
Michal
You have to place all BPs into [YOUR_GAME_PROJECT]\Content\Blueprints\ folder (you need to create this folder either in UE Editor or in Explorer). As far as I can see you placed it to: [YOUR_GAME_PROJECT]\Content\StarterContent\Blueprints\
Michal
Hey Michal.
Thank you very much for your help and you time. by moving them to main folder of My game, it’s working perfectly.
One more question, after stopping the game, all meshes disappear.
I replace it from level BP to Construction Script of Start workers in order to keep all of those meshes in designed level.
But it’s not working. Do you have any idea to keep them in level for reducing level design time ?
And also did you check it with other thinks for instance creating lots of BPs or particles or other actors?
Hello, thank you for this it is awesome! I’m trying to get this to work on a dedicated server with 4.6.1 to no avail. Ive tried the different replication settings, replicating different variables, RPC the start worker, start worker with authority switch, etc. I want the server to generate the trees in the same spot on all clients, I either end up with the trees only generating on the server and not on the clients or I get them generating on the clients but in different spots. Its driving me nuts, can anyone help me out please?
Ok that solved the error.One last question,For the level BP
How do I set up that StartForest from persistent level node?
Sorry if sounds stupid,I have just began learning about BPs and I couldn’t figure that out.
Is it a new variable? if so what type..I really want to make it work
Works but…
maybe only works with small landscapes.
Tried it in a 8129×8129 landscape in UE4.6 and only works for the first component selector taken from (0,0,0) world origin. You can move the worker inside this landscape section/quad but never outside it.
Tried with different options:
Radius:40.000
Grid cell size: 4096, 8129, 16292..
Nothing.
Beautifull for a tech-demo but not usefull for real game.
P.D. [YOUR_GAME_PROJECT]\Content\StarterContent\Blueprints\
After installed they can be moved to another folder. Seens to work.
Hello Bhbilbao,
BP works on 8k landscapes. However it kills your PC. Also, „runaway loop” happens when you want the forest to be more dense on such area.
Try following settings:
http://imgur.com/xGFchdp
I increased the size of the bush to 20, so it’s more visible.
It’s useless now – I agree, but you can always divide your level on smaller chunks and stream it. Here’s more info about world composition :https://docs.unrealengine.com/latest/INT/Engine/LevelStreaming/WorldBrowser/index.html
p.d Yeah, it’s because Engine controls references when you move assets within folder structure.
I tried with those settings and dont work.
The scattered objects are bigger but only covers the first section/quad of the landscape.
Using a streamed small 1024×1024 landscape with world composition probably works, but Im working in a 8K world composition.
I need something like this blueprint working because using the standard tools of UE4.6 and painting foliage over a 8K landscape are days and days of repeated work. Sometimes, when painting the foliage simply crashes the system.
The grid system in the blueprint is great but I suggest to modify the blueprint to work over black/white bitmaps (slopes) and taking account the standard UE landscape layers.
https://docs.unrealengine.com/latest/images/Engine/Landscape/Materials/HeightBlendProblem.jpg
https://docs.unrealengine.com/latest/images/Engine/Landscape/QuickStart/5/T_Landscape_Blend_Layer.jpg
Use all these settings:
Number of tress: 100000
Tree Density: 1.0
Radius: 400000
Grid Cell Size: 5000
Tree Scale Min: 20 (depends on your mesh)
Collision Range Trace: 7000
Tree Radius ->
0: 50
If you still have problem with 1 segment I don’t know what’s going on. I tested on 2 PCs and worked fine on 8K landscape.
Thanks for suggestion, but we don’t plan to modify this BP for now.
I don’t see a startforest blueprint anywhere. I have start worker, worker, BPI dispatch and gridcell. Am I supposed to create it?
I used old picture for instruction. Plug in the StartWorker.
This is an awesome blueprint system! I’ve gotten it to work however the materials don’t seem to be applied to the meshes that I use, am I doing something wrong or leaving something out?
Nevermind on that! I needed to enable it the Materials (Usage Instanced Static Mesh) to have it work!
Also, in your post you say, „For speed and overall performance this blueprint is using Static Mesh Instances, but the worker can spawn anything (actor, blueprint etc.).”
Is there a simple way of having this happen? This would be amazing if so!
Thanks for sharing your work, it really goes a long way for a level 🙂
thank you for not onl providign the files but also an indepth explaination!
Sorry, making loose your time Michal Orzelek.
I created a new empty-plain landscape in UE4.6 (not imported from worldmachine) and now your blueprint works 100%. Now it covers the whole landscape. The problem wasnt related to the settings. With previous map, there was an incomprensible issue that made the blueprint to work in extrange manner.
Thanks four your time.
I had that problem before.
Go to the objects material tab and under USAGE menu mark:
X Used with instanced Static Mesh.
importing .uassest will break the file so how a is it possible to import??
Could this work for making thick grass? The fill tool is disabled for landscapes right now and instead of filling my landscape with the instance brush i much rather use this. I have messed around but can not get the settings right. Any help would be awesome!
Hello, when I add meshes using this, they dont seem to have any collision so my character just runs through them, how can I fix that ?
Please forgive my for asking a big time NOOBIE question here. I had just started using UDK 4 and had run across this Blueprint. Will some one please help me with some better instructions on how to use this Blueprint. The video says to run the game and Fire the Blueprint. Hu?! How do I do that for one and second am I to make the links in the Blueprint as shown in the pic above? If so, which tab to I create that fire button in order for it to work? I see there are four tabs to work with. Again, will some one please help me with this? That be so Awesome! Thanks! 😀
A small update. I had finally figured how to get the Blueprint to work which is great. Now one small problem now. After I run the blueprint, it will place only 180 trees in one small area and the UDK 4 program will get so laggy it is hard to move or do any thing. How can I fix the settings where it will not be so laggy and the trees will fill in the Zone if possible? Do I need to add more markers and config them to work as I had the first? Also, is there a way that the tree will stay permanently rather than disappear after you press stop?
But when contemplating towards the perspective health, it is
valuable to acquire one pair of excellent ones in place of typical cups.
If you are a savvy online-shopper, you’ve done your research and tested online forums including Glassy Eye.
You’ll need to call the functions from the Construction Script instead of the Event Graph if you want the trees to be permanently placed within the level. As far as the tree count/area go, those should be editable values within the Blueprints. I haven’t downloaded this yet or else I’d be more specific.
As far as performance goes, I’d start by lowering your editor settings.
If you aren’t interested in the ability to spawn trees during gameplay and just want to use this for permanent level creation, I’d suggest checking out UE4’s built-in foliage generator in 4.8.
Thanks for this tutorial. I’d built my own system for this before finding this, and it was great to be able to compare our processes. The Grid creation process in particular was very helpful, as I was getting pretty frustrated by the lack of built-in support for 2/3D arrays!
When you spawn the meshes they don’t have collision. It may be a simple fix that I can’t find myself but how would you give them collision? I want my character to be able to interact with them ): Is is possible for them to have their base collision?
Your meshes need collision created in UE or eternal 3D app. The simplest way to create that is to use Static mesh editor, and click generate collision. (like there: https://www.youtube.com/watch?v=XLvJnFUk3Cc). It if doesn’t solve your issue, you need to modify the Blueprint and spawn blocking volume together with your meshes. Make sure your Player/Pawn has collisions as well.
Hi Michal, I’m really new to Unreal and I’m using it in a landscape representation research project. Your tree generator is really cool. I got your tutorial to work and now I’m trying to implement a function where the trees can be resized while the game is running. Just to test this out I’m trying to get a key hit to increase the scale. What I’ve designed so far does not work, do you have any ideas? I think the problem is all the trees are part of the spawnactor and not their own objects.
Here’s how I set it up: http://imgur.com/a/Ak6Go
Hi,
I am fairly new to Unreal Engine so I am not very experienced. Thus I have encountered a problem. I did as you said, I put the blueprints in the event folder, and put startworker on the map. Changed the variables but from there idk how to do it anymore. Whenever I run the game and the blueprint it doesn’t do anything. It might be because of step 4 which i don’t know what I am supposed to do. Can you help please?
Thanks,
Dear Pawel,
Do you know how to download meshes files from a repository, and show them on real time?
Hi bjahn. I have the same problem to start. could you please share your initial setup? when I fire the event it’s says „0 done” on the top right of the screen.
As far as the „Create Forest” function part goes, How is it supposed to look? Unfortunately I don’t quite understand it and don’t see a picture to serve as a guide lol..
Thanks a million to help me learn procedure generation in Unreal