Advertisement
Bunny83

Barycentric.cs

Jan 6th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. public struct Barycentric
  4. {
  5.     public float u;
  6.     public float v;
  7.     public float w;
  8.     public Barycentric(float aU, float aV, float aW)
  9.     {
  10.         u = aU;
  11.         v = aV;
  12.         w = aW;
  13.     }
  14.     public Barycentric(Vector2 aV1, Vector2 aV2, Vector2 aV3, Vector2 aP)
  15.     {
  16.         Vector2 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
  17.         float aLen = a.x * a.x + a.y * a.y;
  18.         float bLen = b.x * b.x + b.y * b.y;
  19.         float ab = a.x * b.x + a.y * b.y;
  20.         float ac = a.x * c.x + a.y * c.y;
  21.         float bc = b.x * c.x + b.y * c.y;
  22.         float d = aLen * bLen - ab * ab;
  23.         u = (aLen * bc - ab * ac) / d;
  24.         v = (bLen * ac - ab * bc) / d;
  25.         w = 1.0f - u - v;
  26.     }
  27.  
  28.     public Barycentric(Vector3 aV1, Vector3 aV2, Vector3 aV3, Vector3 aP)
  29.     {
  30.         Vector3 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
  31.         float aLen = a.x * a.x + a.y * a.y + a.z * a.z;
  32.         float bLen = b.x * b.x + b.y * b.y + b.z * b.z;
  33.         float ab = a.x * b.x + a.y * b.y + a.z * b.z;
  34.         float ac = a.x * c.x + a.y * c.y + a.z * c.z;
  35.         float bc = b.x * c.x + b.y * c.y + b.z * c.z;
  36.         float d = aLen * bLen - ab * ab;
  37.         u = (aLen * bc - ab * ac) / d;
  38.         v = (bLen * ac - ab * bc) / d;
  39.         w = 1.0f - u - v;
  40.     }
  41.     public Barycentric(Vector4 aV1, Vector4 aV2, Vector4 aV3, Vector4 aP)
  42.     {
  43.         Vector4 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
  44.         float aLen = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
  45.         float bLen = b.x * b.x + b.y * b.y + b.z * b.z + b.w * b.w;
  46.         float ab = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  47.         float ac = a.x * c.x + a.y * c.y + a.z * c.z + a.w * c.w;
  48.         float bc = b.x * c.x + b.y * c.y + b.z * c.z + b.w * c.w;
  49.         float d = aLen * bLen - ab * ab;
  50.         u = (aLen * bc - ab * ac) / d;
  51.         v = (bLen * ac - ab * bc) / d;
  52.         w = 1.0f - u - v;
  53.     }
  54.     public Barycentric(Color aV1, Color aV2, Color aV3, Color aP)
  55.     {
  56.         Color a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
  57.         float aLen = a.r * a.r + a.g * a.g + a.b * a.b;
  58.         float bLen = b.r * b.r + b.g * b.g + b.b * b.b;
  59.         float ab = a.r * b.r + a.g * b.g + a.b * b.b;
  60.         float ac = a.r * c.r + a.g * c.g + a.b * c.b;
  61.         float bc = b.r * c.r + b.g * c.g + b.b * c.b;
  62.         float d = aLen * bLen - ab * ab;
  63.         u = (aLen * bc - ab * ac) / d;
  64.         v = (bLen * ac - ab * bc) / d;
  65.         w = 1.0f - u - v;
  66.     }
  67.  
  68.     public bool IsInside
  69.     {
  70.         get
  71.         {
  72.             return (u >= 0.0f) && (u <= 1.0f) && (v >= 0.0f) && (v <= 1.0f) && (w >= 0.0f); //(w <= 1.0f)
  73.         }
  74.     }
  75.     public Vector2 Interpolate(Vector2 v1, Vector2 v2, Vector2 v3)
  76.     {
  77.         return v1 * u + v2 * v + v3 * w;
  78.     }
  79.     public Vector3 Interpolate(Vector3 v1, Vector3 v2, Vector3 v3)
  80.     {
  81.         return v1 * u + v2 * v + v3 * w;
  82.     }
  83.     public Vector4 Interpolate(Vector4 v1, Vector4 v2, Vector4 v3)
  84.     {
  85.         return v1 * u + v2 * v + v3 * w;
  86.     }
  87.     public Color Interpolate(Color v1, Color v2, Color v3)
  88.     {
  89.         return v1 * u + v2 * v + v3 * w;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement