暫無描述
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.

Vector4MaterialSlot.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector4>
  12. {
  13. [SerializeField]
  14. private Vector4 m_Value;
  15. [SerializeField]
  16. private Vector4 m_DefaultValue = Vector4.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", "W" };
  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 Vector4MaterialSlot()
  30. {
  31. }
  32. public Vector4MaterialSlot(
  33. int slotId,
  34. string displayName,
  35. string shaderOutputName,
  36. SlotType slotType,
  37. Vector4 value,
  38. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  39. string label1 = null,
  40. string label2 = null,
  41. string label3 = null,
  42. string label4 = null,
  43. bool hidden = false)
  44. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  45. {
  46. m_Value = value;
  47. if ((label1 != null) || (label2 != null) || (label3 != null) || (label4 != null))
  48. {
  49. m_Labels = new[]
  50. {
  51. label1 ?? k_LabelDefaults[0],
  52. label2 ?? k_LabelDefaults[1],
  53. label3 ?? k_LabelDefaults[2],
  54. label4 ?? k_LabelDefaults[3]
  55. };
  56. }
  57. }
  58. public Vector4 defaultValue { get { return m_DefaultValue; } }
  59. public Vector4 value
  60. {
  61. get { return m_Value; }
  62. set { m_Value = value; }
  63. }
  64. public override bool isDefaultValue => value.Equals(defaultValue);
  65. public override VisualElement InstantiateControl()
  66. {
  67. return new MultiFloatSlotControlView(owner, labels, () => value, (newValue) => value = newValue);
  68. }
  69. protected override string ConcreteSlotValueAsVariable()
  70. {
  71. return string.Format("$precision4 ({0}, {1}, {2}, {3})"
  72. , NodeUtils.FloatToShaderValue(value.x)
  73. , NodeUtils.FloatToShaderValue(value.y)
  74. , NodeUtils.FloatToShaderValue(value.z)
  75. , NodeUtils.FloatToShaderValue(value.w));
  76. }
  77. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  78. {
  79. if (!generationMode.IsPreview())
  80. return;
  81. var matOwner = owner as AbstractMaterialNode;
  82. if (matOwner == null)
  83. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  84. var property = new Vector4ShaderProperty()
  85. {
  86. overrideReferenceName = matOwner.GetVariableNameForSlot(id),
  87. generatePropertyBlock = false,
  88. value = value
  89. };
  90. properties.AddShaderProperty(property);
  91. }
  92. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  93. {
  94. var pp = new PreviewProperty(PropertyType.Vector4)
  95. {
  96. name = name,
  97. vector4Value = new Vector4(value.x, value.y, value.z, value.w),
  98. };
  99. properties.Add(pp);
  100. }
  101. public override SlotValueType valueType { get { return SlotValueType.Vector4; } }
  102. public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector4; } }
  103. public override void CopyValuesFrom(MaterialSlot foundSlot)
  104. {
  105. var slot = foundSlot as Vector4MaterialSlot;
  106. if (slot != null)
  107. value = slot.value;
  108. }
  109. public override void CopyDefaultValue(MaterialSlot other)
  110. {
  111. base.CopyDefaultValue(other);
  112. if (other is IMaterialSlotHasValue<Vector4> ms)
  113. {
  114. m_DefaultValue = ms.defaultValue;
  115. }
  116. }
  117. }
  118. }