SlotValue.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Graphing;
  4. namespace UnityEditor.ShaderGraph
  5. {
  6. [Serializable]
  7. enum SlotValueType
  8. {
  9. SamplerState,
  10. DynamicMatrix,
  11. Matrix4,
  12. Matrix3,
  13. Matrix2,
  14. Texture2D,
  15. Texture2DArray,
  16. Texture3D,
  17. Cubemap,
  18. Gradient,
  19. DynamicVector,
  20. Vector4,
  21. Vector3,
  22. Vector2,
  23. Vector1,
  24. Dynamic,
  25. Boolean
  26. }
  27. enum ConcreteSlotValueType
  28. {
  29. SamplerState,
  30. Matrix4,
  31. Matrix3,
  32. Matrix2,
  33. Texture2D,
  34. Texture2DArray,
  35. Texture3D,
  36. Cubemap,
  37. Gradient,
  38. Vector4,
  39. Vector3,
  40. Vector2,
  41. Vector1,
  42. Boolean
  43. }
  44. static class SlotValueHelper
  45. {
  46. public static int GetChannelCount(this ConcreteSlotValueType type)
  47. {
  48. switch (type)
  49. {
  50. case ConcreteSlotValueType.Vector4:
  51. return 4;
  52. case ConcreteSlotValueType.Vector3:
  53. return 3;
  54. case ConcreteSlotValueType.Vector2:
  55. return 2;
  56. case ConcreteSlotValueType.Vector1:
  57. return 1;
  58. default:
  59. return 0;
  60. }
  61. }
  62. public static int GetMatrixDimension(ConcreteSlotValueType type)
  63. {
  64. switch (type)
  65. {
  66. case ConcreteSlotValueType.Matrix4:
  67. return 4;
  68. case ConcreteSlotValueType.Matrix3:
  69. return 3;
  70. case ConcreteSlotValueType.Matrix2:
  71. return 2;
  72. default:
  73. return 0;
  74. }
  75. }
  76. public static ConcreteSlotValueType ConvertMatrixToVectorType(ConcreteSlotValueType matrixType)
  77. {
  78. switch (matrixType)
  79. {
  80. case ConcreteSlotValueType.Matrix4:
  81. return ConcreteSlotValueType.Vector4;
  82. case ConcreteSlotValueType.Matrix3:
  83. return ConcreteSlotValueType.Vector3;
  84. default:
  85. return ConcreteSlotValueType.Vector2;
  86. }
  87. }
  88. static Dictionary<ConcreteSlotValueType, List<SlotValueType>> s_ValidConversions;
  89. static List<SlotValueType> s_ValidSlotTypes;
  90. public static bool AreCompatible(SlotValueType inputType, ConcreteSlotValueType outputType)
  91. {
  92. if (s_ValidConversions == null)
  93. {
  94. var validVectors = new List<SlotValueType>()
  95. {
  96. SlotValueType.Dynamic, SlotValueType.DynamicVector,
  97. SlotValueType.Vector1, SlotValueType.Vector2, SlotValueType.Vector3, SlotValueType.Vector4
  98. };
  99. s_ValidConversions = new Dictionary<ConcreteSlotValueType, List<SlotValueType>>()
  100. {
  101. {ConcreteSlotValueType.Boolean, new List<SlotValueType>() {SlotValueType.Boolean}},
  102. {ConcreteSlotValueType.Vector1, validVectors},
  103. {ConcreteSlotValueType.Vector2, validVectors},
  104. {ConcreteSlotValueType.Vector3, validVectors},
  105. {ConcreteSlotValueType.Vector4, validVectors},
  106. {ConcreteSlotValueType.Matrix2, new List<SlotValueType>()
  107. {SlotValueType.Dynamic, SlotValueType.DynamicMatrix, SlotValueType.Matrix2}},
  108. {ConcreteSlotValueType.Matrix3, new List<SlotValueType>()
  109. {SlotValueType.Dynamic, SlotValueType.DynamicMatrix, SlotValueType.Matrix2, SlotValueType.Matrix3}},
  110. {ConcreteSlotValueType.Matrix4, new List<SlotValueType>()
  111. {SlotValueType.Dynamic, SlotValueType.DynamicMatrix, SlotValueType.Matrix2, SlotValueType.Matrix3, SlotValueType.Matrix4}},
  112. {ConcreteSlotValueType.Texture2D, new List<SlotValueType>() {SlotValueType.Texture2D}},
  113. {ConcreteSlotValueType.Texture3D, new List<SlotValueType>() {SlotValueType.Texture3D}},
  114. {ConcreteSlotValueType.Texture2DArray, new List<SlotValueType>() {SlotValueType.Texture2DArray}},
  115. {ConcreteSlotValueType.Cubemap, new List<SlotValueType>() {SlotValueType.Cubemap}},
  116. {ConcreteSlotValueType.SamplerState, new List<SlotValueType>() {SlotValueType.SamplerState}},
  117. {ConcreteSlotValueType.Gradient, new List<SlotValueType>() {SlotValueType.Gradient}},
  118. };
  119. }
  120. if(s_ValidConversions.TryGetValue(outputType, out s_ValidSlotTypes))
  121. {
  122. return s_ValidSlotTypes.Contains(inputType);
  123. }
  124. throw new ArgumentOutOfRangeException("Unknown Concrete Slot Type: " + outputType);
  125. }
  126. }
  127. }