Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Vector1MaterialSlot.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasValue<float>
  12. {
  13. [SerializeField]
  14. float m_Value;
  15. [SerializeField]
  16. float m_DefaultValue;
  17. [SerializeField]
  18. string[] m_Labels; // this can be null, which means fallback to k_LabelDefaults
  19. static readonly string[] k_LabelDefaults = { "X" };
  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 Vector1MaterialSlot()
  30. {
  31. }
  32. public Vector1MaterialSlot(
  33. int slotId,
  34. string displayName,
  35. string shaderOutputName,
  36. SlotType slotType,
  37. float value,
  38. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  39. string label1 = null,
  40. bool hidden = false)
  41. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  42. {
  43. m_DefaultValue = value;
  44. m_Value = value;
  45. if (label1 != null)
  46. m_Labels = new[] { label1 };
  47. }
  48. public float defaultValue { get { return m_DefaultValue; } }
  49. public float value
  50. {
  51. get { return m_Value; }
  52. set { m_Value = value; }
  53. }
  54. public override bool isDefaultValue => value.Equals(defaultValue);
  55. public override VisualElement InstantiateControl()
  56. {
  57. return new MultiFloatSlotControlView(owner, labels, () => new Vector4(value, 0f, 0f, 0f), (newValue) => value = newValue.x);
  58. }
  59. protected override string ConcreteSlotValueAsVariable()
  60. {
  61. return string.Format("$precision({0})", NodeUtils.FloatToShaderValue(value));
  62. }
  63. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  64. {
  65. if (!generationMode.IsPreview())
  66. return;
  67. var matOwner = owner as AbstractMaterialNode;
  68. if (matOwner == null)
  69. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  70. var property = new Vector1ShaderProperty()
  71. {
  72. overrideReferenceName = matOwner.GetVariableNameForSlot(id),
  73. generatePropertyBlock = false,
  74. value = value
  75. };
  76. properties.AddShaderProperty(property);
  77. }
  78. public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
  79. public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }
  80. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  81. {
  82. var pp = new PreviewProperty(PropertyType.Float)
  83. {
  84. name = name,
  85. floatValue = value,
  86. };
  87. properties.Add(pp);
  88. }
  89. public override void CopyValuesFrom(MaterialSlot foundSlot)
  90. {
  91. var slot = foundSlot as Vector1MaterialSlot;
  92. if (slot != null)
  93. value = slot.value;
  94. }
  95. public override void CopyDefaultValue(MaterialSlot other)
  96. {
  97. base.CopyDefaultValue(other);
  98. if (other is IMaterialSlotHasValue<float> ms)
  99. {
  100. m_DefaultValue = ms.defaultValue;
  101. }
  102. }
  103. }
  104. }