barycentric: Barycentric Coordinate Computation
Mathematical Foundation for Triangle Point Interpolation
Barycentric coordinates provide a coordinate system for expressing points relative to a triangle. For any point in the plane of a triangle, barycentric coordinates satisfy the constraint , where each component represents the weight of the corresponding triangle vertex.
The mathematical definition involves solving the linear system:
Where is the target point and , , are triangle vertices. The computation uses dot products to solve for the weights:
Two primary functions handle different coordinate computation scenarios:
barycentric(a, b, c)
computes normalized barycentric coordinates directly from three vertices. The computation projects vectors onto a coordinate system defined by the triangle edges.
barycentricFromPosition(a, b, c, pos)
determines barycentric coordinates of a specific position relative to the triangle using cross product area ratios:
const fragment = () => Scope(() => { const tri = Triangle({ a: vec3(-0.5, -0.4, 0), b: vec3(0.5, -0.4, 0), c: vec3(0, 0.6, 0) }) const pos = vec3(uv, 0) const bary = barycentricFromPosition(tri, pos) return vec4(bary, 1) })