THREE.js raycasting very slow against single > 500k poly (faces) object, line intersection with globe

Do not use three.js Raycaster.

Consider Ray.js that offers function intersectTriangle(a, b, c, backfaceCulling, target)

Suggested optimizations:

  1. If player starts from some known positions ⇒ you must know his initial height, − no need to raycast (or just do one time full mesh slow intersection)

  2. if player moves with small steps ⇒ next raycast will most likely intersect the same face as before.

Optimization #1 − remember previous face, and raycast it first.

  1. if player does not jump ⇒ next raycast will most likely intersect the adjacent face to the face where player was before.

Optimization #2 − build up a cache, so that given a face idx you could retrieve adjacent faces in O(1) time.

This cache may be loaded from the file, if your planet is not generated in real time.


So with my approach on each move you do O(1) read operation from cache and raycast 1-6 faces.

Win!


For a complex object it takes forever. It takes ~200ms for an object with ~1m polys (faces) (1024x512 segments sphere). Does raycasting cast against every single face ?

Out of the box THREE.js does check every triangle when performing a raycast against a mesh and there are no acceleration structures built into THREE.

I've worked with others on the three-mesh-bvh package (github, npm) to help address this problem, though, which may help you get up to the speeds your looking for. Here's how you might use it:

import * as THREE from 'three';
import { MeshBVH, acceleratedRaycast } from 'three-mesh-bvh';

THREE.Mesh.prototype.raycast = acceleratedRaycast;

// ... initialize the scene...

globeMesh.geometry.boundsTree = new MeshBVH(globeMesh.geometry);

// ... initialize raycaster...

// Optional. Improves the performance of the raycast
// if you only need the first collision
raycaster.firstHitOnly = true;
const intersects = raycaster.intersectObject(globeMesh, true);

// do something with the intersections

There are some caveats mentioned in the README so keep those in mind (the mesh index is modified, only nonanimated BufferGeometry is supported, etc). And there's still some memory optimization that could be done but there are some tweakable options to help tune that.

I'll be interested to hear how this works for you! Feel free to leave feedback in the issues on how to improve the package, as well. Hope that helps!


I think you should pre-render the height map of your globe into a texture, assuming your terrain is not dynamic. Read all of it into a typed array, and then whenever your player moves, you only need to back-project her coordinates into that texture, query it, offset and multiply and you should get what you need in O(1) time.

It's up to you how you generate that height map. Actually if you have a bumpy globe, then you should probably start with height map in the first place, and use that in your vertex shader to render the globe (with the input sphere being perfectly smooth). Then you can use the same height map to query the player's Z.