DynamicValueMaterialSlot.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Drawing.Slots;
  6. using UnityEditor.ShaderGraph.Internal;
  7. using UnityEngine;
  8. using UnityEngine.UIElements;
  9. namespace UnityEditor.ShaderGraph
  10. {
  11. [Serializable]
  12. class DynamicValueMaterialSlot : MaterialSlot, IMaterialSlotHasValue<Matrix4x4>
  13. {
  14. [SerializeField]
  15. private Matrix4x4 m_Value;
  16. [SerializeField]
  17. private Matrix4x4 m_DefaultValue = Matrix4x4.identity;
  18. static readonly string[] k_Labels = {"X", "Y", "Z", "W"};
  19. private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Vector4;
  20. public DynamicValueMaterialSlot()
  21. {
  22. }
  23. public DynamicValueMaterialSlot(
  24. int slotId,
  25. string displayName,
  26. string shaderOutputName,
  27. SlotType slotType,
  28. Matrix4x4 value,
  29. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  30. bool hidden = false)
  31. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  32. {
  33. m_Value = value;
  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 VisualElement InstantiateControl()
  42. {
  43. var labels = k_Labels.Take(concreteValueType.GetChannelCount()).ToArray();
  44. return new MultiFloatSlotControlView(owner, labels, () => value.GetRow(0), (newValue) =>
  45. value = new Matrix4x4()
  46. {
  47. m00 = newValue.x, m01 = newValue.y, m02 = newValue.z, m03 = newValue.w,
  48. m10 = value.m10, m11 = value.m11, m12 = value.m12, m13 = value.m13,
  49. m20 = value.m20, m21 = value.m21, m22 = value.m22, m23 = value.m23,
  50. m30 = value.m30, m31 = value.m31, m32 = value.m32, m33 = value.m33,
  51. });
  52. }
  53. public override SlotValueType valueType { get { return SlotValueType.Dynamic; } }
  54. public override ConcreteSlotValueType concreteValueType
  55. {
  56. get { return m_ConcreteValueType; }
  57. }
  58. public void SetConcreteType(ConcreteSlotValueType valueType)
  59. {
  60. m_ConcreteValueType = valueType;
  61. }
  62. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  63. {
  64. var propType = concreteValueType.ToPropertyType();
  65. var pp = new PreviewProperty(propType) { name = name };
  66. if (propType == PropertyType.Vector1)
  67. pp.floatValue = value.m00;
  68. else
  69. pp.vector4Value = new Vector4(value.m00, value.m01, value.m02, value.m03);
  70. properties.Add(pp);
  71. }
  72. protected override string ConcreteSlotValueAsVariable()
  73. {
  74. var channelCount = SlotValueHelper.GetChannelCount(concreteValueType);
  75. string values = NodeUtils.FloatToShaderValue(value.m00);
  76. if (channelCount == 1)
  77. return values;
  78. for (var i = 1; i < channelCount; i++)
  79. values += ", " + NodeUtils.FloatToShaderValue(value.GetRow(0)[i]);
  80. return string.Format("$precision{0}({1})", channelCount, values);
  81. }
  82. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  83. {
  84. if (!generationMode.IsPreview())
  85. return;
  86. var matOwner = owner as AbstractMaterialNode;
  87. if (matOwner == null)
  88. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  89. AbstractShaderProperty property;
  90. switch (concreteValueType)
  91. {
  92. case ConcreteSlotValueType.Vector4:
  93. property = new Vector4ShaderProperty();
  94. break;
  95. case ConcreteSlotValueType.Vector3:
  96. property = new Vector3ShaderProperty();
  97. break;
  98. case ConcreteSlotValueType.Vector2:
  99. property = new Vector2ShaderProperty();
  100. break;
  101. case ConcreteSlotValueType.Vector1:
  102. property = new Vector1ShaderProperty();
  103. break;
  104. case ConcreteSlotValueType.Matrix4:
  105. property = new Matrix4ShaderProperty();
  106. break;
  107. case ConcreteSlotValueType.Matrix3:
  108. property = new Matrix3ShaderProperty();
  109. break;
  110. case ConcreteSlotValueType.Matrix2:
  111. property = new Matrix2ShaderProperty();
  112. break;
  113. default:
  114. throw new ArgumentOutOfRangeException();
  115. }
  116. property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
  117. property.generatePropertyBlock = false;
  118. properties.AddShaderProperty(property);
  119. }
  120. public override void CopyValuesFrom(MaterialSlot foundSlot)
  121. {
  122. var slot = foundSlot as DynamicValueMaterialSlot;
  123. if (slot != null)
  124. value = slot.value;
  125. }
  126. }
  127. }