No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector3MaterialSlot.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Slots;
  5. using UnityEditor.ShaderGraph.Internal;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. [Serializable]
  11. class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector3>
  12. {
  13. [SerializeField]
  14. private Vector3 m_Value;
  15. [SerializeField]
  16. private Vector3 m_DefaultValue = Vector3.zero;
  17. [SerializeField]
  18. string[] m_Labels; // this can be null, which means fallback to k_LabelDefaults
  19. static readonly string[] k_LabelDefaults = { "X", "Y", "Z" };
  20. string[] labels
  21. {
  22. get
  23. {
  24. if ((m_Labels == null) || (m_Labels.Length != k_LabelDefaults.Length))
  25. return k_LabelDefaults;
  26. return m_Labels;
  27. }
  28. }
  29. public Vector3MaterialSlot()
  30. {
  31. }
  32. public Vector3MaterialSlot(
  33. int slotId,
  34. string displayName,
  35. string shaderOutputName,
  36. SlotType slotType,
  37. Vector3 value,
  38. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  39. string label1 = null,
  40. string label2 = null,
  41. string label3 = null,
  42. bool hidden = false)
  43. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  44. {
  45. m_Value = value;
  46. if ((label1 != null) || (label2 != null) || (label3 != null))
  47. {
  48. m_Labels = new[]
  49. {
  50. label1 ?? k_LabelDefaults[0],
  51. label2 ?? k_LabelDefaults[1],
  52. label3 ?? k_LabelDefaults[2]
  53. };
  54. }
  55. }
  56. public Vector3 defaultValue { get { return m_DefaultValue; } }
  57. public Vector3 value
  58. {
  59. get { return m_Value; }
  60. set { m_Value = value; }
  61. }
  62. public override bool isDefaultValue => value.Equals(defaultValue);
  63. public override VisualElement InstantiateControl()
  64. {
  65. return new MultiFloatSlotControlView(owner, labels, () => value, (newValue) => value = newValue);
  66. }
  67. protected override string ConcreteSlotValueAsVariable()
  68. {
  69. return string.Format("$precision3 ({0}, {1}, {2})"
  70. , NodeUtils.FloatToShaderValue(value.x)
  71. , NodeUtils.FloatToShaderValue(value.y)
  72. , NodeUtils.FloatToShaderValue(value.z));
  73. }
  74. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  75. {
  76. if (!generationMode.IsPreview())
  77. return;
  78. var matOwner = owner as AbstractMaterialNode;
  79. if (matOwner == null)
  80. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  81. var property = new Vector3ShaderProperty()
  82. {
  83. overrideReferenceName = matOwner.GetVariableNameForSlot(id),
  84. generatePropertyBlock = false,
  85. value = value
  86. };
  87. properties.AddShaderProperty(property);
  88. }
  89. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  90. {
  91. var pp = new PreviewProperty(PropertyType.Vector3)
  92. {
  93. name = name,
  94. vector4Value = new Vector4(value.x, value.y, value.z, 0)
  95. };
  96. properties.Add(pp);
  97. }
  98. public override SlotValueType valueType { get { return SlotValueType.Vector3; } }
  99. public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } }
  100. public override void CopyValuesFrom(MaterialSlot foundSlot)
  101. {
  102. var slot = foundSlot as Vector3MaterialSlot;
  103. if (slot != null)
  104. value = slot.value;
  105. }
  106. public override void CopyDefaultValue(MaterialSlot other)
  107. {
  108. base.CopyDefaultValue(other);
  109. if (other is IMaterialSlotHasValue<Vector3> ms)
  110. {
  111. m_DefaultValue = ms.defaultValue;
  112. }
  113. }
  114. }
  115. }