Skip to content

Scene: Matrix4.decompose leaves a singular matrix as identity rotation and unit scale, as three.js does - #126

Merged
doodlewind merged 2 commits into
meshy-dev:mainfrom
kvnloo:fix/trt-compat-matrix4
Nov 24, 2026
Merged

doodlewind merged 2 commits into
meshy-dev:mainfrom
kvnloo:fix/trt-compat-matrix4

Conversation

@kvnloo

@kvnloo kvnloo commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

three.js's Matrix4.decompose checks the affine determinant first. When it is 0 (a zero-scaled axis, say), it still writes the position but sets the rotation to identity and the scale to (1, 1, 1) instead of dividing by a zero axis length (r187 Matrix4.js#L1071-L1095). packages/scene divides by the zero length, so the quaternion comes out [NaN, NaN, NaN, NaN].

Change: Matrix4.decompose in math.ts writes the position, computes the affine determinant, and returns identity rotation and unit scale when it is 0. Otherwise it is unchanged; the same determinant now also picks the sign of sx.

Behaviour change, matching three.js: callers that decompose a zero-scaled matrix (for example getWorldScale, getWorldQuaternion, attach, Object3D.applyMatrix4, lookAt under a parent and the light orientation in engine.ts) now get scale (1, 1, 1) and an identity rotation where they used to get the true scale with a NaN rotation. Checked with an object scaled (0, 2, 3): on main getWorldScale gives (0, 2, 3) and getWorldQuaternion gives NaN; on this branch they give (1, 1, 1) and (0, 0, 0, 1), the same as three@0.187.0. attach and Object3D.applyMatrix4 on such an object change the same way. Code that hides objects by scaling an axis to 0 and then calls one of these will see the three.js result.

Test: one node:test case in a new file, packages/scene/test/three-compat-matrix4.test.ts: a makeScale(0, 2, 3) matrix with a translation keeps its position, gets identity rotation and unit scale, and is not itself modified.

The test is in its own file so it merges independently of the other open three.js-parity PRs.

Tier T (packages/scene TypeScript; no kernel, IR or export changes).

Merge gate

Runs were on a Linux VM (Node 24.22.1) in a fresh home directory, from a fresh clone and npm ci.

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.

2 participants