暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BooleanMaterialSlot.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 BooleanMaterialSlot : MaterialSlot, IMaterialSlotHasValue<bool>
  12. {
  13. [SerializeField]
  14. private bool m_Value;
  15. [SerializeField]
  16. private bool m_DefaultValue;
  17. public BooleanMaterialSlot()
  18. { }
  19. public BooleanMaterialSlot(
  20. int slotId,
  21. string displayName,
  22. string shaderOutputName,
  23. SlotType slotType,
  24. bool value,
  25. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  26. bool hidden = false)
  27. : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
  28. {
  29. m_DefaultValue = value;
  30. m_Value = value;
  31. }
  32. public override VisualElement InstantiateControl()
  33. {
  34. return new BooleanSlotControlView(this);
  35. }
  36. public bool defaultValue { get { return m_DefaultValue; } }
  37. public bool value
  38. {
  39. get { return m_Value; }
  40. set { m_Value = value; }
  41. }
  42. public override bool isDefaultValue => value.Equals(defaultValue);
  43. protected override string ConcreteSlotValueAsVariable()
  44. {
  45. return (value ? 1 : 0).ToString();
  46. }
  47. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  48. {
  49. if (!generationMode.IsPreview())
  50. return;
  51. var matOwner = owner as AbstractMaterialNode;
  52. if (matOwner == null)
  53. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  54. var property = new BooleanShaderProperty()
  55. {
  56. overrideReferenceName = matOwner.GetVariableNameForSlot(id),
  57. generatePropertyBlock = false,
  58. value = value
  59. };
  60. properties.AddShaderProperty(property);
  61. }
  62. public override SlotValueType valueType { get { return SlotValueType.Boolean; } }
  63. public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Boolean; } }
  64. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  65. {
  66. var pp = new PreviewProperty(PropertyType.Boolean)
  67. {
  68. name = name,
  69. booleanValue = value
  70. };
  71. properties.Add(pp);
  72. }
  73. public override void CopyValuesFrom(MaterialSlot foundSlot)
  74. {
  75. var slot = foundSlot as BooleanMaterialSlot;
  76. if (slot != null)
  77. value = slot.value;
  78. }
  79. public override void CopyDefaultValue(MaterialSlot other)
  80. {
  81. base.CopyDefaultValue(other);
  82. if (other is IMaterialSlotHasValue<bool> ms)
  83. {
  84. m_DefaultValue = ms.defaultValue;
  85. }
  86. }
  87. }
  88. }