From 30dcd8a491434944baee22bdb93a5b7853d3bb74 Mon Sep 17 00:00:00 2001 From: Coty Getzelman Date: Thu, 22 Aug 2019 11:53:14 -0700 Subject: [PATCH] SSR allows toggling motion vectors. Still needs the custom editor to be updated. --- .../Runtime/Effects/ScreenSpaceReflections.cs | 28 +++++++++++++++++-- .../Builtins/ScreenSpaceReflections.hlsl | 17 +++++++++-- .../Builtins/ScreenSpaceReflections.shader | 2 ++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs b/PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs index 80769aac..9d15b17f 100644 --- a/PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs +++ b/PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs @@ -97,12 +97,19 @@ public sealed class ScreenSpaceReflections : PostProcessEffectSettings [Range(0f, 1f), Tooltip("Fades reflections close to the screen edges.")] public FloatParameter vignette = new FloatParameter { value = 0.5f }; + + /// + /// Enables the use of motion vectors to slightly improve visual quality. + /// + [Tooltip("Enables the use of motion vectors to slightly improve visual quality.")] + public BoolParameter useMotionVectors = new BoolParameter { value = false }; + /// public override bool IsEnabledAndSupported(PostProcessRenderContext context) { return enabled && context.camera.actualRenderingPath == RenderingPath.DeferredShading - && SystemInfo.supportsMotionVectors + && (!useMotionVectors.value || SystemInfo.supportsMotionVectors) && SystemInfo.supportsComputeShaders && SystemInfo.copyTextureSupport > CopyTextureSupport.None && context.resources.shaders.screenSpaceReflections @@ -148,7 +155,14 @@ enum Pass public override DepthTextureMode GetCameraFlags() { - return DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + if(settings.useMotionVectors.value) + { + return DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + } + else + { + return DepthTextureMode.Depth; + } } internal void CheckRT(ref RenderTexture rt, int width, int height, FilterMode filterMode, bool useMipMap) @@ -207,6 +221,16 @@ public override void Render(PostProcessRenderContext context) var noiseTex = context.resources.blueNoise256[0]; var sheet = context.propertySheets.Get(context.resources.shaders.screenSpaceReflections); + + if (settings.useMotionVectors.value) + { + sheet.EnableKeyword("_UseMotionVectors"); + } + else + { + sheet.DisableKeyword("_UseMotionVectors"); + } + sheet.properties.SetTexture(ShaderIDs.Noise, noiseTex); var screenSpaceProjectionMatrix = new Matrix4x4(); diff --git a/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl b/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl index 58bc4bdd..34722ccc 100644 --- a/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl +++ b/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl @@ -54,9 +54,12 @@ Texture2D _MainTex; SamplerState sampler_MainTex; Texture2D _History; SamplerState sampler_History; Texture2D _CameraDepthTexture; SamplerState sampler_CameraDepthTexture; -Texture2D _CameraMotionVectorsTexture; SamplerState sampler_CameraMotionVectorsTexture; Texture2D _CameraReflectionsTexture; SamplerState sampler_CameraReflectionsTexture; +#ifdef _UseMotionVectors +Texture2D _CameraMotionVectorsTexture; SamplerState sampler_CameraMotionVectorsTexture; +#endif + Texture2D _CameraGBufferTexture0; // albedo = g[0].rgb Texture2D _CameraGBufferTexture1; // roughness = g[1].a Texture2D _CameraGBufferTexture2; SamplerState sampler_CameraGBufferTexture2; // normal.xyz 2. * g[2].rgb - 1. @@ -302,8 +305,12 @@ float4 FragResolve(VaryingsDefault i) : SV_Target float4 FragReproject(VaryingsDefault i) : SV_Target { - float2 motion = _CameraMotionVectorsTexture.SampleLevel(sampler_CameraMotionVectorsTexture, i.texcoordStereo, 0).xy; - float2 uv = i.texcoord - motion; +#ifdef _UseMotionVectors + float2 motion = _CameraMotionVectorsTexture.SampleLevel(sampler_CameraMotionVectorsTexture, i.texcoordStereo, 0).xy; + float2 uv = i.texcoord - motion; +#else + float2 uv = i.texcoord; +#endif const float2 k = SSR_COLOR_NEIGHBORHOOD_SAMPLE_SPREAD * _MainTex_TexelSize.xy; @@ -339,7 +346,11 @@ float4 FragReproject(VaryingsDefault i) : SV_Target float4 history = _History.SampleLevel(sampler_History, UnityStereoTransformScreenSpaceTex(uv), 0); history = clamp(history, minimum, maximum); +#ifdef _UseMotionVectors color.a = saturate(smoothstep(0.002 * _MainTex_TexelSize.z, 0.0035 * _MainTex_TexelSize.z, length(motion))); +#else + color.a = saturate(smoothstep(0.002 * _MainTex_TexelSize.z, 0.0035 * _MainTex_TexelSize.z, 0)); +#endif float weight = clamp(lerp(SSR_FINAL_BLEND_STATIC_FACTOR, SSR_FINAL_BLEND_DYNAMIC_FACTOR, history.a * 100.0), SSR_FINAL_BLEND_DYNAMIC_FACTOR, SSR_FINAL_BLEND_STATIC_FACTOR); diff --git a/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader b/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader index 9e1d0d28..f23a778e 100644 --- a/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader +++ b/PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader @@ -8,6 +8,8 @@ Shader "Hidden/PostProcessing/ScreenSpaceReflections" #include "UnityCG.cginc" #pragma target 5.0 + #pragma multi_compile _ _UseMotionVectors + // Ported from StdLib, we can't include it as it'll conflict with internal Unity includes struct AttributesDefault {