Capítulo 581 de 859

Chapter 581: Octree

Core Idea

An octree is a hierarchical tree data structure used to partition a three-dimensional space by recursively subdividing it into eight octants.

This particular implementation can have up to sixteen levels and stores up to eight triangles in leaf nodes.

Octree can be used in games to compute collision between the game world and colliders from the player or other dynamic 3D objects.

Key Concepts

  • bounds : Box3: The bounds of the Octree. Compared to Octree#box, no margin is applied.
  • box : Box3: The base box with enclose the entire Octree.
  • layers : Layers: Can by used for layers configuration for refine testing.
  • maxLevel : number: The maximum level of the Octree. It defines the maximum hierarchical depth of the data structure.
  • trianglesPerLeaf : number: The number of triangles a leaf can store before it is split.

Code Examples

const octree = new Octree().fromGraphNode( scene );
const result = octree.capsuleIntersect( playerCollider ); // collision detection
  • What it demonstrates: Typical usage of Octree.

Reference Tables

Constructor

Signature
new Octree( box : Box3 )

Properties

PropertyDescription
.bounds : Box3The bounds of the Octree. Compared to Octree#box, no margin is applied.
.box : Box3The base box with enclose the entire Octree.
.layers : LayersCan by used for layers configuration for refine testing.
.maxLevel : numberThe maximum level of the Octree. It defines the maximum hierarchical depth of the data structure.
.trianglesPerLeaf : numberThe number of triangles a leaf can store before it is split.

Methods

MethodDescription
.addTriangle( triangle : Triangle ) : OctreeAdds the given triangle to the Octree. The triangle vertices are clamped if they exceed the bounds of the Octree.
`.boxIntersect( box : Box3 ) : Objectboolean`
.build() : OctreeBuilds the Octree.
.calcBox() : OctreePrepares Octree#box for the build.
`.capsuleIntersect( capsule : Capsule ) : Objectboolean`
.clear() : OctreeClears the Octree by making it empty.
.fromGraphNode( group : Object3D ) : OctreeConstructs the Octree from the given 3D object.
.getBoxTriangles( box : Box3, triangles : Array.<Triangle> )Computes the triangles that potentially intersect with the given bounding box.
.getCapsuleTriangles( capsule : Capsule, triangles : Array.<Triangle> )Computes the triangles that potentially intersect with the given capsule.
.getRayTriangles( ray : Ray, triangles : Array.<Triangle> )Computes the triangles that potentially intersect with the given ray.
.getSphereTriangles( sphere : Sphere, triangles : Array.<Triangle> )Computes the triangles that potentially intersect with the given bounding sphere.
`.rayIntersect( ray : Ray ) : Objectboolean`
`.sphereIntersect( sphere : Sphere ) : Objectboolean`
.split( level : number ) : OctreeSplits the Octree. This method is used recursively when building the Octree.
`.triangleBoxIntersect( box : Box3, triangle : Triangle ) : Objectfalse`
`.triangleCapsuleIntersect( capsule : Capsule, triangle : Triangle ) : Objectfalse`
`.triangleSphereIntersect( sphere : Sphere, triangle : Triangle ) : Objectfalse`

Key Takeaways

  1. Most relevant properties: bounds, box, layers, maxLevel.
  2. Key methods: addTriangle, boxIntersect, build, calcBox.

Connects To