HueNode.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Reflection;
  2. using UnityEngine;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. enum HueMode
  8. {
  9. Degrees,
  10. Normalized
  11. };
  12. [Title("Artistic", "Adjustment", "Hue")]
  13. class HueNode : CodeFunctionNode
  14. {
  15. public HueNode()
  16. {
  17. name = "Hue";
  18. }
  19. [SerializeField]
  20. private HueMode m_HueMode = HueMode.Degrees;
  21. [EnumControl("Range")]
  22. public HueMode hueMode
  23. {
  24. get { return m_HueMode; }
  25. set
  26. {
  27. if (m_HueMode == value)
  28. return;
  29. m_HueMode = value;
  30. Dirty(ModificationScope.Graph);
  31. }
  32. }
  33. protected override MethodInfo GetFunctionToConvert()
  34. {
  35. switch (m_HueMode)
  36. {
  37. case HueMode.Normalized:
  38. return GetType().GetMethod("Unity_Hue_Normalized", BindingFlags.Static | BindingFlags.NonPublic);
  39. default:
  40. return GetType().GetMethod("Unity_Hue_Degrees", BindingFlags.Static | BindingFlags.NonPublic);
  41. }
  42. }
  43. static string Unity_Hue_Degrees(
  44. [Slot(0, Binding.None)] Vector3 In,
  45. [Slot(1, Binding.None)] Vector1 Offset,
  46. [Slot(2, Binding.None)] out Vector3 Out)
  47. {
  48. Out = Vector3.zero;
  49. return
  50. @"
  51. {
  52. // RGB to HSV
  53. $precision4 K = $precision4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  54. $precision4 P = lerp($precision4(In.bg, K.wz), $precision4(In.gb, K.xy), step(In.b, In.g));
  55. $precision4 Q = lerp($precision4(P.xyw, In.r), $precision4(In.r, P.yzx), step(P.x, In.r));
  56. $precision D = Q.x - min(Q.w, Q.y);
  57. $precision E = 1e-4;
  58. $precision3 hsv = $precision3(abs(Q.z + (Q.w - Q.y)/(6.0 * D + E)), D / (Q.x + E), Q.x);
  59. $precision hue = hsv.x + Offset / 360;
  60. hsv.x = (hue < 0)
  61. ? hue + 1
  62. : (hue > 1)
  63. ? hue - 1
  64. : hue;
  65. // HSV to RGB
  66. $precision4 K2 = $precision4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  67. $precision3 P2 = abs(frac(hsv.xxx + K2.xyz) * 6.0 - K2.www);
  68. Out = hsv.z * lerp(K2.xxx, saturate(P2 - K2.xxx), hsv.y);
  69. }";
  70. }
  71. static string Unity_Hue_Normalized(
  72. [Slot(0, Binding.None)] Vector3 In,
  73. [Slot(1, Binding.None, 0.5f, 0.5f, 0.5f, 0.5f)] Vector1 Offset,
  74. [Slot(2, Binding.None)] out Vector3 Out)
  75. {
  76. Out = Vector3.zero;
  77. return
  78. @"
  79. {
  80. // RGB to HSV
  81. $precision4 K = $precision4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  82. $precision4 P = lerp($precision4(In.bg, K.wz), $precision4(In.gb, K.xy), step(In.b, In.g));
  83. $precision4 Q = lerp($precision4(P.xyw, In.r), $precision4(In.r, P.yzx), step(P.x, In.r));
  84. $precision D = Q.x - min(Q.w, Q.y);
  85. $precision E = 1e-4;
  86. $precision3 hsv = $precision3(abs(Q.z + (Q.w - Q.y)/(6.0 * D + E)), D / (Q.x + E), Q.x);
  87. $precision hue = hsv.x + Offset;
  88. hsv.x = (hue < 0)
  89. ? hue + 1
  90. : (hue > 1)
  91. ? hue - 1
  92. : hue;
  93. // HSV to RGB
  94. $precision4 K2 = $precision4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  95. $precision3 P2 = abs(frac(hsv.xxx + K2.xyz) * 6.0 - K2.www);
  96. Out = hsv.z * lerp(K2.xxx, saturate(P2 - K2.xxx), hsv.y);
  97. }";
  98. }
  99. }
  100. }