Algorithms to Align glTF Furniture on 3D Planes

Automatically placing 3D glTF furniture flat against detected real-world surfaces is a core requirement of Augmented Reality (AR) and spatial computing applications. This process relies on a combination of plane-detection algorithms, geometric transformation pipelines, and bounding volume analysis. By sampling surface normals from spatial sensors and calculating rotational transforms, systems ensure that digital assets conform to physical floors, walls, and tabletops without clipping or hovering.

1. Raycasting and Hit-Testing

The initial placement process begins with raycasting. When a user selects a location on a screen or spatial interface, a ray is projected from the virtual camera through the screen coordinates into the 3D reconstructed scene.

  • Hit-Test Execution: Frameworks like ARCore, ARKit, and WebXR intersect this ray with detected planar geometry generated from SLAM (Simultaneous Localization and Mapping).
  • Pose Extraction: The hit-test returns a 6-DoF (Degrees of Freedom) pose consisting of a 3D coordinate vector \((x, y, z)\) and an orientation representing the plane's surface normal.

2. Vector Alignment via Quaternions

Once the target surface normal is extracted, the application must compute the rotational transformation required to align the glTF asset's bottom surface to that normal.

  • Two-Vector Rotation: The algorithm defines the model’s default local orientation—typically the positive Y-axis vector \((0, 1, 0)\) representing "up."
  • Quaternion Calculation: Using the dot product and cross product of the model’s up-vector (\(\vec{u}\)) and the detected plane’s surface normal (\(\vec{n}\)), the algorithm computes a rotation quaternion: \[\theta = \arccos(\vec{u} \cdot \vec{n})\] \[\vec{axis} = \frac{\vec{u} \times \vec{n}}{\|\vec{u} \times \vec{n}\|}\]
  • Implementation: In 3D rendering engines (such as Three.js or Babylon.js), this is commonly executed via functions like Quaternion.setFromUnitVectors(objectUp, planeNormal).

3. Oriented Bounding Box (OBB) and Pivot Correction

glTF models often have inconsistent origin (pivot) points. A model's pivot might be positioned at its geometric center, top, or offset arbitrarily, which can cause the furniture to sink into the floor or hover above it.

  • Bottom Face Identification: The algorithm parses the glTF's mesh vertices to generate an Oriented Bounding Box (OBB) or an Axis-Aligned Bounding Box (AABB).
  • Offset Compensation: The lowest point along the model's vertical axis (\(Y_{\text{min}}\)) is calculated. The system then applies a translation offset along the plane normal vector: \[\vec{P}_{\text{adjusted}} = \vec{P}_{\text{hit}} - (\vec{n} \times |Y_{\text{min}}|)\] This translation ensures that the lowest contact patch of the furniture rests directly on the mathematical plane.

4. Gravity Alignment and Yaw Constraints

While the bottom of the furniture must match the surface normal, horizontal furniture (such as chairs and tables) must also account for gravity.

  • Yaw Isolation: For horizontal planes, the surface normal is parallel to gravity. The algorithm locks the pitch and roll to match the plane normal and isolates the yaw axis (rotation around the normal) to allow user rotation or alignment with adjacent walls.
  • Vertical Plane Snapping (Walls): When placing wall-mounted glTF items (such as shelves or artwork), the algorithm aligns the asset's rear bounding plane to the vertical surface normal while constraining the asset's local "up" vector to remain parallel to real-world gravity.

5. RANSAC and ICP for Mesh Refinement

When working with dense raw point clouds or non-planar triangle meshes rather than pre-calculated ARKit/ARCore planes, point-cloud fitting algorithms are used directly:

  • RANSAC (Random Sample Consensus): Iteratively samples subsets of points from a spatial mesh to find the mathematical equation of the dominant plane within a region, filtering out sensor noise.
  • Iterative Closest Point (ICP): Used for advanced contact-surface fitting, ICP minimizes the distance between the contact vertices of the glTF model and the scanned surface points, allowing complex multi-legged furniture to find stable equilibrium on slightly uneven surfaces.