GrassShaderGUI.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. [ExecuteInEditMode]
  10. public class GrassShaderGUI : ShaderGUI
  11. {
  12. MaterialProperty _MaskClipValue;
  13. //Main maps
  14. MaterialProperty _MainTex;
  15. MaterialProperty _BumpMap;
  16. //Color
  17. MaterialProperty _ColorTop;
  18. MaterialProperty _ColorBottom;
  19. MaterialProperty _ColorVariation;
  20. MaterialProperty _AmbientOcclusion;
  21. MaterialProperty _TransmissionSize;
  22. MaterialProperty _TransmissionAmount;
  23. //Animation
  24. MaterialProperty _MaxWindStrength;
  25. MaterialProperty _WindSwinging;
  26. MaterialProperty _WindAmplitudeMultiplier;
  27. MaterialProperty _BendingInfluence;
  28. //VS Touch Bend
  29. #if TOUCH_REACT
  30. MaterialProperty _VS_TOUCHBEND;
  31. MaterialProperty _BendingTint;
  32. #endif
  33. //Heightmap
  34. MaterialProperty _HeightmapInfluence;
  35. MaterialProperty _MinHeight;
  36. MaterialProperty _MaxHeight;
  37. //Pigment map
  38. MaterialProperty _PigmentMapInfluence;
  39. MaterialProperty _PigmentMapHeight;
  40. MaterialEditor m_MaterialEditor;
  41. //Meta
  42. bool showHelp;
  43. bool showHelpColor;
  44. bool showHelpAnimation;
  45. bool showHelpBending;
  46. bool showHelpHeightmap;
  47. bool showHelpPigmentmap;
  48. #if UNITY_5_5_OR_NEWER
  49. bool hasPigmentMap = true;
  50. #endif
  51. bool hasWindController;
  52. WindController windController;
  53. GUIContent mainTexName = new GUIContent("Diffuse", "Diffuse (RGB) and Transparency (A)");
  54. GUIContent normalMapName = new GUIContent("Normal Map");
  55. private bool visualizeVectors;
  56. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  57. {
  58. if (windController == null) LocateWindController();
  59. this.FindProperties(props);
  60. //Receive
  61. visualizeVectors = WindController._visualizeVectors;
  62. this.m_MaterialEditor = materialEditor;
  63. //Style similar to Standard shader
  64. m_MaterialEditor.SetDefaultGUIWidths();
  65. m_MaterialEditor.UseDefaultMargins();
  66. EditorGUIUtility.labelWidth = 0f;
  67. //GetGlobalTexture is only available since Unity 5.5
  68. #if UNITY_5_5_OR_NEWER
  69. hasPigmentMap = (Shader.GetGlobalTexture("_PigmentMap")) ? true : false;
  70. #endif
  71. EditorGUI.BeginChangeCheck();
  72. //Draw fields
  73. DoHeader();
  74. DoMapsArea();
  75. DoColorArea();
  76. DoAnimationArea();
  77. DoBendingArea();
  78. DoHeightmapArea();
  79. DoPigmentMapArea();
  80. if (EditorGUI.EndChangeCheck())
  81. {
  82. //Send
  83. WindController.VisualizeVectors(visualizeVectors);
  84. }
  85. GUIHelper.DrawExtraFields(m_MaterialEditor);
  86. GUIHelper.DrawFooter();
  87. }
  88. void DoHeader()
  89. {
  90. EditorGUILayout.BeginHorizontal();
  91. showHelp = GUILayout.Toggle(showHelp, "Toggle help", "Button");
  92. GUILayout.Label("FAE Grass Shader", GUIHelper.Header);
  93. EditorGUILayout.EndHorizontal();
  94. if (showHelp) EditorGUILayout.HelpBox("Please bear in mind, when using custom meshes, that most shader feature require the top of the mesh to be vertex colored.", MessageType.Warning);
  95. }
  96. void DoMapsArea()
  97. {
  98. GUILayout.Label("Main maps", EditorStyles.boldLabel);
  99. EditorGUILayout.BeginHorizontal();
  100. EditorGUILayout.PrefixLabel(_MaskClipValue.displayName);
  101. _MaskClipValue.floatValue = EditorGUILayout.Slider(_MaskClipValue.floatValue, 0f, 1f);
  102. EditorGUILayout.EndHorizontal();
  103. this.m_MaterialEditor.TexturePropertySingleLine(mainTexName, this._MainTex);
  104. this.m_MaterialEditor.TexturePropertySingleLine(normalMapName, this._BumpMap);
  105. EditorGUILayout.Space();
  106. }
  107. void DoColorArea()
  108. {
  109. EditorGUILayout.BeginHorizontal();
  110. showHelpColor = GUILayout.Toggle(showHelpColor, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Color", EditorStyles.boldLabel);
  111. EditorGUILayout.EndHorizontal();
  112. EditorGUI.BeginDisabledGroup(_PigmentMapInfluence.floatValue == 1);
  113. m_MaterialEditor.ShaderProperty(_ColorTop, "Top");
  114. m_MaterialEditor.ShaderProperty(_ColorBottom, "Bottom");
  115. EditorGUI.EndDisabledGroup();
  116. if (_PigmentMapInfluence.floatValue == 1)
  117. {
  118. EditorGUILayout.HelpBox("These colors are disabled because the pigment map influence value is set to 1, so they would have no effect", MessageType.None);
  119. }
  120. m_MaterialEditor.ShaderProperty(_ColorVariation, _ColorVariation.displayName);
  121. if (showHelpColor) EditorGUILayout.HelpBox("Vizualises the wind by adding a slight white tint. When the wind strength is set 0, this effect doesn't appear", MessageType.None);
  122. m_MaterialEditor.ShaderProperty(_AmbientOcclusion, _AmbientOcclusion.displayName);
  123. if (showHelpColor) EditorGUILayout.HelpBox("Darkens the areas of the mesh where vertex colors are applied", MessageType.None);
  124. m_MaterialEditor.ShaderProperty(_TransmissionAmount, _TransmissionAmount.displayName);
  125. if (showHelpColor) EditorGUILayout.HelpBox("Simulates light passing through the material. This will have no effect on short grass.", MessageType.None);
  126. m_MaterialEditor.ShaderProperty(_TransmissionSize, _TransmissionSize.displayName);
  127. EditorGUILayout.Space();
  128. }
  129. void DoAnimationArea()
  130. {
  131. EditorGUILayout.BeginHorizontal();
  132. showHelpAnimation = GUILayout.Toggle(showHelpAnimation, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Wind animation", EditorStyles.boldLabel);
  133. EditorGUILayout.EndHorizontal();
  134. visualizeVectors = EditorGUILayout.Toggle("Visualize wind", visualizeVectors);
  135. #if !VEGETATION_STUDIO_PRO //VS Pro has an FAE wind controller
  136. if (!hasWindController)
  137. {
  138. 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);
  139. EditorGUI.BeginDisabledGroup(true);
  140. }
  141. #else
  142. EditorGUI.BeginDisabledGroup(false);
  143. #endif
  144. 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);
  145. m_MaterialEditor.ShaderProperty(_MaxWindStrength, _MaxWindStrength.displayName);
  146. if (showHelpAnimation) EditorGUILayout.HelpBox("Determines how much influence the wind has on the object", MessageType.None);
  147. m_MaterialEditor.ShaderProperty(_WindSwinging, _WindSwinging.displayName);
  148. if (showHelpAnimation) EditorGUILayout.HelpBox("Higher values mean the object always sways back against the wind direction", MessageType.None);
  149. EditorGUILayout.BeginHorizontal();
  150. EditorGUILayout.LabelField(_WindAmplitudeMultiplier.displayName);
  151. _WindAmplitudeMultiplier.floatValue = EditorGUILayout.FloatField(_WindAmplitudeMultiplier.floatValue, GUILayout.Width(65f));
  152. EditorGUILayout.EndHorizontal();
  153. if (showHelpAnimation) EditorGUILayout.HelpBox("Multiply the wind amplitude for this material. Essentally this is the size of the wind waves.", MessageType.None);
  154. if (!hasWindController)
  155. {
  156. EditorGUI.EndDisabledGroup();
  157. }
  158. if (hasWindController && showHelpAnimation)
  159. {
  160. GUIHelper.DrawWindInfo();
  161. }
  162. EditorGUILayout.Space();
  163. }
  164. void DoBendingArea()
  165. {
  166. EditorGUILayout.BeginHorizontal();
  167. showHelpBending = GUILayout.Toggle(showHelpBending, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Bending", EditorStyles.boldLabel);
  168. EditorGUILayout.EndHorizontal();
  169. #if TOUCH_REACT
  170. m_MaterialEditor.ShaderProperty(_VS_TOUCHBEND, new GUIContent("Use Vegetation Studio TouchBend"));
  171. if (showHelpBending) EditorGUILayout.HelpBox("Utilize Vegetation Studio's TouchBendSystem", MessageType.None);
  172. if (_VS_TOUCHBEND.floatValue == 1)
  173. {
  174. m_MaterialEditor.ShaderProperty(_BendingTint, _BendingTint.displayName);
  175. if (showHelpBending) EditorGUILayout.HelpBox("Darken the grass where it is bending", MessageType.None);
  176. }
  177. else
  178. #endif
  179. {
  180. m_MaterialEditor.ShaderProperty(_BendingInfluence, _BendingInfluence.displayName);
  181. if (showHelpBending) EditorGUILayout.HelpBox("Determines how much influence the FoliageBender script has on the object", MessageType.None);
  182. }
  183. EditorGUILayout.Space();
  184. }
  185. void LocateWindController()
  186. {
  187. //Debug.Log("Searching scene for WindController script");
  188. windController = GameObject.FindObjectOfType<WindController>();
  189. hasWindController = (windController) ? true : false;
  190. }
  191. void DoHeightmapArea()
  192. {
  193. EditorGUILayout.BeginHorizontal();
  194. showHelpHeightmap = GUILayout.Toggle(showHelpHeightmap, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Heightmap", EditorStyles.boldLabel);
  195. EditorGUILayout.EndHorizontal();
  196. #if UNITY_5_5_OR_NEWER
  197. if(!hasPigmentMap)
  198. {
  199. EditorGUILayout.HelpBox("No height map was found, please add the PigmentMapGenerator script to your terrain to enable these features", MessageType.Warning);
  200. _HeightmapInfluence.floatValue = 0;
  201. EditorGUI.BeginDisabledGroup(true);
  202. }
  203. #endif
  204. if (showHelpHeightmap) EditorGUILayout.HelpBox("The heightmap is generated through the PigmentMapGenerator script on your terrain", MessageType.None);
  205. m_MaterialEditor.ShaderProperty(_HeightmapInfluence, "Influence");
  206. if (showHelpHeightmap) EditorGUILayout.HelpBox("Determines the influence the heightmap has on the object", MessageType.None);
  207. m_MaterialEditor.ShaderProperty(_MinHeight, _MinHeight.displayName);
  208. if (showHelpHeightmap) EditorGUILayout.HelpBox("Minimum grass height", MessageType.None);
  209. m_MaterialEditor.ShaderProperty(_MaxHeight, _MaxHeight.displayName);
  210. if (showHelpHeightmap) EditorGUILayout.HelpBox("Maximum grass height", MessageType.None);
  211. EditorGUILayout.Space();
  212. #if UNITY_5_5_OR_NEWER
  213. if (!hasPigmentMap)
  214. {
  215. EditorGUI.EndDisabledGroup();
  216. }
  217. #endif
  218. }
  219. void DoPigmentMapArea()
  220. {
  221. EditorGUILayout.BeginHorizontal();
  222. showHelpPigmentmap = GUILayout.Toggle(showHelpPigmentmap, "?", "Button", GUILayout.Width(25f)); GUILayout.Label("Pigment map", EditorStyles.boldLabel);
  223. EditorGUILayout.EndHorizontal();
  224. #if UNITY_5_5_OR_NEWER
  225. if(!hasPigmentMap)
  226. {
  227. EditorGUILayout.HelpBox("No pigment map was found, please add the PigmentMapGenerator script to your terrain to enable these features", MessageType.Warning);
  228. _PigmentMapInfluence.floatValue = 0;
  229. EditorGUI.BeginDisabledGroup(true);
  230. }
  231. #endif
  232. if (showHelpPigmentmap) EditorGUILayout.HelpBox("The pigment map is generated through the PigmentMapGenerator script on your terrain. It colors the grass by the terrain's color.", MessageType.None);
  233. m_MaterialEditor.ShaderProperty(_PigmentMapInfluence, "Influence");
  234. if (showHelpPigmentmap) EditorGUILayout.HelpBox("Determines how much the object should be colored through the pigment map", MessageType.None);
  235. m_MaterialEditor.ShaderProperty(_PigmentMapHeight, "Height");
  236. if (showHelpPigmentmap) EditorGUILayout.HelpBox("With this parameter you can choose to only color the base of the grass", MessageType.None);
  237. EditorGUILayout.Space();
  238. if (showHelpPigmentmap) EditorGUILayout.HelpBox("If your grass is completely white, bring the Influence parameter to 0. Or add the PigmentmapGenerator script to your terrain.", MessageType.Info);
  239. #if UNITY_5_4 && !UNITY_5_5_OR_NEWER
  240. if (showHelpPigmentmap) EditorGUILayout.HelpBox("In versions older than Unity 5.5, it is possible for your grass to still be colored by the last pigment map generated", MessageType.Info);
  241. #endif
  242. EditorGUILayout.Space();
  243. #if UNITY_5_5_OR_NEWER
  244. if (!hasPigmentMap)
  245. {
  246. EditorGUI.EndDisabledGroup();
  247. }
  248. #endif
  249. }
  250. public void FindProperties(MaterialProperty[] props)
  251. {
  252. //Rendering
  253. _MaskClipValue = FindProperty("_Cutoff", props);
  254. //Main maps
  255. _MainTex = FindProperty("_MainTex", props);
  256. _BumpMap = FindProperty("_BumpMap", props);
  257. //Color
  258. _ColorTop = FindProperty("_ColorTop", props);
  259. _ColorBottom = FindProperty("_ColorBottom", props);
  260. _ColorVariation = FindProperty("_ColorVariation", props);
  261. _AmbientOcclusion = FindProperty("_AmbientOcclusion", props);
  262. _TransmissionSize = FindProperty("_TransmissionSize", props);
  263. _TransmissionAmount = FindProperty("_TransmissionAmount", props);
  264. //Animation
  265. _MaxWindStrength = FindProperty("_MaxWindStrength", props);
  266. _WindSwinging = FindProperty("_WindSwinging", props);
  267. _WindAmplitudeMultiplier = FindProperty("_WindAmplitudeMultiplier", props);
  268. _BendingInfluence = FindProperty("_BendingInfluence", props);
  269. #if TOUCH_REACT
  270. //TouchBend
  271. _VS_TOUCHBEND = FindProperty("_VS_TOUCHBEND", props);
  272. _BendingTint = FindProperty("_BendingTint", props);
  273. #endif
  274. //Heightmap
  275. _HeightmapInfluence = FindProperty("_HeightmapInfluence", props);
  276. _MinHeight = FindProperty("_MinHeight", props);
  277. _MaxHeight = FindProperty("_MaxHeight", props);
  278. //Pigment map
  279. _PigmentMapInfluence = FindProperty("_PigmentMapInfluence", props);
  280. _PigmentMapHeight = FindProperty("_PigmentMapHeight", props);
  281. }
  282. }
  283. }