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 [Title("Input", "Texture", "Texture 2D Asset")]
10 [HasDependencies(typeof(Minimal2d3dTextureAssetNode))]
11 class Texture2DAssetNode : AbstractMaterialNode, IPropertyFromNode
12 {
13 public const int OutputSlotId = 0;
14
15 const string kOutputSlotName = "Out";
16
17 public Texture2DAssetNode()
18 {
19 name = "Texture 2D Asset";
20 UpdateNodeAfterDeserialization();
21 }
22
23 public sealed override void UpdateNodeAfterDeserialization()
24 {
25 AddSlot(new Texture2DMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
26 RemoveSlotsNameNotMatching(new[] { OutputSlotId });
27 }
28
29 [SerializeField]
30 private SerializableTexture m_Texture = new SerializableTexture();
31
32 [TextureControl("")]
33 public Texture texture
34 {
35 get { return m_Texture.texture; }
36 set
37 {
38 if (m_Texture.texture == value)
39 return;
40 m_Texture.texture = value;
41 Dirty(ModificationScope.Node);
42 }
43 }
44
45 string GetTexturePropertyName()
46 {
47 return base.GetVariableNameForSlot(OutputSlotId);
48 }
49
50 public override string GetVariableNameForSlot(int slotId)
51 {
52 return $"UnityBuildTexture2DStructNoScale({GetTexturePropertyName()})";
53 }
54
55 public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
56 {
57 properties.AddShaderProperty(new Texture2DShaderProperty()
58 {
59 overrideReferenceName = GetTexturePropertyName(),
60 generatePropertyBlock = true,
61 value = m_Texture,
62 modifiable = false
63 });
64 }
65
66 public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
67 {
68 properties.Add(new PreviewProperty(PropertyType.Texture2D)
69 {
70 name = GetTexturePropertyName(),
71 textureValue = texture,
72 texture2DDefaultType = Texture2DShaderProperty.DefaultType.White
73 });
74 }
75
76 public AbstractShaderProperty AsShaderProperty()
77 {
78 var prop = new Texture2DShaderProperty { value = m_Texture };
79 if (texture != null)
80 prop.displayName = texture.name;
81 return prop;
82 }
83
84 public int outputSlotId { get { return OutputSlotId; } }
85 }
86
87 // this is used for Texture2D AND Texture3D
88 class Minimal2d3dTextureAssetNode : IHasDependencies
89 {
90 [SerializeField]
91 private SerializableTexture m_Texture = null;
92
93 public void GetSourceAssetDependencies(AssetCollection assetCollection)
94 {
95 var guidString = m_Texture.guid;
96 if (!string.IsNullOrEmpty(guidString) && GUID.TryParse(guidString, out var guid))
97 {
98 assetCollection.AddAssetDependency(guid, AssetCollection.Flags.IncludeInExportPackage);
99 }
100 }
101 }
102}