threejs applyMatrix4 appears to do nothing

Viewed 70

The method applyMatrix4 seems like it does nothing... Why can I not apply this transformation matrix to my vector?

const vec = new THREE.Vector3(1,1,1)
const geometry = new THREE.BoxGeometry(1,1,1)
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 })
const mesh = new THREE.Mesh(geometry, material)
mesh.rotateX(Math.PI)

const rotatedVec = vec.applyMatrix4(mesh.matrix)
console.log(rotatedVec)

Expectation (taking the cross product):

{x: 1, y: -1, z: -1}

Reality (the vector is unchanged)

{x: 1, y: 1, z: 1}

My mesh's matrix has changed - it is not the identity matrix.

[
  [1, 0, 0,  0],
  [0, -1, 0, 0],
  [0, 0, -1, 0],
  [0, 0, 0,  1],
]

1 Answers

Object3D.rotateX() only affects the object's quaternion property. It does not update its local matrix. If you say your matrix has changed, I assume you have checked it at a later point when other engine logic triggers a recalculation.

You can solve this issue by adding mesh.updateMatrix(); after you have called Object3D.rotateX().

Or even better use Vector3.applyQuaternion(). In this way, you don't have to recompute the matrix because you don't need it anyway.

const rotatedVec = vec.applyQuaternion(mesh.quaternion)
Related