DynamicVectorMaterialSlot.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector4>
  13. {
  14. [SerializeField]
  15. private Vector4 m_Value;
  16. [SerializeField]
  17. private Vector4 m_DefaultValue = Vector4.zero;
  18. static readonly string[] k_Labels = {"X", "Y", "Z", "W"};
  19. private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Vector4;
  20. public DynamicVectorMaterialSlot()
  21. {
  22. }
  23. public DynamicVectorMaterialSlot(
  24. int slotId,
  25. string displayName,
  26. string shaderOutputName,
  27. SlotType slotType,
  28. Vector4 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 Vector4 defaultValue { get { return m_DefaultValue; } }
  36. public Vector4 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, (newValue) => value = newValue);
  45. }
  46. public override SlotValueType valueType { get { return SlotValueType.DynamicVector; } }
  47. public override ConcreteSlotValueType concreteValueType
  48. {
  49. get { return m_ConcreteValueType; }
  50. }
  51. public void SetConcreteType(ConcreteSlotValueType valueType)
  52. {
  53. m_ConcreteValueType = valueType;
  54. }
  55. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  56. {
  57. var propType = concreteValueType.ToPropertyType();
  58. var pp = new PreviewProperty(propType) { name = name };
  59. if (propType == PropertyType.Vector1)
  60. pp.floatValue = value.x;
  61. else
  62. pp.vector4Value = new Vector4(value.x, value.y, value.z, value.w);
  63. properties.Add(pp);
  64. }
  65. protected override string ConcreteSlotValueAsVariable()
  66. {
  67. var channelCount = SlotValueHelper.GetChannelCount(concreteValueType);
  68. string values = NodeUtils.FloatToShaderValue(value.x);
  69. if (channelCount == 1)
  70. return values;
  71. for (var i = 1; i < channelCount; i++)
  72. values += ", " + NodeUtils.FloatToShaderValue(value[i]);
  73. return string.Format("$precision{0}({1})", channelCount, values);
  74. }
  75. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  76. {
  77. if (!generationMode.IsPreview())
  78. return;
  79. var matOwner = owner as AbstractMaterialNode;
  80. if (matOwner == null)
  81. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  82. AbstractShaderProperty property;
  83. switch (concreteValueType)
  84. {
  85. case ConcreteSlotValueType.Vector4:
  86. property = new Vector4ShaderProperty();
  87. break;
  88. case ConcreteSlotValueType.Vector3:
  89. property = new Vector3ShaderProperty();
  90. break;
  91. case ConcreteSlotValueType.Vector2:
  92. property = new Vector2ShaderProperty();
  93. break;
  94. case ConcreteSlotValueType.Vector1:
  95. property = new Vector1ShaderProperty();
  96. break;
  97. default:
  98. // This shouldn't happen due to edge validation. The generated shader will
  99. // have errors.
  100. Debug.LogError($"Invalid value type {concreteValueType} passed to Vector Slot {displayName}. Value will be ignored, please plug in an edge with a vector type.");
  101. return;
  102. }
  103. property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
  104. property.generatePropertyBlock = false;
  105. properties.AddShaderProperty(property);
  106. }
  107. public override void CopyValuesFrom(MaterialSlot foundSlot)
  108. {
  109. var slot = foundSlot as DynamicVectorMaterialSlot;
  110. if (slot != null)
  111. value = slot.value;
  112. }
  113. }
  114. }