triangleDistanceSq: Point-to-Triangle Distance Calculation
Hybrid Distance Computation Using Edge and Plane Methods
The triangle distance function computes the shortest squared distance from a point to a triangle surface using a hybrid approach combining edge projection and plane distance calculations.
The algorithm determines point classification relative to the triangle using cross product sign testing:
Where represents the triangle normal vector.
Edge Distance Calculation
For points outside the triangle (), distance is computed using edge projections:
The saturate function clamps projection parameters to , ensuring closest points lie within edge segments.
Plane Distance Calculation
For points inside the triangle projection (), distance uses perpendicular plane distance:
This computes the squared perpendicular distance from the point to the triangle plane.
ライブエディター
const fragment = () => Scope(() => { const tri = Triangle({ a: vec3(-0.4, -0.3, 0), b: vec3(0.4, -0.3, 0), c: vec3(0, 0.5, 0) }) const pos = vec3(uv.mul(2).sub(1), 0) const dist = distanceSq(tri, pos) const field = dist.sqrt().mul(8) const gradient = field.fract() return vec4(vec3(gradient), 1) })