DynamicMatrixMaterialSlot.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using UnityEditor.Graphing;
  3. using UnityEditor.ShaderGraph.Drawing.Slots;
  4. using UnityEditor.ShaderGraph.Internal;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. [Serializable]
  10. class DynamicMatrixMaterialSlot : MaterialSlot, IMaterialSlotHasValue<Matrix4x4>
  11. {
  12. [SerializeField]
  13. private Matrix4x4 m_Value = Matrix4x4.identity;
  14. [SerializeField]
  15. private Matrix4x4 m_DefaultValue = Matrix4x4.identity;
  16. private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Matrix4;
  17. public DynamicMatrixMaterialSlot()
  18. {
  19. }
  20. public DynamicMatrixMaterialSlot(
  21. int slotId,
  22. string displayName,
  23. string shaderOutputName,
  24. SlotType slotType,
  25. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  26. bool hidden = false)
  27. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  28. {
  29. m_Value = value;
  30. }
  31. public override VisualElement InstantiateControl()
  32. {
  33. return new LabelSlotControlView("Identity");
  34. }
  35. public Matrix4x4 defaultValue { get { return m_DefaultValue; } }
  36. public Matrix4x4 value
  37. {
  38. get { return m_Value; }
  39. set { m_Value = value; }
  40. }
  41. public override SlotValueType valueType { get { return SlotValueType.DynamicMatrix; } }
  42. public override ConcreteSlotValueType concreteValueType
  43. {
  44. get { return m_ConcreteValueType; }
  45. }
  46. public void SetConcreteType(ConcreteSlotValueType valueType)
  47. {
  48. m_ConcreteValueType = valueType;
  49. }
  50. protected override string ConcreteSlotValueAsVariable()
  51. {
  52. var channelCount = (int)SlotValueHelper.GetMatrixDimension(concreteValueType);
  53. var values = "";
  54. bool isFirst = true;
  55. for (var r = 0; r < channelCount; r++)
  56. {
  57. for (var c = 0; c < channelCount; c++)
  58. {
  59. if (!isFirst)
  60. values += ", ";
  61. isFirst = false;
  62. values += value.GetRow(r)[c];
  63. }
  64. }
  65. return string.Format("$precision{0}x{0}({1})", channelCount, values);
  66. }
  67. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  68. {
  69. if (!generationMode.IsPreview())
  70. return;
  71. var matOwner = owner as AbstractMaterialNode;
  72. if (matOwner == null)
  73. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  74. AbstractShaderProperty property;
  75. switch (concreteValueType)
  76. {
  77. case ConcreteSlotValueType.Matrix4:
  78. property = new Matrix4ShaderProperty();
  79. break;
  80. case ConcreteSlotValueType.Matrix3:
  81. property = new Matrix3ShaderProperty();
  82. break;
  83. case ConcreteSlotValueType.Matrix2:
  84. property = new Matrix2ShaderProperty();
  85. break;
  86. default:
  87. throw new ArgumentOutOfRangeException();
  88. }
  89. property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
  90. property.generatePropertyBlock = false;
  91. properties.AddShaderProperty(property);
  92. }
  93. public override void CopyValuesFrom(MaterialSlot foundSlot)
  94. {
  95. var slot = foundSlot as DynamicMatrixMaterialSlot;
  96. if (slot != null)
  97. value = slot.value;
  98. }
  99. }
  100. }