Optimizations for raycasting?

Hey guys. Can you tell me, what optimizations can I apply for raycast if I gonna use a very short ray in a known area (small area around a known coordinate). I use raycast on a huge mesh with a lot of faces to find a normal and UV coordinate near the coordinate that already lies on the surface of this mesh.
I have to use some raycasts at each draw tick and have a performance problem with it (especially on phones where even one raycast action in one draw round greatly reduce performance).

May be I have to cut my big mesh into small pieces or something like that? Who has any ideas about this?

I believe you can use FacetData for those kind of geometry things:

https://doc.babylonjs.com/how_to/how_to_use_facetdata

It also has a method called getClosestFacetAtCoordinates, maybe you can use that instead of a raycast?

Here is a demo how I use it. I raycast down in front of a cube to find a next position for it. And use normal to set rotation, and use UV to draw on the texture.
Facet data can not use directed vectors for it, only coordinate, so I don’t think it well suited for it, or I missed something?

Hi Avin

I have found Raycasting larger geometries can kill performance just like you are saying. I have tried to keep my target geometries low in order to keep it quick. Number of targets will play a role too. If you break them up you will have simpler meshes but have more meshes to iterate through each frame so I don’t know what performance you will gain.

How many vertices are in your mesh?

about ~8500 verts

Any way you can reduce the vertices and make the mesh a low poly mesh, even if just for your mobile version?

Also, are you using a helper?

For certain reasons I cannot reduce the number of vertices, BUT…

Just found this article Optimize Your Scene with Octrees - Babylon.js Documentation

using subdivide + createOrUpdateSubmeshesOctree on the fat mesh gave me a VERY BIG performance boost!!!

Babylon.js continues to amaze me with a set of ready-made solutions. Love to use it :heart:

5 Likes