FoliageShaderGUI.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Fantasy Adventure Environment
  2. // Copyright Staggart Creations
  3. // staggart.xyz
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections;
  7. namespace FAE
  8. {
  9. public class FoliageShaderGUI : ShaderGUI
  10. {
  11. MaterialProperty _MaskClipValue;
  12. //Main maps
  13. MaterialProperty _MainTex;
  14. MaterialProperty _BumpMap;
  15. MaterialProperty _Color;
  16. //Color
  17. MaterialProperty _WindTint;
  18. MaterialProperty _AmbientOcclusion;
  19. MaterialProperty _TransmissionSize;
  20. MaterialProperty _TransmissionAmount;
  21. //Animation
  22. MaterialProperty _MaxWindStrength;
  23. MaterialProperty _GlobalWindMotion;
  24. MaterialProperty _LeafFlutter;
  25. MaterialProperty _WindAmplitudeMultiplier;
  26. MaterialProperty _WindSwinging;
  27. MaterialProperty _BendingInfluence;
  28. MaterialEditor m_MaterialEditor;
  29. private Material targetMat;
  30. //Meta
  31. bool showHelp;
  32. bool showHelpColor;
  33. bool showHelpAnimation;
  34. bool hasWindController;
  35. WindController windController;
  36. GUIContent mainTexName = new GUIContent("Diffuse", "Diffuse (RGB) and Transparency (A)");
  37. GUIContent normalMapName = new GUIContent("Normal Map");
  38. private bool visualizeVectors;
  39. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  40. {
  41. if (windController == null) LocateWindController();
  42. //Receive
  43. visualizeVectors = WindController._visualizeVectors;
  44. this.m_MaterialEditor = materialEditor;
  45. targetMat = (Material)materialEditor.target;
  46. this.FindProperties(props);
  47. //Style similar to Standard shader
  48. m_MaterialEditor.SetDefaultGUIWidths();
  49. m_MaterialEditor.UseDefaultMargins();
  50. EditorGUIUtility.labelWidth = 0f;
  51. #if UNITY_2019_3_OR_NEWER
  52. if (UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline != null &&
  53. !targetMat.shader.name.Contains("Universal Render Pipeline"))
  54. {
  55. EditorGUILayout.HelpBox("A render pipeline is in use, but this material is using a shader for the Built-in render pipeline.\n\nShaders and materials can be converted through the Help window", MessageType.Error);
  56. EditorGUILayout.Space();
  57. }
  58. #endif
  59. EditorGUI.BeginChangeCheck();
  60. //Draw fields
  61. DoHeader();
  62. DoMapsArea();
  63. DoColorArea();
  64. DoAnimationArea();
  65. if (EditorGUI.EndChangeCheck())
  66. {
  67. //Send
  68. WindController.VisualizeVectors(visualizeVectors);
  69. }
  70. GUIHelper.DrawExtraFields(m_MaterialEditor);
  71. GUIHelper.DrawFooter();
  72. }
  73. void DoHeader()
  74. {
  75. EditorGUILayout.BeginHorizontal();
  76. showHelp = GUILayout.Toggle(showHelp, "Toggle help", "Button");
  77. GUILayout.Label("FAE Foliage Shader", GUIHelper.Header);
  78. EditorGUILayout.EndHorizontal();
  79. if (showHelp) EditorGUILayout.HelpBox("Please bear in mind, when using custom meshes, that most shader feature require the tips of the mesh to be vertex colored.\n\nBaking Ambient Occlusion into the vertex colors will yield correct results.", MessageType.Warning);
  80. }
  81. void DoMapsArea()
  82. {
  83. GUILayout.Label("Main maps", EditorStyles.boldLabel);
  84. EditorGUILayout.BeginHorizontal();
  85. EditorGUILayout.PrefixLabel(_MaskClipValue.displayName);
  86. _MaskClipValue.floatValue = EditorGUILayout.Slider(_MaskClipValue.floatValue, 0f, 1f);
  87. EditorGUILayout.EndHorizontal();
  88. this.m_MaterialEditor.TexturePropertySingleLine(mainTexName, this._MainTex);
  89. if(targetMat.HasProperty("_BumpMap"))this.m_MaterialEditor.TexturePropertySingleLine(normalMapName, this._BumpMap);
  90. EditorGUILayout.Space();
  91. }
  92. void DoColorArea()
  93. {
  94. EditorGUILayout.BeginHorizontal();
  95. showHelpColor = GUILayout.Toggle(showHelpColor, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Color", EditorStyles.boldLabel);
  96. EditorGUILayout.EndHorizontal();
  97. m_MaterialEditor.ShaderProperty(_Color, _Color.displayName);
  98. m_MaterialEditor.ShaderProperty(_AmbientOcclusion, _AmbientOcclusion.displayName);
  99. if (showHelpColor) EditorGUILayout.HelpBox("Darkens the areas of the mesh where vertex colors are applied", MessageType.None);
  100. m_MaterialEditor.ShaderProperty(_TransmissionAmount, _TransmissionAmount.displayName);
  101. if (showHelpColor) EditorGUILayout.HelpBox("Simulates light passing through the material. This will have no effect on short grass.", MessageType.None);
  102. m_MaterialEditor.ShaderProperty(_TransmissionSize, _TransmissionSize.displayName);
  103. EditorGUILayout.Space();
  104. }
  105. void DoAnimationArea()
  106. {
  107. EditorGUILayout.BeginHorizontal();
  108. showHelpAnimation = GUILayout.Toggle(showHelpAnimation, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Wind animation", EditorStyles.boldLabel);
  109. EditorGUILayout.EndHorizontal();
  110. #if UNITY_2019_3_OR_NEWER
  111. if (UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline == null)
  112. {
  113. #endif
  114. visualizeVectors = EditorGUILayout.Toggle("Visualize wind", visualizeVectors);
  115. #if UNITY_2019_3_OR_NEWER
  116. }
  117. #endif
  118. #if !VEGETATION_STUDIO_PRO //VS Pro has an FAE wind controller
  119. if (!hasWindController)
  120. {
  121. EditorGUILayout.HelpBox("No \"WindController\" component was found in your scene. Please add this script to an empty GameObject\n\nA prefab can be found in the Prefabs/Effects folder.", MessageType.Warning);
  122. EditorGUI.BeginDisabledGroup(true);
  123. }
  124. #else
  125. EditorGUI.BeginDisabledGroup(false);
  126. #endif
  127. if (showHelpAnimation) EditorGUILayout.HelpBox("Toggle a visualisation of the wind vectors on all the objects that use FAE shaders featuring wind.\n\nThis allows you to more clearly see the effects of the settings.", MessageType.None);
  128. m_MaterialEditor.ShaderProperty(_MaxWindStrength, _MaxWindStrength.displayName);
  129. if (showHelpAnimation) EditorGUILayout.HelpBox("Determines how much influence the wind has on the object", MessageType.None);
  130. m_MaterialEditor.ShaderProperty(_GlobalWindMotion, _GlobalWindMotion.displayName);
  131. if (showHelpAnimation) EditorGUILayout.HelpBox("Back and forth motion", MessageType.None);
  132. m_MaterialEditor.ShaderProperty(_LeafFlutter, _LeafFlutter.displayName);
  133. if (showHelpAnimation) EditorGUILayout.HelpBox("Local wind turbulence", MessageType.None);
  134. m_MaterialEditor.ShaderProperty(_WindAmplitudeMultiplier, _WindAmplitudeMultiplier.displayName);
  135. if (showHelpAnimation) EditorGUILayout.HelpBox("Multiply the wind amplitude for this material.Essentally this is the size of the wind waves.", MessageType.None);
  136. m_MaterialEditor.ShaderProperty(_WindSwinging, _WindSwinging.displayName);
  137. if (showHelpAnimation) EditorGUILayout.HelpBox("Higher values mean the object always sways back against the wind direction", MessageType.None);
  138. if (targetMat.HasProperty("_WindTint"))
  139. {
  140. m_MaterialEditor.ShaderProperty(_WindTint, _WindTint.displayName);
  141. if (showHelpAnimation)
  142. EditorGUILayout.HelpBox("Vizualises the wind by adding a slight tint, either dark (<0) or light (>0)", MessageType.None);
  143. }
  144. if (showHelpAnimation) EditorGUILayout.HelpBox("Multiply the wind amplitude for this material. Essentally this is the size of the wind waves.", MessageType.None);
  145. if (!hasWindController)
  146. {
  147. EditorGUI.EndDisabledGroup();
  148. }
  149. m_MaterialEditor.ShaderProperty(_BendingInfluence, _BendingInfluence.displayName);
  150. if (showHelpAnimation) EditorGUILayout.HelpBox("Determines how much influence the FoliageBender script has on the object", MessageType.None);
  151. if (hasWindController && showHelpAnimation)
  152. {
  153. GUIHelper.DrawWindInfo();
  154. }
  155. EditorGUILayout.Space();
  156. }
  157. void LocateWindController()
  158. {
  159. //Debug.Log("Searching scene for WindController script");
  160. windController = GameObject.FindObjectOfType<WindController>();
  161. hasWindController = (windController) ? true : false;
  162. }
  163. public void FindProperties(MaterialProperty[] props)
  164. {
  165. //Rendering
  166. #if UNITY_2017_1_OR_NEWER
  167. _MaskClipValue = FindProperty("_Cutoff", props);
  168. #else
  169. _MaskClipValue = FindProperty("_Cutoff", props);
  170. #endif
  171. //Main maps
  172. _MainTex = FindProperty("_MainTex", props);
  173. if(targetMat.HasProperty("_BumpMap")) _BumpMap = FindProperty("_BumpMap", props);
  174. //Color
  175. if(targetMat.HasProperty("_WindTint")) _WindTint = FindProperty("_WindTint", props);
  176. _Color = FindProperty("_Color", props);
  177. _AmbientOcclusion = FindProperty("_AmbientOcclusion", props);
  178. _TransmissionSize = FindProperty("_TransmissionSize", props);
  179. _TransmissionAmount = FindProperty("_TransmissionAmount", props);
  180. //Animation
  181. _MaxWindStrength = FindProperty("_MaxWindStrength", props);
  182. _GlobalWindMotion = FindProperty("_GlobalWindMotion", props);
  183. _LeafFlutter = FindProperty("_LeafFlutter", props);
  184. _WindAmplitudeMultiplier = FindProperty("_WindAmplitudeMultiplier", props);
  185. _WindSwinging = FindProperty("_WindSwinging", props);
  186. _BendingInfluence = FindProperty("_BendingInfluence", props);
  187. }
  188. }
  189. }