Skip to content

Fix procedural sky with orthographic camera #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down