GradientShaderProperty.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor.ShaderGraph.Drawing.Controls;
  6. using UnityEngine;
  7. using UnityEditor.Graphing;
  8. using UnityEditor.ShaderGraph.Internal;
  9. namespace UnityEditor.ShaderGraph
  10. {
  11. [Serializable]
  12. class GradientShaderProperty : AbstractShaderProperty<Gradient>
  13. {
  14. public GradientShaderProperty()
  15. {
  16. displayName = "Gradient";
  17. value = new Gradient();
  18. }
  19. public override PropertyType propertyType => PropertyType.Gradient;
  20. internal override bool isBatchable => false;
  21. internal override bool isExposable => false;
  22. internal override bool isRenamable => true;
  23. internal override string GetPropertyDeclarationString(string delimiter = ";")
  24. {
  25. ShaderStringBuilder s = new ShaderStringBuilder();
  26. s.AppendLine("Gradient {0}_Definition()", referenceName);
  27. using (s.BlockScope())
  28. {
  29. string[] colors = new string[8];
  30. for (int i = 0; i < colors.Length; i++)
  31. colors[i] = string.Format("g.colors[{0}] = {1}4(0, 0, 0, 0);", i, concretePrecision.ToShaderString());
  32. for (int i = 0; i < value.colorKeys.Length; i++)
  33. colors[i] = string.Format("g.colors[{0}] = {1}4({2}, {3}, {4}, {5});"
  34. , i
  35. , concretePrecision.ToShaderString()
  36. , NodeUtils.FloatToShaderValue(value.colorKeys[i].color.r)
  37. , NodeUtils.FloatToShaderValue(value.colorKeys[i].color.g)
  38. , NodeUtils.FloatToShaderValue(value.colorKeys[i].color.b)
  39. , NodeUtils.FloatToShaderValue(value.colorKeys[i].time));
  40. string[] alphas = new string[8];
  41. for (int i = 0; i < alphas.Length; i++)
  42. alphas[i] = string.Format("g.alphas[{0}] = {1}2(0, 0);", i, concretePrecision.ToShaderString());
  43. for (int i = 0; i < value.alphaKeys.Length; i++)
  44. alphas[i] = string.Format("g.alphas[{0}] = {1}2({2}, {3});"
  45. , i
  46. , concretePrecision.ToShaderString()
  47. , NodeUtils.FloatToShaderValue(value.alphaKeys[i].alpha)
  48. , NodeUtils.FloatToShaderValue(value.alphaKeys[i].time));
  49. s.AppendLine("Gradient g;");
  50. s.AppendLine("g.type = {0};",
  51. (int)value.mode);
  52. s.AppendLine("g.colorsLength = {0};",
  53. value.colorKeys.Length);
  54. s.AppendLine("g.alphasLength = {0};",
  55. value.alphaKeys.Length);
  56. for (int i = 0; i < colors.Length; i++)
  57. s.AppendLine(colors[i]);
  58. for (int i = 0; i < alphas.Length; i++)
  59. s.AppendLine(alphas[i]);
  60. s.AppendLine("return g;", true);
  61. }
  62. s.AppendIndentation();
  63. s.Append("#define {0} {0}_Definition()", referenceName);
  64. return s.ToString();
  65. }
  66. internal override string GetPropertyAsArgumentString()
  67. {
  68. return "Gradient " + referenceName;
  69. }
  70. internal override AbstractMaterialNode ToConcreteNode()
  71. {
  72. return new GradientNode { gradient = value };
  73. }
  74. internal override PreviewProperty GetPreviewMaterialProperty()
  75. {
  76. return new PreviewProperty(propertyType)
  77. {
  78. name = referenceName,
  79. gradientValue = value
  80. };
  81. }
  82. internal override ShaderInput Copy()
  83. {
  84. return new GradientShaderProperty
  85. {
  86. displayName = displayName,
  87. hidden = hidden,
  88. value = value
  89. };
  90. }
  91. }
  92. }