JSPM

  • Created
  • Published
  • Downloads 534404
  • Score
    100M100P100Q202343F
  • License MIT

A BVH implementation to speed up raycasting against three.js meshes.

Package Exports

  • three-mesh-bvh

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (three-mesh-bvh) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

three-mesh-bvh

npm version lgtm code quality travis build

A BVH implementation to speed up raycasting against three.js meshes.

Demo Here!

screenshot

Casting 500 rays against an 80,000 polygon model at 60fps!

Use

Using pre-made functions

// Import via ES6 modules
import * as THREE from 'three';
import { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } 'three-mesh-bvh';

// Or UMD
const { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } = window.MeshBVHLib;


// Add the extension functions
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;

// Generate geometry and associated BVH
const geom = new THREE.TorusKnotBufferGeometry(10, 3, 400, 100);
const mesh = new THREE.Mesh(geom, material);
geom.computeBoundsTree();

Or manually building the BVH

// Import via ES6 modules
import * as THREE from 'three';
import { MeshBVH, acceleratedRaycast } 'three-mesh-bvh';

// Or UMD
const { MeshBVH, acceleratedRaycast } = window.MeshBVHLib;


// Add the raycast function. Assumes the BVH is available on
// the `boundsTree` variable
THREE.Mesh.prototype.raycast = acceleratedRaycast;

// ...

// Generate the BVH and use the newly generated index
geom.boundsTree = new MeshBVH(geom);
geom.setIndex(geom.boundsTree.index);

Exports

Split Strategy Constants

CENTER

Option for splitting each BVH node down the center of the longest axis of the bounds.

This is the fastest construction option but will yield a less optimal hierarchy.

AVERAGE

Option for splitting each BVH node at the average point along the longest axis for all triangle centroids in the bounds.

SAH

Option to use a Surface Area Heuristic to split the bounds optimally.

This is the slowest construction option.

MeshBVH

index : AttributeBuffer

The generated attribute buffer based on the original mesh index in an order sorted for storing bounds triangles. The BVH will no longer work correctly if this buffer is modified.

constructor(geometry : BufferGeometry, options : Object)

Constructs the bounds tree for the given geometry and produces a new index attribute buffer. The available options are

{
    // Which split strategy to use when constructing the BVH
    strategy: CENTER,

    // The maximum depth to allow the tree to build to
    // Setting this to a smaller trades raycast speed for better construction
    // time and less memory allocation
    maxDepth: 40,

    // The number of triangles to aim for in a leaf node
    maxLeafTris: 10,

    // Print out warnings encountered during tree construction
    verbose: true

}

raycast(mesh : Mesh, raycaster : Raycaster, ray : Ray, intersects : Array)

Adds all raycast triangle hits in unsorted order to the intersects array. It is expected that ray is in the frame of the mesh being raycast against and that the geometry on mesh is the same as the one used to generate the bvh.

raycastFirst(mesh : Mesh, raycaster : Raycaster, ray : Ray) : RaycastHit

Returns the first raycast hit in the model. This is typically much faster than returning all hits.

Extension Functions

computeBoundsTree(options : Object)

A pre-made BufferGeometry extension function that builds a new BVH, assigns it to boundsTree, and applies the new index buffer to the geometry. Comparable to computeBoundingBox and computeBoundingSphere.

THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;

disposeBoundsTree()

A BufferGeometry extension function that disposes of the BVH.

THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;

acceleratedRaycast(...)

An accelerated raycast function with the same signature as THREE.Mesh.raycast. Uses the BVH for raycasting if it's available otherwise it falls back to the built-in approach.

If the raycaster object being used has a property firstHitOnly set to true, then the raycasting will terminate as soon as it finds the closest intersection to the ray's origin and return only that intersection. This is typically several times faster than searching for all intersections.

THREE.Mesh.prototype.raycast = acceleratedRaycast;

Gotchas

  • This is intended to be used with complicated, high-poly meshes. With less complex meshes, the benefits are negligible.
  • A bounds tree can be generated for either an indexed or non-indexed BufferGeometry, but an index will be produced and retained as a side effect of the construction.
  • The bounds hierarchy is not dynamic, so geometry that uses morph targets cannot be used.
  • If the geometry is changed, then a call to computedBoundsTree() is required to update the bounds tree.