I've been quite slow and thoughtful about how I want to approach trees in this project.
You might have noticed I posted some thoughts earlier in Post 6 . Essentially that was a post about conflicting pros and cons of using the standard terrain tree versus using gameObjects.
Using gameobjects for trees makes it possible for characters to interact with trees and affect them - for example harvesting fruit from them. This is not straightforward with terrain trees because they don't have their own colliders, the terrain might have 300 trees of the same type strewn across it, and they all reference the same prototype object - therefore if you change that prototype (e.g. remove a fruit from a branch) this is replicated across all 300 instances on the terrain ! Not desirable!
Adding/removing terrain tree instances at runtime:
So my conclusion was to use traditional terrain trees for performance but first make sure I learned how to add and remove instances at runtime. The idea here is that if i need to interact with a specific tree instance that i would at runtime swap the terrain tree for a full gameobject that can be interacted with without affecting all other tree instances in the landscape.
First Tree assets and LOD
Having had sufficient success in this area i was happy to move forward with creating some terrain tree assets for the project. To this end I searched online for ideas for low-poly alien vegetation and found a brilliant unity pack by Ronja at Fantasy Haven Assets that I decided would do most of the art work for me.
The meshes could be reduced in Blender to even fewer triangles to suit my project. I mainly used Blender's 'Decimate' modifier to do this with some occasional cleaning up. I've used 3 levels of detail and set the LOD Group in Unity to cull the trees when smaller than 3% of screen height. The highest LOD is in the order of 1800 tris and the lowest LOD is in the order of 100-200 tris. This is far too many triangles at the low end, given their crudity and distance from viewer. It will do for now, but once all the trees have been added to the landscape I'll take some time to reduce the tris in those models to improve performance -- possibly i might take the time to create billboards for the lowest LOD.
Blowing in the wind
Here's an intro video showing my recolouring of a couple of tree assets from Ronja and also a palm tree of my own that I thought was good enough to use. This video mentions how I use a 'wind map' texture to achieve a realistic wind effect for grass , tree trunks and leaf canopies so that a wind gust or swirl progresses across the terrain rather than just random movements.
'Wind map' to give coordinated movement to grass and trees.
Cartoony style and directionally lit (silouhetting effect)
I noticed that the flat (unfaceted) appearance that an unlit shader yields was quite attractive for the foliage. Especially if set against the faceted appearance of the tree trunk provided by a lit shader.
However I wanted that flat look of the unlit shader combined with directional lightening/darkening of a lit shader so that foliage would respond better to the day/night cycle.
This video shows what i mean and how i went about it creating a custom shadergraph to achieve something I am happy with.
Interesting 'silouhetting' effect and cartoon style flat shaded foliage.
Wind motion for palm fronds vs tree leaves
For the normal trees my wind movement was straightforward and the same as for grass - the wind movement is applied to each vertex in the mesh proportional to the vertex height above the ground (squared) and the vertex is lowered to compensate for the lateral movement, otherwise trees and grass would appear to be stretching.
The compensatory vertical dip is currently estimated by multiplying the lateral wind motion by the squareroot of 2 as a simplification that the final movement approximates a 1 by 1 rightangular triangle where height and length are equal and the hypotenuse represents the finished grass stem or tree trunk's rotated position.
To be honest that sounds fancy but I'm not sure it's working that well and I'll probably re-look at that in future. The great thing is that it's realistic enough for now for me to progress with the game and when i do come back and improve it, i only have to tweak a couple of shaders and all the materials using them instantly get the change! :)
So what's the difference for palm fronds?
You cannot apply the same movement logic to palm fronds because they are attached to the top of the tree stem and hang downwards - whereas normal foliage is modelled as attaching to the top or tips of the tree and branches and continuing outward and upwards.
Applying the same shader would result in palm fronds showing most movement where the join the tree trunk and would be increasingly motionless towards their lower tips!
This video illustrates the result of my shadergraphs showing both palm fronds and normal foliage blowing in the wind in a realistic manner.
Making use of the object space origin's three ordinate values.
My explanation in the video above isn't easy to follow. Partly because Blender and Unity refer to the vertical axis as Z or as Y respectively which makes for instant confusion!
The main point to understand is that my shaders calculate wind movement based on world space lateral position of a vertex AND a single float to represent height. Height is a dimension from object space and it is not the vertex coordinate - it is only one ordinate value of the vertex coordinate (let's say the Z value for Blender) whilst the other two ordinate values (X and Y) are ignored.
Realising this meant I could move the object space origin in the X and Y axes without detriment to their Z values and therefore without effecting the height being fed into my wind motion calculation. Therefore I could use the vertex's X and Y values to represent anything i wanted - in this case i used it to store the height at which the frond attaches to the tree trunk. This value is otherwise unavailable to any vertex that is not on the tree trunk.
This following video takes a look at my shadergraphs nodes in more detail if you are in to that kind of thing...