Skip to content

Commit d004981

Browse files
anonymous2585anonymous
andauthored
Add support for Unity URP SSAO in "Depth normal" mode (#225) (#226)
Co-authored-by: anonymous <[email protected]>
1 parent 85fb721 commit d004981

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

com.microsoft.mrtk.graphicstools.unity/Runtime/Shaders/GraphicsToolsStandard.shader

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Shader "Graphics Tools/Standard"
182182
#pragma multi_compile_instancing
183183
#pragma multi_compile _ LIGHTMAP_ON
184184
#pragma multi_compile_local _ _CLIPPING_PLANE _CLIPPING_SPHERE _CLIPPING_BOX
185+
#pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
185186

186187
#pragma shader_feature_local_fragment _CLIPPING_BORDER
187188

@@ -229,6 +230,124 @@ Shader "Graphics Tools/Standard"
229230

230231
ENDHLSL
231232
}
233+
234+
// From Packages/com.unity.render-pipelines.universal/Shader/Lit.hlsl
235+
Pass
236+
{
237+
Name "DepthOnly"
238+
Tags
239+
{
240+
"LightMode" = "DepthOnly"
241+
}
242+
243+
// -------------------------------------
244+
// Render State Commands
245+
ZWrite On
246+
ColorMask R
247+
Cull[_Cull]
248+
249+
HLSLPROGRAM
250+
#if UNITY_VERSION >= 202230
251+
#pragma exclude_renderers gles gles3 glcore
252+
#pragma target 4.5
253+
#else
254+
#pragma target 2.0
255+
#endif
256+
257+
// -------------------------------------
258+
// Shader Stages
259+
#pragma vertex DepthOnlyVertex
260+
#pragma fragment DepthOnlyFragment
261+
262+
// -------------------------------------
263+
// Material Keywords
264+
#pragma shader_feature_local _ALPHATEST_ON
265+
#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
266+
267+
#if UNITY_VERSION >= 202200
268+
// -------------------------------------
269+
// Unity defined keywords
270+
#pragma multi_compile _ LOD_FADE_CROSSFADE
271+
#endif
272+
273+
//--------------------------------------
274+
// GPU Instancing
275+
#pragma multi_compile_instancing
276+
#if UNITY_VERSION >= 202230
277+
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
278+
#else
279+
#pragma multi_compile _ DOTS_INSTANCING_ON
280+
#endif
281+
282+
// -------------------------------------
283+
// Includes
284+
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
285+
#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
286+
ENDHLSL
287+
}
288+
289+
// From Packages/com.unity.render-pipelines.universal/Shader/Lit.hlsl
290+
// This pass is used when drawing to a _CameraNormalsTexture texture
291+
Pass
292+
{
293+
Name "DepthNormals"
294+
Tags
295+
{
296+
"LightMode" = "DepthNormals"
297+
}
298+
299+
// -------------------------------------
300+
// Render State Commands
301+
ZWrite On
302+
Cull[_Cull]
303+
304+
HLSLPROGRAM
305+
#if UNITY_VERSION >= 202230
306+
#pragma target 2.0
307+
#else
308+
#pragma exclude_renderers gles gles3 glcore
309+
#pragma target 4.5
310+
#endif
311+
312+
// -------------------------------------
313+
// Shader Stages
314+
#pragma vertex DepthNormalsVertex
315+
#pragma fragment DepthNormalsFragment
316+
317+
// -------------------------------------
318+
// Material Keywords
319+
#pragma shader_feature_local _NORMALMAP
320+
#pragma shader_feature_local _PARALLAXMAP
321+
#pragma shader_feature_local _ _DETAIL_MULX2 _DETAIL_SCALED
322+
#pragma shader_feature_local _ALPHATEST_ON
323+
#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
324+
325+
#if UNITY_VERSION >= 202200
326+
// -------------------------------------
327+
// Unity defined keywords
328+
#pragma multi_compile _ LOD_FADE_CROSSFADE
329+
#endif
330+
#if UNITY_VERSION >= 202230
331+
// -------------------------------------
332+
// Universal Pipeline keywords
333+
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl"
334+
#endif
335+
336+
//--------------------------------------
337+
// GPU Instancing
338+
#pragma multi_compile_instancing
339+
#if UNITY_VERSION >= 202230
340+
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
341+
#else
342+
#pragma multi_compile _ DOTS_INSTANCING_ON
343+
#endif
344+
345+
// -------------------------------------
346+
// Includes
347+
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
348+
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitDepthNormalsPass.hlsl"
349+
ENDHLSL
350+
}
232351
}
233352

234353
/// <summary>

com.microsoft.mrtk.graphicstools.unity/Runtime/Shaders/GraphicsToolsStandardProgram.hlsl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@
133133
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
134134
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
135135
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
136+
#ifdef _SCREEN_SPACE_OCCLUSION
137+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/AmbientOcclusion.hlsl"
138+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
139+
#endif
136140
#else
137141
#include "UnityCG.cginc"
138142
#include "UnityStandardConfig.cginc"
@@ -801,6 +805,12 @@ half4 PixelStage(Varyings input, bool facing : SV_IsFrontFace) : SV_Target
801805
half3 reflection = GTGlossyEnvironmentReflection(reflectVector, GTPerceptualSmoothnessToPerceptualRoughness(_Smoothness), occlusion);
802806
output.rgb = (albedo.rgb * 0.5h) + (reflection * (_Smoothness + _Metallic) * 0.5h);
803807
#endif
808+
809+
#if defined(_SCREEN_SPACE_OCCLUSION)
810+
float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.position);
811+
AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
812+
output.rgb *= aoFactor.directAmbientOcclusion;
813+
#endif
804814

805815
// Fresnel lighting.
806816
#if defined(_RIM_LIGHT)
@@ -836,7 +846,6 @@ half4 PixelStage(Varyings input, bool facing : SV_IsFrontFace) : SV_Target
836846
incident.y * incident.y * _EnvironmentColorY +
837847
incident.z * incident.z * _EnvironmentColorZ;
838848
output.rgb += environmentColor * max(0.0, dot(incident, worldNormal) + _EnvironmentColorThreshold) * _EnvironmentColorIntensity;
839-
840849
#endif
841850

842851
#if defined(_NEAR_PLANE_FADE)

0 commit comments

Comments
 (0)