Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.
Merged
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
19 changes: 17 additions & 2 deletions PostProcessing/Runtime/PostProcessLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ void OnPreCull()
// We also need to force reset the non-jittered projection matrix here as it's not done
// when ResetProjectionMatrix() is called and will break transparent rendering if TAA
// is switched off and the FOV or any other camera property changes.
m_Camera.ResetProjectionMatrix();

#if UNITY_2018_2_OR_NEWER
if (!m_Camera.usePhysicalProperties)
#endif
m_Camera.ResetProjectionMatrix();
m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;

#if !UNITY_SWITCH
Expand Down Expand Up @@ -446,7 +450,13 @@ void OnPostRender()

if (m_CurrentContext.IsTemporalAntialiasingActive())
{
m_Camera.ResetProjectionMatrix();
#if UNITY_2018_2_OR_NEWER
// TAA calls SetProjectionMatrix so if the camera projection mode was physical, it gets set to explicit. So we set it back to physical.
if (m_CurrentContext.physicalCamera)
m_Camera.usePhysicalProperties = true;
else
#endif
m_Camera.ResetProjectionMatrix();

if (m_CurrentContext.stereoActive)
{
Expand Down Expand Up @@ -569,6 +579,11 @@ void SetupContext(PostProcessRenderContext context)
context.antialiasing = antialiasingMode;
context.temporalAntialiasing = temporalAntialiasing;
context.logHistogram = m_LogHistogram;

#if UNITY_2018_2_OR_NEWER
context.physicalCamera = context.camera.usePhysicalProperties;
#endif

SetLegacyCameraFlags(context);

// Prepare debug overlay
Expand Down
8 changes: 6 additions & 2 deletions PostProcessing/Runtime/PostProcessRenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public Camera camera
internal Texture logLut;
internal AutoExposure autoExposure;
internal int bloomBufferNameID;

#if UNITY_2018_2_OR_NEWER
internal bool physicalCamera;
#endif
public void Reset()
{
m_Camera = null;
Expand All @@ -144,7 +146,9 @@ public void Reset()
#if UNITY_2017_2_OR_NEWER
m_sourceDescriptor = new RenderTextureDescriptor(0, 0);
#endif

#if UNITY_2018_2_OR_NEWER
physicalCamera = false;
#endif
stereoActive = false;
xrActiveEye = (int)Camera.StereoscopicEye.Left;
screenWidth = 0;
Expand Down
40 changes: 9 additions & 31 deletions PostProcessing/Runtime/Utils/RuntimeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,44 +576,22 @@ public static float Exp2(float x)
return Mathf.Exp(x * 0.69314718055994530941723212145818f);
}

// Adapted heavily from PlayDead's TAA code
// https://github.com/playdeadgames/temporal/blob/master/Assets/Scripts/Extensions.cs

public static Matrix4x4 GetJitteredPerspectiveProjectionMatrix(Camera camera, Vector2 offset)
{
float vertical = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView);
float horizontal = vertical * camera.aspect;
float near = camera.nearClipPlane;
float far = camera.farClipPlane;

offset.x *= horizontal / (0.5f * camera.pixelWidth);
offset.y *= vertical / (0.5f * camera.pixelHeight);

float left = (offset.x - horizontal) * near;
float right = (offset.x + horizontal) * near;
float top = (offset.y + vertical) * near;
float bottom = (offset.y - vertical) * near;

var matrix = new Matrix4x4();
float far = camera.farClipPlane;

matrix[0, 0] = (2f * near) / (right - left);
matrix[0, 1] = 0f;
matrix[0, 2] = (right + left) / (right - left);
matrix[0, 3] = 0f;
float vertical = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView) * near;
float horizontal = vertical * camera.aspect;

matrix[1, 0] = 0f;
matrix[1, 1] = (2f * near) / (top - bottom);
matrix[1, 2] = (top + bottom) / (top - bottom);
matrix[1, 3] = 0f;
offset.x *= horizontal / (0.5f * camera.pixelWidth);
offset.y *= vertical / (0.5f * camera.pixelHeight);

matrix[2, 0] = 0f;
matrix[2, 1] = 0f;
matrix[2, 2] = -(far + near) / (far - near);
matrix[2, 3] = -(2f * far * near) / (far - near);
var matrix = camera.projectionMatrix;

matrix[3, 0] = 0f;
matrix[3, 1] = 0f;
matrix[3, 2] = -1f;
matrix[3, 3] = 0f;
matrix[0, 2] += offset.x / horizontal;
matrix[1, 2] += offset.y / vertical;

return matrix;
}
Expand Down