Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add wind to custom trees/grass in unity using tree creator shaders !

Discussion in 'General Graphics' started by marcb152, Apr 18, 2020.

  1. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi, I finally figured out how to add wind to custom trees using unity's default shaders !
    I created this thread to help others people because I spent a lot,a lot and a lot of time to get a quite good / realistic working wind without using custom shaders.

    But first, here are the pros and cons : (EDIT: Check the #10 post)
    • Working fine on all the trees
    • Customizable wind for each tree prefab
    • Don't react to Windzones (I think it's possible to do something by script for that)
    • Works good on trees outside of terrain with multiples meshes or materials
    • Don't work with unity terrain
    • Quite a good optimization
    So let's get started : First you'll need the Tree Creator shaders.
    If you don't have them, you can still get them here : https://unity3d.com/get-unity/download/archive. Just download the built-in shaders, extract the file. The shaders are under : builtin_shaders\DefaultResourcesExtra\Nature\TreeCreator. Drag and drop them into your project folder and drag and drop them onto your material.

    For meshes with a single material we will use MaterialPropertyBlocks :
    Then add this little code to your tree (at the same place of your renderer) : https://pastebin.com/RaUpaRTu
    UnityWind1.png The Vector4 wind value works as :
    • x = wind offset on the x value of the leaves
    • y = *same for y
    • z = *same for z
    • w = Wind force applied to all the values above
    Now you're ready to go !
    Repeat this operation if you have several meshes with one material. For the trunk, divide the values of the leaves by 3 and put them in negative.

    Let's see how to do it with several materials :

    There aren't many differences except for the script we're going to use : https://pastebin.com/Wgfx30uj
    UnityWind2bis.png
    The first Vector4 will be applied to the first material, the second to the second material...

    Some useful links & sources :
    How to vertex-paint a tree for unity wind ?
    Well, I don't know. I used the TreeIt software, which is a FREE tree creator with default vertex painting. I used the Maple Tree of this software for the two screenshots.
    You can get it here : https://www.evolved-software.com/treeit/treeit

    If you have a question, please feel free to ask it below.

    I just have one question : did anyone know how to bake the uv2 of a Tree ?

    Hope this helps !
     
    Last edited: Aug 29, 2021
  2. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi, after some in-depth researches using Unity Tree Creator and this thread : https://connect.unity.com/p/deconstructing-unity-trees-for-wind-zone-dynamics.
    I finally figured out how Unity Tree Creator uses Vertex colors and uv2 !
    To do so, I downloaded the Vertex Paint Tool available here : https://github.com/slipster216/VertexPaint

    I then created a simple but weird Unity tree :
    UnityWind3.png
    Using the Vertex Paint tool (Window --> Vertex Painter Pro) :
    You can see the vertex colors, the first uv and the second uv generated by Unity :
    UnityWind4.png UnityWind5.png UnityWind6.png
    This tools allows us to see specific values such as RGBA values of vertex colors or the XYZW values of uvs.

    The interesting things start here :
    The red channel of the vertex colors contains informations about the branches, each branch have its own red value (represented below by different shades of grey) :
    UnityWind7.png
    Well, the Unity forum doesn't support more than 5 attachments:confused:. Thus, I will reply a second time to show you the end of my researches;).
     
    istmo likes this.
  3. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Second part just below :):

    The Green channel contains informations about which part of the tree is a leaf, and which part is a branch (leaves are displayed in white and branches in black) :
    UnityWind8.png

    Blue and Alpha channels contain nothing useful (I think, maybe I'm wrong).

    So now, let's switch to the second uv :
    (All texts in italics are quotes from this thread (I've used it a lot, thanks to this guy) : https://connect.unity.com/p/deconstructing-unity-trees-for-wind-zone-dynamics)

    The X channel contains "how much a vertex should bend to the wind direction" :
    UnityWind9.png

    The Y channel looks pretty the same but is used to control "the swaying of the branches parallel to the wind direction":
    UnityWind10.png

    Using this Vertex Paint tool allows us to paint our custom trees correctly ! They look more realistic now than before;).
     
    istmo likes this.
  4. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi, this is just a little check of all the scripts (in order of performance) :

    NOTE : All the scripts for a single material worked with several materials, it will just apply the same values on both of them.

    Optimized scripts : you can't modify the wind value during runtime, it can be a good practice to use non-optimized script to define goods values for your wind and after that just replace it by an optimized one with the good values.

    Script for single material optimized : https://pastebin.com/hijJnuxC

    Script for single material non-optimized : https://pastebin.com/RaUpaRTu

    Script for several materials optimized : https://pastebin.com/CAZugZfu

    Script for several materials non-optimized : https://pastebin.com/Wgfx30uj

    Of course, the optimized script needed to be used the same as the non-optimized ones.:)

    I planned to make 3 tutorials about :
    • How to export a Tree from the TreeIt software to Blender.
    • How to correctly vertex & uv paint a tree in Blender for a decent unity wind.
    • How to set-up a painted Tree in unity.
    I will make a new post in this forum when each tutorial will be available.

    Still hope this helps !;)
     
    babaukos and istmo like this.
  5. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi everyone !
    First of all, I really apologize for the long wait between this post and the last one. It has been two months in between, and I've done a lot of various things, such as a mesh combiner with the possibility to make atlases available here : https://forum.unity.com/threads/optimising-dense-forest-scene.905081/

    I said I'll make a tutorial on how to paint a tree in Blender, I found a great addon wich handle painting on a specific layer (painting only the red layer of the vertex colors, and so on).
    You can download this addon here : https://github.com/andyp123/blender_vertex_color_master

    After some experiments with this addon, I discovered that a linear gradient is sufficient for the red value of the vertex colors, and a circular gradient makes a simple great wind on uv2. But exporting a tree from Unity to Blender and reimporting back to Unity is a waste of time.
    So I decided to make a script to handle these gradients directly in Unity, available here : https://pastebin.com/UkWsmnkn

    Unity WindCreator 1.png
    This script is highly flexible, you can use it for a wide variety of trees. You can define the wind strength directly using the script, Vertex Percent and Uv2 Percent are the main sliders for this. For a better control of the tree sway, you can play with the multiplier sliders of the second uv.

    EDIT: I forgot to say that you have to put your script on your 3d tree model (your tree must be made of one mesh and two materials).
    Don't hesitate to ask if you have difficulties with this script.;)
     
    Last edited: Jul 15, 2021
  6. adrianre12

    adrianre12

    Joined:
    Jul 16, 2016
    Posts:
    3
    Hi, Thanks for posting this, very useful.
    I am having a little trouble getting it working. I have the TreeIt Maple imported and have added the script. When I run it the leaves move to (0,0,0) leaving the trunk at its original position. Neither show wind motion when I press play.
    Any hints?
     
  7. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi, I'm glad you find this helpful.
    For the "Wind Creator" script to work, you need to click on the 3 vertical dots in the upper left corner of the script, and you'll see a new button: "Create wind". Click on it and the script will create a new mesh with the vertex colors and the uv2 baked in, then add one of the four scripts on this new mesh to control the wind in playmode.
    This should work for now, I hope this explanation was clear enough.
    As usual, feel free to reply for any help or questions.
    I've a lot of things to do at the moment, I'll try to improve this script to support more cases in an easier way.
     
  8. Abdulrahman2020n

    Abdulrahman2020n

    Joined:
    Jan 30, 2020
    Posts:
    2
    Is it work on URP
     
  9. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi!
    It doesn't, since URP is another render pipeline, shaders are not compatible. Try searching for "foliage wind URP shaders", there's lots of tutorials on Shader Graph to achieve this effect.
    You can download and try the Boat Attack demo Unity has made, there are some cool shaders you can take a look at.
    Here's a video tutorial by Unity itself about what I said lastly:

    Hope this helps :)
     
  10. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi guys!
    I'm back with new scripts! Easier than ever, you can now add realistic wind to any tree or grass, and it reacts to Windzones!

    Here are the few drawbacks of this new script:
    • You need Unity 2019.3 or newer for the baking to work, you can later export the baked mesh into older Unity versions
    • It works as I explained before only for the built-in render pipeline, I didn't find any free shader for URP/HDRP
    And the advantages:
    • It works outside the terrain system
    • Very good optimization
    I created a Github repo to store the Unity package files and the documentation (I'm a noob at using Github, sorry :confused:), available here: https://github.com/marcb152/Wind-Creator/releases
    You no longer need the scripts I posted before, and I made a little mistake in the documentation, some "Nature/TreeCreator" shaders work, you don't need to download them.
    As always, don't hesitate to contact me if you experience problems, even if you are years behind (I doubt Unity will maintain this feature for a long time).

    Hope this helps
    - Marc // πNutStudio // marcb152
     
    Last edited: Aug 29, 2021
  11. delayman

    delayman

    Joined:
    Aug 13, 2019
    Posts:
    2
    It's working with terrain and outside of it. But I've tried with LOD on terrain and tree was static. Is this expected behaviour?
     
  12. marcb152

    marcb152

    Joined:
    Jun 20, 2019
    Posts:
    22
    Hi!

    I sincerely apologize for this very late reply, I made some tests and you're right, trees with LODs don't work with Unity terrain, but they work as prefabs (placed by hand in the hierarchy).
    However, trees or grass without LODs work fine inside the terrain, the only exception is that Unity will send you an annoying warning saying that you need to change the shaders to "Tree Soft Occlusion" ones. These shaders are looking bad and are making the wind misbehave, so just ignore this warning. In addition, lighting may look bad if your terrain is marked as static and your tree prefab is not (if your terrain isn't static then your tree shouldn't be either, and the same goes if your terrain is static).

    This behavior is kinda expected, let me explain myself; The purpose of this script is to make Unity believe that your custom tree/grass is a tree made using the Unity Tree Creator. Since Unity believes it is, it will start applying wind to it inside and outside the terrain. But, the Unity Tree Creator doesn't have any LOD component, it instead uses an internal automatic LOD system, so when you try to add a tree with a LOD component on your terrain, Unity thinks you're adding a SpeedTree (because SpeedTrees have LODs, but my scripts didn't work for this case). I hope my explanation was clear :confused:

    I'll try to update the GitHub repo in a few weeks (the documentation mainly).

    TL; DR; Here are the cases where it works (Tree & Grass):
    • Without LODs:
      • Prefab
      • Unity terrain
    • With LODs:
      • Prefab
    As usual, feel free to ask questions if any, I hope it helps!
    - Marc // πNutStudio // marcb152
     
    Bakanovskiy95 and MonkeyPuzzle like this.