I am trying to interact with an object in an instancedMesh. I have set up an example here which uses react-three-fiber and a bit of drei but hopefully the principles are the same.
When I click on a cube, I want to get the information for that object, with the aim to then set the position of that cube to something different. However, when using getMatrixAt() it’s returning undefined.
I’ve seen a few examples, mainly here and another here but I can’t seem to get mine to work in a similar way.
My hunch is it’s something to do with the matrix value I’m passing through, but I’m not sure.
In the docs: .getMatrixAt ( index : Integer, matrix : Matrix4 ) : undefined
It’s a method, that puts matrix data in the Matrix4 object, passed in the second parameter, and returns undefined.
If you do it this way:
Ah right, thank you. I hadn’t appreciated that it puts data into a Matrix object. Do you know how I can update the position of that object? Tbh I’m not the best with dealing with matrices. I’m going to do some digging now but thought I’d ask if you know off the top of your head?
You can have an instance of Object3D and re-use it to decompose a matrix.
let tempObj = new THREE Object3D();
// in animation loop or anywhere else
mesh.getMatrixAt(i, matrix);
matrix.decompose(tempObj.position, tempObj.quaternion, tempObj.scale); // now position is in tempObj.position
You are an absolute lifesaver. Thanks you @prisoner849 , that’s a massive, massive help. I completely didn’t appreciate that when we get these values we’re setting them to objects we pass in.