Raycast for instances from merged geometry

How can I check that my raycast crosses exactly into a visible part of the mesh and not into the full geometry? Maybe I should somehow rely on a face id from picked result? Raycast doesn’t take into account that I multiply the unnecessary vertices by 0

 const custom = new BABYLON.CustomMaterial("material");
    custom.Vertex_Definitions('in vec2 range;')
        float id = float(gl_VertexID);
        if (id < range[0] || id > range[1]) {
            positionUpdated *= 0.0;
const ray = scene.createPickingRay(scene.pointerX, scene.pointerY, Matrix.Identity(), scene.activeCamera);
const p = scene.pickWithRay(ray).pickedPoint;


The ray casting functions can’t know that you changed some vertex positions in the shader, as the computation happens on the CPU.

Using face indices may work in your case if you are removing whole faces in your shader.

Yes, I understand that. Is there any other way to properly identify a hit?

Maybe GPU picking can help?

Search for “GPU picking” in the forum, there are a number of threads about this topic.


Thank you. I’ll take a closer look at this one. So far, I’ve cleaned up my problem with this code in _intersectTriangles method:

 if (mesh instanceof InstancedMesh) {
        if (indexA < mesh.instancedBuffers.range.x || indexA > mesh.instancedBuffers.range.y) {