Skip to content

Commit

Permalink
Fixed Camera jittering in some Unity versions when using TAA
Browse files Browse the repository at this point in the history
JoachimCoenen committed Oct 29, 2017
1 parent 68221eb commit eb7903f
Showing 13 changed files with 191 additions and 29 deletions.
14 changes: 11 additions & 3 deletions Cat/PostProcessing/CatAA/CatAA.cs
Original file line number Diff line number Diff line change
@@ -137,18 +137,26 @@ override protected void UpdateCameraMatricesPerFrame(Camera camera, VectorInt2 c
TMSAACounter = (TMSAACounter + 1) % 4 ;
}

var newP = jitterVectors[TMSAACounter] * Settings.jitterStrength;
var newP = jitterVectors[TMSAACounter];

if (settings.jitterMatrix == JitterMatrixType.HaltonSequence) {
newP = newP - new Vector2(0.5f, 0.5f);
}

newP *= Settings.jitterStrength;


newP.x /= (float)cameraSize.x;
newP.y /= (float)cameraSize.y;

camera.nonJitteredProjectionMatrix = camera.projectionMatrix;
if (camera.orthographic) {
camera.projectionMatrix = GetOrthographicProjectionMatrix(newP, camera);
} else {
camera.projectionMatrix = GetPerspectiveProjectionMatrix(newP, camera);
}

Shader.SetGlobalVector(PropertyIDs.TAAJitterVelocity_v, isSceneView ? Vector2.zero : newP);

}

override protected void UpdateMaterialPerFrame(Material material, Camera camera, VectorInt2 cameraSize) {
@@ -174,7 +182,7 @@ private void OnPostRender() {
//[ImageEffectTransformsToLDR]
internal override void RenderImage(RenderTexture source, RenderTexture destination) {
var isSceneView = postProcessingManager.isSceneView;
if (isSceneView && disableTAAInSceneView) {
if (false || isSceneView && disableTAAInSceneView) {
Blit(source, destination);
return;
}
24 changes: 22 additions & 2 deletions Cat/PostProcessing/CatAA/Resources/CatAA.shader
Original file line number Diff line number Diff line change
@@ -200,11 +200,31 @@ Shader "Hidden/CatAA" {

if (0) {
float3 result = 0;
float2 pureVelocity = Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy;
float2 jitterVelocity = _TAAJitterVelocity*2;

velocity = !_IsVelocityPredictionEnabled ? 0 : Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy;
result.xy = abs(velocity.xy) * 1.0*pow(10, 2.2);
float2 divisor = max(abs(pureVelocity), abs(pureVelocity));
result.rgb = float3(divisor == 0 ? 0 : abs(pureVelocity - jitterVelocity) / divisor, any(divisor == 00));
result.rg /= (1 + result.rg);

//result.rg = abs(pureVelocity) * 100000;
return float4(result, 1);
}

if (0) {
float3 result = 0;
//velocity = !_IsVelocityPredictionEnabled ? 0 : Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy;
velocity = Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy;
velocity -= _TAAJitterVelocity * 1.5;
//result.xy = abs(velocity.xy-0) * 100;//1.0*pow(10, 2.3);

result = pow(saturate(result), 2.2);

float2 uvPrev = uv - velocity.xy;
float4 history = tex2D(historyTex, uvPrev);
float confidence = any(history)*0.98750011;
float4 mainTex = tex2D(currentTex, uv);
result = lerp(mainTex, history, confidence).rgb;
return float4(result, 1);
}

16 changes: 10 additions & 6 deletions Cat/PostProcessing/CatAmbientOcclusion/CatAO.cs
Original file line number Diff line number Diff line change
@@ -51,10 +51,12 @@ public Settings settings {
OnValidate();
}
}
private Settings lastSettings;

private CameraEvent GetAppropriateCameraEvent(bool isDebugModeOn) {
return isDebugModeOn ? CameraEvent.BeforeImageEffectsOpaque : CameraEvent.BeforeReflections;
private RenderingPath m_currentRenderingPath;
private CameraEvent GetAppropriateCameraEvent(bool isDebugModeOn, RenderingPath renderingPath) {
return isDebugModeOn || renderingPath != RenderingPath.DeferredShading
? CameraEvent.BeforeImageEffectsOpaque
: CameraEvent.BeforeReflections;
}
private CameraEvent m_CameraEvent = CameraEvent.BeforeReflections;
override protected CameraEvent cameraEvent {
@@ -88,6 +90,7 @@ override protected void UpdateRenderTextures(Camera camera, VectorInt2 cameraSiz
}

override protected void UpdateMaterialPerFrame(Material material, Camera camera, VectorInt2 cameraSize) {
m_currentRenderingPath = camera.actualRenderingPath;
setMaterialDirty();
}

@@ -125,9 +128,11 @@ override protected void PopulateCommandBuffer(CommandBuffer buffer, Material mat
// ReleaseTemporaryRT(buffer, PropertyIDs.OcclusionNormals3_t);
ReleaseTemporaryRT(buffer, PropertyIDs.OcclusionNormals2_t);
ReleaseTemporaryRT(buffer, PropertyIDs.OcclusionNormals1_t);
Blit(buffer, BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.GBuffer0, material, (int)SSRPass.MultiplyAlpha);
if (m_currentRenderingPath == RenderingPath.DeferredShading) {
Blit(buffer, BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.GBuffer0, material, (int)SSRPass.MultiplyAlpha);
}

var appropriateCameraEvent = GetAppropriateCameraEvent(settings.debugOn);
var appropriateCameraEvent = GetAppropriateCameraEvent(settings.debugOn, m_currentRenderingPath);
if (appropriateCameraEvent != m_CameraEvent) {
postProcessingManager.RemoveCommandBuffer(this, cameraEvent, buffer);
m_CameraEvent = appropriateCameraEvent;
@@ -137,7 +142,6 @@ override protected void PopulateCommandBuffer(CommandBuffer buffer, Material mat

public void OnValidate () {
setMaterialDirty();
lastSettings = m_Settings;
}
}

4 changes: 0 additions & 4 deletions Cat/PostProcessing/CatColorGrading/CatColorGrading.cs
Original file line number Diff line number Diff line change
@@ -133,17 +133,13 @@ override protected void UpdateMaterial(Material material, Camera camera, VectorI
var exposure = Mathf.Pow(2, settings.exposure);
var contrast = Mathf.Max(EPSILON, settings.contrast + Mathf.Max(0, settings.contrast) * Mathf.Max(0, settings.contrast) + 1);
var saturation = (settings.saturation + Mathf.Max(0, settings.saturation) * Mathf.Max(0, settings.saturation) + 1) / contrast;
var temperature = settings.temperature;
var tint = settings.tint;
var blackPoint = 0 + settings.blackPoint * 0.25f;
var whitePoint = 1 +settings. whitePoint * 0.25f;

material.SetFloat(PropertyIDs.Exposure_f, exposure);
material.SetFloat(PropertyIDs.Contrast_f, contrast);
material.SetFloat(PropertyIDs.Saturation_f, saturation);

//material.SetFloat(PropertyIDs.Temperature_f, settings.temperature);
//material.SetFloat(PropertyIDs.Tint_f, settings.tint);
material.SetVector(PropertyIDs.ColorBalance_v, CalculateColorBalance(settings.temperature, settings.tint));
material.SetFloat(PropertyIDs.BlackPoint_f, blackPoint);
material.SetFloat(PropertyIDs.WhitePoint_f, whitePoint);
2 changes: 0 additions & 2 deletions Cat/PostProcessing/CatDepthOfField/CatDepthOfField.cs
Original file line number Diff line number Diff line change
@@ -121,8 +121,6 @@ private enum DOFPass {
}

internal override void RenderImage(RenderTexture source, RenderTexture destination) {
const int maxMipLvl = 7;

//var mipLevelFloat = Mathf.Clamp(Mathf.Log(Mathf.Max(source.width, source.height) / 32.0f + 1, 2), maxUpsample, maxMipLvl);
material.SetFloat(PropertyIDs.MipLevel_f, 0);

9 changes: 9 additions & 0 deletions Cat/PostProcessing/CatDepthOfField/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cat/PostProcessing/CatSSR/CatSSR.cs
Original file line number Diff line number Diff line change
@@ -414,8 +414,6 @@ override protected void PopulateCommandBuffer(CommandBuffer buffer, Material mat
new VectorInt2(reflRTSize.x / 64, reflRTSize.y / 128),
};

var reflectionSource_t = settings.useTemporalSampling ? new RenderTargetIdentifier(lastFrame) : BuiltinRenderTextureType.CameraTarget;

// #region Depth
// GetTemporaryRT(buffer, PropertyIDs.Depth_t, HitTextureSize, RenderTextureFormat.RHalf, FilterMode.Point, RenderTextureReadWrite.Linear);
// Blit(buffer, PropertyIDs.Depth_t, material, (int)SSRPass.Depth);
2 changes: 1 addition & 1 deletion Cat/PostProcessing/Includes/PostProcessingCommon.cginc
Original file line number Diff line number Diff line change
@@ -201,6 +201,6 @@ VertexOutput vert(VertexInput v) {
return o;
}
*/
float2 GetVelocity(float2 uv) { return !_IsVelocityPredictionEnabled ? 0 : Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy - _TAAJitterVelocity; }
float2 GetVelocity(float2 uv) { return !_IsVelocityPredictionEnabled ? 0 : Tex2Dlod(_CameraMotionVectorsTexture, uv, 0).xy; } // - _TAAJitterVelocity * 0; } // Does not seem to be neccessary in Unity2017.2f3

#endif // POST_PROCESSING_COMMON_INCLUDED
47 changes: 40 additions & 7 deletions Cat/PostProcessing/PostProcessingManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using Cat.Common;
@@ -63,7 +62,7 @@ protected void OnDestroy() {
if (m_DepthTexture != null) {
m_DepthTexture.Release();
}
camera.RemoveCommandBuffer(CameraEvent.AfterDepthTexture, depthCommandBuffer);
camera.RemoveCommandBuffer(m_DepthCommandBufferCameraEvent, depthCommandBuffer);
}


@@ -170,6 +169,7 @@ where effect is PostProcessingBaseImageEffect
// select makePostRenderDelegate(effect);
}

private bool m_requiresDepthTexture = false;
internal void UpdateCameraDepthTextureMode() {
var depthTextureMode = DepthTextureMode.None;
var effects = GetEffects(camera);
@@ -183,11 +183,8 @@ internal void UpdateCameraDepthTextureMode() {
}

camera.depthTextureMode = depthTextureMode;
camera.RemoveCommandBuffer(CameraEvent.BeforeLighting, depthCommandBuffer);
if ((depthTextureMode & DepthTextureMode.Depth) == DepthTextureMode.Depth) {
camera.AddCommandBuffer(CameraEvent.BeforeLighting, depthCommandBuffer);
}

m_requiresDepthTexture = (depthTextureMode & DepthTextureMode.Depth) == DepthTextureMode.Depth;
}

internal void UpdateCameraCommandBuffers() {
@@ -283,6 +280,7 @@ private void OnPreCull(){
UpdateDepthTexture();
m_lastCameraSize = cameraSize;
}
UpdateCameraDepthBufferCameraEvent(cam.actualRenderingPath);

foreach (var preCullFunc in m_preCullChain) {
preCullFunc(cam, size);
@@ -342,6 +340,40 @@ private RenderTexture depthTexture {
}
}

private Material m_DepthMaterial = null;
protected Material depthMaterial {
get {
const string shaderName = "Hidden/Cat Depth Shader";
if (m_DepthMaterial == null) {
var shader = Shader.Find(shaderName);
if (shader == null) {
this.enabled = false;
throw new ArgumentException(String.Format("Shader not found: '{0}'", shaderName));
}
m_DepthMaterial = new Material(shader);
m_DepthMaterial.hideFlags = HideFlags.DontSave;
}
return m_DepthMaterial;
}
}

private CameraEvent GetAppropriateDepthBufferCameraEvent(RenderingPath renderingPath) {
return renderingPath != RenderingPath.DeferredShading
? CameraEvent.AfterForwardOpaque
: CameraEvent.BeforeLighting;
}

internal void UpdateCameraDepthBufferCameraEvent(RenderingPath renderingPath) {
camera.RemoveCommandBuffer(m_DepthCommandBufferCameraEvent, depthCommandBuffer);

if (m_requiresDepthTexture ) {
m_DepthCommandBufferCameraEvent = GetAppropriateDepthBufferCameraEvent(renderingPath);
camera.AddCommandBuffer(m_DepthCommandBufferCameraEvent, depthCommandBuffer);
}
}


private CameraEvent m_DepthCommandBufferCameraEvent;
private CommandBuffer m_DepthCommandBuffer;
private CommandBuffer depthCommandBuffer {
get {
@@ -375,7 +407,8 @@ void UpdateDepthCommandBuffer() {
var cb = depthCommandBuffer;
cb.Clear();

cb.Blit(BuiltinRenderTextureType.ResolvedDepth, depthTexture);
//cb.Blit(BuiltinRenderTextureType.Depth, depthTexture);
cb.Blit(BuiltinRenderTextureType.None, depthTexture, depthMaterial, 0);
cb.SetGlobalTexture(PropertyIDs.Depth_t, depthTexture);
}

9 changes: 9 additions & 0 deletions Cat/PostProcessing/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions Cat/PostProcessing/Resources/CatDepthShader.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//The MIT License(MIT)

//Copyright(c) 2017 Joachim Coenen

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

Shader "Hidden/Cat Depth Shader" {
Properties {
_MainTex ("Base (RGB)", 2D) = "black" {}
}

CGINCLUDE
#include "UnityCG.cginc"
#include "../../Includes/CatCommon.cginc"


uniform UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture); uniform float4 _CameraDepthTexture_TexelSize;
static const bool _IsDebugOn = false;


struct VertexInput {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD;
};

struct VertexOutput {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};

VertexOutput vert(VertexInput v) {
VertexOutput o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord.xy;

return o;
}


half4 frag(VertexOutput i) : SV_Target {
return float4(sampleDepthLod(_CameraDepthTexture, i.uv.xy, 0), 0, 0, 1);

}

ENDCG

Subshader {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }

//Pass 0 ChromaticAberration
Pass {

CGPROGRAM
#pragma target 3.0

#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback "Diffuse"
}
9 changes: 9 additions & 0 deletions Cat/PostProcessing/Resources/CatDepthShader.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cat/Scripts/Editor/Graph.cs
Original file line number Diff line number Diff line change
@@ -25,8 +25,6 @@ public Graph(Rect viewPort, Rect range) {

// Transform a point into the graph rect
Vector2 PointToScreen(Vector2 p) {
var x01 = (p.x - m_Range.xMin) / m_Range.width;
var xvb = x01 * m_ViewPort.width + m_ViewPort.xMin;
return Vector2.Scale(p, m_ParamA) + m_ParamB;
}

0 comments on commit eb7903f

Please sign in to comment.