withintheruins14
withintheruins14

Reputation: 427

Three.js Reseting Matrix before Transformation

I want to apply the same matrix transformation to a three.js mesh multiple times, but I only ever want the transformation as describe by the first transform. How can I "reset" the mesh's matrix to its pre transformation value before I re-apply the transformation?

I have tried the following, but it hasn't worked

const identity = model.matrix.identity();
model.matrix = identity;
model.matrixWorld = identity;
model.updateMatrix();
model.updateMatrixWorld();
model.applyMatrix4(newMatrix);

Upvotes: 2

Views: 1139

Answers (2)

Valera Kovalenko
Valera Kovalenko

Reputation: 21

The easiest way (https://discourse.threejs.org/t/how-to-reset-matrix/36939):

newMatrix.decompose(model.position, model.quaternion, model.scale);
model.updateMatrix();

or using applyMatrix4 method and some "first transformation":

var identityMatrix = new THREE.Matrix4().identity(); //any "first transformation" matrix
identityMatrix.decompose(model.position, model.quaternion, model.scale);
model.updateMatrix();

model.applyMatrix4(newMatrix);
model.updateMatrix();

Upvotes: 1

TheJim01
TheJim01

Reputation: 8896

EDIT: Replacing my entire answer because I was unaware that Object3D.applyMatrix4 updates the position/quaterion/scale properties in addition to modifying the matrix.

The easiest way to approach this is to back-up the model's original matrix property, and copy it back in place before the next update. This can be accomplished using Object3D.userData.

// before doing any updates, ensure the local matrix is set from postion/quaternion/scale:
model.updateMatrix();

// back-up the matrix:
model.userData.originalMatrix = model.matrix.clone();

// reset the model before each update
model.userData.originalMatrix.decompose( model.position, model.quaternion, model.scale );
model.matrix.copy( model.userData.originalMatrix );

Upvotes: 4

Related Questions