StealthMaterialInspector.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Linq;
  5. public class StealthMaterialInspector : MaterialEditor {
  6. //EditorVariables
  7. static bool customEditor = true;
  8. static bool vis = false;
  9. static bool s1= true,s2= true;
  10. static bool triplanarToggle;
  11. static string[] options = new string[] {"UV2", "Triplanar"};
  12. static int optionsindex = 0;
  13. static string[] options2 = new string[] {"UV1", "Triplanar"};
  14. static int optionsindex2 = 0;
  15. Material mat;
  16. int selected ;
  17. public override void OnInspectorGUI()
  18. {
  19. base.serializedObject.Update();
  20. var theShader = serializedObject.FindProperty ("m_Shader");
  21. if (isVisible && !theShader.hasMultipleDifferentValues && theShader.objectReferenceValue != null )
  22. {
  23. Shader shader = theShader.objectReferenceValue as Shader;
  24. mat = target as Material;
  25. if (mat.GetFloat ("_VisibleEffect") == 1) {
  26. vis = true;
  27. } else {
  28. vis = false;
  29. }
  30. //optionsindex = (int) mat.GetFloat ("_TriplanarUV2");
  31. mat.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)EditorGUILayout.EnumPopup( "Emission GI", mat.globalIlluminationFlags);
  32. GUILayout.Space(5);
  33. //CUSTOM EDITOR SECTIONS
  34. customEditor = GUILayout.Toggle (customEditor, "Use custom material editor.");
  35. RangeProperty ("_Stealth", "Stealth",0,1);
  36. GUILayout.Space (5);
  37. FloatProperty ("_StealthScale", "Stealth Scale");
  38. EditorGUILayout.HelpBox("Increase Stealth Scale if the effect does not cover your mesh completely",MessageType.Info);
  39. GUILayout.Space (5);
  40. GUILayout.BeginHorizontal ();
  41. GUILayout.Label("Pattern Mapping: ");
  42. int selectedoption2 = Mathf.RoundToInt(mat.GetFloat("_PatternTriplanarUV1"));
  43. optionsindex2 = EditorGUILayout.Popup(selectedoption2, options2);
  44. mat.SetFloat("_PatternTriplanarUV1", optionsindex2);
  45. GUILayout.EndHorizontal ();
  46. GUILayout.BeginHorizontal ();
  47. GUILayout.Label("Stealth Mapping: ");
  48. int selectedoption1 = Mathf.RoundToInt(mat.GetFloat("_TriplanarUV2"));
  49. optionsindex = EditorGUILayout.Popup(selectedoption1, options);
  50. mat.SetFloat("_TriplanarUV2", optionsindex);
  51. GUILayout.EndHorizontal ();
  52. GUILayout.Space (10);
  53. if (customEditor) {
  54. GUILayout.BeginHorizontal ();
  55. Texture coloricon = Resources.Load ("Icons/textures") as Texture;
  56. GUILayout.Label (coloricon,labelStyle());
  57. if (foldButton (s1))
  58. s1 = !s1;
  59. GUILayout.EndHorizontal ();
  60. if (s1){
  61. if(shader.name == "Marc Sureda/Stealth(Metallic Setup)")
  62. MetallicSection ();
  63. else
  64. SpecularSection ();
  65. }
  66. GUILayout.BeginHorizontal ();
  67. Texture mainfoamicon = Resources.Load ("Icons/stealth") as Texture;
  68. GUILayout.Label (mainfoamicon,labelStyle());
  69. if (foldButton (s2))
  70. s2 = !s2;
  71. GUILayout.EndHorizontal ();
  72. if(s2)
  73. StealthSection ();
  74. } else {
  75. //DEFAULT EDITOR
  76. if (this.PropertiesGUI ())
  77. this.PropertiesChanged ();
  78. }
  79. if( GUILayout.Button( "Open shader code") ) {
  80. UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal( AssetDatabase.GetAssetPath( shader ), 1 );
  81. }
  82. GUILayout.Box("Stealth Camouflage shader created by Marc Sureda, 2017");
  83. }
  84. }
  85. GUIStyle labelStyle(){
  86. GUIStyle label = new GUIStyle ();
  87. label.fixedHeight = 25;
  88. label.alignment = TextAnchor.LowerLeft;
  89. return label;
  90. }
  91. GUIStyle headerStyle(){
  92. GUIStyle header = new GUIStyle ();
  93. header.alignment = TextAnchor.MiddleLeft;
  94. header.fontStyle = FontStyle.Bold;
  95. header.fontSize = 11;
  96. return header;
  97. }
  98. public void SpecularSection(){
  99. GUILayout.Space (10);
  100. ColorPickerHDRConfig hdr = new ColorPickerHDRConfig(0,1,0,1);
  101. MaterialProperty albedo = MaterialEditor.GetMaterialProperty (targets, "_MainTex");
  102. MaterialProperty ao = MaterialEditor.GetMaterialProperty (targets, "_AmbientOclussion");
  103. MaterialProperty specular = MaterialEditor.GetMaterialProperty (targets, "_Specular");
  104. MaterialProperty emissive = MaterialEditor.GetMaterialProperty (targets, "_Emissive");
  105. MaterialProperty normalbump = MaterialEditor.GetMaterialProperty (targets, "_BumpMap");
  106. MaterialProperty gloss = MaterialEditor.GetMaterialProperty (targets, "_GlossR");
  107. GUILayout.TextField ("Albedo", headerStyle ());
  108. GUILayout.BeginHorizontal ();
  109. Color color1 = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_Color"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  110. mat.SetColor("_Color", color1);
  111. TextureProperty (albedo, "");
  112. GUILayout.EndHorizontal ();
  113. EditorGUILayout.HelpBox("Alpha as opacity map",MessageType.Info);
  114. GUILayout.TextField ("Specular", headerStyle ());
  115. GUILayout.BeginHorizontal ();
  116. Color colorspec = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_SpecularColor"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  117. mat.SetColor("_SpecularColor", colorspec);
  118. TextureProperty (specular, "");
  119. GUILayout.EndHorizontal ();
  120. RangeProperty("_SpecularIntensity", "Specular Intensity",0,1);
  121. GUILayout.TextField ("Emission", headerStyle ());
  122. GUILayout.BeginHorizontal ();
  123. Color coloremis = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_EmissiveColor"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  124. mat.SetColor("_EmissiveColor", coloremis);
  125. TextureProperty (emissive, "");
  126. GUILayout.EndHorizontal ();
  127. FloatProperty("_PBREmissiveIntensity", "Emissive Intensity");
  128. GUILayout.TextField ("Gloss (R)", headerStyle ());
  129. TextureProperty (gloss, "");
  130. RangeProperty("_GlossIntensity", "Gloss Intensity",0,1);
  131. GUILayout.TextField ("Normal", headerStyle ());
  132. TextureProperty (normalbump, "");
  133. RangeProperty("_NormalIntensity", "Normal Intensity",0,1);
  134. GUILayout.TextField ("Ambient Oclussion", headerStyle ());
  135. TextureProperty (ao, "");
  136. }
  137. public void MetallicSection(){
  138. GUILayout.Space (10);
  139. ColorPickerHDRConfig hdr = new ColorPickerHDRConfig(0,1,0,1);
  140. MaterialProperty albedo = MaterialEditor.GetMaterialProperty (targets, "_MainTex");
  141. MaterialProperty ao = MaterialEditor.GetMaterialProperty (targets, "_AmbientOclussion");
  142. MaterialProperty metallic = MaterialEditor.GetMaterialProperty (targets, "_MetallicR");
  143. MaterialProperty emissive = MaterialEditor.GetMaterialProperty (targets, "_Emissive");
  144. MaterialProperty normalbump = MaterialEditor.GetMaterialProperty (targets, "_BumpMap");
  145. MaterialProperty rough = MaterialEditor.GetMaterialProperty (targets, "_RoughnessR");
  146. GUILayout.TextField ("Albedo", headerStyle ());
  147. GUILayout.BeginHorizontal ();
  148. Color color1 = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_Color"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  149. mat.SetColor("_Color", color1);
  150. TextureProperty (albedo, "");
  151. GUILayout.EndHorizontal ();
  152. EditorGUILayout.HelpBox("Alpha as opacity map",MessageType.Info);
  153. GUILayout.TextField ("Metallic", headerStyle ());
  154. GUILayout.BeginHorizontal ();
  155. TextureProperty (metallic, "");
  156. GUILayout.EndHorizontal ();
  157. RangeProperty("_MetallicIntensity", "Metallic Intensity",0,1);
  158. GUILayout.TextField ("Emission", headerStyle ());
  159. GUILayout.BeginHorizontal ();
  160. Color coloremis = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_EmissiveColor"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  161. mat.SetColor("_EmissiveColor", coloremis);
  162. TextureProperty (emissive, "");
  163. GUILayout.EndHorizontal ();
  164. FloatProperty("_PBREmissiveIntensity", "Emissive Intensity");
  165. GUILayout.TextField ("Roughness (R)", headerStyle ());
  166. TextureProperty (rough, "");
  167. RangeProperty("_RoughnessIntensity", "Roughness Intensity",0,1);
  168. GUILayout.TextField ("Normal", headerStyle ());
  169. TextureProperty (normalbump, "");
  170. RangeProperty("_NormalIntensity", "Normal Intensity",0,1);
  171. GUILayout.TextField ("Ambient Oclussion", headerStyle ());
  172. TextureProperty (ao, "");
  173. }
  174. public void StealthSection(){
  175. GUILayout.Space (10);
  176. string[] options = new string[]
  177. {
  178. "Bottom", "Top",
  179. };
  180. selected = EditorGUILayout.Popup ("Start Gradient", Mathf.RoundToInt( mat.GetFloat ("_StartTopBottom")), options);
  181. mat.SetFloat ("_StartTopBottom", selected);
  182. GUILayout.Space (15);
  183. ColorPickerHDRConfig hdr = new ColorPickerHDRConfig(0,1,0,1);
  184. GUILayout.BeginHorizontal ();
  185. Color colorpattern = EditorGUILayout.ColorField (GUIContent.none, mat.GetColor ("_PatternColor"), true, false, false, hdr, GUILayout.MaxWidth(50),GUILayout.MinHeight(15));
  186. mat.SetColor("_PatternColor", colorpattern);
  187. MaterialProperty pattern = MaterialEditor.GetMaterialProperty (targets, "_Pattern");
  188. TextureProperty (pattern, "Pattern");
  189. GUILayout.EndHorizontal ();
  190. FloatProperty ("_EmissiveIntensity", "Pattern Emissive Intensity");
  191. FloatProperty ("_PatternSpeed", "Pattern Speed");
  192. FloatProperty ("_PatternScale", "Pattern Scale");
  193. GUILayout.Space (15);
  194. vis = EditorGUILayout.Toggle ("Visible to Player", vis);
  195. if (vis) {
  196. mat.SetFloat ("_VisibleEffect", 1);
  197. } else {
  198. mat.SetFloat ("_VisibleEffect", 0);
  199. }
  200. FloatProperty ("_VisibleEffectIntensity", "Visible Effect Intensity");
  201. GUILayout.Space (15);
  202. FloatProperty ("_RefractionIntensity", "Refraction Intensity");
  203. RangeProperty("_MinVisibility", "Min Visibility",0,1);
  204. }
  205. public bool foldButton(bool b){
  206. string path = "Icons/fold";
  207. if (b)
  208. path = "Icons/unfold";
  209. Texture fold = Resources.Load (path) as Texture;
  210. Color c = GUI.backgroundColor;
  211. GUI.backgroundColor = Color.clear;
  212. bool v = GUILayout.Button (fold, GUILayout.MaxWidth(25), GUILayout.MaxHeight(25));
  213. GUI.backgroundColor = c;
  214. return v;
  215. }
  216. }