Skip to main content

signedDistance: Directional Distance to Triangle Surface

Geometric Proximity with Surface Orientation

The signed distance function determines not only how far a point lies from a triangle surface, but also whether the point exists above or below the triangle plane. This directional information proves essential for spatial calculations involving solid geometry and volumetric operations.

Unlike standard distance calculations that return only positive values, signed distance incorporates surface normal direction to distinguish interior and exterior regions. Points positioned on the positive side of the triangle normal yield positive distances, while points on the negative side produce negative values.

Mathematical Foundation

The signed distance calculation follows this sequence:

dsigned(p,T)=ppclosestsign(nppclosestppclosest)d_{signed}(p, T) = |p - p_{closest}| \cdot \text{sign}(\vec{n} \cdot \frac{p - p_{closest}}{|p - p_{closest}|})

Where:

  • TT represents the triangle with vertices aa, bb, cc
  • pclosestp_{closest} denotes the nearest point on triangle surface to pp
  • n\vec{n} indicates the triangle surface normal vector
  • The sign function determines spatial orientation relative to the surface

The surface normal calculation employs the cross product of two edge vectors:

n=normalize((ba)×(ca))\vec{n} = \text{normalize}((b - a) \times (c - a))

Implementation Analysis

The TSL implementation provides two function variants:

FunctionParametersDescription
signedDistanceWithNormaltriangle, normal, pointDirect calculation with precomputed normal
signedDistancetriangle, pointConvenience function computing normal internally

Both functions utilize the closest point calculation to establish the nearest surface position, then apply directional analysis through vector dot product operations.

Live Editor
const fragment = () => Scope(() => {
      const tri = Triangle({
              a: vec3(-0.5, -0.3, 0),
              b: vec3(0.5, -0.3, 0),
              c: vec3(0, 0.4, 0)
      })
      const point = vec3(uv.mul(2).sub(1), 0.2)
      const dist = signedDistance(tri, point)
      const color = vec3(0.8, 0.2, 0.2).select(vec3(0.2, 0.2, 0.8), dist.greaterThan(0))
      return vec4(color.mul(dist.abs().mul(8).add(0.3)), 1)
})

The visualization demonstrates spatial regions through color coding. Red areas indicate positive signed distances (points above triangle surface), while blue areas show negative distances (points below surface). Color intensity reflects distance magnitude, creating clear spatial boundaries around the triangle geometry.