From d0715ce3c8d9dcdcdb19e5ca89129bc2701a37e9 Mon Sep 17 00:00:00 2001 From: Evgenii Date: Thu, 17 Sep 2020 17:14:00 -0700 Subject: [PATCH 1/2] Add orthographic support to ComputePixelCoordToWorldSpaceViewDirectionMatrix --- .../Material/Fabric/IBLFilterCharlie.cs | 2 +- .../Material/GGXConvolution/IBLFilterGGX.cs | 2 +- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 2 +- .../Runtime/RenderPipeline/Utility/HDUtils.cs | 73 ++++++++++++------- .../Runtime/Sky/SkyManager.cs | 2 +- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Fabric/IBLFilterCharlie.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/Fabric/IBLFilterCharlie.cs index 297c9743d49..5f0721c9675 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Fabric/IBLFilterCharlie.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Fabric/IBLFilterCharlie.cs @@ -66,7 +66,7 @@ void FilterCubemapCommon(CommandBuffer cmd, for (int face = 0; face < 6; ++face) { var faceSize = new Vector4(source.width >> mip, source.height >> mip, 1.0f / (source.width >> mip), 1.0f / (source.height >> mip)); - var transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(0.5f * Mathf.PI, Vector2.zero, faceSize, worldToViewMatrices[face], true); + var transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(false, 0.5f * Mathf.PI, Vector2.zero, faceSize, worldToViewMatrices[face], true); props.SetMatrix(HDShaderIDs._PixelCoordToViewDirWS, transform); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs index 4c4c4101dd4..688603b98e7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/GGXConvolution/IBLFilterGGX.cs @@ -149,7 +149,7 @@ void FilterCubemapCommon(CommandBuffer cmd, for (int face = 0; face < 6; ++face) { var faceSize = new Vector4(source.width >> mip, source.height >> mip, 1.0f / (source.width >> mip), 1.0f / (source.height >> mip)); - var transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(0.5f * Mathf.PI, Vector2.zero, faceSize, worldToViewMatrices[face], true); + var transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(false, 0.5f * Mathf.PI, Vector2.zero, faceSize, worldToViewMatrices[face], true); props.SetMatrix(HDShaderIDs._PixelCoordToViewDirWS, transform); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index 92491545f7b..971675b4c9d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -1330,7 +1330,7 @@ Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(ViewConstants viewCon } Vector2 lensShift = camera.GetGateFittedLensShift(); - return HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(verticalFoV, lensShift, resolution, viewConstants.viewMatrix, false, aspect); + return HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(camera.orthographic, verticalFoV, lensShift, resolution, viewConstants.viewMatrix, false, aspect); } void Dispose() diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs index d783e6006b4..2761b86e0f8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs @@ -155,45 +155,62 @@ internal static void NextOverlayCoord(ref float x, ref float y, float overlayWid internal static float ProjectionMatrixAspect(in Matrix4x4 matrix) => -matrix.m11 / matrix.m00; - internal static Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(float verticalFoV, Vector2 lensShift, Vector4 screenSize, Matrix4x4 worldToViewMatrix, bool renderToCubemap, float aspectRatio = -1) + internal static Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(bool isOrthographic, float verticalFoV, Vector2 lensShift, Vector4 screenSize, Matrix4x4 worldToViewMatrix, bool renderToCubemap, float aspectRatio = -1) { - aspectRatio = aspectRatio < 0 ? screenSize.x * screenSize.w : aspectRatio; + Matrix4x4 result; - // Compose the view space version first. - // V = -(X, Y, Z), s.t. Z = 1, - // X = (2x / resX - 1) * tan(vFoV / 2) * ar = x * [(2 / resX) * tan(vFoV / 2) * ar] + [-tan(vFoV / 2) * ar] = x * [-m00] + [-m20] - // Y = (2y / resY - 1) * tan(vFoV / 2) = y * [(2 / resY) * tan(vFoV / 2)] + [-tan(vFoV / 2)] = y * [-m11] + [-m21] + if (isOrthographic) + { + // Goal: find M, s.t. M * P = -F, where P is the pixel coord vector and F is direction of the focal axis of the camera in the world space. + // -F = (V)^-1 * [0,0,1,0]^T, where V is the world-to-view matrix. + // -F = (R * T)^-1 * [0,0,1,0]^T = (T^-1) * (R^-1) * [0,0,1,0]^T = (T^-1) * (R^T) * [0,0,1,0]^T. + Vector3 negF = worldToViewMatrix.GetRow(2); - float tanHalfVertFoV = Mathf.Tan(0.5f * verticalFoV); + result = Matrix4x4.zero; + result.SetColumn(3, new Vector4(negF.x, negF.y, negF.z, 1)); + } + else + { + aspectRatio = aspectRatio < 0 ? screenSize.x * screenSize.w : aspectRatio; - // Compose the matrix. - float m21 = (1.0f - 2.0f * lensShift.y) * tanHalfVertFoV; - float m11 = -2.0f * screenSize.w * tanHalfVertFoV; + // Compose the view space version first. + // V = -(X, Y, Z), s.t. Z = 1, + // X = (2x / resX - 1) * tan(vFoV / 2) * ar = x * [(2 / resX) * tan(vFoV / 2) * ar] + [-tan(vFoV / 2) * ar] = x * [-m00] + [-m20] + // Y = (2y / resY - 1) * tan(vFoV / 2) = y * [(2 / resY) * tan(vFoV / 2)] + [-tan(vFoV / 2)] = y * [-m11] + [-m21] - float m20 = (1.0f - 2.0f * lensShift.x) * tanHalfVertFoV * aspectRatio; - float m00 = -2.0f * screenSize.z * tanHalfVertFoV * aspectRatio; + float tanHalfVertFoV = Mathf.Tan(0.5f * verticalFoV); - if (renderToCubemap) - { - // Flip Y. - m11 = -m11; - m21 = -m21; - } + // Compose the matrix. + float m21 = (1.0f - 2.0f * lensShift.y) * tanHalfVertFoV; + float m11 = -2.0f * screenSize.w * tanHalfVertFoV; - var viewSpaceRasterTransform = new Matrix4x4(new Vector4(m00, 0.0f, 0.0f, 0.0f), - new Vector4(0.0f, m11, 0.0f, 0.0f), - new Vector4(m20, m21, -1.0f, 0.0f), - new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); + float m20 = (1.0f - 2.0f * lensShift.x) * tanHalfVertFoV * aspectRatio; + float m00 = -2.0f * screenSize.z * tanHalfVertFoV * aspectRatio; - // Remove the translation component. - var homogeneousZero = new Vector4(0, 0, 0, 1); - worldToViewMatrix.SetColumn(3, homogeneousZero); + if (renderToCubemap) + { + // Flip Y. + m11 = -m11; + m21 = -m21; + } + + var viewSpaceRasterTransform = new Matrix4x4(new Vector4(m00, 0.0f, 0.0f, 0.0f), + new Vector4(0.0f, m11, 0.0f, 0.0f), + new Vector4(m20, m21, -1.0f, 0.0f), + new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); - // Flip the Z to make the coordinate system left-handed. - worldToViewMatrix.SetRow(2, -worldToViewMatrix.GetRow(2)); + // Remove the translation component. + var homogeneousZero = new Vector4(0, 0, 0, 1); + worldToViewMatrix.SetColumn(3, homogeneousZero); + + // Flip the Z to make the coordinate system left-handed. + worldToViewMatrix.SetRow(2, -worldToViewMatrix.GetRow(2)); + + result = worldToViewMatrix.transpose * viewSpaceRasterTransform; + } // Transpose for HLSL. - return Matrix4x4.Transpose(worldToViewMatrix.transpose * viewSpaceRasterTransform); + return Matrix4x4.Transpose(result); } internal static float ComputZPlaneTexelSpacing(float planeDepth, float verticalFoV, float resolutionY) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyManager.cs index b37b39cae19..83033b70230 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyManager.cs @@ -288,7 +288,7 @@ public void Build(HDRenderPipelineAsset hdAsset, RenderPipelineResources default var lookAt = Matrix4x4.LookAt(Vector3.zero, CoreUtils.lookAtList[i], CoreUtils.upVectorList[i]); var worldToView = lookAt * Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f)); // Need to scale -1.0 on Z to match what is being done in the camera.wolrdToCameraMatrix API. ... - m_facePixelCoordToViewDirMatrices[i] = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(0.5f * Mathf.PI, Vector2.zero, m_CubemapScreenSize, worldToView, true); + m_facePixelCoordToViewDirMatrices[i] = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(false, 0.5f * Mathf.PI, Vector2.zero, m_CubemapScreenSize, worldToView, true); m_CameraRelativeViewMatrices[i] = worldToView; } From 33aebd2f6e102e889f82cae4974ef6e111d347e1 Mon Sep 17 00:00:00 2001 From: Evgenii Date: Thu, 17 Sep 2020 17:20:39 -0700 Subject: [PATCH 2/2] Changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 98deeab0605..280c5326cc0 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fix for lookdev toggling renderers that are set to non editable or are hidden in the inspector. - Fixed issue with mipmap debug mode not properly resetting full screen mode (and viceversa). - Added unsupported message when using tile debug mode with MSAA. +- Fix procedural sky with orthographic camera (case 1278013). ### Changed - Preparation pass for RTSSShadows to be supported by render graph.