Vector3Barycenter function

Vector3 Vector3Barycenter(
  1. Vector3 p,
  2. Vector3 a,
  3. Vector3 b,
  4. Vector3 c,
)

Barycentric coordinates (u, v, w) of p relative to triangle (a, b, c).

Implementation

Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) {
  final v0 = b - a, v1 = c - a, v2 = p - a;
  final d00 = v0.dot(v0), d01 = v0.dot(v1), d11 = v1.dot(v1);
  final d20 = v2.dot(v0), d21 = v2.dot(v1);
  final denom = d00 * d11 - d01 * d01;
  final bv = (d11 * d20 - d01 * d21) / denom;
  final bw = (d00 * d21 - d01 * d20) / denom;
  return Vector3(1 - bv - bw, bv, bw);
}