Internal workings of MeshCollider.Raycast

Hi,

does anyone know if MeshCollider.Raycast first checks the ray against the colliders bounding volume, and only then checks the actual mesh collider, or does it always check the polygons in the collider mesh?

Suppose I have 10 mesh colliders and I want to know if user is doing a mouse-over on any of them. If each mesh has 500 polygons, will there be 5000(500poly X 10meshes) ray-polygon intersection tests every frame?

OR will there be 10 ray-box (or ray-sphere) intersections, and only when it’s determined which mesh is even being intersected, will the algorithm check the actual polygons?

I’m wondering because I want to know should I add a box collider to all my meshes and check them first, for better performance. But I won’t do that, if this function does that internally anyway.

The standard way these things work internally for most game engines, is a bounding box intersection test first as they’re rather quick. Only then does it do more expensive tests.

I realize it’s the standard way, but I’m not sure if it’s so in Unity. And even if it might be so for Physics.Raycast(where it test every collider in the scene), i’m not sure if it will do that if I do just MeshCollider.Raycast(since you’re effectively telling it “test exactly against this mesh”, so it might skip a box-ray collision step in such case)

Perhaps you could just use OnMouseOver instead if you don’t need poly accurate collision info. As it doesn’t supply the info, it’s probably just testing against the bounding box.

I doubt that. Even if you test against a single mesh, it pays to do the box-ray intersection first. Plus, I happen to know the guy who wrote the very first version of PhysX and he would not write code that doesn’t first do a bounds check. ever. :wink:

(I’m assuming Unity uses PhysX functions for all intersection tests, but they might not…)

Alright then, thanks guys