How to get the vertex of an face with raycasting in instacemesh

Hello All,

I am trying to get the vertex position in my instanceMesh.

This is my current algo

That is working fine when i have the individual buffer objects

function setPos(faceIndex, index) {

tp[0].fromBufferAttribute(intersects[index].object.geometry.attributes.position, faceIndex * 3 + 0);

console.log(tp[0])

tp[1].fromBufferAttribute(intersects[index].object.geometry.attributes.position, faceIndex * 3 + 1);

console.log(tp[1])

tp[2].fromBufferAttribute(intersects[index].object.geometry.attributes.position, faceIndex * 3 + 2);

console.log(tp[2])

tri.set(tp[0], tp[1], tp[2]);

tri.getBarycoord(pointOfIntersection, bc);

if (bc.x > bc.y && bc.x > bc.z) {

  idx = 0;

} else if (bc.y > bc.x && bc.y > bc.z) {

  idx = 1;

} else if (bc.z > bc.x && bc.z > bc.y) {

  idx = 2;

}

pos.copy(tp[idx]);

}

     let object = intersects[i];

    pointOfIntersection.copy(object.point);

    object.object.worldToLocal(pointOfIntersection);

    setPos(object.faceIndex, i);

Any suggestion how i can achieve it for instancemesh

The following example demonstrates how you get the instance ID when raycasting with an InstancedMesh.

https://threejs.org/examples/webgl_instancing_raycast

You can also extract the vertex indices of the intersected face by accessing the face property of the intersection object.

You then use the instance ID to extract the respective instance matrix via InstancedMesh.getMatrixAt() and multiply it with the world matrix of the instanced mesh. You use the resulting matrix to transform the vertices to world space which you extract from the position buffer with the above vertex indices.

Seems like a solution but i think i am doing something wrong please correct me on this.

var a = intersect.face.a
var b = intersect.face.b
var c = intersect.face.c

positionAttr = intersect.object.attributes.position.array

var vector = new THREE.Vector3(positionAttr[a],positionAttr[b],positionAttr[c])
var instanceMatrix = new THREE.Matrix4()
intersect.object.getMatrixAt(intersect.instanceId,instanceMatrix)

var result = instaceMatrix.multiply(intersect.object.matrixWorld)

Here it is giving me same result as instaceMatrix because my matrix world is

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

vector.applyMatrix4(result)

I am getting vertex but the position is not correct

This is the solution after following your suggestions

Thanks

const positionAttr = intersects[i].object.geometry.attributes.position

    var instanceMatrix = new THREE.Matrix4()

    intersects[i].object.getMatrixAt(intersects[i].instanceId, instanceMatrix)

    vA.fromBufferAttribute( positionAttr, a ).applyMatrix4(instanceMatrix)

    vB.fromBufferAttribute( positionAttr, b ).applyMatrix4(instanceMatrix)

    vC.fromBufferAttribute( positionAttr, c ).applyMatrix4(instanceMatrix)

    var minDistance = vA.distanceTo(intersects[i].point)

    var point = vA

    if(minDistance >  vB.distanceTo(intersects[i].point)){

      minDistance = vB.distanceTo(intersects[i].point)

      point = vB

    }

   

    if(minDistance >  vC.distanceTo(intersects[i].point)){

      minDistance = vC.distanceTo(intersects[i].point)

      point = vC

    }