SampleGradientNode.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph
  4. {
  5. [Title("Input", "Gradient", "Sample Gradient")]
  6. class SampleGradient : CodeFunctionNode
  7. {
  8. public SampleGradient()
  9. {
  10. name = "Sample Gradient";
  11. }
  12. public override bool hasPreview
  13. {
  14. get { return true; }
  15. }
  16. protected override MethodInfo GetFunctionToConvert()
  17. {
  18. return GetType().GetMethod("Unity_SampleGradient", BindingFlags.Static | BindingFlags.NonPublic);
  19. }
  20. static string Unity_SampleGradient(
  21. [Slot(0, Binding.None)] Gradient Gradient,
  22. [Slot(1, Binding.None)] Vector1 Time,
  23. [Slot(2, Binding.None)] out Vector4 Out)
  24. {
  25. Out = Vector4.zero;
  26. return
  27. @"
  28. {
  29. $precision3 color = Gradient.colors[0].rgb;
  30. [unroll]
  31. for (int c = 1; c < 8; c++)
  32. {
  33. $precision colorPos = saturate((Time - Gradient.colors[c-1].w) / (Gradient.colors[c].w - Gradient.colors[c-1].w)) * step(c, Gradient.colorsLength-1);
  34. color = lerp(color, Gradient.colors[c].rgb, lerp(colorPos, step(0.01, colorPos), Gradient.type));
  35. }
  36. #ifndef UNITY_COLORSPACE_GAMMA
  37. color = SRGBToLinear(color);
  38. #endif
  39. $precision alpha = Gradient.alphas[0].x;
  40. [unroll]
  41. for (int a = 1; a < 8; a++)
  42. {
  43. $precision alphaPos = saturate((Time - Gradient.alphas[a-1].y) / (Gradient.alphas[a].y - Gradient.alphas[a-1].y)) * step(a, Gradient.alphasLength-1);
  44. alpha = lerp(alpha, Gradient.alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), Gradient.type));
  45. }
  46. Out = $precision4(color, alpha);
  47. }
  48. ";
  49. }
  50. }
  51. }