1
0

LayaBlinnPhongGUI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. bool m_FirstTimeApply = true;
  122. public void FindProperties(MaterialProperty[] props)
  123. {
  124. lighting = FindProperty("_Lighting", props);
  125. albedoTexture = FindProperty("_MainTex", props);
  126. albedoColor = FindProperty("_Color", props);
  127. albedoIntensity = FindProperty("_AlbedoIntensity", props);
  128. specularTexture = FindProperty("_SpecGlossMap", props);
  129. specularColor = FindProperty("_SpecColor", props);
  130. specularShininess = FindProperty("_Shininess", props);
  131. normalTexture = FindProperty("_BumpMap", props);
  132. renderMode = FindProperty("_Mode", props);
  133. cullMode = FindProperty("_Cull", props);
  134. alphaTest = FindProperty("_AlphaTest", props, false);
  135. alphaCutoff = FindProperty("_Cutoff", props, false);
  136. alphaBlend = FindProperty("_AlphaBlend", props, false);
  137. srcBlendMode = FindProperty("_SrcBlend", props);
  138. dstBlendMode = FindProperty("_DstBlend", props);
  139. depthWrite = FindProperty("_ZWrite", props);
  140. depthTest = FindProperty("_ZTest", props);
  141. isVertexColor = FindProperty("_IsVertexColor", props);
  142. }
  143. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  144. {
  145. // render the default gui
  146. FindProperties(props);
  147. m_MaterialEditor = materialEditor;
  148. Material material = materialEditor.target as Material;
  149. ShaderPropertiesGUI(material);
  150. }
  151. public void ShaderPropertiesGUI(Material material)
  152. {
  153. // Use default labelWidth
  154. EditorGUIUtility.labelWidth = 0f;
  155. // Detect any changes to the material
  156. EditorGUI.BeginChangeCheck();
  157. {
  158. //renderMode
  159. GUILayout.BeginHorizontal();
  160. GUILayout.Label(Styles.renderModeText, GUILayout.Width(120));
  161. var mode = (RenderMode)renderMode.floatValue;
  162. mode = (RenderMode)EditorGUILayout.Popup((int)mode, Styles.renderModeNames);
  163. GUILayout.EndHorizontal();
  164. //lightingMode
  165. GUILayout.BeginHorizontal();
  166. GUILayout.Label(Styles.lightModeText, GUILayout.Width(120));
  167. var light = (LightingMode)lighting.floatValue;
  168. light = (LightingMode)EditorGUILayout.Popup((int)light, Styles.lightingNames);
  169. GUILayout.EndHorizontal();
  170. //IsVertexColor
  171. m_MaterialEditor.ShaderProperty(isVertexColor, Styles.enableVertexColor);
  172. //Primary properties
  173. GUILayout.Label(Styles.PrimaryText, EditorStyles.boldLabel);
  174. //albedo
  175. m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoTexture, albedoColor);
  176. //albedo Intensity
  177. m_MaterialEditor.ShaderProperty(albedoIntensity, Styles.albedoIntensityText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  178. if (lighting.floatValue == 0)
  179. {
  180. //specular
  181. m_MaterialEditor.TexturePropertySingleLine(Styles.specularText, specularTexture, specularColor);
  182. //specular Shininess
  183. m_MaterialEditor.ShaderProperty(specularShininess, Styles.specularShininessText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  184. //mormal
  185. m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalTexture);
  186. }
  187. //scaleAndOffset
  188. m_MaterialEditor.TextureScaleOffsetProperty(albedoTexture);
  189. GUILayout.Box("", GUILayout.Height(1), GUILayout.ExpandWidth(true));
  190. //Advanced properties
  191. GUILayout.Label(Styles.AdvancedText, EditorStyles.boldLabel);
  192. //alphaTest
  193. m_MaterialEditor.ShaderProperty(alphaTest, Styles.alphaTestText);
  194. if (alphaTest.floatValue == 1)
  195. {
  196. m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText, MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1);
  197. }
  198. //alphaBlend
  199. m_MaterialEditor.ShaderProperty(alphaBlend, Styles.alphaBlendText);
  200. var dstMode = (DstBlendMode)dstBlendMode.floatValue;
  201. var srcMode = (SrcBlendMode)srcBlendMode.floatValue;
  202. if (alphaBlend.floatValue == 1)
  203. {
  204. GUILayout.BeginHorizontal();
  205. GUILayout.Label("", GUILayout.Width(20));
  206. srcMode = (SrcBlendMode)EditorGUILayout.Popup((int)srcMode, Styles.srcBlendNames);
  207. dstMode = (DstBlendMode)EditorGUILayout.Popup((int)dstMode, Styles.dstBlendNames);
  208. GUILayout.EndHorizontal();
  209. }
  210. //depthWrite
  211. GUILayout.BeginHorizontal();
  212. GUILayout.Label(Styles.depthWriteText, GUILayout.Width(120));
  213. var depthW = (DepthWrite)depthWrite.floatValue;
  214. depthW = (DepthWrite)EditorGUILayout.Popup((int)depthW, Styles.depthWriteNames);
  215. GUILayout.EndHorizontal();
  216. //depthTest
  217. GUILayout.BeginHorizontal();
  218. GUILayout.Label(Styles.depthTestText, GUILayout.Width(120));
  219. var depthT = (DepthTest)depthTest.floatValue;
  220. depthT = (DepthTest)EditorGUILayout.Popup((int)depthT, Styles.depthTestNames);
  221. GUILayout.EndHorizontal();
  222. //cullMode
  223. GUILayout.BeginHorizontal();
  224. GUILayout.Label(Styles.cullModeText, GUILayout.Width(120));
  225. var cull = (CullMode)cullMode.floatValue;
  226. cull = (CullMode)EditorGUILayout.Popup((int)cull, Styles.cullModeNames);
  227. GUILayout.EndHorizontal();
  228. if (EditorGUI.EndChangeCheck())
  229. {
  230. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  231. //renderMode
  232. renderMode.floatValue = (float)mode;
  233. //lightMode
  234. lighting.floatValue = (float)light;
  235. material.SetInt("_Lighting", (int)light);
  236. if (lighting.floatValue == 0)
  237. {
  238. material.EnableKeyword("EnableLighting");
  239. }
  240. else
  241. {
  242. material.DisableKeyword("EnableLighting");
  243. }
  244. //cullMode
  245. cullMode.floatValue = (float)cull;
  246. material.SetInt("_Cull", (int)cull);
  247. if ((RenderMode)material.GetFloat("_Mode") == RenderMode.Custom)
  248. {
  249. //alphaTest
  250. if (alphaTest.floatValue == 1)
  251. {
  252. material.EnableKeyword("EnableAlphaCutoff");
  253. material.EnableKeyword("_ALPHATEST_ON");
  254. }
  255. else
  256. {
  257. material.DisableKeyword("EnableAlphaCutoff");
  258. material.DisableKeyword("_ALPHATEST_ON");
  259. }
  260. //alphaBlend
  261. if (alphaBlend.floatValue == 1)
  262. {
  263. srcBlendMode.floatValue = (float)srcMode;
  264. dstBlendMode.floatValue = (float)dstMode;
  265. material.SetInt("_SrcBlend", (int)srcMode);
  266. material.SetInt("_DstBlend", (int)dstMode);
  267. material.EnableKeyword("_ALPHABLEND_ON");
  268. material.SetInt("_AlphaBlend", 1);
  269. }
  270. else
  271. {
  272. material.DisableKeyword("_ALPHABLEND_ON");
  273. material.SetInt("_AlphaBlend", 0);
  274. material.SetInt("_SrcBlend", (int)1);
  275. material.SetInt("_DstBlend", (int)0);
  276. }
  277. //depthWrite
  278. depthWrite.floatValue = (float)depthW;
  279. material.SetInt("_ZWrite", (int)depthW);
  280. //depthTest
  281. depthTest.floatValue = (float)depthT;
  282. material.SetInt("_ZTest", (int)depthT);
  283. }
  284. if (specularTexture.textureValue != null)
  285. {
  286. material.EnableKeyword("SpecularTexture");
  287. }
  288. else
  289. {
  290. material.DisableKeyword("SpecularTexture");
  291. }
  292. if (normalTexture.textureValue != null)
  293. {
  294. material.EnableKeyword("NormalTexture");
  295. }
  296. else
  297. {
  298. material.DisableKeyword("NormalTexture");
  299. }
  300. if (isVertexColor.floatValue == 1)
  301. {
  302. material.EnableKeyword("ENABLEVERTEXCOLOR");
  303. }
  304. else
  305. {
  306. material.DisableKeyword("ENABLEVERTEXCOLOR");
  307. }
  308. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  309. }
  310. }
  311. m_MaterialEditor.RenderQueueField();
  312. }
  313. public void onChangeRender(Material material, RenderMode mode)
  314. {
  315. switch (mode)
  316. {
  317. case RenderMode.Opaque:
  318. material.SetInt("_Mode", 0);
  319. material.SetInt("_AlphaTest", 0);
  320. material.SetInt("_AlphaBlend", 0);
  321. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  322. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  323. material.SetInt("_ZWrite", 1);
  324. material.SetInt("_ZTest", 4);
  325. material.DisableKeyword("_ALPHATEST_ON");
  326. material.DisableKeyword("_ALPHABLEND_ON");
  327. material.DisableKeyword("EnableAlphaCutoff");
  328. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  329. break;
  330. case RenderMode.Cutout:
  331. material.SetInt("_Mode", 1);
  332. material.SetInt("_AlphaTest", 1);
  333. material.SetInt("_AlphaBlend", 0);
  334. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  335. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  336. material.SetInt("_ZWrite", 1);
  337. material.SetInt("_ZTest", 4);
  338. material.EnableKeyword("_ALPHATEST_ON");
  339. material.DisableKeyword("_ALPHABLEND_ON");
  340. material.EnableKeyword("EnableAlphaCutoff");
  341. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
  342. break;
  343. case RenderMode.Transparent:
  344. material.SetInt("_Mode", 2);
  345. material.SetInt("_AlphaTest", 0);
  346. material.SetInt("_AlphaBlend", 1);
  347. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  348. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  349. material.SetInt("_ZWrite", 0);
  350. material.SetInt("_ZTest", 4);
  351. material.DisableKeyword("_ALPHATEST_ON");
  352. material.EnableKeyword("_ALPHABLEND_ON");
  353. material.DisableKeyword("EnableAlphaCutoff");
  354. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  355. break;
  356. case RenderMode.Custom:
  357. material.SetInt("_Mode", 3);
  358. break;
  359. default:
  360. material.SetInt("_Mode", 0);
  361. material.SetInt("_AlphaTest", 0);
  362. material.SetInt("_AlphaBlend", 0);
  363. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  364. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  365. material.SetInt("_ZWrite", 1);
  366. material.SetInt("_ZTest", 4);
  367. material.DisableKeyword("_ALPHATEST_ON");
  368. material.DisableKeyword("_ALPHABLEND_ON");
  369. material.DisableKeyword("EnableAlphaCutoff");
  370. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  371. break;
  372. }
  373. if (lighting != null)
  374. {
  375. if (lighting.floatValue == 0)
  376. {
  377. material.EnableKeyword("EnableLighting");
  378. }
  379. else
  380. {
  381. material.DisableKeyword("EnableLighting");
  382. }
  383. }
  384. }
  385. public static class Styles
  386. {
  387. public static GUIStyle optionsButton = "PaneOptions";
  388. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  389. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  390. public static string emptyTootip = "";
  391. public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
  392. public static GUIContent albedoIntensityText = new GUIContent("Intensity", "Albedo Intensity");
  393. public static GUIContent specularText = new GUIContent("Specular", "Specular (RGB) and Transparency (A)");
  394. public static GUIContent specularShininessText = new GUIContent("Shininess", "Specular Range");
  395. public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map");
  396. public static GUIContent lightModeText = new GUIContent("Lighting", "Lighting");
  397. public static GUIContent cullModeText = new GUIContent("Cull", "CullMode");
  398. public static GUIContent renderModeText = new GUIContent("RenderMode", "RenderMode");
  399. public static GUIContent alphaTestText = new GUIContent("AlphaTest", "AlphaTest");
  400. public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
  401. public static GUIContent alphaBlendText = new GUIContent("AlphaBlend", "AlphaBlend");
  402. public static GUIContent depthWriteText = new GUIContent("DepthWrite", "DepthWrite");
  403. public static GUIContent depthTestText = new GUIContent("DepthTest", "DepthTest");
  404. public static string whiteSpaceString = " ";
  405. public static string PrimaryText = "Primary Properties";
  406. public static string AdvancedText = "Advanced Properties";
  407. public static GUIContent enableVertexColor = new GUIContent("Enable VertexColor", "Enable VertexColor");
  408. public static readonly string[] srcBlendNames = Enum.GetNames(typeof(SrcBlendMode));
  409. public static readonly string[] dstBlendNames = Enum.GetNames(typeof(DstBlendMode));
  410. public static readonly string[] renderModeNames = Enum.GetNames(typeof(RenderMode));
  411. public static readonly string[] cullModeNames = Enum.GetNames(typeof(CullMode));
  412. public static readonly string[] depthWriteNames = Enum.GetNames(typeof(DepthWrite));
  413. public static readonly string[] depthTestNames = Enum.GetNames(typeof(DepthTest));
  414. public static readonly string[] lightingNames = Enum.GetNames(typeof(LightingMode));
  415. }
  416. }
  417. //#endif