A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using UnityEditor.ShaderGraph.Drawing.Controls;
3using UnityEngine;
4using UnityEditor.Graphing;
5using UnityEditor.ShaderGraph.Internal;
6
7namespace UnityEditor.ShaderGraph
8{
9#if PROCEDURAL_VT_IN_GRAPH
10 [Title("Input", "Texture", "Procedural Virtual Texture")]
11 class ProceduralVirtualTextureNode : AbstractMaterialNode
12 {
13 public const int OutputSlotId = 0;
14
15 const string kOutputSlotName = "Out";
16
17 public ProceduralVirtualTextureNode()
18 {
19 UpdateNodeAfterDeserialization();
20 SetLayerCount(2);
21
22 vtProperty.displayName = "ProceduralVirtualTexture";
23 vtProperty.overrideReferenceName = "MyPVT";
24 vtProperty.value.procedural = true;
25 vtProperty.value.shaderDeclaration = HLSLDeclaration.UnityPerMaterial;
26
27 UpdateName();
28 }
29
30 void UpdateName()
31 {
32 name = "Procedural Virtual Texture: " + vtProperty.overrideReferenceName;
33 }
34
35 public sealed override void UpdateNodeAfterDeserialization()
36 {
37 AddSlot(new VirtualTextureMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
38 RemoveSlotsNameNotMatching(new[] { OutputSlotId });
39 SetLayerCount(layers);
40 vtProperty.generatePropertyBlock = true;
41 vtProperty.hidden = true;
42 }
43
44 public override int latestVersion => 1;
45 public override void OnAfterMultiDeserialize(string json)
46 {
47 if (sgVersion == 0)
48 {
49 // version 0 was implicitly declaring PVT stacks as Global shader properties
50 shaderDeclaration = HLSLDeclaration.Global;
51 ChangeVersion(1);
52 }
53 }
54
55 [SerializeField]
56 private VirtualTextureShaderProperty vtProperty = new VirtualTextureShaderProperty();
57
58 void SetLayerCount(int layers)
59 {
60 var uniqueName = objectId;
61 vtProperty.value.layers.Clear();
62 layers = System.Math.Max(System.Math.Min(layers, SampleVirtualTextureNode.kMaxLayers), SampleVirtualTextureNode.kMinLayers);
63 for (int x = 0; x < layers; x++)
64 {
65 vtProperty.value.layers.Add(new SerializableVirtualTextureLayer("Layer" + x + "_" + uniqueName, "Layer" + x + "_" + uniqueName, null));
66 }
67 }
68
69 [IdentifierControl("Name")]
70 public string vtName
71 {
72 get { return vtProperty.overrideReferenceName; }
73 set
74 {
75 if (vtProperty.overrideReferenceName == value)
76 return;
77 vtProperty.overrideReferenceName = value;
78 UpdateName();
79 Dirty(ModificationScope.Graph);
80 }
81 }
82
83 [IntegerControl("Layers")]
84 public int layers
85 {
86 get { return vtProperty.value.layers.Count; }
87 set
88 {
89 if (vtProperty.value.layers.Count == value)
90 return;
91
92 SetLayerCount(value);
93 Dirty(ModificationScope.Topological);
94 //Hack to handle downstream SampleVirtualTextureNodes
95 owner.ValidateGraph();
96 }
97 }
98
99 internal HLSLDeclaration shaderDeclaration
100 {
101 get { return vtProperty.value.shaderDeclaration; }
102 set
103 {
104 if (vtProperty.value.shaderDeclaration == value)
105 return;
106
107 vtProperty.value.shaderDeclaration = value;
108 Dirty(ModificationScope.Graph);
109 }
110 }
111
112 public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
113 {
114 properties.AddShaderProperty(vtProperty);
115 }
116
117 public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
118 {
119 properties.Add(new PreviewProperty(PropertyType.VirtualTexture)
120 {
121 name = GetVariableNameForSlot(OutputSlotId),
122 vtProperty = vtProperty
123 });
124 }
125
126 public AbstractShaderProperty AsShaderProperty()
127 {
128 return vtProperty;
129 }
130
131 // to show Shader Declaration in the node settings, as if this node was itself a real AbstractShaderProperty
132 internal bool AllowHLSLDeclaration(HLSLDeclaration decl) =>
133 (decl == HLSLDeclaration.Global || decl == HLSLDeclaration.UnityPerMaterial);
134 }
135#endif // PROCEDURAL_VT_IN_GRAPH
136}