! UPDATE !
For those interested in a complete grass/tree, easy to setup system based on this tree and the grass system check out ‘** Critias Foliage System **’.
A big Unity issue is the lack of a decent tree system. Developing an open world game I had to solve it. Therefore my goal was a tree system that makes SpeedTrees usable.
! VIDEO TUTORIAL !
You can see a step-by-step setup video tutorial here and here.
! DEMO + SOURCE !
Download the demo here. The complete open-source demo project can be found here.
! ASSET STORE !
You can find the asset store version here.
! Version 0.9.5 Beta !
Features:
- Ability to automatically add ‘LODGroups’ to batched tree billboards
Upgrades:
- Optimized tree matrix copy
- Optimized cell managing visibility/distance checking
! Version 0.9.2 Beta !
Fixes:
- Y Terrain Offset Fix
- Tree shadow popping issue
Features:
- Support for 100k+ tress with collisions
- Painting trees on non-terrains
- Being able to completely remove the terrain after editing the trees
- Support for multiple tree types
- SpeedTree XML files NOT required
- CullingGroup API for cells
- Instancing support
- Tillable terrain support
- Billboard rotation
- Tree Y rotation
- Tree wind
- LOD System
- Animated SpeedTree style CrossFade
- Only SpeedTree supported
- Uber-fast billboard system with 400+ FPS at 200k+ trees
Research:
- Grid subdivisions for faster ‘distance’ & 'AABB testing
- Billboard wind animation
- Billboard color variation
- Adding support for non SpeedTree objects
- Uneven terrain sizes like 3600x3900
- Store/restore the trees from the managed terrains
Implementation details:
Tree mesh data counts for 3 LOD levels: 8000/6000/3000 8000/6000/3000 tris with uv/uv2/uv3/uv4/colors, just like SpeedTree.
Tree count: 300000 - Again, like the grass, it doesn’t matter. Only the trees around the player do
Terrain size: 10km x 10km
Big cell size: 2000 meters, 5 cells in length, 25 total cells
Before going into the system I will explain, in general, how it works. First we know that we can have a tree in two states. One is the 3D state and the other is the 2D state, a.k.a a billboard. We want to do ‘something’ that we can draw hundreds of thousands of billboard trees at hundreds of FPS. We know that Unity’s system is slow due to the fact that is batches 2D tree billboards at run-time. We want to get very very far from that approach. Another point that makes Unity slow is that having many trees as ‘objects’. So we see that even is you have 100k INVISIBLE trees it still goes slow. That is due to the fact that there is some kind of processing for each extra tree instance. We also want to get very far away from that.
So, the main ideas are:
- Build the 2D trees mesh at edit time. They will have each packed in the meshes extra UV channels all the instance data: world position, world rotation, world scale. The shader will also receive some extra billboard data retrieved from the ‘BillboardAsset’ item. Calculate all the world data in the vertex shader. Vertical billboards are supported with this approach.
- Invsibile 2D instances in the vertex shader when we get very close to the player so that we don’t have to regenerate the mesh data on the CPU
Some shader code:
float3 campos = _WorldSpaceCameraPos;
float3 pos = IN.texcoord1.xyz;
float dist = distance(pos, campos);
if (dist >= DISTANCE)
{ // Compute world positions }
else
{ // Do nothing, so that the billboard is invisible }
All those are generated at edit-time and are uber-fast at run-time. 100k billboard trees are rendered at 300+ FPS when all of them are billboards. And horizontal billboards are also supported.
- Store all tree instance data so that when a tree is visible render it properly
- Hold per-instance tree data in order to be able to calculate on the CPU the LOD level, when it has to become a billboard etc… Was required in order not to loose ANY of the SpeedTree’s transition quality
- Have the transitions animated
- Extract the billboard data from the SpeedTree xml. For more details on that, I have received an special ‘exe’ from the SpeedTree guys so that when a model is saved also an ‘xml’ representation of the billboard’s UV’s and data is saved so that I can extract it and pass it to the shader (this part can be skipped, if we don’t have the XML)
Now that the main ideas are sorted out, let’s get started!
Take one terrain and divide it into large cells, creating a grid:
(10x10km terrain divided into cells)
See all the instances that are in the grid and build the offline-mesh that I have talked about. After that grab the structured cells that contain the trees it and store it for runtime use.
At runtime we:
- See in what cell we are and grab the surrounding cells (refer to the grass system)
- Iterate the cells and see if they are within 3D model draw distance
for (int cell = 0; cell < m_Cells.Length; cell++)
{
StructuredTrees str = m_Cells[cell];
if(Vector3.Distance(pos, str.m_Bounds.center) < cellDist && GeometryUtility.TestPlanesAABB(planes, str.m_Bounds))
{
// Perform with drawing the instances within the cell
}
{
- If a cell is within distance see if it is completely or only partly in frustum. If it is completely in the frustum we can send all the trees in it to the rendering pipeline, else we need to test each tree’s AABB against the frustum. The tree’s AABB is generated offline
// If the cell is completely inside the frustum
bool insideFrustum = IsCompletelyInsideFrustum(ref planes, BoundsCorners(ref m_Bounds, ref m_TempCorners));
if(insideFrustum) // Send the tree straight to rendering
if (distToTree <= system.m_Settings.m_MaxTreeDistance)
else // Send the tree to rendering only if it is in the frustum
if (distToTree <= system.m_Settings.m_MaxTreeDistance && GeometryUtility.TestPlanesAABB(planes, bounds[treeIndex]))
- Not only that we send the tree down to rendering but we will also update it’s LOD state for cool transitions
- Last thing we need to take care of is a smooth animated transition between the 3D tree and the 2D billboard. We do that by having one more intermediary 2D billboard between the 3D model and the 2D offline-batched billboards. When the 3D model is close on becoming a 2D billboard we draw an animated intermediary 2D billboard. During that transition we need to send two models for rendering per tree. After a certain distance that 2D billboard dissapears and the manually batched billboard remains behind it, but without having the user noticing it. Having that makes practically almost absolutely no difference between the SpeedTree trees and our system (except the awesome performance, of course)
Some other things that needed taking care of were the modification of the SpeedTree stock shader so that we can manually cross-fade between the trees.
Result:
(60FPS 200k+ trees, four types)
(70FPS, 200k+ trees, going crazy with bush density. But, please, keep in mind, this is not a grass system)
(420 FPS 250k trees, with four tree types, all billboards)
NOTE: This system is not designed for grass. You might wish to check the grass system, designed for millions of instances.





