WindController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Fantasy Adventure Environment
  2. // Copyright Staggart Creations
  3. // staggart.xyz
  4. using UnityEngine;
  5. using System.Collections;
  6. namespace FAE
  7. {
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. [ExecuteInEditMode]
  11. #endif
  12. /// <summary>
  13. /// Sets the wind properties of the FAE shaders
  14. /// </summary>
  15. public class WindController : MonoBehaviour
  16. {
  17. public Texture2D windVectors;
  18. public bool visualizeVectors = false;
  19. /// <summary>
  20. /// Used to retreive the current state of the wind visualization, either on or off
  21. /// </summary>
  22. public static bool _visualizeVectors;
  23. public bool listenToWindZone = false;
  24. public WindZone windZone;
  25. [Range(0f, 1f)]
  26. public float windSpeed = 0.33f;
  27. [Range(0f, 3f)]
  28. public float windStrength = 1f;
  29. [Range(0f, 5f)]
  30. public float windAmplitude = 14f;
  31. [Range(0f, 150f)]
  32. public float trunkWindSpeed = 10f;
  33. [Range(0f, 30f)]
  34. public float trunkWindWeight = 4f;
  35. [Range(0f, 0.99f)]
  36. public float trunkWindSwinging = 0.5f;
  37. //Current wind parameters to be read externally
  38. public static float _windStrength;
  39. public static float _windAmplitude;
  40. /// <summary>
  41. /// Set the wind strength
  42. /// </summary>
  43. /// <param name="value"></param>
  44. public void SetStrength(float value)
  45. {
  46. Shader.SetGlobalFloat("_WindStrength", value);
  47. }
  48. /// <summary>
  49. /// Set the trunk weight
  50. /// </summary>
  51. /// <param name="value"></param>
  52. public void SetTrunkWeight(float value)
  53. {
  54. Shader.SetGlobalFloat("_TrunkWindWeight", value);
  55. }
  56. void OnEnable()
  57. {
  58. #if UNITY_5_5_OR_NEWER
  59. visualizeVectors = (Shader.GetGlobalFloat("_WindDebug") == 1) ? true : false;
  60. #endif
  61. #if UNITY_EDITOR
  62. if (windVectors == null) windVectors = GetDefaultWindVectors();
  63. #endif
  64. SetShaderParameters();
  65. }
  66. private void OnValidate()
  67. {
  68. if (!windZone && listenToWindZone)
  69. {
  70. if (this.GetComponent<WindZone>())
  71. {
  72. windZone = this.GetComponent<WindZone>();
  73. }
  74. else
  75. {
  76. windZone = FindObjectOfType<WindZone>();
  77. }
  78. }
  79. }
  80. private void Update()
  81. {
  82. if (windZone && listenToWindZone)
  83. {
  84. SetStrength(windStrength * windZone.windMain);
  85. SetTrunkWeight(trunkWindWeight * windZone.windMain);
  86. }
  87. Shader.SetGlobalVector("_WindDirection", this.transform.rotation * Vector3.back);
  88. }
  89. public void Apply()
  90. {
  91. #if UNITY_EDITOR
  92. //Sync the static var to the local var
  93. visualizeVectors = _visualizeVectors;
  94. VisualizeVectors(visualizeVectors);
  95. SetShaderParameters();
  96. #endif
  97. }
  98. private void SetShaderParameters()
  99. {
  100. Shader.SetGlobalTexture("_WindVectors", windVectors);
  101. Shader.SetGlobalFloat("_WindSpeed", windSpeed);
  102. Shader.SetGlobalFloat("_WindStrength", windStrength);
  103. Shader.SetGlobalFloat("_WindAmplitude", windAmplitude);
  104. Shader.SetGlobalVector("_WindDirection", this.transform.rotation * Vector3.back);
  105. Shader.SetGlobalFloat("_TrunkWindSpeed", trunkWindSpeed);
  106. Shader.SetGlobalFloat("_TrunkWindWeight", trunkWindWeight);
  107. Shader.SetGlobalFloat("_TrunkWindSwinging", trunkWindSwinging);
  108. //Set static var
  109. WindController._windStrength = windStrength;
  110. WindController._windAmplitude = windAmplitude;
  111. }
  112. /// <summary>
  113. /// Toggles the visualization of the wind vectors on all shaders that feature wind animations
  114. /// </summary>
  115. /// <param name="state">boolean</param>
  116. public static void VisualizeVectors(bool state)
  117. {
  118. _visualizeVectors = state;
  119. Shader.SetGlobalFloat("_WindDebug", state ? 1f : 0f);
  120. }
  121. #if UNITY_EDITOR
  122. private void OnDisable()
  123. {
  124. VisualizeVectors(false);
  125. Shader.SetGlobalVector("_GlobalWindParams", new Vector4(0, 0, 0, 0));
  126. }
  127. void OnDrawGizmos()
  128. {
  129. Vector3 dir = (transform.position + transform.forward).normalized;
  130. Gizmos.color = Color.magenta;
  131. Vector3 up = transform.up;
  132. Vector3 side = transform.right;
  133. Vector3 end = transform.position + transform.forward * (windSpeed * 10f);
  134. Gizmos.DrawLine(transform.position, end);
  135. float s = windSpeed;
  136. Vector3 front = transform.forward * windSpeed;
  137. Gizmos.DrawLine(end, end - front + up * s);
  138. Gizmos.DrawLine(end, end - front - up * s);
  139. Gizmos.DrawLine(end, end - front + side * s);
  140. Gizmos.DrawLine(end, end - front - side * s);
  141. Gizmos.DrawLine(end - front - side * s, end - front + up * s);
  142. Gizmos.DrawLine(end - front + up * s, end - front + side * s);
  143. Gizmos.DrawLine(end - front + side * s, end - front - up * s);
  144. Gizmos.DrawLine(end - front - up * s, end - front - side * s);
  145. }
  146. [MenuItem("GameObject/3D Object/FAE Wind Controller")]
  147. private static void NewMenuOption()
  148. {
  149. WindController currentWindController = GameObject.FindObjectOfType<WindController>();
  150. if (currentWindController != null)
  151. {
  152. if (EditorUtility.DisplayDialog("FAE Wind Controller", "A WindController object already exists in your scene", "Create anyway", "Cancel"))
  153. {
  154. CreateNewWindController();
  155. }
  156. }
  157. else
  158. {
  159. CreateNewWindController();
  160. }
  161. }
  162. private static void CreateNewWindController()
  163. {
  164. GameObject newWindController = new GameObject()
  165. {
  166. name = "Wind Controller"
  167. };
  168. newWindController.AddComponent<WindController>();
  169. Undo.RegisterCreatedObjectUndo(newWindController, "Created Wind Controller");
  170. }
  171. private Texture2D GetDefaultWindVectors()
  172. {
  173. string[] assets = AssetDatabase.FindAssets("FAE_WindVectors t:Texture2D");
  174. if (assets.Length > 0)
  175. {
  176. string path = AssetDatabase.GUIDToAssetPath(assets[0]);
  177. return (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
  178. }
  179. else
  180. {
  181. return null;
  182. }
  183. }
  184. #endif
  185. }
  186. }