Skip to content

Commit

Permalink
Add motion vector to FPS Foreground custom pass
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Dec 18, 2023
1 parent bca866e commit 745c448
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 31 deletions.
58 changes: 43 additions & 15 deletions Assets/CustomPasses/FPS Foreground/FPSForeground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FPSForeground : CustomPass

Material depthClearMaterial;

RTHandle trueDepthBuffer;
// RTHandle trueDepthBuffer;

protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
=> cullingParameters.cullingMask |= (uint)foregroundMask.value;
Expand All @@ -44,11 +44,20 @@ protected override void Setup(ScriptableRenderContext renderContext, CommandBuff

depthClearMaterial = new Material(Shader.Find("Hidden/Renderers/ForegroundDepthClear"));

var trueDethBuffer = new RenderTargetIdentifier(BuiltinRenderTextureType.Depth);
// var trueDethBuffer = new RenderTargetIdentifier(BuiltinRenderTextureType.Depth);

trueDepthBuffer = RTHandles.Alloc(trueDethBuffer);
// trueDepthBuffer = RTHandles.Alloc(trueDethBuffer);

foregroundCamera = cam.GetComponent<Camera>();

// var outline = new Outline
// {
// outlineColor = Color.red,
// outlineLayer = LayerMask.GetMask("Outline"),
// threshold = 0,
// };
// CustomPassVolume.RegisterGlobalCustomPass(CustomPassInjectionPoint.BeforePostProcess, outline);

}

protected override void Execute(CustomPassContext ctx)
Expand All @@ -72,28 +81,48 @@ protected override void Execute(CustomPassContext ctx)
depthState = new DepthState(true, CompareFunction.LessEqual),
};

// TODO: Nuke the depth in the after depth and normal injection point
// Override depth to 0 (avoid artifacts with screen-space effects)
CoreUtils.SetKeyword(ctx.cmd, "WRITE_NORMAL_BUFFER", true);
ctx.cmd.SetRenderTarget(ctx.cameraNormalBuffer, trueDepthBuffer, 0, CubemapFace.Unknown, 0); // TODO: make it work in VR
RenderFromCameraDepthPass(ctx, foregroundCamera, null, null, ClearFlag.None, foregroundMask, overrideMaterial: depthClearMaterial, overrideMaterialIndex: 0);
CoreUtils.SetKeyword(ctx.cmd, "WRITE_NORMAL_BUFFER", false);

// Render the object color or normal + depth depending on the injection point
if (injectionPoint == CustomPassInjectionPoint.AfterOpaqueDepthAndNormal)
{
// Clean the custom depth buffer
CoreUtils.SetRenderTarget(ctx.cmd, ctx.customDepthBuffer.Value, ClearFlag.None);
CoreUtils.ClearRenderTarget(ctx.cmd, ClearFlag.Depth, Color.black);

CoreUtils.SetKeyword(ctx.cmd, "WRITE_NORMAL_BUFFER", true);
RenderFromCameraDepthPass(ctx, foregroundCamera, ctx.cameraNormalBuffer, ctx.cameraDepthBuffer, ClearFlag.None, foregroundMask, overrideRenderState: depthTestOverride);
{
// Render the object normals with the cleared depth buffer.
RenderPassFromCamera(ctx, foregroundCamera, ctx.cameraNormalBuffer, ctx.cameraDepthBuffer, ClearFlag.None, foregroundMask, overrideRenderState: depthTestOverride, renderDepth: true);

// // Override depth to 0 so that screen space effects don't apply to the foreground objects.
// ctx.cmd.SetRenderTarget(ctx.cameraNormalBuffer, trueDepthBuffer, 0, CubemapFace.Unknown, 0); // TODO: make it work in VR
// RenderPassFromCamera(ctx, foregroundCamera, null, ctx.cameraDepthBuffer, ClearFlag.None, foregroundMask, overrideMaterial: depthClearMaterial, overrideMaterialIndex: 0, renderDepth: true);
}
CoreUtils.SetKeyword(ctx.cmd, "WRITE_NORMAL_BUFFER", false);
}
else
{
var depthTestOverride2 = new RenderStateBlock(RenderStateMask.Depth)
{
depthState = new DepthState(false, CompareFunction.Equal),
};

// Before rendering the transparent objects, we render the foreground objects into the color buffer.
CustomPassUtils.RenderFromCamera(ctx, foregroundCamera, ctx.cameraColorBuffer, ctx.cameraDepthBuffer, ClearFlag.None, foregroundMask, overrideRenderState: depthTestOverride);


// Finally, clear the motion vectors to avoid ghosting.
RenderPassFromCamera(ctx, foregroundCamera, ctx.cameraMotionVectorsBuffer, null, ClearFlag.None, foregroundMask, overrideMaterial: depthClearMaterial, overrideMaterialIndex: 0, renderDepth: true);
}
}

ProfilingSampler s_RenderFromCameraSampler = new ProfilingSampler("Render From Camera");

public void RenderFromCameraDepthPass(in CustomPassContext ctx, Camera view, RTHandle targetColor, RTHandle targetDepth, ClearFlag clearFlag, LayerMask layerMask, CustomPass.RenderQueueType renderQueueFilter = CustomPass.RenderQueueType.All, Material overrideMaterial = null, int overrideMaterialIndex = 0, RenderStateBlock overrideRenderState = default(RenderStateBlock))
public void RenderPassFromCamera(in CustomPassContext ctx, Camera view, RTHandle targetColor, RTHandle targetDepth, ClearFlag clearFlag, LayerMask layerMask, RenderQueueType renderQueueFilter = RenderQueueType.All, Material overrideMaterial = null, int overrideMaterialIndex = 0, RenderStateBlock overrideRenderState = default(RenderStateBlock), bool renderDepth = true)
{
ShaderTagId[] depthTags = { HDShaderPassNames.s_DepthForwardOnlyName, HDShaderPassNames.s_DepthOnlyName };
ShaderTagId[] motionTags = { HDShaderPassNames.s_MotionVectorsName, new ShaderTagId("FirstPass") };

if (targetColor != null && targetDepth != null)
CoreUtils.SetRenderTarget(ctx.cmd, targetColor, targetDepth, clearFlag);
else if (targetColor != null)
Expand All @@ -105,16 +134,15 @@ protected override void Execute(CustomPassContext ctx)
{
using (new CustomPassUtils.OverrideCameraRendering(ctx, view))
{
// using (new ProfilingScope(ctx.cmd, renderFromCameraSampler))
CustomPassUtils.DrawRenderers(ctx, depthTags, layerMask, renderQueueFilter, overrideMaterial, overrideMaterialIndex, overrideRenderState);
using (new ProfilingScope(ctx.cmd, s_RenderFromCameraSampler))
CustomPassUtils.DrawRenderers(ctx, renderDepth ? depthTags : motionTags, layerMask, renderQueueFilter, overrideMaterial, overrideMaterialIndex, overrideRenderState);
}
}
}

protected override void Cleanup()
{
trueDepthBuffer.Release();
// trueDepthBuffer.Release();
CoreUtils.Destroy(depthClearMaterial);
// CoreUtils.Destroy(foregroundCamera);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Shader "Hidden/Renderers/ForegroundDepthClear"
Blend Off
ZWrite On
ZTest Always
ColorMask 0

Cull Back

Expand Down Expand Up @@ -56,7 +55,7 @@ Shader "Hidden/Renderers/ForegroundDepthClear"

float4 Frag (float4 vertex : SV_POSITION, out float depth : SV_Depth) : SV_Target
{
depth = 0;
depth = 0.1; // Use 1 which is the nearest depth possible, this forces the build light list to process this tile and make the lighting work on the foreground objects.
return 0;
}

Expand Down
40 changes: 26 additions & 14 deletions Assets/Scenes/FPS Foreground.unity
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ LightmapSettings:
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
Expand Down Expand Up @@ -183,13 +180,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 336336602}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &426100123
GameObject:
Expand Down Expand Up @@ -277,6 +274,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 426100123}
serializedVersion: 2
m_LocalRotation: {x: 0.07779779, y: 0.91151613, z: -0.20317058, w: 0.34900957}
m_LocalPosition: {x: -2.592, y: 3.992, z: 49.72}
m_LocalScale: {x: 1, y: 1, z: 1}
Expand All @@ -285,7 +283,6 @@ Transform:
- {fileID: 606725472}
- {fileID: 1994038400}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &426100127
MonoBehaviour:
Expand All @@ -312,6 +309,8 @@ MonoBehaviour:
stopNaNs: 0
taaSharpenStrength: 0.5
TAAQuality: 1
taaSharpenMode: 0
taaRingingReduction: 0
taaHistorySharpening: 0.35
taaAntiFlicker: 0.5
taaMotionVectorRejection: 0
Expand Down Expand Up @@ -362,11 +361,12 @@ MonoBehaviour:
sssQualityMode: 0
sssQualityLevel: 0
sssCustomSampleBudget: 20
sssCustomDownsampleSteps: 0
msaaMode: 4
materialQuality: 0
renderingPathCustomFrameSettingsOverrideMask:
mask:
data1: 17
data1: 1
data2: 0
defaultFrameSettings: 0
m_Version: 9
Expand Down Expand Up @@ -456,14 +456,14 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 606725471}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.022, y: -0.106, z: 0.306}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1678336237}
m_Father: {fileID: 426100126}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &625592531
GameObject:
Expand All @@ -490,13 +490,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 625592531}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0.70710576, w: 0.70710784}
m_LocalPosition: {x: -0.28734353, y: 0.60754037, z: 0}
m_LocalScale: {x: 0.78228724, y: 0.29637712, z: 0.78228724}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1678336237}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: -180}
--- !u!23 &625592533
MeshRenderer:
Expand Down Expand Up @@ -589,13 +589,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 967290021}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -3.9119453, y: 10.3260975, z: 2.7467709}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1678336234
GameObject:
Expand Down Expand Up @@ -672,14 +672,14 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1678336234}
serializedVersion: 2
m_LocalRotation: {x: 0.50000006, y: -0.5000001, z: -0.5000002, w: 0.49999976}
m_LocalPosition: {x: 0.11, y: -0.005, z: 0.373}
m_LocalScale: {x: 0.0751529, y: 0.0751529, z: 0.0751529}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 625592532}
m_Father: {fileID: 606725472}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: -90, z: -90}
--- !u!1 &1755695389
GameObject:
Expand Down Expand Up @@ -717,7 +717,7 @@ MonoBehaviour:
customPasses:
- rid: 0
- rid: 7911419928889786368
injectionPoint: 1
injectionPoint: 4
m_TargetCamera: {fileID: 0}
useTargetCamera: 0
references:
Expand Down Expand Up @@ -756,13 +756,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1755695389}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0.5519966, y: 1.4000415, z: 43.721645}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1755695392
MonoBehaviour:
Expand Down Expand Up @@ -877,13 +877,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1994038398}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 426100126}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1994038401
MonoBehaviour:
Expand All @@ -910,6 +910,8 @@ MonoBehaviour:
stopNaNs: 0
taaSharpenStrength: 0.5
TAAQuality: 1
taaSharpenMode: 0
taaRingingReduction: 0
taaHistorySharpening: 0.35
taaAntiFlicker: 0.5
taaMotionVectorRejection: 0
Expand Down Expand Up @@ -960,6 +962,7 @@ MonoBehaviour:
sssQualityMode: 0
sssQualityLevel: 0
sssCustomSampleBudget: 20
sssCustomDownsampleSteps: 0
msaaMode: 1
materialQuality: 0
renderingPathCustomFrameSettingsOverrideMask:
Expand Down Expand Up @@ -1054,11 +1057,20 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2079892256}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 426100126}
- {fileID: 1755695391}
- {fileID: 2079892258}
- {fileID: 336336605}
- {fileID: 967290023}

0 comments on commit 745c448

Please sign in to comment.