LayaBlinnPhongGUI.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //#if UNITY_EDITOR
  2. using System;
  3. using UnityEngine;
  4. using UnityEditor;
  5. class LayaShaderGUI : ShaderGUI
  6. {
  7. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  8. {
  9. material.shader = newShader;
  10. material.EnableKeyword("EnableLighting");
  11. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  12. }
  13. public enum RenderMode
  14. {
  15. /**渲染状态_不透明。*/
  16. Opaque = 0,
  17. /**渲染状态_透明测试。*/
  18. Cutout = 1,
  19. /**渲染状态_透明混合。*/
  20. Transparent = 2,
  21. /**渲染状态_自定义。*/
  22. Custom = 3
  23. }
  24. public enum SrcBlendMode
  25. {
  26. //Blend factor is (0, 0, 0, 0).
  27. Zero = 0,
  28. //Blend factor is (1, 1, 1, 1).
  29. One = 1,
  30. //Blend factor is (Rd, Gd, Bd, Ad).
  31. DstColor = 2,
  32. //Blend factor is (Rs, Gs, Bs, As).
  33. SrcColor = 3,
  34. //Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad).
  35. OneMinusDstColor = 4,
  36. //Blend factor is (As, As, As, As).
  37. SrcAlpha = 5,
  38. //Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As).
  39. OneMinusSrcColor = 6,
  40. //Blend factor is (Ad, Ad, Ad, Ad).
  41. DstAlpha = 7,
  42. //Blend factor is (1 - Ad, 1 - Ad, 1 - Ad, 1 - Ad).
  43. OneMinusDstAlpha = 8,
  44. //Blend factor is (f, f, f, 1); where f = min(As, 1 - Ad).
  45. SrcAlphaSaturate = 9,
  46. //Blend factor is (1 - As, 1 - As, 1 - As, 1 - As).
  47. OneMinusSrcAlpha = 10
  48. }
  49. public enum DstBlendMode
  50. {
  51. //Blend factor is (0, 0, 0, 0).
  52. Zero = 0,
  53. //Blend factor is (1, 1, 1, 1).
  54. One = 1,
  55. //Blend factor is (Rd, Gd, Bd, Ad).
  56. DstColor = 2,
  57. //Blend factor is (Rs, Gs, Bs, As).
  58. SrcColor = 3,
  59. //Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad).
  60. OneMinusDstColor = 4,
  61. //Blend factor is (As, As, As, As).
  62. SrcAlpha = 5,
  63. //Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As).
  64. OneMinusSrcColor = 6,
  65. //Blend factor is (Ad, Ad, Ad, Ad).
  66. DstAlpha = 7,
  67. //Blend factor is (1 - Ad, 1 - Ad, 1 - Ad, 1 - Ad).
  68. OneMinusDstAlpha = 8,
  69. //Blend factor is (f, f, f, 1); where f = min(As, 1 - Ad).
  70. SrcAlphaSaturate = 9,
  71. //Blend factor is (1 - As, 1 - As, 1 - As, 1 - As).
  72. OneMinusSrcAlpha = 10
  73. }
  74. public enum CullMode
  75. {
  76. CULL_NONE = 0,
  77. CULL_FRONT = 1,
  78. CULL_BACK = 2,
  79. }
  80. public enum DepthWrite
  81. {
  82. OFF = 0,
  83. ON = 1
  84. }
  85. public enum DepthTest
  86. {
  87. OFF = 0,
  88. Never = 1,
  89. LESS = 2,
  90. EQUAL = 3,
  91. LEQUAL = 4,
  92. GREATER = 5,
  93. NOTEQUAL = 6,
  94. GEQUAL = 7,
  95. ALWAYS = 8
  96. }
  97. public enum LightingMode
  98. {
  99. ON = 0,
  100. OFF = 1,
  101. }
  102. MaterialProperty lighting = null;
  103. MaterialProperty albedoTexture = null;
  104. MaterialProperty albedoColor = null;
  105. MaterialProperty albedoIntensity = null;
  106. MaterialProperty specularTexture = null;
  107. MaterialProperty specularColor = null;
  108. MaterialProperty specularShininess = null;
  109. MaterialProperty normalTexture = null;
  110. MaterialProperty cullMode = null;
  111. MaterialProperty renderMode = null;
  112. MaterialProperty alphaTest = null;
  113. MaterialProperty alphaCutoff = null;
  114. MaterialProperty alphaBlend = null;
  115. MaterialProperty srcBlendMode = null;
  116. MaterialProperty dstBlendMode = null;
  117. MaterialProperty depthWrite = null;
  118. MaterialProperty depthTest = null;
  119. MaterialEditor m_MaterialEditor;
  120. MaterialProperty isVertexColor = null;
  121. public void FindProperties(MaterialProperty[] props)
  122. {
  123. lighting = FindProperty("_Lighting", props);
  124. albedoTexture = FindProperty("_MainTex", props);
  125. albedoColor = FindProperty("_Color", props);
  126. albedoIntensity = FindProperty("_AlbedoIntensity", props);
  127. specularTexture = FindProperty("_SpecGlossMap", props);
  128. specularColor = FindProperty("_SpecColor", props);
  129. specularShininess = FindProperty("_Shininess", props);
  130. normalTexture = FindProperty("_BumpMap", props);
  131. renderMode = FindProperty("_Mode", props);
  132. cullMode = FindProperty("_Cull", props);
  133. alphaTest = FindProperty("_AlphaTest", props, false);
  134. alphaCutoff = FindProperty("_Cutoff", props, false);
  135. alphaBlend = FindProperty("_AlphaBlend", props, false);
  136. srcBlendMode = FindProperty("_SrcBlend", props);
  137. dstBlendMode = FindProperty("_DstBlend", props);
  138. depthWrite = FindProperty("_ZWrite", props);
  139. depthTest = FindProperty("_ZTest", props);
  140. isVertexColor = FindProperty("_IsVertexColor", props);
  141. }
  142. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  143. {
  144. // render the default gui
  145. FindProperties(props);
  146. m_MaterialEditor = materialEditor;
  147. Material material = materialEditor.target as Material;
  148. ShaderPropertiesGUI(material);
  149. }
  150. public void ShaderPropertiesGUI(Material material)
  151. {
  152. // Use default labelWidth
  153. EditorGUIUtility.labelWidth = 0f;
  154. // Detect any changes to the material
  155. EditorGUI.BeginChangeCheck();
  156. {
  157. //renderMode
  158. GUILayout.BeginHorizontal();
  159. GUILayout.Label(Styles.renderModeText, GUILayout.Width(120));
  160. var mode = (RenderMode)renderMode.floatValue;
  161. mode = (RenderMode)EditorGUILayout.Popup((int)mode, Styles.renderModeNames);
  162. GUILayout.EndHorizontal();
  163. //lightingMode
  164. GUILayout.BeginHorizontal();
  165. GUILayout.Label(Styles.lightModeText, GUILayout.Width(120));
  166. var light = (LightingMode)lighting.floatValue;
  167. light = (LightingMode)EditorGUILayout.Popup((int)light, Styles.lightingNames);
  168. GUILayout.EndHorizontal();
  169. //IsVertexColor
  170. m_MaterialEditor.ShaderProperty(isVertexColor, Styles.enableVertexColor);
  171. //Primary properties
  172. GUILayout.Label(Styles.PrimaryText, EditorStyles.boldLabel);
  173. //albedo
  174. m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoTexture, albedoColor);
  175. //albedo Intensity
  176. m_MaterialEditor.ShaderProperty(albedoIntensity, Styles.albedoIntensityText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  177. if (lighting.floatValue == 0)
  178. {
  179. //specular
  180. m_MaterialEditor.TexturePropertySingleLine(Styles.specularText, specularTexture, specularColor);
  181. //specular Shininess
  182. m_MaterialEditor.ShaderProperty(specularShininess, Styles.specularShininessText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  183. //mormal
  184. m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalTexture);
  185. }
  186. //scaleAndOffset
  187. m_MaterialEditor.TextureScaleOffsetProperty(albedoTexture);
  188. GUILayout.Box("", GUILayout.Height(1), GUILayout.ExpandWidth(true));
  189. //Advanced properties
  190. GUILayout.Label(Styles.AdvancedText, EditorStyles.boldLabel);
  191. //alphaTest
  192. m_MaterialEditor.ShaderProperty(alphaTest, Styles.alphaTestText);
  193. if (alphaTest.floatValue == 1)
  194. {
  195. m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText, MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1);
  196. }
  197. //alphaBlend
  198. m_MaterialEditor.ShaderProperty(alphaBlend, Styles.alphaBlendText);
  199. var dstMode = (DstBlendMode)dstBlendMode.floatValue;
  200. var srcMode = (SrcBlendMode)srcBlendMode.floatValue;
  201. if (alphaBlend.floatValue == 1)
  202. {
  203. GUILayout.BeginHorizontal();
  204. GUILayout.Label("", GUILayout.Width(20));
  205. srcMode = (SrcBlendMode)EditorGUILayout.Popup((int)srcMode, Styles.srcBlendNames);
  206. dstMode = (DstBlendMode)EditorGUILayout.Popup((int)dstMode, Styles.dstBlendNames);
  207. GUILayout.EndHorizontal();
  208. }
  209. //depthWrite
  210. GUILayout.BeginHorizontal();
  211. GUILayout.Label(Styles.depthWriteText, GUILayout.Width(120));
  212. var depthW = (DepthWrite)depthWrite.floatValue;
  213. depthW = (DepthWrite)EditorGUILayout.Popup((int)depthW, Styles.depthWriteNames);
  214. GUILayout.EndHorizontal();
  215. //depthTest
  216. GUILayout.BeginHorizontal();
  217. GUILayout.Label(Styles.depthTestText, GUILayout.Width(120));
  218. var depthT = (DepthTest)depthTest.floatValue;
  219. depthT = (DepthTest)EditorGUILayout.Popup((int)depthT, Styles.depthTestNames);
  220. GUILayout.EndHorizontal();
  221. //cullMode
  222. GUILayout.BeginHorizontal();
  223. GUILayout.Label(Styles.cullModeText, GUILayout.Width(120));
  224. var cull = (CullMode)cullMode.floatValue;
  225. cull = (CullMode)EditorGUILayout.Popup((int)cull, Styles.cullModeNames);
  226. GUILayout.EndHorizontal();
  227. if (EditorGUI.EndChangeCheck())
  228. {
  229. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  230. //renderMode
  231. renderMode.floatValue = (float)mode;
  232. //lightMode
  233. lighting.floatValue = (float)light;
  234. material.SetInt("_Lighting", (int)light);
  235. if (lighting.floatValue == 0)
  236. {
  237. material.EnableKeyword("EnableLighting");
  238. }
  239. else
  240. {
  241. material.DisableKeyword("EnableLighting");
  242. }
  243. //cullMode
  244. cullMode.floatValue = (float)cull;
  245. material.SetInt("_Cull", (int)cull);
  246. if ((RenderMode)material.GetFloat("_Mode") == RenderMode.Custom)
  247. {
  248. //alphaTest
  249. if (alphaTest.floatValue == 1)
  250. {
  251. material.EnableKeyword("EnableAlphaCutoff");
  252. material.EnableKeyword("_ALPHATEST_ON");
  253. }
  254. else
  255. {
  256. material.DisableKeyword("EnableAlphaCutoff");
  257. material.DisableKeyword("_ALPHATEST_ON");
  258. }
  259. //alphaBlend
  260. if (alphaBlend.floatValue == 1)
  261. {
  262. srcBlendMode.floatValue = (float)srcMode;
  263. dstBlendMode.floatValue = (float)dstMode;
  264. material.SetInt("_SrcBlend", (int)srcMode);
  265. material.SetInt("_DstBlend", (int)dstMode);
  266. material.EnableKeyword("_ALPHABLEND_ON");
  267. material.SetInt("_AlphaBlend", 1);
  268. }
  269. else
  270. {
  271. material.DisableKeyword("_ALPHABLEND_ON");
  272. material.SetInt("_AlphaBlend", 0);
  273. material.SetInt("_SrcBlend", (int)1);
  274. material.SetInt("_DstBlend", (int)0);
  275. }
  276. //depthWrite
  277. depthWrite.floatValue = (float)depthW;
  278. material.SetInt("_ZWrite", (int)depthW);
  279. //depthTest
  280. depthTest.floatValue = (float)depthT;
  281. material.SetInt("_ZTest", (int)depthT);
  282. }
  283. if (specularTexture.textureValue != null)
  284. {
  285. material.EnableKeyword("SpecularTexture");
  286. }
  287. else
  288. {
  289. material.DisableKeyword("SpecularTexture");
  290. }
  291. if (normalTexture.textureValue != null)
  292. {
  293. material.EnableKeyword("NormalTexture");
  294. }
  295. else
  296. {
  297. material.DisableKeyword("NormalTexture");
  298. }
  299. if (isVertexColor.floatValue == 1)
  300. {
  301. material.EnableKeyword("ENABLEVERTEXCOLOR");
  302. }
  303. else
  304. {
  305. material.DisableKeyword("ENABLEVERTEXCOLOR");
  306. }
  307. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  308. }
  309. }
  310. m_MaterialEditor.RenderQueueField();
  311. }
  312. public void onChangeRender(Material material, RenderMode mode)
  313. {
  314. switch (mode)
  315. {
  316. case RenderMode.Opaque:
  317. material.SetInt("_Mode", 0);
  318. material.SetInt("_AlphaTest", 0);
  319. material.SetInt("_AlphaBlend", 0);
  320. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  321. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  322. material.SetInt("_ZWrite", 1);
  323. material.SetInt("_ZTest", 4);
  324. material.DisableKeyword("_ALPHATEST_ON");
  325. material.DisableKeyword("_ALPHABLEND_ON");
  326. material.DisableKeyword("EnableAlphaCutoff");
  327. if (lighting.floatValue == 0)
  328. {
  329. material.EnableKeyword("EnableLighting");
  330. }
  331. else
  332. {
  333. material.DisableKeyword("EnableLighting");
  334. }
  335. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  336. break;
  337. case RenderMode.Cutout:
  338. material.SetInt("_Mode", 1);
  339. material.SetInt("_AlphaTest", 1);
  340. material.SetInt("_AlphaBlend", 0);
  341. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  342. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  343. material.SetInt("_ZWrite", 1);
  344. material.SetInt("_ZTest", 4);
  345. material.EnableKeyword("_ALPHATEST_ON");
  346. material.DisableKeyword("_ALPHABLEND_ON");
  347. material.EnableKeyword("EnableAlphaCutoff");
  348. if (lighting.floatValue == 0)
  349. {
  350. material.EnableKeyword("EnableLighting");
  351. }
  352. else
  353. {
  354. material.DisableKeyword("EnableLighting");
  355. }
  356. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
  357. break;
  358. case RenderMode.Transparent:
  359. material.SetInt("_Mode", 2);
  360. material.SetInt("_AlphaTest", 0);
  361. material.SetInt("_AlphaBlend", 1);
  362. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  363. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  364. material.SetInt("_ZWrite", 0);
  365. material.SetInt("_ZTest", 4);
  366. material.DisableKeyword("_ALPHATEST_ON");
  367. material.EnableKeyword("_ALPHABLEND_ON");
  368. material.DisableKeyword("EnableAlphaCutoff");
  369. if (lighting.floatValue == 0)
  370. {
  371. material.EnableKeyword("EnableLighting");
  372. }
  373. else
  374. {
  375. material.DisableKeyword("EnableLighting");
  376. }
  377. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  378. break;
  379. case RenderMode.Custom:
  380. material.SetInt("_Mode", 3);
  381. break;
  382. default:
  383. material.SetInt("_Mode", 0);
  384. material.SetInt("_AlphaTest", 0);
  385. material.SetInt("_AlphaBlend", 0);
  386. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  387. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  388. material.SetInt("_ZWrite", 1);
  389. material.SetInt("_ZTest", 4);
  390. material.DisableKeyword("_ALPHATEST_ON");
  391. material.DisableKeyword("_ALPHABLEND_ON");
  392. material.DisableKeyword("EnableAlphaCutoff");
  393. if (lighting.floatValue == 0)
  394. {
  395. material.EnableKeyword("EnableLighting");
  396. }
  397. else
  398. {
  399. material.DisableKeyword("EnableLighting");
  400. }
  401. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  402. break;
  403. }
  404. }
  405. public static class Styles
  406. {
  407. public static GUIStyle optionsButton = "PaneOptions";
  408. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  409. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  410. public static string emptyTootip = "";
  411. public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
  412. public static GUIContent albedoIntensityText = new GUIContent("Intensity", "Albedo Intensity");
  413. public static GUIContent specularText = new GUIContent("Specular", "Specular (RGB) and Transparency (A)");
  414. public static GUIContent specularShininessText = new GUIContent("Shininess", "Specular Range");
  415. public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map");
  416. public static GUIContent lightModeText = new GUIContent("Lighting", "Lighting");
  417. public static GUIContent cullModeText = new GUIContent("Cull", "CullMode");
  418. public static GUIContent renderModeText = new GUIContent("RenderMode", "RenderMode");
  419. public static GUIContent alphaTestText = new GUIContent("AlphaTest", "AlphaTest");
  420. public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
  421. public static GUIContent alphaBlendText = new GUIContent("AlphaBlend", "AlphaBlend");
  422. public static GUIContent depthWriteText = new GUIContent("DepthWrite", "DepthWrite");
  423. public static GUIContent depthTestText = new GUIContent("DepthTest", "DepthTest");
  424. public static string whiteSpaceString = " ";
  425. public static string PrimaryText = "Primary Properties";
  426. public static string AdvancedText = "Advanced Properties";
  427. public static GUIContent enableVertexColor = new GUIContent("Enable VertexColor", "Enable VertexColor");
  428. public static readonly string[] srcBlendNames = Enum.GetNames(typeof(SrcBlendMode));
  429. public static readonly string[] dstBlendNames = Enum.GetNames(typeof(DstBlendMode));
  430. public static readonly string[] renderModeNames = Enum.GetNames(typeof(RenderMode));
  431. public static readonly string[] cullModeNames = Enum.GetNames(typeof(CullMode));
  432. public static readonly string[] depthWriteNames = Enum.GetNames(typeof(DepthWrite));
  433. public static readonly string[] depthTestNames = Enum.GetNames(typeof(DepthTest));
  434. public static readonly string[] lightingNames = Enum.GetNames(typeof(LightingMode));
  435. }
  436. }
  437. //#endif