| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using UnityEngine;
- [ExecuteInEditMode]
- public class NM_Wind : MonoBehaviour
- {
- [Header("General Parameters")]
- [Tooltip("Wind Speed in Kilometers per hour")]
- public float WindSpeed = 30;
- [Range(0.0f, 2.0f)]
- [Tooltip("Wind Turbulence in percentage of wind Speed")]
- public float Turbulence = 0.25f;
- [Header("Noise Parameters")]
- [Tooltip("Texture used for wind turbulence")]
- public Texture2D NoiseTexture;
- [Tooltip("Size of one world tiling patch of the Noise Texture, for bending trees")]
- public float FlexNoiseWorldSize = 175.0f;
- [Tooltip("Size of one world tiling patch of the Noise Texture, for leaf shivering")]
- public float ShiverNoiseWorldSize = 10.0f;
- [Header("Gust Parameters")]
- [Tooltip("Texture used for wind gusts")]
- public Texture2D GustMaskTexture;
- [Tooltip("Size of one world tiling patch of the Gust Texture, for leaf shivering")]
- public float GustWorldSize = 600.0f;
- [Tooltip("Wind Gust Speed in Kilometers per hour")]
- public float GustSpeed = 50;
- [Tooltip("Wind Gust Influence on trees")]
- public float GustScale = 1.0f;
- [Header("Wind Sherical")]
- [Tooltip("Wind Gust Influence on trees")]
- public WindZone point1;
- public WindZone point2;
- public WindZone point3;
- public WindZone point4;
- Vector4 pos1 = new Vector4();
- Vector4 pos2 = new Vector4();
- Vector4 pos3 = new Vector4();
- Vector4 pos4 = new Vector4();
- Vector4 radius = new Vector4();
- // Use this for initialization
- void Start()
- {
- ApplySettings();
- }
- // Update is called once per frame
- void Update()
- {
- ApplySettings();
- }
- void OnValidate()
- {
- ApplySettings();
- }
- void ApplySettings()
- {
- Shader.SetGlobalTexture("WIND_SETTINGS_TexNoise", NoiseTexture);
- Shader.SetGlobalTexture("WIND_SETTINGS_TexGust", GustMaskTexture);
- Shader.SetGlobalVector("WIND_SETTINGS_WorldDirectionAndSpeed", GetDirectionAndSpeed());
- Shader.SetGlobalFloat("WIND_SETTINGS_FlexNoiseScale", 1.0f / Mathf.Max(0.01f, FlexNoiseWorldSize));
- Shader.SetGlobalFloat("WIND_SETTINGS_ShiverNoiseScale", 1.0f / Mathf.Max(0.01f, ShiverNoiseWorldSize));
- Shader.SetGlobalFloat("WIND_SETTINGS_Turbulence", WindSpeed * Turbulence);
- Shader.SetGlobalFloat("WIND_SETTINGS_GustSpeed", GustSpeed);
- Shader.SetGlobalFloat("WIND_SETTINGS_GustScale", GustScale);
- Shader.SetGlobalFloat("WIND_SETTINGS_GustWorldScale", 1.0f / Mathf.Max(0.01f, GustWorldSize));
- if (point1 != null)
- {
- pos1 = new Vector4(point1.transform.position.x, point1.transform.position.y, point1.transform.position.z, point1.windMain * 0.2777f);
- radius[0] = point1.radius;
- }
- else
- {
- pos1 = new Vector4(0, 0, 0, 0);
- radius[0] = 0.1f;
- }
- if (point2 != null)
- {
- pos2 = new Vector4(point2.transform.position.x, point2.transform.position.y, point2.transform.position.z, point2.windMain * 0.2777f);
- radius[1] = point2.radius;
- }
- else
- {
- pos2 = new Vector4(0, 0, 0, 0);
- radius[1] = 0.1f;
- }
- if (point3 != null)
- {
- pos3 = new Vector4(point3.transform.position.x, point3.transform.position.y, point3.transform.position.z, point3.windMain * 0.2777f);
- radius[2] = point3.radius;
- }
- else
- {
- pos3 = new Vector4(0, 0, 0, 0);
- radius[2] = 0.1f;
- }
- if (point4 != null)
- {
- pos4 = new Vector4(point4.transform.position.x, point4.transform.position.y, point4.transform.position.z, point4.windMain * 0.2777f);
- radius[3] = point4.radius;
- }
- else
- {
- pos4 = new Vector4(0, 0, 0, 0);
- radius[3] = 0.1f;
- }
- Shader.SetGlobalMatrix("WIND_SETTINGS_Points", new Matrix4x4(pos1, pos2, pos3, pos4));
- Shader.SetGlobalVector("WIND_SETTINGS_Points_Radius", radius);
- }
- Vector4 GetDirectionAndSpeed()
- {
- Vector3 dir = transform.forward.normalized;
- return new Vector4(dir.x, dir.y, dir.z, WindSpeed * 0.2777f);
- }
- }
|