SliderNode.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections.Generic;
  2. using UnityEditor.ShaderGraph.Drawing.Controls;
  3. using UnityEngine;
  4. using UnityEditor.Graphing;
  5. using System.Globalization;
  6. using UnityEditor.ShaderGraph.Internal;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. [Title("Input", "Basic", "Slider")]
  10. class SliderNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
  11. {
  12. [SerializeField]
  13. private Vector3 m_Value = new Vector3(0f, 0f, 1f);
  14. public const int OutputSlotId = 0;
  15. private const string kOutputSlotName = "Out";
  16. public SliderNode()
  17. {
  18. name = "Slider";
  19. UpdateNodeAfterDeserialization();
  20. }
  21. public sealed override void UpdateNodeAfterDeserialization()
  22. {
  23. AddSlot(new Vector1MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, 0));
  24. RemoveSlotsNameNotMatching(new[] { OutputSlotId });
  25. }
  26. [SliderControl("", true)]
  27. public Vector3 value
  28. {
  29. get { return m_Value; }
  30. set
  31. {
  32. if (m_Value == value)
  33. return;
  34. m_Value = value;
  35. Dirty(ModificationScope.Node);
  36. }
  37. }
  38. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  39. {
  40. if (!generationMode.IsPreview())
  41. return;
  42. properties.AddShaderProperty(new Vector1ShaderProperty()
  43. {
  44. overrideReferenceName = GetVariableNameForNode(),
  45. generatePropertyBlock = false,
  46. value = value.x,
  47. rangeValues = new Vector2(value.y, value.z),
  48. floatType = FloatType.Slider
  49. });
  50. }
  51. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  52. {
  53. if (generationMode.IsPreview())
  54. return;
  55. sb.AppendLine(string.Format(CultureInfo.InvariantCulture,"$precision {0} = {1};", GetVariableNameForNode(), m_Value.x));
  56. }
  57. public override string GetVariableNameForSlot(int slotId)
  58. {
  59. return GetVariableNameForNode();
  60. }
  61. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  62. {
  63. properties.Add(new PreviewProperty(PropertyType.Vector1)
  64. {
  65. name = GetVariableNameForNode(),
  66. floatValue = m_Value.x
  67. });
  68. }
  69. public AbstractShaderProperty AsShaderProperty()
  70. {
  71. return new Vector1ShaderProperty
  72. {
  73. value = value.x,
  74. rangeValues = new Vector2(value.y, value.z),
  75. floatType = FloatType.Slider
  76. };
  77. }
  78. public int outputSlotId { get { return OutputSlotId; } }
  79. }
  80. }