Vector1ShaderProperty.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using UnityEditor.Graphing;
  6. using UnityEngine;
  7. namespace UnityEditor.ShaderGraph.Internal
  8. {
  9. [Serializable]
  10. [FormerName("UnityEditor.ShaderGraph.FloatShaderProperty")]
  11. [FormerName("UnityEditor.ShaderGraph.Vector1ShaderProperty")]
  12. public sealed class Vector1ShaderProperty : AbstractShaderProperty<float>
  13. {
  14. internal Vector1ShaderProperty()
  15. {
  16. displayName = "Vector1";
  17. }
  18. public override PropertyType propertyType => PropertyType.Vector1;
  19. internal override bool isBatchable => true;
  20. internal override bool isExposable => true;
  21. internal override bool isRenamable => true;
  22. internal override bool isGpuInstanceable => true;
  23. string enumTagString
  24. {
  25. get
  26. {
  27. switch(enumType)
  28. {
  29. case EnumType.CSharpEnum:
  30. return $"[Enum({m_CSharpEnumType.ToString()})]";
  31. case EnumType.KeywordEnum:
  32. return $"[KeywordEnum({string.Join(", ", enumNames)})]";
  33. default:
  34. string enumValuesString = "";
  35. for (int i = 0; i < enumNames.Count; i++)
  36. {
  37. int value = (i < enumValues.Count) ? enumValues[i] : i;
  38. enumValuesString += (enumNames[i] + ", " + value + ((i != enumNames.Count - 1) ? ", " : ""));
  39. }
  40. return $"[Enum({enumValuesString})]";
  41. }
  42. }
  43. }
  44. internal override string GetPropertyBlockString()
  45. {
  46. switch(floatType)
  47. {
  48. case FloatType.Slider:
  49. return $"{hideTagString}{referenceName}(\"{displayName}\", Range({NodeUtils.FloatToShaderValue(m_RangeValues.x)}, {NodeUtils.FloatToShaderValue(m_RangeValues.y)})) = {NodeUtils.FloatToShaderValue(value)}";
  50. case FloatType.Integer:
  51. return $"{hideTagString}{referenceName}(\"{displayName}\", Int) = {NodeUtils.FloatToShaderValue(value)}";
  52. case FloatType.Enum:
  53. return $"{hideTagString}{enumTagString}{referenceName}(\"{displayName}\", Float) = {NodeUtils.FloatToShaderValue(value)}";
  54. default:
  55. return $"{hideTagString}{referenceName}(\"{displayName}\", Float) = {NodeUtils.FloatToShaderValue(value)}";
  56. }
  57. }
  58. [SerializeField]
  59. FloatType m_FloatType = FloatType.Default;
  60. public FloatType floatType
  61. {
  62. get => m_FloatType;
  63. set => m_FloatType = value;
  64. }
  65. [SerializeField]
  66. Vector2 m_RangeValues = new Vector2(0, 1);
  67. public Vector2 rangeValues
  68. {
  69. get => m_RangeValues;
  70. set => m_RangeValues = value;
  71. }
  72. EnumType m_EnumType = EnumType.Enum;
  73. public EnumType enumType
  74. {
  75. get => m_EnumType;
  76. set => m_EnumType = value;
  77. }
  78. Type m_CSharpEnumType;
  79. public Type cSharpEnumType
  80. {
  81. get => m_CSharpEnumType;
  82. set => m_CSharpEnumType = value;
  83. }
  84. List<string> m_EnumNames = new List<string>();
  85. public List<string> enumNames
  86. {
  87. get => m_EnumNames;
  88. set => m_EnumNames = value;
  89. }
  90. List<int> m_EnumValues = new List<int>();
  91. public List<int> enumValues
  92. {
  93. get => m_EnumValues;
  94. set => m_EnumValues = value;
  95. }
  96. internal override AbstractMaterialNode ToConcreteNode()
  97. {
  98. switch (m_FloatType)
  99. {
  100. case FloatType.Slider:
  101. return new SliderNode { value = new Vector3(value, m_RangeValues.x, m_RangeValues.y) };
  102. case FloatType.Integer:
  103. return new IntegerNode { value = (int)value };
  104. default:
  105. var node = new Vector1Node();
  106. node.FindInputSlot<Vector1MaterialSlot>(Vector1Node.InputSlotXId).value = value;
  107. return node;
  108. }
  109. }
  110. internal override PreviewProperty GetPreviewMaterialProperty()
  111. {
  112. return new PreviewProperty(propertyType)
  113. {
  114. name = referenceName,
  115. floatValue = value
  116. };
  117. }
  118. internal override ShaderInput Copy()
  119. {
  120. return new Vector1ShaderProperty()
  121. {
  122. displayName = displayName,
  123. hidden = hidden,
  124. value = value,
  125. floatType = floatType,
  126. rangeValues = rangeValues,
  127. enumType = enumType,
  128. enumNames = enumNames,
  129. enumValues = enumValues
  130. };
  131. }
  132. }
  133. public enum FloatType { Default, Slider, Integer, Enum }
  134. public enum EnumType { Enum, CSharpEnum, KeywordEnum, }
  135. }