Raycast hit to entire mesh

Hi All,

   I want to find out mesh and camera position have any object or not.

so I am trying find solution above problem using reycast. I will fire ray and check any object or any other mesh in between.

now I have issue. how many percentage mesh camera can able see/view.

orange color is camera location and I want check green mesh many percentage able to see/ view.

I’m not sure if BabylonJS already offers a better way, but in general you could approach the problem this way:

  1. Set up a temporary RenderTargetTexture. This can probably be a lot smaller than your actual render resolution; I’d start with 10 %.
  2. Render your green mesh to the render target using a single matte (unlit) color. You could use a simple ShaderMaterial for this, that would always output the same fragment color.
  3. Read the rendered scene using your rendertargettexture’s readPixels and count the non-black pixels.
  4. Add the rest of your scene to your render target, and render the rest of the meshes totally black.
  5. Render the scene again, read the pixels and count how many of them are visible now.

Note that the results will come several frames late. Each readPixels takes multiple frames to complete.

There’s no built in solution for this so Phvli’s solution works well.