A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System;
3using UnityEngine;
4using UnityEditor.Graphing;
5using UnityEditor.ShaderGraph;
6using UnityEditor.ShaderGraph.Drawing.Controls;
7using UnityEditor.ShaderGraph.Internal;
8using System.Reflection;
9
10namespace UnityEditor.ShaderGraph
11{
12 [FormerName("UnityEditor.Rendering.HighDefinition.HDSceneDepthDifferenceNode")]
13 [Title("Input", "Scene", "Scene Depth Difference")]
14 sealed class SceneDepthDifferenceNode : CodeFunctionNode, IMayRequireDepthTexture, IMayRequireScreenPosition, IMayRequirePosition
15 {
16 [SerializeField]
17 private DepthSamplingMode m_DepthSamplingMode = DepthSamplingMode.Linear01;
18
19 [EnumControl("Sampling Mode")]
20 public DepthSamplingMode depthSamplingMode
21 {
22 get { return m_DepthSamplingMode; }
23 set
24 {
25 if (m_DepthSamplingMode == value)
26 return;
27
28 m_DepthSamplingMode = value;
29 Dirty(ModificationScope.Graph);
30 }
31 }
32
33 public SceneDepthDifferenceNode()
34 {
35 name = "Scene Depth Difference";
36 synonyms = new string[] { "zbuffer", "zdepth", "difference" };
37 UpdateNodeAfterDeserialization();
38 }
39
40 public override bool hasPreview { get { return false; } }
41
42 protected override MethodInfo GetFunctionToConvert()
43 {
44 switch (m_DepthSamplingMode)
45 {
46 case DepthSamplingMode.Raw:
47 return GetType().GetMethod("Unity_SceneDepthDifference_Raw", BindingFlags.Static | BindingFlags.NonPublic);
48 case DepthSamplingMode.Eye:
49 return GetType().GetMethod("Unity_SceneDepthDifference_Eye", BindingFlags.Static | BindingFlags.NonPublic);
50 case DepthSamplingMode.Linear01:
51 default:
52 return GetType().GetMethod("Unity_SceneDepthDifference_Linear01", BindingFlags.Static | BindingFlags.NonPublic);
53 }
54 }
55
56 static string Unity_SceneDepthDifference_Linear01(
57 [Slot(0, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out,
58 [Slot(1, Binding.ScreenPosition)] Vector2 SceneUV,
59 [Slot(2, Binding.WorldSpacePosition)] Vector2 PositionWS)
60 {
61 return
62@"
63{
64 $precision dist = Remap01(length(PositionWS), _ProjectionParams.y, _ProjectionParams.z);
65#if defined(UNITY_REVERSED_Z)
66 Out = Linear01Depth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), _ZBufferParams) - dist;
67#else
68 Out = dist - Linear01Depth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), _ZBufferParams);
69#endif
70}
71";
72 }
73
74 static string Unity_SceneDepthDifference_Raw(
75 [Slot(0, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out,
76 [Slot(1, Binding.ScreenPosition)] Vector2 SceneUV,
77 [Slot(2, Binding.WorldSpacePosition)] Vector3 PositionWS)
78 {
79 return
80@"
81{
82 $precision deviceDepth = ComputeNormalizedDeviceCoordinatesWithZ(PositionWS, GetWorldToHClipMatrix()).z;
83#if defined(UNITY_REVERSED_Z)
84 Out = deviceDepth - SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy);
85#else
86 Out = SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy) - deviceDepth;
87#endif
88}
89";
90 }
91
92 static string Unity_SceneDepthDifference_Eye(
93 [Slot(0, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out,
94 [Slot(1, Binding.ScreenPosition)] Vector2 SceneUV,
95 [Slot(2, Binding.WorldSpacePosition)] Vector3 PositionWS)
96 {
97 return
98@"
99{
100 if (IsPerspectiveProjection())
101 {
102#if defined(UNITY_REVERSED_Z)
103 Out = LinearEyeDepth(ComputeWorldSpacePosition(SceneUV.xy, SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), UNITY_MATRIX_I_VP), UNITY_MATRIX_V) - length(PositionWS);
104#else
105 Out = length(PositionWS) - LinearEyeDepth(ComputeWorldSpacePosition(SceneUV.xy, SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), UNITY_MATRIX_I_VP), UNITY_MATRIX_V);
106#endif
107 }
108 else
109 {
110#if defined(UNITY_REVERSED_Z)
111 Out = LinearEyeDepth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), _ZBufferParams) - length(PositionWS);
112#else
113 Out = length(PositionWS) - LinearEyeDepth(SHADERGRAPH_SAMPLE_SCENE_DEPTH(SceneUV.xy), _ZBufferParams);
114#endif
115 }
116}
117";
118 }
119
120 bool IMayRequireDepthTexture.RequiresDepthTexture(ShaderStageCapability stageCapability)
121 {
122 return true;
123 }
124
125 bool IMayRequireScreenPosition.RequiresScreenPosition(ShaderStageCapability stageCapability)
126 {
127 return true;
128 }
129
130 NeededCoordinateSpace IMayRequirePosition.RequiresPosition(ShaderStageCapability stageCapability)
131 {
132 return NeededCoordinateSpace.World;
133 }
134 }
135}