lineSDF: Linear Segment Distance Field
Point-to-Line Distance Calculation in 2D and 3D Space
The lineSDF
function family computes the shortest distance from any point to a line segment defined by two endpoints. This primitive enables line rendering, path tracing, and geometric analysis in both 2D and 3D coordinate systems.
Mathematical Foundation
2D Line Segment Distance
The 2D line distance uses orthogonal projection onto the line segment:
where:
3D Line Distance
The 3D version uses the cross product for perpendicular distance:
Function Signatures
lineSDF2D (2D Line Segment)
Parameter | Type | Description |
---|---|---|
st | vec2 | Sample point position |
a | vec2 | Line segment start point |
b | vec2 | Line segment end point |
lineSDF3D (3D Line)
Parameter | Type | Description |
---|---|---|
p | vec3 | Sample point position |
a | vec3 | Line start point |
b | vec3 | Line end point |
Implementation Demonstrations
ライブエディター
const fragment = () => { const a = vec2(0.2, 0.3).add(iTime.sin().mul(0.1)) const b = vec2(0.8, 0.7).add(iTime.mul(1.3).cos().mul(0.1)) const dist = lineSDF2D(uv, a, b) const line = float(0.02).smoothstep(0.005, dist) const endpoints = distance(uv, a).step(0.03).max(distance(uv, b).step(0.03)) return vec4(line.mul(vec3(0.8, 0.4, 1)).add(endpoints.mul(vec3(1, 0.2, 0.3))), 1) }