LayaBlinnPhongGUI.cs 19 KB

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