Exemplo n.º 1
0
        public static void DrawMeshEdges(KrablMesh.MeshEdges mesh, Transform transform, float normalLength = 0.0f, SkinnedMeshRenderer smr = null)
        {
            int numVerts = mesh.vertCount();
            int numEdges = mesh.edgeCount();

            Vector3[]   pts          = new Vector3[numVerts];
            Matrix4x4[] boneMatrices = null;
            Matrix4x4   mat          = new Matrix4x4();

            bool useBones = (smr != null) && (mesh.hasBoneWeights);

            if (useBones)
            {
                boneMatrices = BoneMatricesFromBonesAndBindposes(mesh.bindposes, smr.bones);
                for (int i = 0; i < numVerts; ++i)
                {
                    Vertex v = mesh.vertices[i];
                    DeformationMatrixForBoneWeight(v.boneWeight, boneMatrices, ref mat);
                    pts[i] = mat.MultiplyPoint3x4(v.coords);
                }
            }
            else
            {
                for (int i = 0; i < numVerts; ++i)
                {
                    Vertex v = mesh.vertices[i];
                    pts[i] = transform.TransformPoint(v.coords);
                }
            }

            for (int i = 0; i < numEdges; ++i)
            {
                if (mesh.IsEdgeValid(i))
                {
                    Edge  e   = mesh.edges[i];
                    Color col = Color.clear;
                    if (e.linkedFaces.Count <= 1)
                    {
                        col = Color.blue;
                    }
                    if (mesh.hasUV1 && mesh.IsEdgeUV1Seam(i))
                    {
                        col = Color.magenta;
                    }
                    if (e.crease > 0.0f)
                    {
                        col = Color.red;
                    }
                    if (col.a != 0.0f)
                    {
                        Gizmos.color = col;
                        Gizmos.DrawLine(pts[e.v[0]], pts[e.v[1]]);
                    }
                }
            }

            if (normalLength > 0.0f)
            {
                Gizmos.color = Color.white;
                int numFaces = mesh.faceCount();
                if (useBones == false)
                {
                    for (int i = 0; i < numFaces; ++i)
                    {
                        Face f = mesh.faces[i];
                        if (f.valid)
                        {
                            int[] vi = f.v;
                            for (int j = 0; j < f.cornerCount; ++j)
                            {
                                Vector3 n = f.vertexNormal[j];
                                n = transform.TransformDirection(n);
                                Vector3 p = pts[vi[j]];
                                Gizmos.DrawLine(p, p + n * normalLength);
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < numFaces; ++i)
                    {
                        Face f = mesh.faces[i];
                        if (f.valid)
                        {
                            int[] vi = f.v;
                            for (int j = 0; j < f.cornerCount; ++j)
                            {
                                int vindex = vi[j];
                                DeformationMatrixForBoneWeight(mesh.vertices[vindex].boneWeight, boneMatrices, ref mat);
                                Vector3 n = f.vertexNormal[j];
                                n = mat.MultiplyVector(n);
                                Vector3 p = pts[vindex];
                                Gizmos.DrawLine(p, p + n.normalized * normalLength);
                            }
                        }
                    }
                }
            }
        }