TreeBranchShaderGUI.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 TreeBranchShaderGUI : ShaderGUI
  10. {
  11. MaterialProperty _MaskClipValue;
  12. MaterialProperty _UseSpeedTreeWind;
  13. //Main maps
  14. MaterialProperty _MainTex;
  15. MaterialProperty _BumpMap;
  16. //Color
  17. MaterialProperty _Color;
  18. MaterialProperty _HueVariation;
  19. MaterialProperty _AmbientOcclusion;
  20. MaterialProperty _TransmissionColor;
  21. MaterialProperty _GradientBrightness;
  22. MaterialProperty _Smoothness;
  23. MaterialProperty _FlatLighting;
  24. //Animation
  25. MaterialProperty _MaxWindStrength;
  26. MaterialProperty _WindAmplitudeMultiplier;
  27. MaterialEditor m_MaterialEditor;
  28. //Meta
  29. bool showHelp;
  30. bool showHelpColor;
  31. bool showHelpAnimation;
  32. bool hasWindController;
  33. WindController windController;
  34. GUIContent mainTexName = new GUIContent("Diffuse", "Diffuse (RGB) and Transparency (A)");
  35. GUIContent normalMapName = new GUIContent("Normal Map");
  36. private Material targetMat;
  37. private bool visualizeVectors;
  38. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  39. {
  40. if (windController == null) LocateWindController();
  41. //Receive
  42. visualizeVectors = WindController._visualizeVectors;
  43. this.m_MaterialEditor = materialEditor;
  44. targetMat = (Material)materialEditor.target;
  45. this.FindProperties(props);
  46. //Style similar to Standard shader
  47. m_MaterialEditor.SetDefaultGUIWidths();
  48. m_MaterialEditor.UseDefaultMargins();
  49. EditorGUIUtility.labelWidth = 0f;
  50. #if UNITY_2019_3_OR_NEWER
  51. if (UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline != null &&
  52. !targetMat.shader.name.Contains("Universal Render Pipeline"))
  53. {
  54. 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);
  55. EditorGUILayout.Space();
  56. }
  57. #endif
  58. EditorGUI.BeginChangeCheck();
  59. //Draw fields
  60. DoHeader();
  61. DoMapsArea();
  62. DoColorArea();
  63. DoAnimationArea();
  64. if (EditorGUI.EndChangeCheck())
  65. {
  66. //Send
  67. WindController.VisualizeVectors(visualizeVectors);
  68. }
  69. GUILayout.Label("Advanced Options", EditorStyles.boldLabel);
  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 Tree Branch Shader", GUIHelper.Header);
  78. EditorGUILayout.EndHorizontal();
  79. if (showHelp) EditorGUILayout.HelpBox("Please bear in mind, when using custom meshes, that most shader features require the Ambient Occlusion to be baked into the RGB vertex colors.", 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, this._Color);
  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(_HueVariation, _HueVariation.displayName);
  98. if (showHelpColor) EditorGUILayout.HelpBox("Uses the object's world-space position to add a color variation. This effect is controlled through the alpha channel.\n\n Note: Does not work with meshes that are batched or combined", MessageType.None);
  99. m_MaterialEditor.ShaderProperty(_AmbientOcclusion, _AmbientOcclusion.displayName);
  100. if (showHelpColor) EditorGUILayout.HelpBox("Darkens the areas of the mesh where red vertex colors are applied", MessageType.None);
  101. m_MaterialEditor.ShaderProperty(_TransmissionColor, new GUIContent("Translucency"));
  102. if (showHelpColor) EditorGUILayout.HelpBox("Simulates light passing through the material. The alpha channel controls the intensity. This effect is controlled through the blue channel.", MessageType.None);
  103. m_MaterialEditor.ShaderProperty(_GradientBrightness, _GradientBrightness.displayName);
  104. if (showHelpColor) EditorGUILayout.HelpBox("Adds a gradient to the branch mesh from bottom to top. This information is stored in the alpha vertex color channel.\n\nWithout this information, the parameter will have no effect.", MessageType.None);
  105. m_MaterialEditor.ShaderProperty(_Smoothness, _Smoothness.displayName);
  106. if (showHelpColor) EditorGUILayout.HelpBox("Determines the shininess of the material", MessageType.None);
  107. m_MaterialEditor.ShaderProperty(_FlatLighting, _FlatLighting.displayName);
  108. if (showHelpColor) EditorGUILayout.HelpBox("A value of 1 makes the mesh normals point upwards. For some trees this is necessary to achieve the best visual result.", MessageType.None);
  109. EditorGUILayout.Space();
  110. }
  111. void DoAnimationArea()
  112. {
  113. EditorGUILayout.BeginHorizontal();
  114. showHelpAnimation = GUILayout.Toggle(showHelpAnimation, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Wind animation", EditorStyles.boldLabel);
  115. EditorGUILayout.EndHorizontal();
  116. #if !VEGETATION_STUDIO_PRO //VS Pro has an FAE wind controller
  117. if (!hasWindController)
  118. {
  119. EditorGUILayout.HelpBox("No \"WindController\" component was found in your scene. Please add this script to an empty GameObject\n\nGo to GameObject->3D Object to create one", MessageType.Warning);
  120. EditorGUI.BeginDisabledGroup(true);
  121. }
  122. #else
  123. EditorGUI.BeginDisabledGroup(false);
  124. #endif
  125. #if UNITY_2019_3_OR_NEWER
  126. if (UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline == null)
  127. {
  128. #endif
  129. visualizeVectors = EditorGUILayout.Toggle("Visualize wind", visualizeVectors);
  130. #if UNITY_2019_3_OR_NEWER
  131. }
  132. #endif
  133. 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);
  134. m_MaterialEditor.ShaderProperty(_UseSpeedTreeWind, new GUIContent("Sample SpeedTree wind"));
  135. if (showHelpAnimation) EditorGUILayout.HelpBox("If this is a tree created using the Unity SpeedTree Modeler, this toggle will make the shader read the wind information as stored by SpeedTree.", MessageType.None);
  136. m_MaterialEditor.ShaderProperty(_MaxWindStrength, new GUIContent("Max wind strength"));
  137. if (showHelpAnimation) EditorGUILayout.HelpBox("Determines how much influence the wind has on the branches", MessageType.None);
  138. EditorGUILayout.BeginHorizontal();
  139. EditorGUILayout.LabelField(new GUIContent("Wind Amplitude Multiplier"));
  140. _WindAmplitudeMultiplier.floatValue = EditorGUILayout.FloatField(_WindAmplitudeMultiplier.floatValue, GUILayout.Width(65f));
  141. EditorGUILayout.EndHorizontal();
  142. //m_MaterialEditor.ShaderProperty(_WindAmplitudeMultiplier, _WindAmplitudeMultiplier.displayName);
  143. if (showHelpAnimation) EditorGUILayout.HelpBox("Multiply the wind amplitude for this material. Essentally this is the size of the wind waves.", MessageType.None);
  144. if (hasWindController && showHelpAnimation)
  145. {
  146. GUIHelper.DrawWindInfo();
  147. }
  148. EditorGUI.EndDisabledGroup();
  149. EditorGUILayout.Space();
  150. }
  151. void LocateWindController()
  152. {
  153. //Debug.Log("Searching scene for WindController script");
  154. windController = GameObject.FindObjectOfType<WindController>();
  155. hasWindController = (windController) ? true : false;
  156. }
  157. public void FindProperties(MaterialProperty[] props)
  158. {
  159. //Rendering
  160. _MaskClipValue = FindProperty("_Cutoff", props);
  161. _UseSpeedTreeWind = FindProperty("_UseSpeedTreeWind", props);
  162. //Main maps
  163. _Color = FindProperty("_Color", props);
  164. _MainTex = FindProperty("_MainTex", props);
  165. if(targetMat.HasProperty("_BumpMap")) _BumpMap = FindProperty("_BumpMap", props);
  166. //Color
  167. _HueVariation = FindProperty("_HueVariation", props);
  168. _AmbientOcclusion = FindProperty("_AmbientOcclusion", props);
  169. _TransmissionColor = FindProperty("_TransmissionColor", props);
  170. _GradientBrightness = FindProperty("_GradientBrightness", props);
  171. _Smoothness = FindProperty("_Smoothness", props);
  172. _FlatLighting = FindProperty("_FlatLighting", props);
  173. //Animation
  174. _MaxWindStrength = FindProperty("_MaxWindStrength", props);
  175. _WindAmplitudeMultiplier = FindProperty("_WindAmplitudeMultiplier", props);
  176. }
  177. }
  178. }