TreeTrunkShaderGUI.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 TreeTrunkShaderGUI : ShaderGUI
  10. {
  11. //Main maps
  12. MaterialProperty _MainTex;
  13. MaterialProperty _BumpMap;
  14. MaterialProperty _UseSpeedTreeWind;
  15. //Color
  16. MaterialProperty _AmbientOcclusion;
  17. MaterialProperty _GradientBrightness;
  18. MaterialProperty _Smoothness;
  19. MaterialEditor m_MaterialEditor;
  20. #if UNITY_2019_3_OR_NEWER
  21. private Material targetMat;
  22. #endif
  23. //Meta
  24. bool showHelp;
  25. GUIContent mainTexName = new GUIContent("Diffuse", "Diffuse (RGB) and Transparency (A)");
  26. GUIContent normalMapName = new GUIContent("Normal Map");
  27. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  28. {
  29. this.m_MaterialEditor = materialEditor;
  30. #if UNITY_2019_3_OR_NEWER
  31. targetMat = materialEditor.target as Material;
  32. #endif
  33. this.FindProperties(props);
  34. //Style similar to Standard shader
  35. m_MaterialEditor.SetDefaultGUIWidths();
  36. m_MaterialEditor.UseDefaultMargins();
  37. EditorGUIUtility.labelWidth = 0f;
  38. #if UNITY_2019_3_OR_NEWER
  39. if (UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline != null &&
  40. !targetMat.shader.name.Contains("Universal Render Pipeline"))
  41. {
  42. EditorGUILayout.HelpBox("Universal 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);
  43. EditorGUILayout.Space();
  44. }
  45. #endif
  46. EditorGUI.BeginChangeCheck();
  47. //Draw fields
  48. DoHeader();
  49. DoMapsArea();
  50. DoColorArea();
  51. if (EditorGUI.EndChangeCheck())
  52. {
  53. //Apply changes
  54. }
  55. GUILayout.Label("Advanced Options", EditorStyles.boldLabel);
  56. GUIHelper.DrawExtraFields(m_MaterialEditor);
  57. m_MaterialEditor.ShaderProperty(_UseSpeedTreeWind, new GUIContent("Sample SpeedTree wind"));
  58. if (showHelp) EditorGUILayout.HelpBox("If this is a tree created using the SpeedTree Unity Modeler, this toggle will make the shader read the wind information as stored by SpeedTree.", MessageType.None);
  59. if(showHelp) GUIHelper.DrawWindInfo();
  60. GUIHelper.DrawFooter();
  61. }
  62. void DoHeader()
  63. {
  64. EditorGUILayout.BeginHorizontal();
  65. showHelp = GUILayout.Toggle(showHelp, "Toggle help", "Button");
  66. GUILayout.Label("FAE Tree Trunk Shader", GUIHelper.Header);
  67. EditorGUILayout.EndHorizontal();
  68. if (showHelp) EditorGUILayout.HelpBox("Tree trunk shader, featuring global wind motion", MessageType.Info);
  69. }
  70. void DoMapsArea()
  71. {
  72. GUILayout.Label("Main maps", EditorStyles.boldLabel);
  73. this.m_MaterialEditor.TexturePropertySingleLine(mainTexName, this._MainTex);
  74. this.m_MaterialEditor.TexturePropertySingleLine(normalMapName, this._BumpMap);
  75. EditorGUILayout.Space();
  76. }
  77. void DoColorArea()
  78. {
  79. m_MaterialEditor.ShaderProperty(_AmbientOcclusion, _AmbientOcclusion.displayName);
  80. if (showHelp) EditorGUILayout.HelpBox("Darkens the areas of the mesh where red vertex colors are applied", MessageType.None);
  81. m_MaterialEditor.ShaderProperty(_GradientBrightness, _GradientBrightness.displayName);
  82. if (showHelp) 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);
  83. m_MaterialEditor.ShaderProperty(_Smoothness, _Smoothness.displayName);
  84. if (showHelp) EditorGUILayout.HelpBox("Multiplies the value of the texture's alpha channel to decrease the roughness amount", MessageType.None);
  85. EditorGUILayout.Space();
  86. }
  87. public void FindProperties(MaterialProperty[] props)
  88. {
  89. _UseSpeedTreeWind = FindProperty("_UseSpeedTreeWind", props);
  90. //Main maps
  91. _MainTex = FindProperty("_MainTex", props);
  92. _BumpMap = FindProperty("_BumpMap", props);
  93. //Color
  94. _AmbientOcclusion = FindProperty("_AmbientOcclusion", props);
  95. _GradientBrightness = FindProperty("_GradientBrightness", props);
  96. _Smoothness = FindProperty("_Smoothness", props);
  97. }
  98. }
  99. }