marcb152

Use wind on custom trees with multiple materials for Unity

Apr 18th, 2020
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 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 WindMultipleMat : MonoBehaviour
  5. {
  6.     //WORKS WITH ALL THE NATURE/TREE CREATOR SHADERS
  7.  
  8.     private Renderer render;
  9.     private Material[] mats;
  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.     [Header("Set the length of the array the same as the number of materials")]
  18.     [Tooltip("x, y, z -> wind offset; w -> wind force")]
  19.     [SerializeField] private Vector4[] wind;
  20.     /*
  21.      * Good settings to start with :
  22.      * Leafs : Vector4(0.1f, 0.1f, 0.1f, 0.1f)
  23.      * Trunk : Vector4(0, 0, 0, -0.03f)
  24.      *
  25.      * Put smaller values if your tree isn't vertex paint
  26.      *
  27.      * Set a negative value to the trunk, makes it moving with the leaves (leaves must have a positive value)
  28.      *
  29.      * This script looks very weird and bad with strong wind forces
  30.     */
  31.  
  32.     void Awake()
  33.     {
  34.         if (!render) render = GetComponent<Renderer>();
  35.         mats = render.materials;
  36.     }
  37.  
  38.     void Update ()
  39.     {
  40.         //This script will affects each material with one Vector4 in the same order
  41.         for (int i = 0; i < mats.Length; i++)
  42.             mats[i].SetVector("_Wind", wind[i]);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment