Raycaster can't find all objects

My raycaster can’t seem to find all objects, mostly related to when an animation is added, when I remove the animation it seems ok

I am using this code to find objects

      raycaster.setFromCamera(mouse, camera);

      ObjectToTest = [];
      scene.traverse(function (child) {
        if (child.isMesh || child.isSkinnedMesh) {
          ObjectToTest.push(child);
          //console.log("rayobj", child.name);
        }
      });

If log them I see these skinned meshes names. But when I log them at intersects, I don’t see them any more.

intersects = raycaster.intersectObjects(ObjectToTest);

      for (const intersect of intersects) {

        console.log("intersect", intersect.object.name);
      }
    }
1 Like

I don’t know what might be happening for you, but for what it’s worth, we’re also finding the ray caster can fail to return expected intersections in some circumstances. I haven’t dug into why, but it could easily be explained be the presence of skinned mesh, if that throws it off.

TL;DR :eyes:

Hmmm, that is not really good news, how do you work around it?

You may need to set a correct .boundingBox on the skinnedmesh:

1 Like

I managed to get something to work with

            scene.traverse(function (child) {
              if (child.isSkinnedMesh && child.boundingBox !== undefined) {
                child.computeBoundingBox();
              }
            });

I applying this when loading the animation.

I thought it would be a but much to traverse the whole scene for this, so I tried this

            model.traverse(function (child) {
              if (child.isSkinnedMesh && child.boundingBox !== undefined) {
                child.computeBoundingBox();
              }

But this has no effect. Where/when should I computeBoundingBox best?

Animated mesh bounding boxes change as the animation progresses, so computing the bounding box of a skinnedmesh with no animation applied will get the bounds of the bind pose which is often tiny/giant/wrong.
You might want to just make an educated guess and then just set it directly as a test like…

child.boundingBox = new THREE.Box();
let sz = 100; //Adjust this until it works try like 1, 10, 100, 1000, etc
child.boundingBox.min.set(-sz ,-sz ,-sz );
child.boundingBox.max.set( sz , sz , sz );

as a guess…

Otherwise… you could try playing your idle animation or whatever and then compute the boundingbox on the first frame, but that’s a bit fiddly.

1 Like

That was a good idea. I put some BoxHelper on them and now I see that none of these boxes are in the location of the models, have huge overlapping sizes. However when I force this smaller size they are positioned on the scene in the same pattern as the models, they just have an offset to the models

I have this boundingbox code directly after assigning the animation. Is it possible to apply here the first frame

            glbaction.setLoop(THREE.LoopRepeat);
            glbaction.play();
            mixers.push(mixer);
            mixer.update(1);

This gives me at least boxes with the correct ratios, just scale and position is off.

1 Like