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:
Where:
- represents the triangle with vertices , ,
- denotes the nearest point on triangle surface to
- 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:
Implementation Analysis
The TSL implementation provides two function variants:
Function | Parameters | Description |
---|---|---|
signedDistanceWithNormal | triangle, normal, point | Direct calculation with precomputed normal |
signedDistance | triangle, point | Convenience 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.
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.