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

Commit 0f702dc

Browse files
committed
Prep-work to add TAA support in SRPs
1 parent 4732129 commit 0f702dc

4 files changed

Lines changed: 83 additions & 72 deletions

File tree

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -74,84 +74,25 @@ Vector2 GenerateRandomOffset()
7474
return offset;
7575
}
7676

77-
// Adapted heavily from PlayDead's TAA code
78-
// https://github.com/playdeadgames/temporal/blob/master/Assets/Scripts/Extensions.cs
79-
Matrix4x4 GetPerspectiveProjectionMatrix(Camera camera, Vector2 offset)
80-
{
81-
float vertical = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView);
82-
float horizontal = vertical * camera.aspect;
83-
float near = camera.nearClipPlane;
84-
float far = camera.farClipPlane;
85-
86-
offset.x *= horizontal / (0.5f * camera.pixelWidth);
87-
offset.y *= vertical / (0.5f * camera.pixelHeight);
88-
89-
float left = (offset.x - horizontal) * near;
90-
float right = (offset.x + horizontal) * near;
91-
float top = (offset.y + vertical) * near;
92-
float bottom = (offset.y - vertical) * near;
93-
94-
var matrix = new Matrix4x4();
95-
96-
matrix[0, 0] = (2f * near) / (right - left);
97-
matrix[0, 1] = 0f;
98-
matrix[0, 2] = (right + left) / (right - left);
99-
matrix[0, 3] = 0f;
100-
101-
matrix[1, 0] = 0f;
102-
matrix[1, 1] = (2f * near) / (top - bottom);
103-
matrix[1, 2] = (top + bottom) / (top - bottom);
104-
matrix[1, 3] = 0f;
105-
106-
matrix[2, 0] = 0f;
107-
matrix[2, 1] = 0f;
108-
matrix[2, 2] = -(far + near) / (far - near);
109-
matrix[2, 3] = -(2f * far * near) / (far - near);
110-
111-
matrix[3, 0] = 0f;
112-
matrix[3, 1] = 0f;
113-
matrix[3, 2] = -1f;
114-
matrix[3, 3] = 0f;
115-
116-
return matrix;
117-
}
118-
119-
Matrix4x4 GetOrthographicProjectionMatrix(Camera camera, Vector2 offset)
120-
{
121-
float vertical = camera.orthographicSize;
122-
float horizontal = vertical * camera.aspect;
123-
124-
offset.x *= horizontal / (0.5f * camera.pixelWidth);
125-
offset.y *= vertical / (0.5f * camera.pixelHeight);
126-
127-
float left = offset.x - horizontal;
128-
float right = offset.x + horizontal;
129-
float top = offset.y + vertical;
130-
float bottom = offset.y - vertical;
131-
132-
return Matrix4x4.Ortho(left, right, bottom, top, camera.nearClipPlane, camera.farClipPlane);
133-
}
134-
135-
public void SetProjectionMatrix(Camera camera)
77+
public Matrix4x4 GetJitteredProjectionMatrix(Camera camera)
13678
{
79+
Matrix4x4 cameraProj;
13780
jitter = GenerateRandomOffset();
13881
jitter *= jitterSpread;
13982

140-
camera.nonJitteredProjectionMatrix = camera.projectionMatrix;
141-
14283
if (jitteredMatrixFunc != null)
14384
{
144-
camera.projectionMatrix = jitteredMatrixFunc(camera, jitter);
85+
cameraProj = jitteredMatrixFunc(camera, jitter);
14586
}
14687
else
14788
{
148-
camera.projectionMatrix = camera.orthographic
149-
? GetOrthographicProjectionMatrix(camera, jitter)
150-
: GetPerspectiveProjectionMatrix(camera, jitter);
89+
cameraProj = camera.orthographic
90+
? RuntimeUtilities.GetJitteredOrthographicProjectionMatrix(camera, jitter)
91+
: RuntimeUtilities.GetJitteredPerspectiveProjectionMatrix(camera, jitter);
15192
}
15293

153-
camera.useJitteredProjectionMatrixForTransparentRendering = false;
15494
jitter = new Vector2(jitter.x / camera.pixelWidth, jitter.y / camera.pixelHeight);
95+
return cameraProj;
15596
}
15697

15798
RenderTexture CheckHistory(int id, PostProcessRenderContext context)

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,13 @@ public void Render(PostProcessRenderContext context)
518518
int lastTarget = -1;
519519
if (context.IsTemporalAntialiasingActive())
520520
{
521-
temporalAntialiasing.SetProjectionMatrix(context.camera);
521+
if (!RuntimeUtilities.scriptableRenderPipelineActive)
522+
{
523+
var camera = context.camera;
524+
camera.nonJitteredProjectionMatrix = camera.projectionMatrix;
525+
camera.projectionMatrix = temporalAntialiasing.GetJitteredProjectionMatrix(camera);
526+
camera.useJitteredProjectionMatrixForTransparentRendering = false;
527+
}
522528

523529
lastTarget = m_TargetPool.Get();
524530
var finalDestination = context.destination;

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,64 @@ public static float Exp2(float x)
294294
return Mathf.Exp(x * 0.69314718055994530941723212145818f);
295295
}
296296

297+
// Adapted heavily from PlayDead's TAA code
298+
// https://github.com/playdeadgames/temporal/blob/master/Assets/Scripts/Extensions.cs
299+
public static Matrix4x4 GetJitteredPerspectiveProjectionMatrix(Camera camera, Vector2 offset)
300+
{
301+
float vertical = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView);
302+
float horizontal = vertical * camera.aspect;
303+
float near = camera.nearClipPlane;
304+
float far = camera.farClipPlane;
305+
306+
offset.x *= horizontal / (0.5f * camera.pixelWidth);
307+
offset.y *= vertical / (0.5f * camera.pixelHeight);
308+
309+
float left = (offset.x - horizontal) * near;
310+
float right = (offset.x + horizontal) * near;
311+
float top = (offset.y + vertical) * near;
312+
float bottom = (offset.y - vertical) * near;
313+
314+
var matrix = new Matrix4x4();
315+
316+
matrix[0, 0] = (2f * near) / (right - left);
317+
matrix[0, 1] = 0f;
318+
matrix[0, 2] = (right + left) / (right - left);
319+
matrix[0, 3] = 0f;
320+
321+
matrix[1, 0] = 0f;
322+
matrix[1, 1] = (2f * near) / (top - bottom);
323+
matrix[1, 2] = (top + bottom) / (top - bottom);
324+
matrix[1, 3] = 0f;
325+
326+
matrix[2, 0] = 0f;
327+
matrix[2, 1] = 0f;
328+
matrix[2, 2] = -(far + near) / (far - near);
329+
matrix[2, 3] = -(2f * far * near) / (far - near);
330+
331+
matrix[3, 0] = 0f;
332+
matrix[3, 1] = 0f;
333+
matrix[3, 2] = -1f;
334+
matrix[3, 3] = 0f;
335+
336+
return matrix;
337+
}
338+
339+
public static Matrix4x4 GetJitteredOrthographicProjectionMatrix(Camera camera, Vector2 offset)
340+
{
341+
float vertical = camera.orthographicSize;
342+
float horizontal = vertical * camera.aspect;
343+
344+
offset.x *= horizontal / (0.5f * camera.pixelWidth);
345+
offset.y *= vertical / (0.5f * camera.pixelHeight);
346+
347+
float left = offset.x - horizontal;
348+
float right = offset.x + horizontal;
349+
float top = offset.y + vertical;
350+
float bottom = offset.y - vertical;
351+
352+
return Matrix4x4.Ortho(left, right, bottom, top, camera.nearClipPlane, camera.farClipPlane);
353+
}
354+
297355
#endregion
298356

299357
#region Reflection

PostProcessing/Shaders/Builtins/TemporalAntialiasing.shader

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
Shader "Hidden/PostProcessing/TemporalAntialiasing"
22
{
33
HLSLINCLUDE
4-
4+
55
#pragma exclude_renderers gles
66
#include "../StdLib.hlsl"
77
#include "../Colors.hlsl"
88

9-
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
9+
#if UNITY_VERSION >= 201710
10+
#define _MainTexSampler sampler_LinearClamp
11+
#else
12+
#define _MainTexSampler sampler_MainTex
13+
#endif
14+
15+
TEXTURE2D_SAMPLER2D(_MainTex, _MainTexSampler);
1016
float4 _MainTex_TexelSize;
1117

1218
TEXTURE2D_SAMPLER2D(_HistoryTex, sampler_HistoryTex);
@@ -86,10 +92,10 @@ Shader "Hidden/PostProcessing/TemporalAntialiasing"
8692
const float2 k = _MainTex_TexelSize.xy;
8793
float2 uv = texcoord - _Jitter;
8894

89-
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
95+
float4 color = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv);
9096

91-
float4 topLeft = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - k * 0.5);
92-
float4 bottomRight = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + k * 0.5);
97+
float4 topLeft = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv - k * 0.5);
98+
float4 bottomRight = SAMPLE_TEXTURE2D(_MainTex, _MainTexSampler, uv + k * 0.5);
9399

94100
float4 corners = 4.0 * (topLeft + bottomRight) - 2.0 * color;
95101

0 commit comments

Comments
 (0)