Raycast not detecting GlTF file/ Three.js

Viewed 628

My binary gltf file(modelled in blender and animated using mixamo) is not detecting on raycast. I read bunch of tutorials and questions about it to try to fix it but it does not work what so ever:(

const loader = new GLTFLoader();
let modelLoader = "/models/c2.glb";
let model, mixer, neck, waist;

loader.load(modelLoader, (gltf) => {
  model = gltf.scene;
  let fileAnimations = gltf.animations;
  model.traverse((o) => {
    // console.log(o);
    if (o.isBone && o.name === "mixamorigNeck") {
      neck = o;
    }
    if (o.isBone && o.name === "mixamorigSpine") {
      waist = o;
    }
    if (o.isMesh) {
      o.material.reflectivity = 1;
    }
  });

 document.addEventListener("click", (e) => raycast(e));
  document.addEventListener("touchend", (e) => raycast(e, true));

  function raycast(e, touch = false) {
    const raycaster = new THREE.Raycaster();
    const mouse = new THREE.Vector2();
    e.preventDefault();

    if (touch) {
      mouse.x = 2 * (e.changedTouches[0].clientX / window.innerWidth) - 1;
      mouse.y = 1 - 2 * (e.changedTouches[0].clientY / window.innerHeight);
    } else {
      mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
    }

    raycaster.setFromCamera(mouse, camera);

    const intersects = raycaster.intersectObjects(scene.children, true);
    console.log(scene.children);
    if (intersects.length > 0) {
      console.log("hi");}}

It does not have error, and if I console.log scene.children outside of ‘if (intersects){}’ and click on the window, it works and contains everything it should, but if I put it inside the ‘if’ and click on the 3d object it is empty array.

I also tried to .push the meshes inside new array and detect but did not work.

Please help!

1 Answers

The problem is that you are performing ray casting against a skinned mesh. And three.js is currently (r128) not able to compute proper bounding volumes for this type of 3D object. Bounding volumes however are important for ray casting since they are used to detect early outs.

The workaround for this issue is to manually define bounding volumes so they properly enclose the skinned mesh. I suggest you traverse through gltf.scene and set the boundingSphere and boundingBox property of the skinned mesh's geometry.

More information at GitHub here: https://github.com/mrdoob/three.js/pull/19178

Related