Raycasting with camera

[Example Project] (https://holonext.io/api/v1/scene/holonextViewer/6299f72d967706c1fcb89524)

Hi, we are trying to raycasting object and camera to detect is any other mesh exist between mesh and our object .if other mesh exist we are decreasing opacity(alpha) of object. Every camera movement we have to check situation and it causes to fps problems.

const checkMeshisVisible = (mesh) => {
        let hitFlag = false;
        if (mesh) {
          let ray = BABYLON.Ray.CreateNewFromTo(
            scene.cameras[0].position,
            mesh.position
          );
          var hits = scene.multiPickWithRay(ray);

          if (hits) {
            for (var i = 0; i < hits.length; i++) {
              if (hits[i].pickedMesh.name != "BackgroundPlane") {
                // discard ground plane
                hitFlag = true;
              }
            }
          }

          const picked = scene.pickWithRay(ray);
          titleList.forEach((annoMesh) => {
            if (hitFlag && annoMesh._linkedMesh.name == mesh.name) {
              annoMesh.alpha = 0.7;
            } else if (annoMesh._linkedMesh.name == mesh.name) {
              annoMesh.alpha = 1;
            }
          });
        }
      };

      scene.onPointerMove = (e) => {
        fakeMeshList.forEach((meshName) => {
          let mesh = scene.getMeshByName(meshName + "-fake-mesh");
          checkMeshisVisible(mesh);
        });
      };
    }

here is the some part of code is there any way to optimize this render condition?

Hello and welcome to the Babylon community!

Raycasting is a pretty expensive operation, so you’ll really want to optimize it as much as possible. Some techniques for this are Octrees: Optimizing With Octrees | Babylon.js Documentation (babylonjs.com) and GPU Picking: GPU picking demo - Demos and projects - Babylon.js (babylonjs.com)

Hi Thanks for answer to me.Can you give me octree raycasting example using babylon playground ?

The link I provided up above has an example of how to create the octree, when that’s created it’s automatically used on the mesh intersects function :slight_smile: