Vector1MaterialSlot.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  19. public Vector1MaterialSlot()
  20. {
  21. }
  22. public Vector1MaterialSlot(
  23. int slotId,
  24. string displayName,
  25. string shaderOutputName,
  26. SlotType slotType,
  27. float value,
  28. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  29. string label1 = "X",
  30. bool hidden = false)
  31. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  32. {
  33. m_DefaultValue = value;
  34. m_Value = value;
  35. m_Labels = new[] { label1 };
  36. }
  37. public float defaultValue { get { return m_DefaultValue; } }
  38. public float value
  39. {
  40. get { return m_Value; }
  41. set { m_Value = value; }
  42. }
  43. public override VisualElement InstantiateControl()
  44. {
  45. return new MultiFloatSlotControlView(owner, m_Labels, () => new Vector4(value, 0f, 0f, 0f), (newValue) => value = newValue.x);
  46. }
  47. protected override string ConcreteSlotValueAsVariable()
  48. {
  49. return NodeUtils.FloatToShaderValue(value);
  50. }
  51. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  52. {
  53. if (!generationMode.IsPreview())
  54. return;
  55. var matOwner = owner as AbstractMaterialNode;
  56. if (matOwner == null)
  57. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  58. var property = new Vector1ShaderProperty()
  59. {
  60. overrideReferenceName = matOwner.GetVariableNameForSlot(id),
  61. generatePropertyBlock = false,
  62. value = value
  63. };
  64. properties.AddShaderProperty(property);
  65. }
  66. public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
  67. public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }
  68. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  69. {
  70. var pp = new PreviewProperty(PropertyType.Vector1)
  71. {
  72. name = name,
  73. floatValue = value,
  74. };
  75. properties.Add(pp);
  76. }
  77. public override void CopyValuesFrom(MaterialSlot foundSlot)
  78. {
  79. var slot = foundSlot as Vector1MaterialSlot;
  80. if (slot != null)
  81. value = slot.value;
  82. }
  83. }
  84. }