LuxToonShaderGUI.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. public class LuxToonShaderGUI : ShaderGUI
  5. {
  6. public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties)
  7. {
  8. base.OnGUI (materialEditor, properties);
  9. Material targetMat = materialEditor.target as Material;
  10. // Steps - implement our custom int slider
  11. if (targetMat.HasProperty("_Steps")) {
  12. float steps = targetMat.GetFloat("_Steps");
  13. targetMat.SetFloat("_Steps", Mathf.Round(steps));
  14. }
  15. // Ramp
  16. if (targetMat.HasProperty("_EnableRamp")) {
  17. float ramp = targetMat.GetFloat("_EnableRamp");
  18. if(ramp == 1.0f) {
  19. targetMat.EnableKeyword("GRADIENT_ON");
  20. if (targetMat.HasProperty("_RampSmoothSampling")) {
  21. float smoothRamp = targetMat.GetFloat("_RampSmoothSampling");
  22. if(smoothRamp == 1.0f) {
  23. targetMat.EnableKeyword("SMOOTHGRADIENT_ON");
  24. }
  25. else {
  26. targetMat.DisableKeyword("SMOOTHGRADIENT_ON");
  27. }
  28. }
  29. }
  30. else {
  31. targetMat.DisableKeyword("GRADIENT_ON");
  32. targetMat.DisableKeyword("SMOOTHGRADIENT_ON");
  33. }
  34. }
  35. // Normal
  36. if (targetMat.HasProperty("_EnableNormalMap")) {
  37. float normal = targetMat.GetFloat("_EnableNormalMap");
  38. if(normal == 1.0f) {
  39. targetMat.EnableKeyword("NORMAL_ON");
  40. }
  41. else {
  42. targetMat.DisableKeyword("NORMAL_ON");
  43. }
  44. }
  45. // Specular
  46. if (targetMat.HasProperty("_EnableSpecular")) {
  47. float specular = targetMat.GetFloat("_EnableSpecular");
  48. if(specular == 1.0f) {
  49. targetMat.EnableKeyword("SPECULAR_ON");
  50. }
  51. else {
  52. targetMat.DisableKeyword("SPECULAR_ON");
  53. }
  54. }
  55. // Anisotropic Specular
  56. if (targetMat.HasProperty("_Anisotropic")) {
  57. float specular = targetMat.GetFloat("_Anisotropic");
  58. if(specular == 1.0f) {
  59. targetMat.EnableKeyword("ANISO_ON");
  60. }
  61. else {
  62. targetMat.DisableKeyword("ANISO_ON");
  63. }
  64. }
  65. // Rim
  66. if (targetMat.HasProperty("_EnableToonRim")) {
  67. float rim = targetMat.GetFloat("_EnableToonRim");
  68. if(rim == 1.0f) {
  69. targetMat.EnableKeyword("RIM_ON");
  70. }
  71. else {
  72. targetMat.DisableKeyword("RIM_ON");
  73. }
  74. }
  75. // Shadows - we have to reverse the logic here?!
  76. bool shadows = Array.IndexOf(targetMat.shaderKeywords, "_RECEIVE_SHADOWS_OFF") != -1;
  77. EditorGUI.BeginChangeCheck();
  78. shadows = EditorGUILayout.Toggle("Receive Shadows", !shadows);
  79. if (EditorGUI.EndChangeCheck())
  80. {
  81. if (!shadows)
  82. targetMat.EnableKeyword("_RECEIVE_SHADOWS_OFF");
  83. else
  84. targetMat.DisableKeyword("_RECEIVE_SHADOWS_OFF");
  85. }
  86. }
  87. }