NM_Wind.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. public class NM_Wind : MonoBehaviour
  4. {
  5. [Header ("General Parameters")]
  6. [Tooltip ("Wind Speed in Kilometers per hour")]
  7. public float WindSpeed = 30;
  8. [Range (0.0f, 2.0f)]
  9. [Tooltip ("Wind Turbulence in percentage of wind Speed")]
  10. public float Turbulence = 0.25f;
  11. [Header ("Noise Parameters")]
  12. [Tooltip ("Texture used for wind turbulence")]
  13. public Texture2D NoiseTexture;
  14. [Tooltip ("Size of one world tiling patch of the Noise Texture, for bending trees")]
  15. public float FlexNoiseWorldSize = 175.0f;
  16. [Tooltip ("Size of one world tiling patch of the Noise Texture, for leaf shivering")]
  17. public float ShiverNoiseWorldSize = 10.0f;
  18. [Header ("Gust Parameters")]
  19. [Tooltip ("Texture used for wind gusts")]
  20. public Texture2D GustMaskTexture;
  21. [Tooltip ("Size of one world tiling patch of the Gust Texture, for leaf shivering")]
  22. public float GustWorldSize = 600.0f;
  23. [Tooltip ("Wind Gust Speed in Kilometers per hour")]
  24. public float GustSpeed = 50;
  25. [Tooltip ("Wind Gust Influence on trees")]
  26. public float GustScale = 1.0f;
  27. // Use this for initialization
  28. void Start ()
  29. {
  30. ApplySettings ();
  31. }
  32. // Update is called once per frame
  33. void Update ()
  34. {
  35. ApplySettings ();
  36. }
  37. void OnValidate ()
  38. {
  39. ApplySettings ();
  40. }
  41. void ApplySettings ()
  42. {
  43. Shader.SetGlobalTexture ("WIND_SETTINGS_TexNoise", NoiseTexture);
  44. Shader.SetGlobalTexture ("WIND_SETTINGS_TexGust", GustMaskTexture);
  45. Shader.SetGlobalVector ("WIND_SETTINGS_WorldDirectionAndSpeed", GetDirectionAndSpeed ());
  46. Shader.SetGlobalFloat ("WIND_SETTINGS_FlexNoiseScale", 1.0f / Mathf.Max (0.01f, FlexNoiseWorldSize));
  47. Shader.SetGlobalFloat ("WIND_SETTINGS_ShiverNoiseScale", 1.0f / Mathf.Max (0.01f, ShiverNoiseWorldSize));
  48. Shader.SetGlobalFloat ("WIND_SETTINGS_Turbulence", WindSpeed * Turbulence);
  49. Shader.SetGlobalFloat ("WIND_SETTINGS_GustSpeed", GustSpeed);
  50. Shader.SetGlobalFloat ("WIND_SETTINGS_GustScale", GustScale);
  51. Shader.SetGlobalFloat ("WIND_SETTINGS_GustWorldScale", 1.0f / Mathf.Max (0.01f, GustWorldSize));
  52. }
  53. Vector4 GetDirectionAndSpeed ()
  54. {
  55. Vector3 dir = transform.forward.normalized;
  56. return new Vector4 (dir.x, dir.y, dir.z, WindSpeed * 0.2777f);
  57. }
  58. }