From 7bb85bfae378233e24afb2633c180d6334b4416d Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 20 Apr 2018 15:32:01 -0400 Subject: [PATCH 1/3] Fixed issue with camera physical mode not enableable with postprocessLayer, fixed lensShift ignored when using TAA, updated DoF to use physical cam focal length and LookAtConstraint target to calculate focus distance --- .../Editor/PostProcessLayerEditor.cs | 20 ++++++++++ .../Runtime/Effects/DepthOfField.cs | 13 +++++- PostProcessing/Runtime/PostProcessLayer.cs | 40 ++++++++++++++++++- .../Runtime/PostProcessRenderContext.cs | 12 +++++- .../Runtime/Utils/RuntimeUtilities.cs | 40 +++++-------------- 5 files changed, 88 insertions(+), 37 deletions(-) diff --git a/PostProcessing/Editor/PostProcessLayerEditor.cs b/PostProcessing/Editor/PostProcessLayerEditor.cs index 09f3bf20..d9918a64 100644 --- a/PostProcessing/Editor/PostProcessLayerEditor.cs +++ b/PostProcessing/Editor/PostProcessLayerEditor.cs @@ -5,6 +5,9 @@ using UnityEngine.Rendering.PostProcessing; using UnityEditorInternal; using System.IO; +#if UNITY_2018_2_OR_NEWER +using UnityEngine.Animations; +#endif namespace UnityEditor.Rendering.PostProcessing { @@ -92,6 +95,7 @@ public override void OnInspectorGUI() DoVolumeBlending(); DoAntialiasing(); DoFog(camera); + DoDepthOfField(camera); EditorGUILayout.PropertyField(m_StopNaNPropagation, EditorUtilities.GetContent("Stop NaN Propagation|Automatically replaces NaN/Inf in shaders by a black pixel to avoid breaking some effects. This will slightly affect performances and should only be used if you experience NaN issues that you can't fix. Has no effect on GLES2 platforms.")); EditorGUILayout.Space(); @@ -203,6 +207,22 @@ void DoFog(Camera camera) EditorGUILayout.Space(); } + void DoDepthOfField(Camera camera) + { +#if UNITY_2018_2_OR_NEWER + EditorGUILayout.LabelField(EditorUtilities.GetContent("Depth Of Field"), EditorStyles.boldLabel); + EditorGUI.indentLevel++; + string helpBoxContent =""; + if (camera.usePhysicalProperties) + helpBoxContent += "Physical camera is enabled, DoF effect will use the physical camera's Focal length property\n"; + + if (m_Target.GetComponent()) + helpBoxContent += "This Camera has a LookAtConstraint, DoF will use the constraint's sources weighted average position to calculate the focus distance."; + + EditorGUILayout.HelpBox(helpBoxContent, MessageType.Info); +#endif + } + void DoToolkit() { EditorUtilities.DrawSplitter(); diff --git a/PostProcessing/Runtime/Effects/DepthOfField.cs b/PostProcessing/Runtime/Effects/DepthOfField.cs index 6b511622..f1ee4d58 100644 --- a/PostProcessing/Runtime/Effects/DepthOfField.cs +++ b/PostProcessing/Runtime/Effects/DepthOfField.cs @@ -62,7 +62,6 @@ enum Pass int[] m_HistoryPingPong = new int[k_NumEyes]; // Height of the 35mm full-frame format (36mm x 24mm) - // TODO: Should be set by a physical camera const float k_FilmHeight = 0.024f; public DepthOfFieldRenderer() @@ -134,7 +133,17 @@ public override void Render(PostProcessRenderContext context) // Material setup float scaledFilmHeight = k_FilmHeight * (context.height / 1080f); var f = settings.focalLength.value / 1000f; - var s1 = Mathf.Max(settings.focusDistance.value, f); + var focusDistance = settings.focusDistance.value; +#if UNITY_2018_2_OR_NEWER + if (context.physicalCamera) + { + f = context.camera.focalLength / 1000f; + scaledFilmHeight = (context.camera.sensorSize.y/1000f) * (context.height / 1080f); + } + if (context.cameraHasInterestPosition) + focusDistance = Vector3.Distance(context.cameraInterestPosition,context.camera.gameObject.transform.position); +#endif + var s1 = Mathf.Max(focusDistance, f); var aspect = (float)context.screenWidth / (float)context.screenHeight; var coeff = f * f / (settings.aperture.value * (s1 - f) * scaledFilmHeight * 2f); var maxCoC = CalculateMaxCoCRadius(context.screenHeight); diff --git a/PostProcessing/Runtime/PostProcessLayer.cs b/PostProcessing/Runtime/PostProcessLayer.cs index 1b0866c9..63cc1ac8 100644 --- a/PostProcessing/Runtime/PostProcessLayer.cs +++ b/PostProcessing/Runtime/PostProcessLayer.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using System.Linq; using UnityEngine.Assertions; +#if UNITY_2018_2_OR_NEWER +using UnityEngine.Animations; +#endif namespace UnityEngine.Rendering.PostProcessing { @@ -276,7 +279,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 @@ -446,7 +453,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) { @@ -569,6 +582,29 @@ 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; + + LookAtConstraint lookAtConstraint = context.camera.gameObject.GetComponent(); + if (lookAtConstraint && lookAtConstraint.sourceCount>0 && lookAtConstraint.enabled && lookAtConstraint.constraintActive) + { + List sources = new List(); + lookAtConstraint.GetSources(sources); + float weightSum = 0f; + foreach (var constraintSource in sources) + { + if (constraintSource.sourceTransform) + { + context.cameraHasInterestPosition = true; + context.cameraInterestPosition += constraintSource.sourceTransform.position * constraintSource.weight; + weightSum += constraintSource.weight; + } + } + context.cameraInterestPosition /= weightSum; + } +#endif + SetLegacyCameraFlags(context); // Prepare debug overlay diff --git a/PostProcessing/Runtime/PostProcessRenderContext.cs b/PostProcessing/Runtime/PostProcessRenderContext.cs index 028644ef..8f20eecd 100644 --- a/PostProcessing/Runtime/PostProcessRenderContext.cs +++ b/PostProcessing/Runtime/PostProcessRenderContext.cs @@ -134,7 +134,11 @@ public Camera camera internal Texture logLut; internal AutoExposure autoExposure; internal int bloomBufferNameID; - +#if UNITY_2018_2_OR_NEWER + internal bool physicalCamera; + internal bool cameraHasInterestPosition; + internal Vector3 cameraInterestPosition; +#endif public void Reset() { m_Camera = null; @@ -144,7 +148,11 @@ public void Reset() #if UNITY_2017_2_OR_NEWER m_sourceDescriptor = new RenderTextureDescriptor(0, 0); #endif - +#if UNITY_2018_2_OR_NEWER + physicalCamera = false; + cameraHasInterestPosition = false; + cameraInterestPosition = new Vector3(); +#endif stereoActive = false; xrActiveEye = (int)Camera.StereoscopicEye.Left; screenWidth = 0; diff --git a/PostProcessing/Runtime/Utils/RuntimeUtilities.cs b/PostProcessing/Runtime/Utils/RuntimeUtilities.cs index baf95ddb..1c6a3018 100644 --- a/PostProcessing/Runtime/Utils/RuntimeUtilities.cs +++ b/PostProcessing/Runtime/Utils/RuntimeUtilities.cs @@ -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; } From e46fa0d898ce2124a6ea278c630d2e1ab2a82c7b Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 2 May 2018 10:26:17 -0400 Subject: [PATCH 2/3] fixed inspector empty DoF section and helpbox when Physical cam is off and camera has no LookAtConstraint --- PostProcessing/Editor/PostProcessLayerEditor.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/PostProcessing/Editor/PostProcessLayerEditor.cs b/PostProcessing/Editor/PostProcessLayerEditor.cs index d9918a64..1d301efe 100644 --- a/PostProcessing/Editor/PostProcessLayerEditor.cs +++ b/PostProcessing/Editor/PostProcessLayerEditor.cs @@ -210,8 +210,7 @@ void DoFog(Camera camera) void DoDepthOfField(Camera camera) { #if UNITY_2018_2_OR_NEWER - EditorGUILayout.LabelField(EditorUtilities.GetContent("Depth Of Field"), EditorStyles.boldLabel); - EditorGUI.indentLevel++; + string helpBoxContent =""; if (camera.usePhysicalProperties) helpBoxContent += "Physical camera is enabled, DoF effect will use the physical camera's Focal length property\n"; @@ -219,7 +218,12 @@ void DoDepthOfField(Camera camera) if (m_Target.GetComponent()) helpBoxContent += "This Camera has a LookAtConstraint, DoF will use the constraint's sources weighted average position to calculate the focus distance."; - EditorGUILayout.HelpBox(helpBoxContent, MessageType.Info); + if (helpBoxContent!="") + { + EditorGUILayout.LabelField(EditorUtilities.GetContent("Depth Of Field"), EditorStyles.boldLabel); + EditorGUI.indentLevel++; + EditorGUILayout.HelpBox(helpBoxContent, MessageType.Info); + } #endif } From 2b388caba26bcc8b29924d41ffbf1963ef1f4a4e Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 10 May 2018 10:37:00 -0400 Subject: [PATCH 3/3] removed physical camera driven DoF code. --- .../Editor/PostProcessLayerEditor.cs | 24 ------------------- .../Runtime/Effects/DepthOfField.cs | 13 ++-------- PostProcessing/Runtime/PostProcessLayer.cs | 21 ---------------- .../Runtime/PostProcessRenderContext.cs | 4 ---- 4 files changed, 2 insertions(+), 60 deletions(-) diff --git a/PostProcessing/Editor/PostProcessLayerEditor.cs b/PostProcessing/Editor/PostProcessLayerEditor.cs index 1d301efe..09f3bf20 100644 --- a/PostProcessing/Editor/PostProcessLayerEditor.cs +++ b/PostProcessing/Editor/PostProcessLayerEditor.cs @@ -5,9 +5,6 @@ using UnityEngine.Rendering.PostProcessing; using UnityEditorInternal; using System.IO; -#if UNITY_2018_2_OR_NEWER -using UnityEngine.Animations; -#endif namespace UnityEditor.Rendering.PostProcessing { @@ -95,7 +92,6 @@ public override void OnInspectorGUI() DoVolumeBlending(); DoAntialiasing(); DoFog(camera); - DoDepthOfField(camera); EditorGUILayout.PropertyField(m_StopNaNPropagation, EditorUtilities.GetContent("Stop NaN Propagation|Automatically replaces NaN/Inf in shaders by a black pixel to avoid breaking some effects. This will slightly affect performances and should only be used if you experience NaN issues that you can't fix. Has no effect on GLES2 platforms.")); EditorGUILayout.Space(); @@ -207,26 +203,6 @@ void DoFog(Camera camera) EditorGUILayout.Space(); } - void DoDepthOfField(Camera camera) - { -#if UNITY_2018_2_OR_NEWER - - string helpBoxContent =""; - if (camera.usePhysicalProperties) - helpBoxContent += "Physical camera is enabled, DoF effect will use the physical camera's Focal length property\n"; - - if (m_Target.GetComponent()) - helpBoxContent += "This Camera has a LookAtConstraint, DoF will use the constraint's sources weighted average position to calculate the focus distance."; - - if (helpBoxContent!="") - { - EditorGUILayout.LabelField(EditorUtilities.GetContent("Depth Of Field"), EditorStyles.boldLabel); - EditorGUI.indentLevel++; - EditorGUILayout.HelpBox(helpBoxContent, MessageType.Info); - } -#endif - } - void DoToolkit() { EditorUtilities.DrawSplitter(); diff --git a/PostProcessing/Runtime/Effects/DepthOfField.cs b/PostProcessing/Runtime/Effects/DepthOfField.cs index f1ee4d58..6b511622 100644 --- a/PostProcessing/Runtime/Effects/DepthOfField.cs +++ b/PostProcessing/Runtime/Effects/DepthOfField.cs @@ -62,6 +62,7 @@ enum Pass int[] m_HistoryPingPong = new int[k_NumEyes]; // Height of the 35mm full-frame format (36mm x 24mm) + // TODO: Should be set by a physical camera const float k_FilmHeight = 0.024f; public DepthOfFieldRenderer() @@ -133,17 +134,7 @@ public override void Render(PostProcessRenderContext context) // Material setup float scaledFilmHeight = k_FilmHeight * (context.height / 1080f); var f = settings.focalLength.value / 1000f; - var focusDistance = settings.focusDistance.value; -#if UNITY_2018_2_OR_NEWER - if (context.physicalCamera) - { - f = context.camera.focalLength / 1000f; - scaledFilmHeight = (context.camera.sensorSize.y/1000f) * (context.height / 1080f); - } - if (context.cameraHasInterestPosition) - focusDistance = Vector3.Distance(context.cameraInterestPosition,context.camera.gameObject.transform.position); -#endif - var s1 = Mathf.Max(focusDistance, f); + var s1 = Mathf.Max(settings.focusDistance.value, f); var aspect = (float)context.screenWidth / (float)context.screenHeight; var coeff = f * f / (settings.aperture.value * (s1 - f) * scaledFilmHeight * 2f); var maxCoC = CalculateMaxCoCRadius(context.screenHeight); diff --git a/PostProcessing/Runtime/PostProcessLayer.cs b/PostProcessing/Runtime/PostProcessLayer.cs index 63cc1ac8..1e1628ed 100644 --- a/PostProcessing/Runtime/PostProcessLayer.cs +++ b/PostProcessing/Runtime/PostProcessLayer.cs @@ -2,9 +2,6 @@ using System.Collections.Generic; using System.Linq; using UnityEngine.Assertions; -#if UNITY_2018_2_OR_NEWER -using UnityEngine.Animations; -#endif namespace UnityEngine.Rendering.PostProcessing { @@ -585,24 +582,6 @@ void SetupContext(PostProcessRenderContext context) #if UNITY_2018_2_OR_NEWER context.physicalCamera = context.camera.usePhysicalProperties; - - LookAtConstraint lookAtConstraint = context.camera.gameObject.GetComponent(); - if (lookAtConstraint && lookAtConstraint.sourceCount>0 && lookAtConstraint.enabled && lookAtConstraint.constraintActive) - { - List sources = new List(); - lookAtConstraint.GetSources(sources); - float weightSum = 0f; - foreach (var constraintSource in sources) - { - if (constraintSource.sourceTransform) - { - context.cameraHasInterestPosition = true; - context.cameraInterestPosition += constraintSource.sourceTransform.position * constraintSource.weight; - weightSum += constraintSource.weight; - } - } - context.cameraInterestPosition /= weightSum; - } #endif SetLegacyCameraFlags(context); diff --git a/PostProcessing/Runtime/PostProcessRenderContext.cs b/PostProcessing/Runtime/PostProcessRenderContext.cs index 8f20eecd..21be6ad0 100644 --- a/PostProcessing/Runtime/PostProcessRenderContext.cs +++ b/PostProcessing/Runtime/PostProcessRenderContext.cs @@ -136,8 +136,6 @@ public Camera camera internal int bloomBufferNameID; #if UNITY_2018_2_OR_NEWER internal bool physicalCamera; - internal bool cameraHasInterestPosition; - internal Vector3 cameraInterestPosition; #endif public void Reset() { @@ -150,8 +148,6 @@ public void Reset() #endif #if UNITY_2018_2_OR_NEWER physicalCamera = false; - cameraHasInterestPosition = false; - cameraInterestPosition = new Vector3(); #endif stereoActive = false; xrActiveEye = (int)Camera.StereoscopicEye.Left;