Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

SSR now allows toggling motion vectors. #848

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };


/// <summary>
/// Enables the use of motion vectors to slightly improve visual quality.
/// </summary>
[Tooltip("Enables the use of motion vectors to slightly improve visual quality.")]
public BoolParameter useMotionVectors = new BoolParameter { value = false };

/// <inheritdoc />
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down