How do octrees work?

I set up a playground to do some simple testing of the octree methods. I don’t think I understand what the octree.intersects method is supposed to be doing. In the playground I linked below, an octree is created using the scene once everything has been loaded. I created a sphere centered on the cameras position to visually represent what a 200 unit sphere would contain from that position (it should be empty).

When I run the query:

const nearby = octree.intersects(camera.position, 200)
const nearbyCount = nearby.length

I get a result of 6 meshes instead of zero. So clearly I have the wrong interpretation on what that method does. What is it doing?

Aren’t the 6 meshes these ones that show up when the PG is loaded?

Anyways, what the intersects function does is drill down on the scene’s octree representation checking each block for an intersection with the bounding sphere. You can check its code here: Babylon.js/octree.ts at 16b511f1680df856d44f9e97eeea526cf18cd02d · BabylonJS/Babylon.js · GitHub

1 Like

Thank you for the reference and explanation - so the sphere is not the containing element - instead the result is based on what block from the octree intersects the sphere, and then from there the results are pulled from that block or block(s) if the sphere intersects more than one?

Correct :smiley: