marcb152

Wind for unity custom trees single mat optimized

Apr 23rd, 2020
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. //Unity forum page : https://forum.unity.com/threads/how-to-add-wind-to-custom-trees-in-unity-using-tree-creator-shaders.871126/
  2. using UnityEngine;
  3.  
  4. public class WindSingleMatOptimized : MonoBehaviour
  5. {
  6.     //WORKS WITH ALL THE NATURE/TREE CREATOR SHADERS
  7.  
  8.     private MaterialPropertyBlock materialPropertyBlock;
  9.     private Renderer render;
  10.     /*
  11.      * The Vector4 wind value works as :
  12.      *  -> x = wind offset on the x value of the leaves
  13.      *  -> y = *same for y
  14.      *  -> z = *same for z
  15.      *  -> w = Wind force applied to all the values above
  16.     */
  17.     [Tooltip("x, y, z -> wind offset; w -> wind force")]
  18.     [SerializeField] private Vector4 wind;
  19.     /*
  20.      * Good settings to start with :
  21.      * Leafs : Vector4(0, 0, 0, 0.1f)
  22.      * Trunk : Vector4(0, 0, 0, -0.03f)
  23.      *
  24.      * Put smaller values if your tree isn't vertex paint
  25.      *
  26.      * Set a negative value to the trunk, makes it moving with the leaves (leaves must have a positive value)
  27.      *
  28.      * This MaterialPropertyBlock looks very weird and bad with strong wind forces
  29.     */
  30.  
  31.     void Awake()
  32.     {
  33.         if (!render) render = GetComponent<Renderer>();
  34.     }
  35.  
  36.     void Start()
  37.     {
  38.         materialPropertyBlock = new MaterialPropertyBlock();
  39.  
  40.         //render.SetPropertyBlock() is a MaterialPropertyBlock which works only for the current Tree and affects all the materials of the gameobject
  41.  
  42.         render.GetPropertyBlock(materialPropertyBlock);
  43.         materialPropertyBlock.SetVector("_Wind", wind);
  44.         render.SetPropertyBlock(materialPropertyBlock);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment