Critias Tree System [Asset Store]

! 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.

17 Likes

! MAJOR UPDATE !

With the last of issues fixed I would consider this system ready for some serious beta, maybe even production! Looking forward for the awesome things that you are going to create with it!

! UPDATE !

I’ve also uploaded the package to the asset store, for faster importing and managing, for people who prefer that method. It can be found here.

2 Likes

Fantastic. I need to read through your post in detail, but already I’m super interested. I have a SpeedTree license and would love to get improved performance. I am downloading the demo now and will check it out.

1 Like

The demo looks very nice. I have a couple of questions about the workflow. Do you place a bunch of trees using the Terrain editor and then some process converts them? Or are these trees placed individually on the terrain as objects? Also, I noticed that the trees didn’t have colliders on them so I was able to run right through them. Obviously we don’t want colliders on distant billboards, but definitely want them on up close trees. How does your system plan to handle that? Finally, does this have special requirements like DX11 computer shaders or anything like that?

[EDIT]
Also, what version of Unity is required for this and also your grass system?

1 Like

This looks very promising :slight_smile:

That’s some decent performance gain.
I need this -.-

I’ve built a system to manage tree colliders before based on distance to player and it can be very efficient.
Such a system would not slow this down. Mine wasn’t even optimized and it was fast.

2 Likes

looks nice so far.
but that code does not look very optimized to me:
Vector3.Distance

using:
Vector3 offset = other.position - transform.position;
float sqrLen = offset.sqrMagnitude;

should be way faster.

see: Unity - Scripting API: Vector3.sqrMagnitude

1 Like

Tried the demo, looks promising. Good work!

Hey! Thanks for the encouragement.

For the coliders, I just forgot to enable them when I made the build :)). Nothing to worry about. But at 100k trees they surely eat quite a lot of CPU, so we need to think at something so that we only enable the colliders for the trees around you. For example the system works great at 200k trees but the physics systems goes haywire and you pass through everything. So yea, it is a problem but for this demo I just forgot to enable the colliders :). When I’ll make a demo with 500k trees I’ll need to think at something very smart with the physics stuff.

For editing/creating it’s very very simple. Just use gaia or paint the trees, press a button and they are automatically gridified, created, that’s all. No special editing, no individual trees as game objects (which is impossible for 100k objects anyway), all drawn with ‘Grahpics.DrawMesh’ (hoping for instancing in the future).

No special requirements, dx9+, Unity 5.4+ for the trees (I think it should basically be 5+, it doesn’t really use anything special at ALL, it’s C# with a little bit of easy shader work).

For the grass it’s 5.5 with dx11 since it requires the ‘DrawMeshInstanced’ stuff. The grass can’t live without that at decent performance. I’m sorry, but the grass is a different story and it looks very very very good. If one can live with more modest looking grass, can find some other solution if it targets dx9 but it won’t look as good.

Also keep in mind what you want to do with Unity. If you want to do a desktop title, make it PBR, make it look good and target DX11+ hardware, and stuff in the grass and trees.

If you target mobile, I doubt my systems would help you much. They were not mobile-oriented, and I don’t remember if mobiles can target SM 3.0 on older hardware. Having open-world mobile games with a lot of grass and trees sounds a little weird to me :slight_smile:

No, it’s not heavily optimized, sorry :). Thanks for the tip, I really didn’t knew what that sqrMag is used for . But it still goes a lot better that Unity’s implementation without the smart optimizations and without c++ code :).

One more thing I’ll require help with is the choosing of billboards based on angle, since now is quite wrong. But I’ll have to prepare one more post with some source code. Since the issue is quite extended.

2 Likes

My…My…My…God!!! I’ve tested the demo…and it’s fantastic…and very generous by you to release in the future this for everyone…Thanks in advance.

2 Likes

Actually I’m using Wii U which is SM 4.0, but not DX11 although it can convert most of DX11 to what it needs, but not compute shaders or SM 5.0 specific stuff. And I’m on Unity 5.3.6p2, but Unity 5.4 is coming soon for the Wii U. 5.5 will be quite a ways off though if at all.

Unity have really dropped the ball on terrain in general and SpeedTree specifically… they certainly look beautiful but are completely impractical for a real game, so far! Have you tried your system using multiple tree types yet? I imagine you would still get a good performance boost, but I wonder how much this cuts into performance gains. Having support for 3 or 4 different types of trees (per terrain) is going to be mandatory for real-world use I think.

Looking forward to your development!

Don’t worry, the system must work with dozens(even hundreds) of tree types without any performance loss, so no worries there… That’s part three of the development, I hope I can cook it next week.

And for part four there’s GPU instancing for trees on same LOD, that will also have a huge positive impact on performance.

For part two that’s the billboard choosing, I’m trying to update the project on github right now.

No, no don’t worry, it’s DX9 (SM 3.0). It should work with any unity that is 5.0+.

Please, check out this post, and if anyone can help post here. Keep in mind, this is not for me only, it is for the whole community, so the faster we can fix this, the faster I can move on with making the system for multiple tree types and finally releasing it.

To get the magnitude of a vector you need to get a square root. Those are computationally more expensive than additions or multiplications. I’m curious how much of a performance gain you can see though, sqrt should be pretty fast these days, too.

@Assembler-Maze if you have some sort of beta test, I would like to test at earliest possible point. Since I need this for the Wii U it’s always a bigger challenge to get something like this working. If this could work and work well on the Wii U then it could be the final piece of the puzzle to get my game back up to target frame rate.

1 Like

I will try to release this as soon as possible, in order to get the most of the feedback and to see how this scales in real-world solutions.

That would be after I fix the support for multiple tree types. I hope that will be before the end of the week.

2 Likes

The performance improvement was good, but not enough due to the fact that the subtraction calls a vector’s ctor. It is overkill for testing the ton of trees that might be visible. For 110k trees it took something like 20ms with ‘Vector.Distance’ and 17ms with ‘sqrMagnitude’.

Fastest way is:

x = treePos.x - camPos.x;
y = treePos.y - camPos.y;
z = treePos.z - camPos.z;

float sqrDistToTree = x * x + y * y + z * z;

1-2MS for 110k trees.

But there’s also granularity improvements, for example we can further subdivide the big cells, into smaller subdivisions and we can test for those cells too, in order to exclude as many trees as possible. See the grass system as a reference.

For example if we have 5 visible cells that’s a staggering 20k trees that we have to test the ‘distance’ for. And out of those 20k, probably, many were taken into consideration only due to the fact that the big cell’s center position is closer than our threshold. But that might as well mean that half of the cell’s trees are out of range, and with further cell subdivisions we might quickly exclude those also.

But we’ll see that in practice, after I release the first beta, hopefully this week.