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

Commit 51877f6

Browse files
committed
Use decomposeProjection + cleanups
We recently added Matrix4x4.decomposeProjection, which gives us the frustum planes for the projection matrix. I can then offset those with the jitter value, and create the jittered projection matrix. Also a couple cleanups from PR feedback
1 parent f9d3e71 commit 51877f6

3 files changed

Lines changed: 20 additions & 71 deletions

File tree

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ public void ConfiguredStereoJitteredProjectionMatrices(PostProcessRenderContext
136136
camera.useJitteredProjectionMatrixForTransparentRendering = false;
137137
}
138138

139+
private void GenerateHistoryName(RenderTexture rt, int id, PostProcessRenderContext context)
140+
{
141+
rt.name = "Temporal Anti-aliasing History id #" + id.ToString();
142+
if (XR.XRSettings.isDeviceActive)
143+
rt.name += " for eye " + context.xrActiveEye.ToString();
144+
}
145+
139146
RenderTexture CheckHistory(int id, PostProcessRenderContext context)
140147
{
141148
var rt = m_HistoryTextures[context.xrActiveEye][id];
@@ -145,10 +152,7 @@ RenderTexture CheckHistory(int id, PostProcessRenderContext context)
145152
RenderTexture.ReleaseTemporary(rt);
146153

147154
rt = RenderTexture.GetTemporary(context.width, context.height, 0, context.sourceFormat);
148-
if (XR.XRSettings.isDeviceActive)
149-
rt.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.xrActiveEye.ToString();
150-
else
151-
rt.name = "Temporal Anti-aliasing History id #" + id.ToString();
155+
GenerateHistoryName(rt, id, context);
152156

153157
rt.filterMode = FilterMode.Bilinear;
154158
m_HistoryTextures[context.xrActiveEye][id] = rt;
@@ -160,10 +164,7 @@ RenderTexture CheckHistory(int id, PostProcessRenderContext context)
160164
// On size change, simply copy the old history to the new one. This looks better
161165
// than completely discarding the history and seeing a few aliased frames.
162166
var rt2 = RenderTexture.GetTemporary(context.width, context.height, 0, context.sourceFormat);
163-
if (XR.XRSettings.isDeviceActive)
164-
rt2.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.xrActiveEye.ToString();
165-
else
166-
rt2.name = "Temporal Anti-aliasing History id #" + id.ToString();
167+
GenerateHistoryName(rt2, id, context);
167168

168169
rt2.filterMode = FilterMode.Bilinear;
169170
m_HistoryTextures[context.xrActiveEye][id] = rt2;

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public Camera camera
3030
if (camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
3131
m_xrActiveEye = (int)Camera.StereoscopicEye.Right;
3232

33-
if (m_xrSinglePass && (xrDesc.dimension != TextureDimension.Tex2DArray))
34-
m_xrSingleEyeWidth = m_width / 2;
35-
else
36-
m_xrSingleEyeWidth = m_width;
33+
m_xrSingleEyeWidth = XR.XRSettings.eyeTextureWidth;
3734
}
3835
else
3936
{

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -386,71 +386,22 @@ public static Matrix4x4 GetJitteredOrthographicProjectionMatrix(Camera camera, V
386386
return Matrix4x4.Ortho(left, right, bottom, top, camera.nearClipPlane, camera.farClipPlane);
387387
}
388388

389-
// We can represent a projection matrix by using the tangents of the frustum half angles.
390-
// The 'traditional' representation of the values in a projection matrix for
391-
// left, right, top and bottom are that they represent values on the near clip plane.
392-
// If we take the matrix element (0,0) as an example, it would be equal to
393-
// (2 * clipNearPlane) / (right - left). We can divide the term by clipNearPlane to get
394-
// 2 / ((right - left) / clipNearPlane), which gives us
395-
// 2 / (right/clipNearPlane - left/clipNearPlane). And we can substitue
396-
// tan(rightHalfAngle) = (right/clipNearPlane) and tan(leftHalfAngle) = (left/clipNearPlane).
397-
// Our new term for (0,0) is 2/(rTan - lTan).
398-
//
399-
// Since we have the calculated value for (0,0), we can use that to solve for (rTan - lTan).
400-
// (rTan - lTan) = 2 / proj(0,0)
401-
// We can also get the value for (rTan + lTan), as it is the numerator for term (0,2).
402-
// We have the denominator, so (rTan + lTan) = (rTan - lTan) * proj(0,2) = 2 * proj(0,2) / proj (0,0)
403-
// We can add (rTan + lTan) and (rTan - lTan) to get 2 * rTan, which can gives us rTan.
404-
// rTan = ((2 / proj(0,0)) + (2 * proj(0,2) / proj(0,0))) / 2 =>
405-
// rTan = (1 + proj(0,2)) / proj(0,0)
406-
//
407-
// We can derive lTan via proj(0,0) = 2 / (rTan - lTan), which gives us
408-
// lTan = rTan - 2/proj(0,0)
409-
// If we substitute our derivation for rTan in here, we get the conveniently symmetric:
410-
// lTan = ((1 + proj(0,2)) / proj(0,0)) - 2 / proj(0,0) =>
411-
// lTan = (-1 + proj(0,2)) / proj(0,0)
412-
//
413-
// We can repeat these calculations for the top and bottom tangents as well.
414389
public static Matrix4x4 GenerateJitteredProjectionMatrixFromOriginal(PostProcessRenderContext context, Matrix4x4 origProj, Vector2 jitter)
415390
{
416-
var rTan = (1.0f + origProj[0, 2]) / origProj[0, 0];
417-
var lTan = (-1.0f + origProj[0, 2]) / origProj[0, 0];
391+
FrustumPlanes planes = origProj.decomposeProjection;
418392

419-
var tTan = (1.0f + origProj[1, 2]) / origProj[1, 1];
420-
var bTan = (-1.0f + origProj[1, 2]) / origProj[1, 1];
393+
float vertFov = Math.Abs(planes.top) + Math.Abs(planes.bottom);
394+
float horizFov = Math.Abs(planes.left) + Math.Abs(planes.right);
421395

422-
float tanVertFov = Math.Abs(tTan) + Math.Abs(bTan);
423-
float tanHorizFov = Math.Abs(lTan) + Math.Abs(rTan);
396+
var planeJitter = new Vector2(jitter.x * horizFov / context.singleEyeWidth,
397+
jitter.y * vertFov / context.height);
424398

425-
jitter.x *= tanHorizFov / context.singleEyeWidth;
426-
jitter.y *= tanVertFov / context.height;
399+
planes.left += planeJitter.x;
400+
planes.right += planeJitter.x;
401+
planes.top += planeJitter.y;
402+
planes.bottom += planeJitter.y;
427403

428-
float left = jitter.x + lTan;
429-
float right = jitter.x + rTan;
430-
float top = jitter.y + tTan;
431-
float bottom = jitter.y + bTan;
432-
433-
var jitteredMatrix = new Matrix4x4();
434-
435-
jitteredMatrix[0, 0] = 2f / (right - left);
436-
jitteredMatrix[0, 1] = 0f;
437-
jitteredMatrix[0, 2] = (right + left) / (right - left);
438-
jitteredMatrix[0, 3] = 0f;
439-
440-
jitteredMatrix[1, 0] = 0f;
441-
jitteredMatrix[1, 1] = 2f / (top - bottom);
442-
jitteredMatrix[1, 2] = (top + bottom) / (top - bottom);
443-
jitteredMatrix[1, 3] = 0f;
444-
445-
jitteredMatrix[2, 0] = 0f;
446-
jitteredMatrix[2, 1] = 0f;
447-
jitteredMatrix[2, 2] = origProj[2, 2];
448-
jitteredMatrix[2, 3] = origProj[2, 3];
449-
450-
jitteredMatrix[3, 0] = 0f;
451-
jitteredMatrix[3, 1] = 0f;
452-
jitteredMatrix[3, 2] = -1f;
453-
jitteredMatrix[3, 3] = 0f;
404+
var jitteredMatrix = Matrix4x4.Frustum(planes);
454405

455406
return jitteredMatrix;
456407
}

0 commit comments

Comments
 (0)