Skip to content

Keep Mat4.setLookAt valid when looking along the up vector - #10147

Open
SwastikTripathi wants to merge 1 commit into
playcanvas:mainfrom
SwastikTripathi:fix/mat4-lookat-parallel-up
Open

SwastikTripathi wants to merge 1 commit into
playcanvas:mainfrom
SwastikTripathi:fix/mat4-lookat-parallel-up

Conversation

@SwastikTripathi

Copy link
Copy Markdown
Contributor

Description

Mat4#setLookAt builds its x-axis as cross(up, z). When the line of sight is parallel to up, that cross product is the zero vector, normalize() leaves it at zero, and the matrix gets zero x and y rows. GraphNode#lookAt passes Vec3.UP by default, so the most common top-down setup silently does nothing:

const node = new GraphNode();
node.setPosition(0, 10, 0);
node.lookAt(Vec3.ZERO);
node.forward; // [0, 0, -1], still looking at the horizon
node.lookAt(new Vec3(0, 0, 1e-7));
node.forward; // [0, -1, 1e-8], looking down as expected

The rotation extracted from the zero rows comes out as identity, so the node keeps looking along -z. The same applies to a light aimed at something directly below it (noted in #9244, which left the guard for a separate change) and to XrLightEstimation, which calls setLookAt(..., Vec3.UP) with the estimated light direction.

When the cross product is exactly zero, setLookAt now builds the x-axis from the positive z-axis instead (or the positive x-axis when up itself is along z). That is the limit of a target a tiny step off the vertical, so looking straight down gives the same matrix as looking down at (0, 0, -1e-6): x stays [1, 0, 0] and the view's up becomes -z. Inputs that already worked take the same path as before. The doc comment now describes the fallback instead of forbidding parallel up vectors.

Tests: new cases in test/core/math/mat4.test.mjs (orthonormal basis that still points at the target for four parallel and antiparallel cases, and a match with the nearly vertical look at) and test/scene/graph-node.test.mjs (lookAt straight down and straight up). All four fail on main and pass with the fix. Full npm test: 59 failures both before and after, the same list (asset, font and container loading in node); nothing new. npx eslint on the changed files passes.

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant