Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix-0.1.1 #4

Open
wants to merge 19 commits into
base: master
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
14 changes: 13 additions & 1 deletion Runtime/MotionBlur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace kTools.Motion
[Serializable, VolumeComponentMenu("kMotion/Motion Blur")]
public sealed class MotionBlur: VolumeComponent, IPostProcessComponent
{
/// <summary>
/// Enable/Disable camera-based motion blur. Object-based motion blur is applied anyway.
/// </summary>
[Tooltip("Enable/Disable camera-based motion blur. Object-based motion blur is applied anyway.")]
public BoolParameter cameraBasedMB = new BoolParameter(true);

/// <summary>
/// The quality of the effect. Lower presets will result in better performance at the expense of visual quality.
/// </summary>
Expand All @@ -19,7 +25,13 @@ public sealed class MotionBlur: VolumeComponent, IPostProcessComponent
/// </summary>
[Tooltip("The strength of the motion blur filter. Acts as a multiplier for velocities.")]
public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);


/// <summary>
/// The minimum velocity for the motion blur filter to be applied.
/// </summary>
[Tooltip("The minimum velocity for the motion blur filter to be applied.")]
public ClampedFloatParameter threshold = new ClampedFloatParameter(0.01f, 0f, 1f);

/// <summary>
/// Is the component active?
/// </summary>
Expand Down
8 changes: 5 additions & 3 deletions Runtime/MotionBlurRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sealed class MotionBlurRenderPass : ScriptableRenderPass
internal MotionBlurRenderPass()
{
// Set data
renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}
#endregion

Expand All @@ -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

Expand All @@ -53,7 +54,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");

Expand Down
19 changes: 11 additions & 8 deletions Runtime/MotionRendererFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace kTools.Motion
sealed class MotionRendererFeature : ScriptableRendererFeature
{
#region Fields
public LayerMask m_LayerMask;
static MotionRendererFeature s_Instance;
readonly MotionVectorRenderPass m_MotionVectorRenderPass;
readonly MotionBlurRenderPass m_MotionBlurRenderPass;
static MotionVectorRenderPass m_MotionVectorRenderPass;
static MotionBlurRenderPass m_MotionBlurRenderPass;

Dictionary<Camera, MotionData> m_MotionDatas;
uint m_FrameCount;
Expand All @@ -23,22 +24,26 @@ internal MotionRendererFeature()
{
// Set data
s_Instance = this;
m_MotionVectorRenderPass = new MotionVectorRenderPass();
m_MotionBlurRenderPass = new MotionBlurRenderPass();
m_MotionDatas = new Dictionary<Camera, MotionData>();
}
#endregion

#region Initialization
public override void Create()
{
name = "Motion";
m_MotionVectorRenderPass = new MotionVectorRenderPass();
m_MotionBlurRenderPass = new MotionBlurRenderPass();
m_MotionDatas = new Dictionary<Camera, MotionData>();
}
#endregion

#region RenderPass
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
// Get motion blur settings
var stack = VolumeManager.instance.stack;
var motionBlur = stack.GetComponent<MotionBlur>();

// Get MotionData
var camera = renderingData.cameraData.camera;
MotionData motionData;
Expand All @@ -53,12 +58,10 @@ public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingD
UpdateMotionData(camera, motionData);

// Motion vector pass
m_MotionVectorRenderPass.Setup(motionData);
m_MotionVectorRenderPass.Setup(motionData, motionBlur, m_LayerMask.value);
renderer.EnqueuePass(m_MotionVectorRenderPass);

// Motion blur pass
var stack = VolumeManager.instance.stack;
var motionBlur = stack.GetComponent<MotionBlur>();
if (motionBlur.IsActive() && !renderingData.cameraData.isSceneViewCamera)
{
m_MotionBlurRenderPass.Setup(motionBlur);
Expand Down
23 changes: 16 additions & 7 deletions Runtime/MotionVectorRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ sealed class MotionVectorRenderPass : ScriptableRenderPass
Material m_CameraMaterial;
Material m_ObjectMaterial;
MotionData m_MotionData;
MotionBlur m_MotionBlur;
int m_layerMask;
#endregion

#region Constructors
Expand All @@ -35,24 +37,30 @@ internal MotionVectorRenderPass()
#endregion

#region State
internal void Setup(MotionData motionData)
internal void Setup(MotionData motionData, MotionBlur motionBlur, int layerMask)
{
// Set data
m_MotionData = motionData;
m_CameraMaterial = new Material(Shader.Find(kCameraShader));
m_ObjectMaterial = new Material(Shader.Find(kObjectShader));
m_MotionBlur = motionBlur;
m_layerMask = layerMask;
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)
{
// 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?
cmd.ClearRenderTarget(true, true, Color.black, 1.0f);
// Clear with 0.5 because we packed vectors from clip into NDC space
cmd.ClearRenderTarget(true, true, Color.gray, 1.0f);
}
#endregion

Expand Down Expand Up @@ -80,7 +88,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);
Expand Down Expand Up @@ -128,7 +137,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
Expand Down
3 changes: 1 addition & 2 deletions Shaders/CameraMotionVectors.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions Shaders/MotionBlur.shader
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TEXTURE2D(_MotionVectorTexture); SAMPLER(sampler_MotionVectorTexture);

float _Intensity;
float _Threshold;
float4 _MainTex_TexelSize;

// -------------------------------------
Expand Down Expand Up @@ -52,23 +53,31 @@

// -------------------------------------
// 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)
{
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);

half3 color = 0.0;
half4 color = 0.0;

UNITY_UNROLL
for (int i = 0; i < iterations; i++)
Expand All @@ -77,7 +86,7 @@
color += GatherSample(i, velocity, invSampleCount, uv, randomVal, 1.0);
}

return half4(color * invSampleCount, 1.0);
return color * invSampleCount;
}

ENDHLSL
Expand Down
5 changes: 2 additions & 3 deletions Shaders/ObjectMotionVectors.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down