A game about forced loneliness, made by TACStudios
1using System;
2using UnityEditor.Graphing;
3using UnityEngine;
4
5namespace UnityEditor.ShaderGraph
6{
7 [Serializable]
8 class SamplerStateMaterialSlot : MaterialSlot
9 {
10 public SamplerStateMaterialSlot()
11 {
12 }
13
14 public SamplerStateMaterialSlot(
15 int slotId,
16 string displayName,
17 string shaderOutputName,
18 SlotType slotType,
19 ShaderStageCapability stageCapability = ShaderStageCapability.All,
20 bool hidden = false)
21 : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden)
22 {
23 }
24
25 [SerializeField]
26 bool m_BareResource = false;
27 internal override bool bareResource
28 {
29 get { return m_BareResource; }
30 set { m_BareResource = value; }
31 }
32
33 // NOT serialized -- this is always set by the parent node if they care about it
34 public TextureSamplerState defaultSamplerState { get; set; }
35 public string defaultSamplerStateName => defaultSamplerState?.defaultPropertyName ?? "SamplerState_Linear_Repeat";
36
37 public override void AppendHLSLParameterDeclaration(ShaderStringBuilder sb, string paramName)
38 {
39 if (m_BareResource)
40 {
41 // we have to use our modified macro declaration here
42 // (the standard SAMPLER macro doesn't declare anything, so the commas will be messed up in the parameter list)
43 sb.Append("SAMPLER(");
44 sb.Append(paramName);
45 sb.Append(")");
46 }
47 else
48 base.AppendHLSLParameterDeclaration(sb, paramName);
49 }
50
51 public override string GetDefaultValue(GenerationMode generationMode)
52 {
53 var nodeOwner = owner as AbstractMaterialNode;
54 if (nodeOwner == null)
55 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
56
57 return $"UnityBuildSamplerStateStruct({defaultSamplerStateName})";
58 }
59
60 public override SlotValueType valueType { get { return SlotValueType.SamplerState; } }
61 public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.SamplerState; } }
62 public override bool isDefaultValue => true;
63
64 public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
65 {
66 var nodeOwner = owner as AbstractMaterialNode;
67 if (nodeOwner == null)
68 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
69
70 properties.AddShaderProperty(new SamplerStateShaderProperty()
71 {
72 value = defaultSamplerState ?? new TextureSamplerState()
73 {
74 filter = TextureSamplerState.FilterMode.Linear,
75 wrap = TextureSamplerState.WrapMode.Repeat
76 },
77 overrideReferenceName = defaultSamplerStateName,
78 generatePropertyBlock = false,
79 });
80 }
81
82 public override void CopyValuesFrom(MaterialSlot foundSlot)
83 { }
84
85 public override void CopyDefaultValue(MaterialSlot other)
86 {
87 base.CopyDefaultValue(other);
88 if (other is SamplerStateMaterialSlot ms)
89 {
90 defaultSamplerState = ms.defaultSamplerState;
91 }
92 }
93 }
94}