From 38eb30aab76e28bcb9b951f21e1d5672ba607ff1 Mon Sep 17 00:00:00 2001 From: Eshan Nikouei Date: Wed, 5 Feb 2020 14:35:20 +0330 Subject: [PATCH 01/12] Fix motion blur won't work with camera Post Processing flag is on --- Runtime/MotionBlurRenderPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/MotionBlurRenderPass.cs b/Runtime/MotionBlurRenderPass.cs index 8dfb0e2..8197ac4 100644 --- a/Runtime/MotionBlurRenderPass.cs +++ b/Runtime/MotionBlurRenderPass.cs @@ -24,7 +24,7 @@ sealed class MotionBlurRenderPass : ScriptableRenderPass internal MotionBlurRenderPass() { // Set data - renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing; + renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing; } #endregion From a716c96c950e4d5aec3fe6266df3ad11d3eada9f Mon Sep 17 00:00:00 2001 From: Eshan Nikouei Date: Wed, 5 Feb 2020 15:45:50 +0330 Subject: [PATCH 02/12] Fix wrong motion blur on objects that is attached to the camera When an object is attached to the camera or moves with it, it has no screen space movement. So it shouldn't get blurred. --- Runtime/MotionVectorRenderPass.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 0456709..33c2988 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -8,7 +8,7 @@ namespace kTools.Motion sealed class MotionVectorRenderPass : ScriptableRenderPass { #region Fields - const string kCameraShader = "Hidden/kMotion/CameraMotionVectors"; + //const string kCameraShader = "Hidden/kMotion/CameraMotionVectors"; const string kObjectShader = "Hidden/kMotion/ObjectMotionVectors"; const string kPreviousViewProjectionMatrix = "_PrevViewProjMatrix"; const string kMotionVectorTexture = "_MotionVectorTexture"; @@ -21,7 +21,7 @@ sealed class MotionVectorRenderPass : ScriptableRenderPass }; RenderTargetHandle m_MotionVectorHandle; - Material m_CameraMaterial; + //Material m_CameraMaterial; Material m_ObjectMaterial; MotionData m_MotionData; #endregion @@ -39,7 +39,7 @@ internal void Setup(MotionData motionData) { // Set data m_MotionData = motionData; - m_CameraMaterial = new Material(Shader.Find(kCameraShader)); + //m_CameraMaterial = new Material(Shader.Find(kCameraShader)); m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } @@ -80,7 +80,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth; // Drawing - DrawCameraMotionVectors(context, cmd, camera); + //DrawCameraMotionVectors(context, cmd, camera); DrawObjectMotionVectors(context, ref renderingData, cmd, camera); } ExecuteCommand(context, cmd); @@ -110,12 +110,12 @@ DrawingSettings GetDrawingSettings(ref RenderingData renderingData) return drawingSettings; } - void DrawCameraMotionVectors(ScriptableRenderContext context, CommandBuffer cmd, Camera camera) - { - // Draw fullscreen quad - cmd.DrawProcedural(Matrix4x4.identity, m_CameraMaterial, 0, MeshTopology.Triangles, 3, 1); - ExecuteCommand(context, cmd); - } + //void DrawCameraMotionVectors(ScriptableRenderContext context, CommandBuffer cmd, Camera camera) + //{ + // // Draw fullscreen quad + // cmd.DrawProcedural(Matrix4x4.identity, m_CameraMaterial, 0, MeshTopology.Triangles, 3, 1); + // ExecuteCommand(context, cmd); + //} void DrawObjectMotionVectors(ScriptableRenderContext context, ref RenderingData renderingData, CommandBuffer cmd, Camera camera) { From 00d4655f609673185c03ab512b3492ca86c37641 Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Tue, 9 Mar 2021 15:53:01 +0100 Subject: [PATCH 03/12] can not call GetInternalRenderer in constructor in unity 2020 --- Runtime/MotionRendererFeature.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Runtime/MotionRendererFeature.cs b/Runtime/MotionRendererFeature.cs index 942eb47..891c01d 100644 --- a/Runtime/MotionRendererFeature.cs +++ b/Runtime/MotionRendererFeature.cs @@ -9,8 +9,8 @@ sealed class MotionRendererFeature : ScriptableRendererFeature { #region Fields static MotionRendererFeature s_Instance; - readonly MotionVectorRenderPass m_MotionVectorRenderPass; - readonly MotionBlurRenderPass m_MotionBlurRenderPass; + static MotionVectorRenderPass m_MotionVectorRenderPass; + static MotionBlurRenderPass m_MotionBlurRenderPass; Dictionary m_MotionDatas; uint m_FrameCount; @@ -23,9 +23,6 @@ internal MotionRendererFeature() { // Set data s_Instance = this; - m_MotionVectorRenderPass = new MotionVectorRenderPass(); - m_MotionBlurRenderPass = new MotionBlurRenderPass(); - m_MotionDatas = new Dictionary(); } #endregion @@ -33,6 +30,9 @@ internal MotionRendererFeature() public override void Create() { name = "Motion"; + m_MotionVectorRenderPass = new MotionVectorRenderPass(); + m_MotionBlurRenderPass = new MotionBlurRenderPass(); + m_MotionDatas = new Dictionary(); } #endregion From efedac7a7eb6000e76dad45051ca7f71c3b3f87c Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Tue, 9 Mar 2021 15:53:31 +0100 Subject: [PATCH 04/12] add threshold value --- Runtime/MotionBlur.cs | 8 +++++++- Runtime/MotionBlurRenderPass.cs | 3 ++- Shaders/MotionBlur.shader | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Runtime/MotionBlur.cs b/Runtime/MotionBlur.cs index fb48a15..9e7516c 100644 --- a/Runtime/MotionBlur.cs +++ b/Runtime/MotionBlur.cs @@ -19,7 +19,13 @@ public sealed class MotionBlur: VolumeComponent, IPostProcessComponent /// [Tooltip("The strength of the motion blur filter. Acts as a multiplier for velocities.")] public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f); - + + /// + /// The minimum velocity for the motion blur filter to be applied. + /// + [Tooltip("The minimum velocity for the motion blur filter to be applied.")] + public ClampedFloatParameter threshold = new ClampedFloatParameter(0.01f, 0f, 1f); + /// /// Is the component active? /// diff --git a/Runtime/MotionBlurRenderPass.cs b/Runtime/MotionBlurRenderPass.cs index 8197ac4..6c11d98 100644 --- a/Runtime/MotionBlurRenderPass.cs +++ b/Runtime/MotionBlurRenderPass.cs @@ -53,7 +53,8 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData { // Set Material properties from VolumeComponent m_Material.SetFloat("_Intensity", m_MotionBlur.intensity.value); - + m_Material.SetFloat("_Threshold", m_MotionBlur.threshold.value); + // TODO: Why doesnt RenderTargetHandle.CameraTarget work? var colorTextureIdentifier = new RenderTargetIdentifier("_CameraColorTexture"); diff --git a/Shaders/MotionBlur.shader b/Shaders/MotionBlur.shader index b8ee1b2..d2fb96a 100644 --- a/Shaders/MotionBlur.shader +++ b/Shaders/MotionBlur.shader @@ -20,6 +20,7 @@ TEXTURE2D(_MotionVectorTexture); SAMPLER(sampler_MotionVectorTexture); float _Intensity; + float _Threshold; float4 _MainTex_TexelSize; // ------------------------------------- @@ -65,6 +66,11 @@ float2 uv = UnityStereoTransformScreenSpaceTex(input.uv.xy); float2 velocity = SAMPLE_TEXTURE2D(_MotionVectorTexture, sampler_MotionVectorTexture, uv).rg * _Intensity; + if(length(velocity) < _Threshold) + { + return SAMPLE_TEXTURE2D_X(_MainTex, sampler_PointClamp, uv); + } + velocity *= _Intensity; float randomVal = InterleavedGradientNoise(uv * _MainTex_TexelSize.zw, 0); float invSampleCount = rcp(iterations * 2.0); From 0721a145c877a21e8a4e8c8b0dd7cbc8ff3bdc27 Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Tue, 9 Mar 2021 15:53:54 +0100 Subject: [PATCH 05/12] keep alpha --- Shaders/MotionBlur.shader | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Shaders/MotionBlur.shader b/Shaders/MotionBlur.shader index d2fb96a..0ada6d6 100644 --- a/Shaders/MotionBlur.shader +++ b/Shaders/MotionBlur.shader @@ -53,11 +53,11 @@ // ------------------------------------- // Fragment - float3 GatherSample(float sampleNumber, float2 velocity, float invSampleCount, float2 centerUV, float randomVal, float velocitySign) + float4 GatherSample(float sampleNumber, float2 velocity, float invSampleCount, float2 centerUV, float randomVal, float velocitySign) { float offsetLength = (sampleNumber + 0.5) + (velocitySign * (randomVal - 0.5)); float2 sampleUV = centerUV + (offsetLength * invSampleCount) * velocity * velocitySign; - return SAMPLE_TEXTURE2D_X(_MainTex, sampler_PointClamp, sampleUV).xyz; + return SAMPLE_TEXTURE2D_X(_MainTex, sampler_PointClamp, sampleUV); } half4 DoMotionBlur(VaryingsMB input, int iterations) @@ -74,7 +74,7 @@ float randomVal = InterleavedGradientNoise(uv * _MainTex_TexelSize.zw, 0); float invSampleCount = rcp(iterations * 2.0); - half3 color = 0.0; + half4 color = 0.0; UNITY_UNROLL for (int i = 0; i < iterations; i++) @@ -83,7 +83,7 @@ color += GatherSample(i, velocity, invSampleCount, uv, randomVal, 1.0); } - return half4(color * invSampleCount, 1.0); + return color * invSampleCount; } ENDHLSL From d8821095b5d70c1611888999f076b54005f2cf73 Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Tue, 9 Mar 2021 19:42:44 +0100 Subject: [PATCH 06/12] correctly pack and unpack ndc space --- Shaders/CameraMotionVectors.shader | 3 +-- Shaders/MotionBlur.shader | 5 ++++- Shaders/ObjectMotionVectors.shader | 5 ++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Shaders/CameraMotionVectors.shader b/Shaders/CameraMotionVectors.shader index 5e27f4a..4944d0a 100644 --- a/Shaders/CameraMotionVectors.shader +++ b/Shaders/CameraMotionVectors.shader @@ -69,8 +69,7 @@ // Convert velocity from Clip space (-1..1) to NDC 0..1 space // Note it doesn't mean we don't have negative value, we store negative or positive offset in NDC space. - // Note: ((positionVP * 0.5 + 0.5) - (previousPositionVP * 0.5 + 0.5)) = (velocity * 0.5) - return half4(velocity.xy * 0.5, 0, 0); + return half4(velocity.xy * 0.5 + 0.5, 0, 0); } ENDHLSL diff --git a/Shaders/MotionBlur.shader b/Shaders/MotionBlur.shader index 0ada6d6..eec76c4 100644 --- a/Shaders/MotionBlur.shader +++ b/Shaders/MotionBlur.shader @@ -65,12 +65,15 @@ UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); float2 uv = UnityStereoTransformScreenSpaceTex(input.uv.xy); - float2 velocity = SAMPLE_TEXTURE2D(_MotionVectorTexture, sampler_MotionVectorTexture, uv).rg * _Intensity; + float2 velocity = (SAMPLE_TEXTURE2D(_MotionVectorTexture, sampler_MotionVectorTexture, uv).rg * 2 - 1); + // return half4(abs(velocity), 0, 1); + if(length(velocity) < _Threshold) { return SAMPLE_TEXTURE2D_X(_MainTex, sampler_PointClamp, uv); } velocity *= _Intensity; + float randomVal = InterleavedGradientNoise(uv * _MainTex_TexelSize.zw, 0); float invSampleCount = rcp(iterations * 2.0); diff --git a/Shaders/ObjectMotionVectors.shader b/Shaders/ObjectMotionVectors.shader index 6b9220c..ac1b175 100644 --- a/Shaders/ObjectMotionVectors.shader +++ b/Shaders/ObjectMotionVectors.shader @@ -73,7 +73,7 @@ #else output.positionCS.z += unity_MotionVectorsParams.z * output.positionCS.w; #endif - + output.positionVP = mul(UNITY_MATRIX_VP, mul(UNITY_MATRIX_M, input.position)); output.previousPositionVP = mul(_PrevViewProjMatrix, mul(unity_MatrixPreviousM, unity_MotionVectorsParams.x == 1 ? float4(input.positionOld, 1) : input.position)); return output; @@ -105,8 +105,7 @@ // Convert from Clip space (-1..1) to NDC 0..1 space. // Note it doesn't mean we don't have negative value, we store negative or positive offset in NDC space. - // Note: ((positionCS * 0.5 + 0.5) - (previousPositionCS * 0.5 + 0.5)) = (velocity * 0.5) - return float4(velocity.xy * 0.5, 0, 0); + return float4(velocity.xy * 0.5 + 0.5, 0, 0); } ENDHLSL } From 4491c4f8660205139092efbb5127b379d5f7f216 Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Tue, 9 Mar 2021 19:42:50 +0100 Subject: [PATCH 07/12] clear with 0.5 --- Runtime/MotionVectorRenderPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 33c2988..674f1a4 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -52,7 +52,7 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera cmd.SetRenderTarget(m_MotionVectorHandle.Identifier(), m_MotionVectorHandle.Identifier()); // TODO: Why do I have to clear here? - cmd.ClearRenderTarget(true, true, Color.black, 1.0f); + cmd.ClearRenderTarget(true, true, Color.gray, 1.0f); } #endregion From e6cfbb43aab4c6baba15c3c521a2cc48bed8e6b5 Mon Sep 17 00:00:00 2001 From: Julian Oberbeck Date: Sun, 14 Mar 2021 18:30:31 +0100 Subject: [PATCH 08/12] enabled camera motion blur --- Runtime/MotionVectorRenderPass.cs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 674f1a4..0490ec5 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -8,7 +8,7 @@ namespace kTools.Motion sealed class MotionVectorRenderPass : ScriptableRenderPass { #region Fields - //const string kCameraShader = "Hidden/kMotion/CameraMotionVectors"; + const string kCameraShader = "Hidden/kMotion/CameraMotionVectors"; const string kObjectShader = "Hidden/kMotion/ObjectMotionVectors"; const string kPreviousViewProjectionMatrix = "_PrevViewProjMatrix"; const string kMotionVectorTexture = "_MotionVectorTexture"; @@ -21,7 +21,7 @@ sealed class MotionVectorRenderPass : ScriptableRenderPass }; RenderTargetHandle m_MotionVectorHandle; - //Material m_CameraMaterial; + Material m_CameraMaterial; Material m_ObjectMaterial; MotionData m_MotionData; #endregion @@ -39,7 +39,7 @@ internal void Setup(MotionData motionData) { // Set data m_MotionData = motionData; - //m_CameraMaterial = new Material(Shader.Find(kCameraShader)); + m_CameraMaterial = new Material(Shader.Find(kCameraShader)); m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } @@ -47,11 +47,13 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera { // Configure Render Target m_MotionVectorHandle.Init(kMotionVectorTexture); + cameraTextureDescriptor.colorFormat = RenderTextureFormat.RG16; + // Debug.Log(cameraTextureDescriptor.colorFormat); cmd.GetTemporaryRT(m_MotionVectorHandle.id, cameraTextureDescriptor, FilterMode.Point); ConfigureTarget(m_MotionVectorHandle.Identifier(), m_MotionVectorHandle.Identifier()); cmd.SetRenderTarget(m_MotionVectorHandle.Identifier(), m_MotionVectorHandle.Identifier()); - // TODO: Why do I have to clear here? + // Clear with 0.5 because we packed vectors from clip into NDC space cmd.ClearRenderTarget(true, true, Color.gray, 1.0f); } #endregion @@ -80,7 +82,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth; // Drawing - //DrawCameraMotionVectors(context, cmd, camera); + DrawCameraMotionVectors(context, cmd, camera); DrawObjectMotionVectors(context, ref renderingData, cmd, camera); } ExecuteCommand(context, cmd); @@ -110,12 +112,12 @@ DrawingSettings GetDrawingSettings(ref RenderingData renderingData) return drawingSettings; } - //void DrawCameraMotionVectors(ScriptableRenderContext context, CommandBuffer cmd, Camera camera) - //{ - // // Draw fullscreen quad - // cmd.DrawProcedural(Matrix4x4.identity, m_CameraMaterial, 0, MeshTopology.Triangles, 3, 1); - // ExecuteCommand(context, cmd); - //} + void DrawCameraMotionVectors(ScriptableRenderContext context, CommandBuffer cmd, Camera camera) + { + // Draw fullscreen quad + cmd.DrawProcedural(Matrix4x4.identity, m_CameraMaterial, 0, MeshTopology.Triangles, 3, 1); + ExecuteCommand(context, cmd); + } void DrawObjectMotionVectors(ScriptableRenderContext context, ref RenderingData renderingData, CommandBuffer cmd, Camera camera) { From 1e4da1a879b71c0d55ca63efc56c6bee20911b88 Mon Sep 17 00:00:00 2001 From: Ehsan Nikouei Date: Sat, 17 Apr 2021 16:14:00 +0430 Subject: [PATCH 09/12] Optional camera-based motion blur Camera-based motion blur option is accessible from post-processing profile settings. --- Runtime/MotionBlur.cs | 6 ++++++ Runtime/MotionRendererFeature.cs | 8 +++++--- Runtime/MotionVectorRenderPass.cs | 7 +++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Runtime/MotionBlur.cs b/Runtime/MotionBlur.cs index 9e7516c..b147d5a 100644 --- a/Runtime/MotionBlur.cs +++ b/Runtime/MotionBlur.cs @@ -8,6 +8,12 @@ namespace kTools.Motion [Serializable, VolumeComponentMenu("kMotion/Motion Blur")] public sealed class MotionBlur: VolumeComponent, IPostProcessComponent { + /// + /// Enable/Disable camera-based motion blur. Object-based motion blur is applied anyway. + /// + [Tooltip("Enable/Disable camera-based motion blur. Object-based motion blur is applied anyway.")] + public BoolParameter cameraBasedMB = new BoolParameter(true); + /// /// The quality of the effect. Lower presets will result in better performance at the expense of visual quality. /// diff --git a/Runtime/MotionRendererFeature.cs b/Runtime/MotionRendererFeature.cs index 891c01d..7af8c52 100644 --- a/Runtime/MotionRendererFeature.cs +++ b/Runtime/MotionRendererFeature.cs @@ -39,6 +39,10 @@ public override void Create() #region RenderPass public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { + // Get motion blur settings + var stack = VolumeManager.instance.stack; + var motionBlur = stack.GetComponent(); + // Get MotionData var camera = renderingData.cameraData.camera; MotionData motionData; @@ -53,12 +57,10 @@ public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingD UpdateMotionData(camera, motionData); // Motion vector pass - m_MotionVectorRenderPass.Setup(motionData); + m_MotionVectorRenderPass.Setup(motionData, motionBlur); renderer.EnqueuePass(m_MotionVectorRenderPass); // Motion blur pass - var stack = VolumeManager.instance.stack; - var motionBlur = stack.GetComponent(); if (motionBlur.IsActive() && !renderingData.cameraData.isSceneViewCamera) { m_MotionBlurRenderPass.Setup(motionBlur); diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 0490ec5..480389b 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -24,6 +24,7 @@ sealed class MotionVectorRenderPass : ScriptableRenderPass Material m_CameraMaterial; Material m_ObjectMaterial; MotionData m_MotionData; + private MotionBlur m_MotionBlur; #endregion #region Constructors @@ -35,10 +36,11 @@ internal MotionVectorRenderPass() #endregion #region State - internal void Setup(MotionData motionData) + internal void Setup(MotionData motionData, MotionBlur motionBlur) { // Set data m_MotionData = motionData; + m_MotionBlur = motionBlur; m_CameraMaterial = new Material(Shader.Find(kCameraShader)); m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } @@ -82,7 +84,8 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth; // Drawing - DrawCameraMotionVectors(context, cmd, camera); + if(m_MotionBlur.cameraBasedMB.value) + DrawCameraMotionVectors(context, cmd, camera); DrawObjectMotionVectors(context, ref renderingData, cmd, camera); } ExecuteCommand(context, cmd); From b83f4381a5e10a95edc46ea661ae82547a7898c7 Mon Sep 17 00:00:00 2001 From: Ehsan Nikouei Date: Sat, 17 Apr 2021 16:43:51 +0430 Subject: [PATCH 10/12] Render feature layer mask option The layer mask option is accessible from render feature => Motion feature => Layer Mask --- Runtime/MotionRendererFeature.cs | 3 ++- Runtime/MotionVectorRenderPass.cs | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Runtime/MotionRendererFeature.cs b/Runtime/MotionRendererFeature.cs index 7af8c52..b999485 100644 --- a/Runtime/MotionRendererFeature.cs +++ b/Runtime/MotionRendererFeature.cs @@ -8,6 +8,7 @@ namespace kTools.Motion sealed class MotionRendererFeature : ScriptableRendererFeature { #region Fields + public LayerMask m_LayerMask; static MotionRendererFeature s_Instance; static MotionVectorRenderPass m_MotionVectorRenderPass; static MotionBlurRenderPass m_MotionBlurRenderPass; @@ -57,7 +58,7 @@ public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingD UpdateMotionData(camera, motionData); // Motion vector pass - m_MotionVectorRenderPass.Setup(motionData, motionBlur); + m_MotionVectorRenderPass.Setup(motionData, motionBlur, m_LayerMask.value); renderer.EnqueuePass(m_MotionVectorRenderPass); // Motion blur pass diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 480389b..9629ff0 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -24,7 +24,8 @@ sealed class MotionVectorRenderPass : ScriptableRenderPass Material m_CameraMaterial; Material m_ObjectMaterial; MotionData m_MotionData; - private MotionBlur m_MotionBlur; + MotionBlur m_MotionBlur; + int m_layerMask; #endregion #region Constructors @@ -36,11 +37,12 @@ internal MotionVectorRenderPass() #endregion #region State - internal void Setup(MotionData motionData, MotionBlur motionBlur) + internal void Setup(MotionData motionData, MotionBlur motionBlur, int layerMask) { // Set data m_MotionData = motionData; m_MotionBlur = motionBlur; + m_layerMask = layerMask; m_CameraMaterial = new Material(Shader.Find(kCameraShader)); m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } @@ -133,7 +135,7 @@ void DrawObjectMotionVectors(ScriptableRenderContext context, ref RenderingData var cullingResults = context.Cull(ref cullingParameters); var drawingSettings = GetDrawingSettings(ref renderingData); - var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, camera.cullingMask); + var filteringSettings = new FilteringSettings(RenderQueueRange.opaque, camera.cullingMask & m_layerMask); var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing); // Draw Renderers From 7ae9ca8cfd88f2f95ee1825fb97bcf5f9aacd29a Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Jul 2021 23:38:20 -0700 Subject: [PATCH 11/12] Fixed material memory leak --- Runtime/MotionBlurRenderPass.cs | 3 ++- Runtime/MotionVectorRenderPass.cs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Runtime/MotionBlurRenderPass.cs b/Runtime/MotionBlurRenderPass.cs index 6c11d98..9571829 100644 --- a/Runtime/MotionBlurRenderPass.cs +++ b/Runtime/MotionBlurRenderPass.cs @@ -33,7 +33,8 @@ internal void Setup(MotionBlur motionBlur) { // Set data m_MotionBlur = motionBlur; - m_Material = new Material(Shader.Find(kMotionBlurShader)); + if(!m_Material) + m_Material = new Material(Shader.Find(kMotionBlurShader)); } #endregion diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index 9629ff0..cabc5e5 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -43,8 +43,10 @@ internal void Setup(MotionData motionData, MotionBlur motionBlur, int layerMask) m_MotionData = motionData; m_MotionBlur = motionBlur; m_layerMask = layerMask; - m_CameraMaterial = new Material(Shader.Find(kCameraShader)); - m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); + if(!m_CameraMaterial) + m_CameraMaterial = new Material(Shader.Find(kCameraShader)); + if(!m_ObjectMaterial) + m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) From ad28673fd31f726c2e43d5de922928acebde6ed4 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Jul 2021 23:45:41 -0700 Subject: [PATCH 12/12] Fix formatting --- Runtime/MotionBlurRenderPass.cs | 4 ++-- Runtime/MotionVectorRenderPass.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Runtime/MotionBlurRenderPass.cs b/Runtime/MotionBlurRenderPass.cs index 9571829..e4cd462 100644 --- a/Runtime/MotionBlurRenderPass.cs +++ b/Runtime/MotionBlurRenderPass.cs @@ -33,8 +33,8 @@ internal void Setup(MotionBlur motionBlur) { // Set data m_MotionBlur = motionBlur; - if(!m_Material) - m_Material = new Material(Shader.Find(kMotionBlurShader)); + if(!m_Material) + m_Material = new Material(Shader.Find(kMotionBlurShader)); } #endregion diff --git a/Runtime/MotionVectorRenderPass.cs b/Runtime/MotionVectorRenderPass.cs index cabc5e5..96108be 100644 --- a/Runtime/MotionVectorRenderPass.cs +++ b/Runtime/MotionVectorRenderPass.cs @@ -43,10 +43,10 @@ internal void Setup(MotionData motionData, MotionBlur motionBlur, int layerMask) m_MotionData = motionData; m_MotionBlur = motionBlur; m_layerMask = layerMask; - if(!m_CameraMaterial) + if(!m_CameraMaterial) m_CameraMaterial = new Material(Shader.Find(kCameraShader)); if(!m_ObjectMaterial) - m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); + m_ObjectMaterial = new Material(Shader.Find(kObjectShader)); } public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)