static_getBarycoord static method

Vector3 static_getBarycoord(
  1. dynamic point,
  2. Vector3 a,
  3. Vector3 b,
  4. Vector3 c,
  5. Vector3 target,
)

Implementation

static Vector3 static_getBarycoord(
    point, Vector3 a, Vector3 b, Vector3 c, Vector3 target) {
  _v0.subVectors(c, a);
  _v1.subVectors(b, a);
  _v2.subVectors(point, a);

  var dot00 = _v0.dot(_v0);
  var dot01 = _v0.dot(_v1);
  var dot02 = _v0.dot(_v2);
  var dot11 = _v1.dot(_v1);
  var dot12 = _v1.dot(_v2);

  var denom = (dot00 * dot11 - dot01 * dot01);

  // collinear or singular triangle
  if (denom == 0) {
    // arbitrary location outside of triangle?
    // not sure if this is the best idea, maybe should be returning null
    return target.set(-2, -1, -1);
  }

  var invDenom = 1 / denom;
  var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  var v = (dot00 * dot12 - dot01 * dot02) * invDenom;

  // barycentric coordinates must always sum to 1
  return target.set(1 - u - v, v, u);
}