A game about forced loneliness, made by TACStudios
1using System.Linq;
2using UnityEngine;
3using UnityEditor.Graphing;
4using UnityEditor.ShaderGraph.Internal;
5
6namespace UnityEditor.ShaderGraph
7{
8 [FormerName("UnityEditor.ShaderGraph.CubemapNode")]
9 [Title("Input", "Texture", "Sample Reflected Cubemap")]
10 class SampleCubemapNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireViewDirection, IMayRequireNormal
11 {
12 public const int OutputSlotId = 0;
13 public const int CubemapInputId = 1;
14 public const int ViewDirInputId = 2;
15 public const int NormalInputId = 3;
16 public const int SamplerInputId = 5;
17 public const int LODInputId = 4;
18 const string kOutputSlotName = "Out";
19 const string kCubemapInputName = "Cube";
20 const string kViewDirInputName = "ViewDir";
21 const string kNormalInputName = "Normal";
22 const string kSamplerInputName = "Sampler";
23 const string kLODInputName = "LOD";
24
25 public override bool hasPreview { get { return true; } }
26
27 public SampleCubemapNode()
28 {
29 name = "Sample Reflected Cubemap";
30 m_PreviewMode = PreviewMode.Preview3D;
31 UpdateNodeAfterDeserialization();
32 }
33
34 public sealed override void UpdateNodeAfterDeserialization()
35 {
36 AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
37 AddSlot(new CubemapInputMaterialSlot(CubemapInputId, kCubemapInputName, kCubemapInputName));
38 AddSlot(new ViewDirectionMaterialSlot(ViewDirInputId, kViewDirInputName, kViewDirInputName, CoordinateSpace.Object));
39 AddSlot(new NormalMaterialSlot(NormalInputId, kNormalInputName, kNormalInputName, CoordinateSpace.Object));
40 AddSlot(new SamplerStateMaterialSlot(SamplerInputId, kSamplerInputName, kSamplerInputName, SlotType.Input));
41 AddSlot(new Vector1MaterialSlot(LODInputId, kLODInputName, kLODInputName, SlotType.Input, 0));
42 RemoveSlotsNameNotMatching(new[] { OutputSlotId, CubemapInputId, ViewDirInputId, NormalInputId, SamplerInputId, LODInputId });
43 }
44
45 // Node generations
46 public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
47 {
48 //Sampler input slot
49 var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInputId);
50 var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
51
52 var id = GetSlotValue(CubemapInputId, generationMode);
53 string result = string.Format("$precision4 {0} = SAMPLE_TEXTURECUBE_LOD({1}.tex, {2}.samplerstate, reflect(-{3}, {4}), {5});"
54 , GetVariableNameForSlot(OutputSlotId)
55 , id
56 , edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : id
57 , GetSlotValue(ViewDirInputId, generationMode)
58 , GetSlotValue(NormalInputId, generationMode)
59 , GetSlotValue(LODInputId, generationMode));
60
61 sb.AppendLine(result);
62 }
63
64 public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
65 {
66 var viewDirSlot = FindInputSlot<MaterialSlot>(ViewDirInputId);
67 var edgesViewDir = owner.GetEdges(viewDirSlot.slotReference);
68 if (!edgesViewDir.Any())
69 return CoordinateSpace.Object.ToNeededCoordinateSpace();
70 else
71 return NeededCoordinateSpace.None;
72 }
73
74 public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
75 {
76 var normalSlot = FindInputSlot<MaterialSlot>(NormalInputId);
77 var edgesNormal = owner.GetEdges(normalSlot.slotReference);
78 if (!edgesNormal.Any())
79 return CoordinateSpace.Object.ToNeededCoordinateSpace();
80 else
81 return NeededCoordinateSpace.None;
82 }
83
84 public override void OnAfterDeserialize()
85 {
86 base.OnAfterDeserialize();
87
88 name = "Sample Reflected Cubemap";
89 }
90 }
91}