/// <summary> /// Draws the triangle. /// </summary> /// <param name="triangle">Triangle.</param> /// <param name="color">Color.</param> /// <param name="duration">Duration.</param> public static void DrawTriangle(Triangle3d triangle, Color color, float duration) { DrawLine(triangle.lineA, color, duration); DrawLine(triangle.lineB, color, duration); DrawLine(triangle.lineC, color, duration); }
/// <summary> /// Fills the output array with triangles from the mesh. /// </summary> /// <param name="mesh">Mesh.</param> /// <param name="transform">Transform.</param> /// <param name="output">Output.</param> public static void ToTriangles(Mesh mesh, Matrix4x4 transform, ref Triangle3d[] output) { Vector3[] verts = mesh.vertices; int[] triangles = mesh.triangles; int triangleCount = triangles.Length / 3; Array.Resize(ref output, triangleCount); for (int index = 0; index < triangleCount; index++) { int vert1 = triangles[index * 3]; int vert2 = triangles[index * 3 + 1]; int vert3 = triangles[index * 3 + 2]; Vector3 position1 = transform * verts[vert1]; Vector3 position2 = transform * verts[vert2]; Vector3 position3 = transform * verts[vert3]; output[index] = new Triangle3d(position1, position2, position3); } }
/// <summary> /// Splits the quad into two triangles. Note, corners ABCD are assumed to be /// in order. /// </summary> /// <param name="a">Corner A.</param> /// <param name="b">Corner B.</param> /// <param name="c">Corner C.</param> /// <param name="d">Corner D.</param> /// <param name="t1">Triangle 1.</param> /// <param name="t2">Triangle 1.</param> public static void SplitQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 d, out Triangle3d t1, out Triangle3d t2) { float diagonal1 = (a - c).sqrMagnitude; float diagonal2 = (b - d).sqrMagnitude; if (diagonal1 < diagonal2) { t1 = new Triangle3d(a, b, c); t2 = new Triangle3d(a, c, d); } else { t1 = new Triangle3d(a, b, d); t2 = new Triangle3d(b, c, d); } }
/// <summary> /// Fills the output array with triangles from the mesh. /// </summary> /// <param name="mesh">Mesh.</param> /// <param name="output">Output.</param> public static void ToTriangles(Mesh mesh, ref Triangle3d[] output) { ToTriangles(mesh, Matrix4x4.identity, ref output); }
/// <summary> /// Given the triangle a,b,c normals aN,bN,cN this method returns /// an interpolated normal at the point. /// </summary> /// <returns>The interpolated normal.</returns> /// <param name="nA">Corner A normal.</param> /// <param name="nB">Corner B normal.</param> /// <param name="nC">Corner C normal.</param> /// <param name="point">Point.</param> public Vector3 InterpolatedNormal(Vector3 nA, Vector3 nB, Vector3 nC, Vector3 point) { float totalArea = area; float areaB = new Triangle3d(m_PointA, point, m_PointC).area; float areaC = new Triangle3d(m_PointA, point, m_PointB).area; float areaA = totalArea - areaB - areaC; // Calculate coefficients float c1 = areaA / totalArea; float c2 = areaB / totalArea; float c3 = areaC / totalArea; return new Vector3(nA.x * c1 + nB.x * c2 + nC.x * c3, nA.y * c1 + nB.y * c2 + nC.y * c3, nA.z * c1 + nB.z * c2 + nC.z * c3); }