Hey there!
I’m experimenting with TSL and instancedMesh and i’m kinda lost on how to get the instanceMatrix for each element when i replace the entire vertexNode
Somehow now i’m doing const instMat = buffer(this.instanceMatrix.array, 'mat4', this.count).element(instanceIndex).toMat4().toVar()
but is not the proper way to do it. It works but it fails with a large amount of instances due on how he use the buffer, it goes beyond limits.
I tried to use another approach like const instanceMatrixNode = instancedBufferAttribute(this.instanceMatrix)
but doesn’t work neither.
Can’t find any ref in the docs, neither in the exported values from three/tsl
I recently spent some time trying the same thing (accessing the instanceMatrix for each instance of an InstancedMesh inside the vertexNode), and the short answer is:
No, as of version 180 of three.js and after many days of research, I never found a way to get the original instanceMatrix. But, if each instance’s scale and rotation are unset, you can “reconstruct” the instance matrix, or just simply get the instance’s position in world coordinates. Like this:
material.vertexNode = tsl.Fn(() => {
const posLoc = tsl.positionLocal;
const posGeo = tsl.positionGeometry;
const posMod = tsl.modelPosition;
// Get the instance's center in world coordinates.
// NOTE: This only works if each instance's scale and rotation are unset.
// In other words, the scale is {x:1,y:1,z:1} and rotation is {x:0,y:0,z:0,w:1} for each instance.
const instanceCenter = posLoc.sub(posGeo).add(posMod).toVar();
// Now you can do something with the instance center in world coordinates...
})();
well, this is kinda useless in terms of the usage of instance matrix unfortunately because it takes in consideration just the position unfortunately.
I need to have access on all the props, pos-rot-scale, like the good old way in GLSL
I need to have access to that instanceMatrix in my vertexNode due the fact i’m replacing the entire node, so at some point i need to use the instance matrix value