Determining how much of a box is taken by intersecting Collider - how?

Hello.

I’m prototyping something a little more… experimental when it comes to algorithmics for the game I’m currently working on.
I can’t reveal too many details, but I’ve come across the following problem I would like to solve using mechanisms present in Unity already - if possible.

We have an area of space defined by a cube bounding box (Bounds struct).
This area of space is some portion of the level, irrespective of level geometry - albeit we do assume the biggest possible box contains the entirety of it (the Bounds structures in question are the leafs of the OctTree partition).
Now the question I’m trying to answer - how would one determine how much of such a bounding box is intersected by geometry on a scale of <0;1>, where 0 means it’s “free” space and 1 - the bounding box is completely contained withing geometry?

Checking intersection against Mesh/Renderer.bounds isn’t the way to go, sadly - otherwise I’m pretty sure most of the leafs would be considered as fully inside geometry, when tested against the level “floor”.

Illustration of the problem in simpler, 2D case:

PS: It’s meant to be a part of a larger algorithm to run in editor - NOT during application’s runtime (we calculate data structures)

Assuming the vertices are evenly distributed you can count how many vertices are within the box vs outside.

Alternatively you could render the world from above into a texture and then count how many pixels are inside the box vs outside.

But if this is 3D then you’ll need to use marching cubes and voxels to basically do as above.

p.s. I’m not a maths guy and so take what I say with a big chunk of salt.

1 Like

No, you know what? That is a decent approximation in conjunction with some of my other preconceptions. The counting how many within vs outside.

Each box calculated from partition is a cube.
I have a collection of Colliders resulting from OverlapBox determined on the tested Bounds.
What I can do, is I can take 27 point in the cube (basically all the corners, middles of edges, middles of faces and the center) and then check each of those point:

colliderTestedAgainst.ClosestPoint(testedPoint).Equals(testedPoint)

…because turns out if the point in the argument of ClosestPoint is a point inside the tested collider - the result of this function will be the argument itself.

Given that for my purposes I’m partitioning the space limited to a smallest size of the box (ie. if Bounds of the leaf I would want to part have size < MIN, this is when I no longer divide, and do the test instead), I should get decent results, even if the precision is technically abysmal - which isn’t something one cannot help to a degree by increasing the density of probing points (from 3^3 = 27 to, say, 4^3 = 64).

Cheers for the reply - and regardless of any chunk of salt, it did give me a decent enough for now idea how to deal with the issue. :slight_smile: