Skip to content

Commit f0f660b

Browse files
svc-reach-platform-supportEvergreen
authored andcommitted
[Port] [2021.3] Fix HDRP sky rendering when Camera Relative Rendering is disabled
This PR fixes HDRP sky rendering when Camera Relative Rendering is disabled. `ComputePixelCoordToWorldSpaceViewDirectionMatrix()` in `HDCamera.cs` contains: ``` ... if (useGenericMatrix) { var viewSpaceRasterTransform = new Matrix4x4( new Vector4(2.0f * resolution.z, 0.0f, 0.0f, -1.0f), new Vector4(0.0f, -2.0f * resolution.w, 0.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 0.0f), new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); var transformT = viewConstants.invViewProjMatrix.transpose * Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f)); return viewSpaceRasterTransform * transformT; } ... ``` Here the view matrix that was used to build `viewConstants.invViewProjMatrix` has translation component if Camera Relative Rendering is disabled when it shouldn't. ![scene_view](https://github.cds.internal.unity3d.com/unity/unity/assets/7006/95047d07-52f3-4d19-8698-0120ad86987f) Scene View ![game_view_wrong](https://github.cds.internal.unity3d.com/unity/unity/assets/7006/2cb0b898-ddb2-4819-ae58-7900ce0c4221) Game View, bugged ![game_view_fixed](https://github.cds.internal.unity3d.com/unity/unity/assets/7006/0a9b4699-9737-426f-a605-fd51a5ae72eb) Game View, fixed
1 parent 98c140f commit f0f660b

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,12 @@ internal void GetPixelCoordToViewDirWS(Vector4 resolution, float aspect, ref Mat
10631063
{
10641064
for (int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
10651065
{
1066-
transforms[viewIndex] = ComputePixelCoordToWorldSpaceViewDirectionMatrix(m_XRViewConstants[viewIndex], resolution, aspect);
1066+
transforms[viewIndex] = ComputePixelCoordToWorldSpaceViewDirectionMatrix(m_XRViewConstants[viewIndex], resolution, aspect, ShaderConfig.s_CameraRelativeRendering);
10671067
}
10681068
}
10691069
else
10701070
{
1071-
transforms[0] = ComputePixelCoordToWorldSpaceViewDirectionMatrix(mainViewConstants, resolution, aspect);
1071+
transforms[0] = ComputePixelCoordToWorldSpaceViewDirectionMatrix(mainViewConstants, resolution, aspect, ShaderConfig.s_CameraRelativeRendering);
10721072
}
10731073
}
10741074

@@ -1618,7 +1618,7 @@ void UpdateViewConstants(ref ViewConstants viewConstants, Matrix4x4 projMatrix,
16181618
viewConstants.viewProjectionNoCameraTrans = gpuVPNoTrans;
16191619

16201620
var gpuProjAspect = HDUtils.ProjectionMatrixAspect(gpuProj);
1621-
viewConstants.pixelCoordToViewDirWS = ComputePixelCoordToWorldSpaceViewDirectionMatrix(viewConstants, screenSize, gpuProjAspect);
1621+
viewConstants.pixelCoordToViewDirWS = ComputePixelCoordToWorldSpaceViewDirectionMatrix(viewConstants, screenSize, gpuProjAspect, ShaderConfig.s_CameraRelativeRendering);
16221622

16231623
if (updatePreviousFrameConstants)
16241624
{
@@ -1859,8 +1859,9 @@ internal Matrix4x4 GetJitteredProjectionMatrix(Matrix4x4 origProj)
18591859
///
18601860
/// It is different from the aspect ratio of <paramref name="resolution"/> for anamorphic projections.
18611861
/// </param>
1862+
/// <param name="cameraRelativeRendering">If non-zero, then assume Camera Relative Rendering is enabled.</param>
18621863
/// <returns></returns>
1863-
Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(ViewConstants viewConstants, Vector4 resolution, float aspect = -1)
1864+
internal Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(ViewConstants viewConstants, Vector4 resolution, float aspect = -1, int cameraRelativeRendering = 1)
18641865
{
18651866
// In XR mode, use a more generic matrix to account for asymmetry in the projection
18661867
var useGenericMatrix = xr.enabled;
@@ -1879,7 +1880,20 @@ Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(ViewConstants viewCon
18791880
new Vector4(0.0f, 0.0f, 1.0f, 0.0f),
18801881
new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
18811882

1882-
var transformT = viewConstants.invViewProjMatrix.transpose * Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f));
1883+
Matrix4x4 transformT;
1884+
if (cameraRelativeRendering == 0)
1885+
{
1886+
// In case we are not camera relative, the view matrix used to calculate viewConstants.invViewProjMatrix
1887+
// contains translation component, so we need to remove it.
1888+
var viewNoTrans = viewConstants.viewMatrix;
1889+
viewNoTrans.SetColumn(3, new Vector4(0, 0, 0, 1));
1890+
var invViewProj = (viewConstants.projMatrix * viewNoTrans).inverse;
1891+
transformT = invViewProj.transpose * Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f));
1892+
}
1893+
else
1894+
{
1895+
transformT = viewConstants.invViewProjMatrix.transpose * Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f));
1896+
}
18831897
return viewSpaceRasterTransform * transformT;
18841898
}
18851899

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using NUnit.Framework;
2+
using UnityEngine.Experimental.Rendering;
3+
4+
namespace UnityEngine.Rendering.HighDefinition.Tests
5+
{
6+
class HDCameraTests
7+
{
8+
[SetUp]
9+
public void Setup()
10+
{
11+
if (GraphicsSettings.currentRenderPipeline is not HDRenderPipelineAsset)
12+
Assert.Ignore("This is an HDRP Tests, and the current pipeline is not HDRP.");
13+
}
14+
15+
[Test]
16+
public void TestCameraRelativeRendering()
17+
{
18+
GameObject cameraGameObject = new GameObject("Camera");
19+
var camera = cameraGameObject.AddComponent<Camera>();
20+
var hdCamera = new HDCamera(camera);
21+
22+
var positionSettings = new CameraPositionSettings();
23+
positionSettings.position = new Vector3(100.0f, 300.0f, 500.0f);
24+
positionSettings.rotation = Quaternion.Euler(62.34f, 185.53f, 323.563f);
25+
26+
var resolution = new Vector4(1920.0f, 1080.0f, 1.0f / 1920.0f, 1.0f / 1080.0f);
27+
float aspect = resolution.x * resolution.w;
28+
29+
camera.worldToCameraMatrix = positionSettings.ComputeWorldToCameraMatrix();
30+
camera.projectionMatrix = Matrix4x4.Perspective(75.0f, aspect, 0.1f, 1000.0f);
31+
32+
var view = camera.worldToCameraMatrix;
33+
var proj = camera.projectionMatrix;
34+
35+
// Minimal setup for ComputePixelCoordToWorldSpaceViewDirectionMatrix().
36+
var viewConstants = new HDCamera.ViewConstants();
37+
viewConstants.viewMatrix = view;
38+
viewConstants.projMatrix = proj;
39+
viewConstants.invViewProjMatrix = (proj * view).inverse;
40+
41+
// hdCamera.xr must be initialized for ComputePixelCoordToWorldSpaceViewDirectionMatrix().
42+
var hdrp = HDRenderPipeline.currentPipeline;
43+
hdCamera.Update(hdCamera.frameSettings, hdrp, XRSystem.emptyPass, allocateHistoryBuffers: false);
44+
45+
var matrix0 = hdCamera.ComputePixelCoordToWorldSpaceViewDirectionMatrix(viewConstants, resolution, aspect, 0);
46+
var matrix1 = hdCamera.ComputePixelCoordToWorldSpaceViewDirectionMatrix(viewConstants, resolution, aspect, 1);
47+
48+
// These matrices convert a clip space position to a world space view direction,
49+
// therefore should be same regardless of Camera Relative Rendering.
50+
Assert.AreEqual(matrix0, matrix1, $"{matrix0} != {matrix1}");
51+
52+
CoreUtils.Destroy(cameraGameObject);
53+
}
54+
}
55+
}

Packages/com.unity.render-pipelines.high-definition/Tests/Editor/HDCameraTests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)