GLSL OBB Signed Distance Function Formula

This guide provides the Signed Distance Function (SDF) formula and GLSL implementation for an Oriented Bounding Box (OBB). You will learn how to transform a world-space point into the box's local coordinate system and apply the standard 3D box distance equation to evaluate the shortest signed distance from any point in space to the box surface.

The Mathematical Concept

An Oriented Bounding Box is defined by three components:

  1. Center position (\(C \in \mathbb{R}^3\))
  2. Half-extents (\(E \in \mathbb{R}^3\), representing half of the box's width, height, and depth)
  3. Orientation (represented either as a \(3\times3\) rotation matrix \(R\) or three orthonormal basis vectors)

Calculating the signed distance to an OBB relies on transforming the query point \(P\) into the local coordinate system of the box. Once transformed, the OBB behaves identically to an Axis-Aligned Bounding Box (AABB) centered at the origin.

The local point \(P_{\text{local}}\) is computed as: \[P_{\text{local}} = R^T \cdot (P - C)\]

For an axis-aligned box at the origin with half-extents \(E\), the signed distance is: \[d = \text{length}(\max(|P_{\text{local}}| - E, 0)) + \min(\max(q_x, \max(q_y, q_z)), 0)\] where \(q = |P_{\text{local}}| - E\).

GLSL Implementation

Below is the complete GLSL function using a \(3\times3\) rotation matrix for orientation:

// Signed Distance Function for an Oriented Bounding Box (OBB)
// p:           World-space query point
// center:      World-space center of the box
// halfExtents: Box dimensions divided by 2 (radii along each axis)
// rot:         3x3 rotation matrix representing the box's orientation
float sdOBB(vec3 p, vec3 center, vec3 halfExtents, mat3 rot) {
    // 1. Transform query point to the box's local space
    // Since rot is orthogonal, its inverse equals its transpose
    vec3 pLocal = transpose(rot) * (p - center);

    // 2. Compute distance to the box boundaries
    vec3 q = abs(pLocal) - halfExtents;

    // 3. Combine external and internal distances
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

Alternative: Implementation Using Basis Vectors

If orientation is defined by three normalized, orthogonal direction vectors (xAxis, yAxis, zAxis), project the relative vector using dot products:

float sdOBB(vec3 p, vec3 center, vec3 halfExtents, vec3 xAxis, vec3 yAxis, vec3 zAxis) {
    vec3 d = p - center;
    vec3 pLocal = vec3(dot(d, xAxis), dot(d, yAxis), dot(d, zAxis));

    vec3 q = abs(pLocal) - halfExtents;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

Output Interpretation

  • Positive value (\(> 0\)): The point is strictly outside the box. The value represents the Euclidean distance to the nearest surface point.
  • Zero (\(= 0\)): The point lies exactly on the boundary of the box.
  • Negative value (\(< 0\)): The point is strictly inside the box. The absolute value represents the shortest distance to the nearest face.