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.

GradientInputMaterialSlot.cs 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 GradientInputMaterialSlot : GradientMaterialSlot, IMaterialSlotHasValue<Gradient>
  12. {
  13. [SerializeField]
  14. Gradient m_Value = new Gradient();
  15. [SerializeField]
  16. Gradient m_DefaultValue = new Gradient();
  17. public GradientInputMaterialSlot()
  18. {
  19. }
  20. public GradientInputMaterialSlot(
  21. int slotId,
  22. string displayName,
  23. string shaderOutputName,
  24. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  25. bool hidden = false)
  26. : base(slotId, displayName, shaderOutputName, SlotType.Input, stageCapability, hidden)
  27. {
  28. }
  29. public Gradient value
  30. {
  31. get { return m_Value; }
  32. set { m_Value = value; }
  33. }
  34. public Gradient defaultValue { get { return m_DefaultValue; } }
  35. public override bool isDefaultValue => value.Equals(defaultValue);
  36. public override VisualElement InstantiateControl()
  37. {
  38. return new GradientSlotControlView(this);
  39. }
  40. public override string GetDefaultValue(GenerationMode generationMode)
  41. {
  42. var matOwner = owner as AbstractMaterialNode;
  43. if (matOwner == null)
  44. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  45. if (generationMode.IsPreview())
  46. return GradientUtil.GetGradientForPreview(matOwner.GetVariableNameForSlot(id));
  47. return ConcreteSlotValueAsVariable();
  48. }
  49. protected override string ConcreteSlotValueAsVariable()
  50. {
  51. return GradientUtil.GetGradientValue(value, "");
  52. }
  53. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  54. {
  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. if (generationMode != GenerationMode.Preview)
  59. return;
  60. GradientUtil.GetGradientPropertiesForPreview(properties, matOwner.GetVariableNameForSlot(id), value);
  61. }
  62. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  63. {
  64. properties.Add(new PreviewProperty(PropertyType.Gradient)
  65. {
  66. name = name,
  67. gradientValue = value
  68. });
  69. }
  70. public override void CopyValuesFrom(MaterialSlot foundSlot)
  71. {
  72. var slot = foundSlot as GradientInputMaterialSlot;
  73. if (slot != null)
  74. value = slot.value;
  75. }
  76. }
  77. }