Compute the intersection of a cube with a plane in Three.js

366 Views Asked by At

Given a plane and a cube, I'd like to know whether they intersect. If they do, I'd also like to know:

  1. do their intersection form a triangle, a parallelogram or an hexagon

    • or a point, or a segment in the degenerated cases
  2. what are the coordinates of the vertices of the intersection

    2.1. in the standard frame of reference

    2.2. in the cube's frame of reference

Note:

  • The cube is a new THREE.BoxGeometry(1, 1, 1) which has undergone translation and rotation through applyMatrix4 of a translation and rotation matrix
  • The plane is an arbitrary new THREE.Plane()
1

There are 1 best solutions below

0
Mathieu CAROFF On

Looking through the Three.js documentation, I found the following:

  • Plane has a method intersectBox which tells whether or not the plane intersect with a given Box3.
  • More importantly, Plane has a method intersectLine which tells where a given Line3 intersect the plane.
  • Three.js examples include a class ConvexHull which builds a convex polyhedron from a set of vertices:
new ConvexHull().setFromPoints(points).faces

So a possible approach to the problem would be to:

  1. Break down the cube into 12 lines, one for each edge,
  2. check each line for intersection against the plane. If the line intersect, add the intersection point to the convex hull

I ended up not using the convex hull approach. I just create all the possible faces thanks to a triple nested loop over the intersection points.