A game about forced loneliness, made by TACStudios
1#ifndef UNITY_META_PASS_INCLUDED
2#define UNITY_META_PASS_INCLUDED
3
4#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
5#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
6
7CBUFFER_START(UnityMetaPass)
8 // x = use uv1 as raster position
9 // y = use uv2 as raster position
10 bool4 unity_MetaVertexControl;
11
12 // x = return albedo
13 // y = return normal
14 bool4 unity_MetaFragmentControl;
15
16 // Control which VisualizationMode we will
17 // display in the editor
18 int unity_VisualizationMode;
19CBUFFER_END
20
21struct UnityMetaInput
22{
23 half3 Albedo;
24 half3 Emission;
25#ifdef EDITOR_VISUALIZATION
26 float2 VizUV;
27 float4 LightCoord;
28#endif
29};
30
31#ifdef EDITOR_VISUALIZATION
32// Visualization defines
33// Should be kept in sync with the EditorVisualizationMode enum in EditorCameraDrawing.cpp
34// First two are unused.
35#define EDITORVIZ_PBR_VALIDATION_ALBEDO 0
36#define EDITORVIZ_PBR_VALIDATION_METALSPECULAR 1
37#define EDITORVIZ_TEXTURE 2
38#define EDITORVIZ_SHOWLIGHTMASK 3
39
40uniform sampler2D unity_EditorViz_Texture;
41uniform half4 unity_EditorViz_Texture_ST;
42uniform int unity_EditorViz_UVIndex;
43uniform half4 unity_EditorViz_Decode_HDR;
44uniform bool unity_EditorViz_ConvertToLinearSpace;
45uniform half4 unity_EditorViz_ColorMul;
46uniform half4 unity_EditorViz_ColorAdd;
47uniform half unity_EditorViz_Exposure;
48uniform sampler2D unity_EditorViz_LightTexture;
49uniform sampler2D unity_EditorViz_LightTextureB;
50#define unity_EditorViz_ChannelSelect unity_EditorViz_ColorMul
51#define unity_EditorViz_Color unity_EditorViz_ColorAdd
52#define unity_EditorViz_LightType unity_EditorViz_UVIndex
53uniform float4x4 unity_EditorViz_WorldToLight;
54#endif // EDITOR_VISUALIZATION
55
56float2 UnityMetaVizUV(int uvIndex, float2 uv0, float2 uv1, float2 uv2, float4 st)
57{
58 if (uvIndex == 0)
59 return uv0 * st.xy + st.zw;
60 else if (uvIndex == 1)
61 return uv1 * st.xy + st.zw;
62 else
63 return uv2 * st.xy + st.zw;
64}
65
66void UnityEditorVizData(float3 positionOS, float2 uv0, float2 uv1, float2 uv2, float4 st, out float2 VizUV, out float4 LightCoord)
67{
68#ifdef EDITOR_VISUALIZATION
69 if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
70 VizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, uv0, uv1, uv2, unity_EditorViz_Texture_ST);
71 else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
72 {
73 VizUV = uv1 * st.xy + st.zw;
74 LightCoord = mul(unity_EditorViz_WorldToLight, float4(TransformObjectToWorld(positionOS), 1));
75 }
76#endif
77}
78
79void UnityEditorVizData(float3 positionOS, float2 uv0, float2 uv1, float2 uv2, out float2 VizUV, out float4 LightCoord)
80{
81 float4 st = unity_LightmapST;
82 if (unity_MetaVertexControl.y)
83 st = unity_DynamicLightmapST;
84 UnityEditorVizData(positionOS, uv0, uv1, uv2, st, VizUV, LightCoord);
85}
86
87float4 UnityMetaVertexPosition(float3 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST)
88{
89#ifndef EDITOR_VISUALIZATION
90 if (unity_MetaVertexControl.x)
91 {
92 vertex.xy = uv1 * lightmapST.xy + lightmapST.zw;
93 // OpenGL right now needs to actually use incoming vertex position,
94 // so use it in a very dummy way
95 vertex.z = vertex.z > 0 ? REAL_MIN : 0.0f;
96 }
97 if (unity_MetaVertexControl.y)
98 {
99 vertex.xy = uv2 * dynlightmapST.xy + dynlightmapST.zw;
100 // OpenGL right now needs to actually use incoming vertex position,
101 // so use it in a very dummy way
102 vertex.z = vertex.z > 0 ? REAL_MIN : 0.0f;
103 }
104 return TransformWorldToHClip(vertex);
105#else
106 return TransformObjectToHClip(vertex);
107#endif
108}
109
110float4 UnityMetaVertexPosition(float3 vertex, float2 uv1, float2 uv2)
111{
112 return UnityMetaVertexPosition(vertex, uv1, uv2, unity_LightmapST, unity_DynamicLightmapST);
113}
114
115float unity_OneOverOutputBoost;
116float unity_MaxOutputValue;
117float unity_UseLinearSpace;
118
119half4 UnityMetaFragment (UnityMetaInput IN)
120{
121 half4 res = 0;
122#ifndef EDITOR_VISUALIZATION
123 if (unity_MetaFragmentControl.x)
124 {
125 res = half4(IN.Albedo,1);
126
127 // Apply Albedo Boost from LightmapSettings.
128 res.rgb = clamp(pow(abs(res.rgb), saturate(unity_OneOverOutputBoost)), 0, unity_MaxOutputValue);
129 }
130 if (unity_MetaFragmentControl.y)
131 {
132 half3 emission;
133 if (unity_UseLinearSpace)
134 emission = IN.Emission;
135 else
136 emission = Gamma20ToLinear(IN.Emission);
137
138 res = half4(emission, 1.0);
139 }
140#else
141 // SRPs don't support EDITORVIZ_PBR_VALIDATION_ALBEDO or EDITORVIZ_PBR_VALIDATION_METALSPECULAR
142 if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
143 {
144 res = tex2D(unity_EditorViz_Texture, IN.VizUV);
145
146 if (unity_EditorViz_Decode_HDR.x > 0)
147 res = half4(DecodeHDREnvironment(res, unity_EditorViz_Decode_HDR), 1);
148
149 if (unity_EditorViz_ConvertToLinearSpace)
150 res.rgb = LinearToGamma20(res.rgb);
151
152 res *= unity_EditorViz_ColorMul;
153 res += unity_EditorViz_ColorAdd;
154 res *= exp2(unity_EditorViz_Exposure);
155 }
156 else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
157 {
158 float result = dot(unity_EditorViz_ChannelSelect, tex2D(unity_EditorViz_Texture, IN.VizUV).rgba);
159 if (result == 0)
160 discard;
161
162 float atten = 1;
163 if (unity_EditorViz_LightType == 0)
164 {
165 // directional: no attenuation
166 }
167 else if (unity_EditorViz_LightType == 1)
168 {
169 // point
170 atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
171 }
172 else if (unity_EditorViz_LightType == 2)
173 {
174 // spot
175 atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
176 float cookie = tex2D(unity_EditorViz_LightTextureB, IN.LightCoord.xy / max(IN.LightCoord.w, 0.0001) + 0.5).w;
177 atten *= (IN.LightCoord.z > 0) * cookie;
178 }
179 clip(atten - 0.001f);
180
181 res = float4(unity_EditorViz_Color.xyz * result, unity_EditorViz_Color.w);
182 }
183#endif // EDITOR_VISUALIZATION
184 return res;
185}
186
187
188#endif // UNITY_META_PASS_INCLUDED