Tuesday, July 14, 2020

GetBarycentric Find UV coordinates of mesh without a raycast

  1. public static Vector3 GetBarycentric(Vector3 aV1, Vector3 aV2, Vector3 aV3, Vector3 aP)
  2. {
  3. Vector3 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
  4. float aLen = a.x * a.x + a.y * a.y + a.z * a.z;
  5. float bLen = b.x * b.x + b.y * b.y + b.z * b.z;
  6. float ab = a.x * b.x + a.y * b.y + a.z * b.z;
  7. float ca = a.x * c.x + a.y * c.y + a.z * c.z;
  8. float cb = b.x * c.x + b.y * c.y + b.z * c.z;
  9. float d = aLen * bLen - ab * ab;
  10. Vector3 B = new Vector3((aLen * cb - ab * ca), (bLen * ca - ab * cb)) /d;
  11. B.z = 1.0f - B.x - B.y;
  12. return B;
  13. }

No comments:

Post a Comment