NM_Wind.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. [Header("Wind Sherical")]
  28. [Tooltip("Wind Gust Influence on trees")]
  29. public WindZone point1;
  30. public WindZone point2;
  31. public WindZone point3;
  32. public WindZone point4;
  33. Vector4 pos1 = new Vector4();
  34. Vector4 pos2 = new Vector4();
  35. Vector4 pos3 = new Vector4();
  36. Vector4 pos4 = new Vector4();
  37. Vector4 radius = new Vector4();
  38. // Use this for initialization
  39. void Start()
  40. {
  41. ApplySettings();
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. ApplySettings();
  47. }
  48. void OnValidate()
  49. {
  50. ApplySettings();
  51. }
  52. void ApplySettings()
  53. {
  54. Shader.SetGlobalTexture("WIND_SETTINGS_TexNoise", NoiseTexture);
  55. Shader.SetGlobalTexture("WIND_SETTINGS_TexGust", GustMaskTexture);
  56. Shader.SetGlobalVector("WIND_SETTINGS_WorldDirectionAndSpeed", GetDirectionAndSpeed());
  57. Shader.SetGlobalFloat("WIND_SETTINGS_FlexNoiseScale", 1.0f / Mathf.Max(0.01f, FlexNoiseWorldSize));
  58. Shader.SetGlobalFloat("WIND_SETTINGS_ShiverNoiseScale", 1.0f / Mathf.Max(0.01f, ShiverNoiseWorldSize));
  59. Shader.SetGlobalFloat("WIND_SETTINGS_Turbulence", WindSpeed * Turbulence);
  60. Shader.SetGlobalFloat("WIND_SETTINGS_GustSpeed", GustSpeed);
  61. Shader.SetGlobalFloat("WIND_SETTINGS_GustScale", GustScale);
  62. Shader.SetGlobalFloat("WIND_SETTINGS_GustWorldScale", 1.0f / Mathf.Max(0.01f, GustWorldSize));
  63. if (point1 != null)
  64. {
  65. pos1 = new Vector4(point1.transform.position.x, point1.transform.position.y, point1.transform.position.z, point1.windMain * 0.2777f);
  66. radius[0] = point1.radius;
  67. }
  68. else
  69. {
  70. pos1 = new Vector4(0, 0, 0, 0);
  71. radius[0] = 0.1f;
  72. }
  73. if (point2 != null)
  74. {
  75. pos2 = new Vector4(point2.transform.position.x, point2.transform.position.y, point2.transform.position.z, point2.windMain * 0.2777f);
  76. radius[1] = point2.radius;
  77. }
  78. else
  79. {
  80. pos2 = new Vector4(0, 0, 0, 0);
  81. radius[1] = 0.1f;
  82. }
  83. if (point3 != null)
  84. {
  85. pos3 = new Vector4(point3.transform.position.x, point3.transform.position.y, point3.transform.position.z, point3.windMain * 0.2777f);
  86. radius[2] = point3.radius;
  87. }
  88. else
  89. {
  90. pos3 = new Vector4(0, 0, 0, 0);
  91. radius[2] = 0.1f;
  92. }
  93. if (point4 != null)
  94. {
  95. pos4 = new Vector4(point4.transform.position.x, point4.transform.position.y, point4.transform.position.z, point4.windMain * 0.2777f);
  96. radius[3] = point4.radius;
  97. }
  98. else
  99. {
  100. pos4 = new Vector4(0, 0, 0, 0);
  101. radius[3] = 0.1f;
  102. }
  103. Shader.SetGlobalMatrix("WIND_SETTINGS_Points", new Matrix4x4(pos1, pos2, pos3, pos4));
  104. Shader.SetGlobalVector("WIND_SETTINGS_Points_Radius", radius);
  105. }
  106. Vector4 GetDirectionAndSpeed()
  107. {
  108. Vector3 dir = transform.forward.normalized;
  109. return new Vector4(dir.x, dir.y, dir.z, WindSpeed * 0.2777f);
  110. }
  111. }