Skip to content

Commit b77c968

Browse files
committed
core: mat4Location fuse impl
1 parent bad503a commit b77c968

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ mat4MulDir(out, m, dx,dy,dz) // out = 3×3 block of m applied to direction
279279
280280
```js
281281
mat4ToTranslation(out3, m) // extract translation (col 3)
282-
// out3: Float32Array | number[] | p5.Vector
282+
// out3: Float32Array | number[] | p5.Vector
283283
mat4ToScale(out3, m) // extract scale (column lengths) — assumes no shear
284-
// out3: Float32Array | number[] | p5.Vector
284+
// out3: Float32Array | number[] | p5.Vector
285285
mat4ToRotation(out4, m) // extract rotation as unit quaternion [x,y,z,w]
286-
// out4: Float32Array | number[]
286+
// out4: Float32Array | number[]
287287
```
288288
289289
**Zero-allocation draw-loop pattern:**

deps/tree/src/query.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,30 @@ export function mat4MV(out, model, view) { return mat4Mul(out, view, model); }
219219

220220
/**
221221
* Location transform between frames: out = inv(to) · from.
222+
* Assumes both matrices are affine (bottom row [0,0,0,1]).
222223
* @returns {ArrayLike<number>|null} out, or null if `to` is singular.
223224
*/
224225
export function mat4Location(out, from, to) {
225-
return mat4Invert(out, to) && mat4Mul(out, out, from);
226+
// Same as: return mat4Invert(out, to) && mat4Mul(out, out, from);
227+
const a00=to[0],a01=to[1],a02=to[2],
228+
a10=to[4],a11=to[5],a12=to[6],
229+
a20=to[8],a21=to[9],a22=to[10];
230+
const b01=a22*a11-a12*a21, b11=a12*a20-a22*a10, b21=a21*a10-a11*a20;
231+
let det=a00*b01+a01*b11+a02*b21;
232+
if (Math.abs(det) < 1e-12) return null;
233+
det=1/det;
234+
const i00=b01*det, i01=(a02*a21-a22*a01)*det, i02=(a12*a01-a02*a11)*det;
235+
const i10=b11*det, i11=(a22*a00-a02*a20)*det, i12=(a02*a10-a12*a00)*det;
236+
const i20=b21*det, i21=(a01*a20-a21*a00)*det, i22=(a11*a00-a01*a10)*det;
237+
const f00=from[0],f01=from[1],f02=from[2],
238+
f10=from[4],f11=from[5],f12=from[6],
239+
f20=from[8],f21=from[9],f22=from[10];
240+
out[0]=i00*f00+i01*f01+i02*f02; out[1]=i10*f00+i11*f01+i12*f02; out[2]=i20*f00+i21*f01+i22*f02; out[3]=0;
241+
out[4]=i00*f10+i01*f11+i02*f12; out[5]=i10*f10+i11*f11+i12*f12; out[6]=i20*f10+i21*f11+i22*f12; out[7]=0;
242+
out[8]=i00*f20+i01*f21+i02*f22; out[9]=i10*f20+i11*f21+i12*f22; out[10]=i20*f20+i21*f21+i22*f22; out[11]=0;
243+
const dx=from[12]-to[12], dy=from[13]-to[13], dz=from[14]-to[14];
244+
out[12]=i00*dx+i01*dy+i02*dz; out[13]=i10*dx+i11*dy+i12*dz; out[14]=i20*dx+i21*dy+i22*dz; out[15]=1;
245+
return out;
226246
}
227247

228248
/**

0 commit comments

Comments
 (0)