How to determine whether a line segment is partially contained in (intersects) a frustum in THREE.js?

180 views Asked by At

I need to be able to determine whether the straight line between two points (segment), crosses the space inside a frustum (intersects the frustum). I've looked into the APIs offered by THREE.js and haven't been able to find it.

What I've tried

  • use containsPoint api for the two points of the line. Obviously not good enough, as this requires the entire segment to be contained in the frustum
  • use intersectsBox, with a 1 dimensional box which mimics a segment. The limitation is that Box3 is axis-aligned by default and it would only work with axis-aligned segments.
1

There are 1 answers

0
Nick Kallen On

Adding a method like this frustum could work:

    intersectsLine(line: THREE.Line3): boolean {
        for (const plane of this.planes) {
            const p1 = plane.distanceToPoint(line.start);
            const p2 = plane.distanceToPoint(line.end);
            const inside = p1 >= 0 && p2 >= 0;
            if (inside) continue;
            const entering = p1 < 0 && p2 >= 0;
            const leaving = p1 >= 0 && p2 < 0;
            plane.intersectLine(line, _i);
            if (entering) line.start.copy(_i);
            else if (leaving) line.end.copy(_i);
        }
        return this.containsPoint(line.start) || this.containsPoint(line.end);
    }

...

const _i = new THREE.Vector3();

There may be floating point precision issues, but I think this approach works(?)