_setInfluenceClosestPointInTriangles()

in src/core/animpack/state/Blend2dState.js [295:344]


  _setInfluenceClosestPointInTriangles(p) {
    let globalClosestPoint = null;
    let globalMinDist = Number.POSITIVE_INFINITY;
    let closestTriangle = -1;

    this._triangles.forEach((triangle, index) => {
      const pointA = MathUtils.closestPointOnLine(
        this._vertices[triangle[0]],
        this._vertices[triangle[1]],
        p
      );
      const pointB = MathUtils.closestPointOnLine(
        this._vertices[triangle[1]],
        this._vertices[triangle[2]],
        p
      );
      const pointC = MathUtils.closestPointOnLine(
        this._vertices[triangle[2]],
        this._vertices[triangle[0]],
        p
      );

      const distA = MathUtils.distanceSquared(pointA, p);
      const distB = MathUtils.distanceSquared(pointB, p);
      const distC = MathUtils.distanceSquared(pointC, p);

      let localClosestPoint = pointC;
      let localMinDist = distC;

      if (distA < localMinDist) {
        localClosestPoint = pointA;
        localMinDist = distA;
      }
      if (distB < localMinDist) {
        localClosestPoint = pointB;
        localMinDist = distB;
      }

      if (localMinDist < globalMinDist) {
        globalMinDist = localMinDist;
        globalClosestPoint = [...localClosestPoint];
        closestTriangle = index;
      }
    });

    this._setInfluenceTriangle(
      this._triangles[closestTriangle],
      globalClosestPoint
    );
  }