-
-
Notifications
You must be signed in to change notification settings - Fork 94
Space partitioning optimization #3
Description
Further accelerate ray casting performance with some BVH/space partitioning
Thoughts on how to accomplish
Using bounding volumes, place entities into an octree. Maybe instead of a bounding sphere, find the oriented bounding box (OBB). This seems much better suited because we can simply apply the entity's GlobalTransform to the bounding box, and it will be exactly correct. We only need to recompute the OBB if the mesh changes. Checking if an OBB fits into an octree cell is trivial, we can just test each of the 8 vertices of the OBB to verify that they are within the 6 planes (faces) that bound a cell.
Placing entities in an octree
- Starting at the top level, recurse through the octree until the entity's bounding volume no longer fits into a node
- Place the entity into the last node it fit into
- Repeat for all entities (startup) and all changed entities (subsequent frames)
Ray casting into the octree
When casting a ray, now there will be two options - get all intersections, or just the topmost. If we are only finding the topmost intersection, we can speed up a ray cast by:
- Start in the topmost node that the ray's origin coordinate located in. (level 0)
- Recurse into the octree using the given coordinate, checking which child node it's located in, until the leaf node is reached. Record any entities that are found along the way.
- Check for intersection with all of the entities, first checking their boundary volumes before checking the meshes.
- Sort intersections and return the topmost intersection if one was found - all done!
- Go up a level at a time from the leaf node, until you find a node that has another (untested) child(ren)
- Test if the ray will intersect any of these child cells (up to 8-1 = 7) in this node. If there were no intersections, go to step 5.
- Go into the nearest intersected cell and repeat from step 2 using the intersection point plus some tiny epsilon in the ray direction, so we don't test cells we've already looked in.