Ejemplo n.º 1
0
        /**
         * Remove @triangles from this mesh.
         */
        public static bool DeleteTriangles(qe_Mesh mesh, List <qe_Triangle> triangles)
        {
            List <qe_Triangle> trianglesToRemove = new List <qe_Triangle>(triangles);

            trianglesToRemove.Distinct();

            if (trianglesToRemove.Count == mesh.faces.Length)
            {
                Debug.LogWarning("Cannot delete every triangle on a mesh!");
                return(false);
            }

            int subMeshCount = mesh.cloneMesh.subMeshCount;

            for (int i = 0; i < subMeshCount; i++)
            {
                List <int> remove = new List <int>();
                List <int> tris   = mesh.GetIndices(i).ToList();

                for (int n = 0; n < tris.Count; n += 3)
                {
                    int index = trianglesToRemove.IndexOf(tris[n], tris[n + 1], tris[n + 2]);

                    if (index > -1)
                    {
                        remove.Add(n + 0);
                        remove.Add(n + 1);
                        remove.Add(n + 2);

                        trianglesToRemove.RemoveAt(index);
                    }
                }

                remove.Sort();

                List <int> rebuilt     = new List <int>();
                int        removeIndex = 0;

                for (int n = 0; n < tris.Count; n++)
                {
                    if (removeIndex < remove.Count && n == remove[removeIndex])
                    {
                        removeIndex++;
                        continue;
                    }

                    rebuilt.Add(tris[n]);
                }

                mesh.SetIndices(i, rebuilt.ToArray());
            }

            RemoveUnusedVertices(ref mesh.cloneMesh);

            mesh.CacheElements();
            return(true);
        }
Ejemplo n.º 2
0
        public static void Facetize(qe_Mesh qmesh)
        {
            Mesh mesh = qmesh.cloneMesh;

            int triangleCount = mesh.triangles.Length;

            bool boneWeights_isNull = mesh.boneWeights.NullOrEmpty();
            bool colors_isNull      = mesh.colors.NullOrEmpty();
            bool colors32_isNull    = mesh.colors32.NullOrEmpty();
            bool normals_isNull     = mesh.normals.NullOrEmpty();
            bool tangents_isNull    = mesh.tangents.NullOrEmpty();
            bool uv_isNull          = mesh.uv.NullOrEmpty();
            bool uv2_isNull         = mesh.uv2.NullOrEmpty();

#if UNITY_5
            bool uv3_isNull = mesh.uv3.NullOrEmpty();
            bool uv4_isNull = mesh.uv4.NullOrEmpty();
#endif
            bool vertices_isNull = mesh.vertices.NullOrEmpty();

            BoneWeight[] boneWeights = boneWeights_isNull ? null : new BoneWeight[triangleCount];
            Color[]      colors      = colors_isNull ? null : new Color[triangleCount];
            Color32[]    colors32    = colors32_isNull ? null : new Color32[triangleCount];
            Vector3[]    normals     = normals_isNull ? null : new Vector3[triangleCount];
            Vector4[]    tangents    = tangents_isNull ? null : new Vector4[triangleCount];
            Vector2[]    uv          = uv_isNull ? null : new Vector2[triangleCount];
            Vector2[]    uv2         = uv2_isNull ? null : new Vector2[triangleCount];
#if UNITY_5
            Vector2[] uv3 = uv3_isNull ? null : new Vector2[triangleCount];
            Vector2[] uv4 = uv4_isNull ? null : new Vector2[triangleCount];
#endif
            Vector3[] vertices = new Vector3[triangleCount];

            // cache mesh arrays because accessing them through the reference is slooooow
            Vector3[]    mVertices    = mesh.vertices;
            BoneWeight[] mBoneWeights = mesh.boneWeights;
            Color[]      mColors      = mesh.colors;
            Color32[]    mColors32    = mesh.colors32;
            Vector3[]    mNormals     = mesh.normals;
            Vector4[]    mTangents    = mesh.tangents;
            Vector2[]    mUv          = mesh.uv;
            Vector2[]    mUv2         = mesh.uv2;
#if UNITY_5
            Vector2[] mUv3 = mesh.uv3;
            Vector2[] mUv4 = mesh.uv4;
#endif

            int     index     = 0;
            int[][] triangles = new int[mesh.subMeshCount][];

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                triangles[i] = qmesh.GetIndices(i);

                for (int t = 0; t < triangles[i].Length; t++)
                {
                    int n = triangles[i][t];

                    if (!boneWeights_isNull)
                    {
                        boneWeights[index] = mBoneWeights[n];
                    }

                    if (!colors_isNull)
                    {
                        colors[index] = mColors[n];
                    }

                    if (!colors32_isNull)
                    {
                        colors32[index] = mColors32[n];
                    }

                    if (!normals_isNull)
                    {
                        normals[index] = mNormals[n];
                    }

                    if (!tangents_isNull)
                    {
                        tangents[index] = mTangents[n];
                    }

                    if (!uv_isNull)
                    {
                        uv[index] = mUv[n];
                    }

                    if (!uv2_isNull)
                    {
                        uv2[index] = mUv2[n];
                    }

#if UNITY_5
                    if (!uv3_isNull)
                    {
                        uv3[index] = mUv3[n];
                    }

                    if (!uv4_isNull)
                    {
                        uv4[index] = mUv4[n];
                    }
#endif
                    if (!vertices_isNull)
                    {
                        vertices[index] = mVertices[n];
                    }


                    triangles[i][t] = index;
                    index++;
                }
            }

            mesh.vertices    = vertices;
            mesh.boneWeights = boneWeights;
            mesh.colors      = colors;
            mesh.colors32    = colors32;
            mesh.normals     = normals;
            mesh.tangents    = tangents;
            mesh.uv          = uv;
            mesh.uv2         = uv2;
#if UNITY_5
            mesh.uv3 = uv3;
            mesh.uv4 = uv4;
#endif

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                qmesh.SetIndices(i, triangles[i]);
            }

            mesh.RecalculateNormals();

            qmesh.CacheElements();
        }