Ejemplo n.º 1
0
 public void LOD(float lod)
 {
     for (int i = 0; i < meshlist.Count; i++)
     {
         Mesh original = originalMeshs[meshlist[i]];
         if (meshlist[i] is SkinnedMeshRenderer)
         {
             SkinnedMeshRenderer smr = meshlist[i] as SkinnedMeshRenderer;
             if (lod > 0f)
             {
                 LODMaker.MakeLODMesh(original, smr, lod);
             }
             else
             {
                 smr.sharedMesh = original;
             }
         }
         else
         {
             MeshFilter mf = meshlist[i] as MeshFilter;
             if (lod > 0f)
             {
                 LODMaker.MakeLODMesh(original, mf, lod);
             }
             else
             {
                 mf.sharedMesh = original;
             }
         }
     }
 }
Ejemplo n.º 2
0
 public static Mesh MakeLODMesh(Mesh orig, float aMaxWeight, bool recalcNormals, [Optional, DefaultParameterValue(1f)] float removeSmallParts, [Optional, DefaultParameterValue(1f)] float protectNormals, [Optional, DefaultParameterValue(1f)] float protectUvs, [Optional, DefaultParameterValue(1f)] float protectSubMeshesAndSharpEdges, [Optional, DefaultParameterValue(1f)] float smallTrianglesFirst)
 {
     return(LODMaker.MakeLODMesh(orig, aMaxWeight, removeSmallParts, protectNormals, protectUvs, protectSubMeshesAndSharpEdges, smallTrianglesFirst, recalcNormals, false));
 }
Ejemplo n.º 3
0
        public static Mesh CopyAndRemoveSubmeshes(Mesh orig, int[] submeshesToRemove)
        {
            Mesh mesh = UnityEngine.Object.Instantiate <Mesh>(orig);
            List <List <int> > subMeshes = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     vs        = new List <Vector3>(orig.vertices);
            List <Vector3>     ns        = new List <Vector3>(orig.vertexCount);
            List <Vector2>     list4     = new List <Vector2>(orig.vertexCount);
            List <Vector2>     list5     = new List <Vector2>();
            List <Vector2>     list6     = new List <Vector2>();
            List <Vector2>     list7     = new List <Vector2>();
            List <Color32>     list8     = new List <Color32>();
            List <BoneWeight>  bws       = new List <BoneWeight>();

            ns.AddRange(orig.normals);
            list4.AddRange(orig.uv);
            list5.AddRange(orig.uv2);
            list6.AddRange(orig.uv3);
            list7.AddRange(orig.uv4);
            list8.AddRange(orig.colors32);
            bws.AddRange(orig.boneWeights);
            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                bool flag = false;
                for (int k = 0; k < submeshesToRemove.Length; k++)
                {
                    if (submeshesToRemove[k] == i)
                    {
                        flag = true;
                    }
                }
                if (!flag)
                {
                    List <int> item = new List <int>();
                    item.AddRange(mesh.GetTriangles(i));
                    subMeshes.Add(item);
                }
            }
            LODMaker.RemoveUnusedVertices(vs, ns, list4, list5, list6, list7, list8, bws, subMeshes);
            mesh.uv4         = null;
            mesh.uv3         = null;
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = vs.ToArray();
            if (ns.Count > 0)
            {
                mesh.normals = ns.ToArray();
            }
            if (list4.Count > 0)
            {
                mesh.uv = list4.ToArray();
            }
            if (list5.Count > 0)
            {
                mesh.uv2 = list5.ToArray();
            }
            if (list6.Count > 0)
            {
                mesh.uv3 = list6.ToArray();
            }
            if (list7.Count > 0)
            {
                mesh.uv4 = list7.ToArray();
            }
            if (list8.Count > 0)
            {
                mesh.colors32 = list8.ToArray();
            }
            if (bws.Count > 0)
            {
                mesh.boneWeights = bws.ToArray();
            }
            mesh.subMeshCount = subMeshes.Count;
            for (int j = 0; j < subMeshes.Count; j++)
            {
                mesh.SetTriangles(subMeshes[j].ToArray(), j);
            }
            if ((ns == null) || (ns.Count <= 0))
            {
                mesh.RecalculateNormals();
            }
            RecalculateTangents(mesh);
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 4
0
        public static Mesh CopyAndRemoveHiddenTriangles(Mesh orig, int subMeshIdx, Matrix4x4 localToWorldMatrix, Mesh[] hidingMeshes, int[] hidingSubMeshes, Matrix4x4[] hidingLocalToWorldMatrices, [Optional, DefaultParameterValue(0.01f)] float maxRemoveDistance)
        {
            if (subMeshIdx >= orig.subMeshCount)
            {
                return(null);
            }
            if (hidingMeshes.Length <= 0)
            {
                return(null);
            }
            if ((hidingMeshes.Length != hidingSubMeshes.Length) || (hidingMeshes.Length != hidingLocalToWorldMatrices.Length))
            {
                return(null);
            }
            Mesh mesh = UnityEngine.Object.Instantiate <Mesh>(orig);
            List <List <int> > subMeshes = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     vs        = new List <Vector3>(orig.vertices);
            List <Vector3>     ns        = new List <Vector3>(orig.vertexCount);
            List <Vector2>     list4     = new List <Vector2>(orig.vertexCount);
            List <Vector2>     list5     = new List <Vector2>();
            List <Vector2>     list6     = new List <Vector2>();
            List <Vector2>     list7     = new List <Vector2>();
            List <Color32>     list8     = new List <Color32>();
            List <BoneWeight>  bws       = new List <BoneWeight>();

            ns.AddRange(orig.normals);
            if ((ns == null) || (ns.Count <= 0))
            {
                orig.RecalculateNormals();
                ns.AddRange(orig.normals);
            }
            list4.AddRange(orig.uv);
            list5.AddRange(orig.uv2);
            list6.AddRange(orig.uv3);
            list7.AddRange(orig.uv4);
            list8.AddRange(orig.colors32);
            bws.AddRange(orig.boneWeights);
            for (int i = 0; i < orig.subMeshCount; i++)
            {
                List <int> item = new List <int>();
                item.AddRange(orig.GetTriangles(i));
                subMeshes.Add(item);
            }
            List <int>     list11   = subMeshes[subMeshIdx];
            List <Vector3> hidingVs = new List <Vector3>();
            List <int>     hidingTs = new List <int>();
            Mesh           mesh2    = null;
            int            num2     = 0;
            int            length   = 0;

            for (int j = 0; j < hidingMeshes.Length; j++)
            {
                Mesh  mesh3     = hidingMeshes[j];
                int[] triangles = mesh3.GetTriangles(hidingSubMeshes[j]);
                if (mesh2 != mesh3)
                {
                    num2 += length;
                }
                for (int num5 = 0; num5 < triangles.Length; num5++)
                {
                    hidingTs.Add(triangles[num5] + num2);
                }
                if (mesh2 != mesh3)
                {
                    Matrix4x4 matrixx  = hidingLocalToWorldMatrices[j];
                    Vector3[] vertices = mesh3.vertices;
                    for (int num6 = 0; num6 < vertices.Length; num6++)
                    {
                        hidingVs.Add(matrixx.MultiplyPoint3x4(vertices[num6]));
                    }
                    length = vertices.Length;
                    mesh2  = mesh3;
                }
            }
            List <Vector3> triMinCorners = new List <Vector3>();
            List <Vector3> triMaxCorners = new List <Vector3>();

            for (int k = 0; k < hidingTs.Count; k += 3)
            {
                Vector3 vector  = hidingVs[hidingTs[k]];
                Vector3 vector2 = hidingVs[hidingTs[k + 1]];
                Vector3 vector3 = hidingVs[hidingTs[k + 2]];
                float   x       = Mathf.Min(Mathf.Min(vector.x, vector2.x), vector3.x);
                float   y       = Mathf.Min(Mathf.Min(vector.y, vector2.y), vector3.y);
                triMinCorners.Add(new Vector3(x, y, Mathf.Min(Mathf.Min(vector.z, vector2.z), vector3.z)));
                float introduced39 = Mathf.Max(Mathf.Max(vector.x, vector2.x), vector3.x);
                float introduced40 = Mathf.Max(Mathf.Max(vector.y, vector2.y), vector3.y);
                triMaxCorners.Add(new Vector3(introduced39, introduced40, Mathf.Max(Mathf.Max(vector.z, vector2.z), vector3.z)));
            }
            List <int> list16 = new List <int>();

            for (int m = 0; m < list11.Count; m += 3)
            {
                Vector3 vector4 = localToWorldMatrix.MultiplyPoint3x4(vs[list11[m]]);
                Vector3 vector5 = localToWorldMatrix.MultiplyPoint3x4(vs[list11[m + 1]]);
                Vector3 vector6 = localToWorldMatrix.MultiplyPoint3x4(vs[list11[m + 2]]);
                if (!IsTriangleHidden(vector4, vector5, vector6, maxRemoveDistance, triMinCorners, triMaxCorners, hidingVs, hidingTs))
                {
                    list16.Add(list11[m]);
                    list16.Add(list11[m + 1]);
                    list16.Add(list11[m + 2]);
                }
            }
            subMeshes[subMeshIdx] = list16;
            LODMaker.RemoveUnusedVertices(vs, ns, list4, list5, list6, list7, list8, bws, subMeshes);
            mesh.uv4         = null;
            mesh.uv3         = null;
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = vs.ToArray();
            if (ns.Count > 0)
            {
                mesh.normals = ns.ToArray();
            }
            if (list4.Count > 0)
            {
                mesh.uv = list4.ToArray();
            }
            if (list5.Count > 0)
            {
                mesh.uv2 = list5.ToArray();
            }
            if (list6.Count > 0)
            {
                mesh.uv3 = list6.ToArray();
            }
            if (list7.Count > 0)
            {
                mesh.uv4 = list7.ToArray();
            }
            if (list8.Count > 0)
            {
                mesh.colors32 = list8.ToArray();
            }
            if (bws.Count > 0)
            {
                mesh.boneWeights = bws.ToArray();
            }
            mesh.subMeshCount = subMeshes.Count;
            for (int n = 0; n < subMeshes.Count; n++)
            {
                if (n == subMeshIdx)
                {
                    mesh.SetTriangles(list16.ToArray(), n);
                }
                else
                {
                    mesh.SetTriangles(subMeshes[n].ToArray(), n);
                }
            }
            if ((ns == null) || (ns.Count <= 0))
            {
                mesh.RecalculateNormals();
            }
            RecalculateTangents(mesh);
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 5
0
        public static Mesh CopyAndRemoveSubmeshes(this Mesh orig, int[] submeshesToRemove)
        {
            Mesh mesh = (Mesh)Mesh.Instantiate(orig);
            List <List <int> > submeshes = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     vs        = new List <Vector3>(orig.vertices);
            List <Vector3>     ns        = new List <Vector3>(orig.vertexCount);
            List <Vector2>     uv1s      = new List <Vector2>(orig.vertexCount);
            List <Vector2>     uv2s      = new List <Vector2>();
            List <Vector2>     uv3s      = new List <Vector2>();
            List <Vector2>     uv4s      = new List <Vector2>();
            List <Color32>     colors32  = new List <Color32>();
            List <BoneWeight>  bws       = new List <BoneWeight>();

            ns.AddRange(orig.normals);
            uv1s.AddRange(orig.uv);
            uv2s.AddRange(orig.uv2);
                        #if UNITY_4_3
                        #elif UNITY_4_4
                        #elif UNITY_4_5
                        #elif UNITY_4_6
                        #else
            uv3s.AddRange(orig.uv3);
            uv4s.AddRange(orig.uv4);
                        #endif
            colors32.AddRange(orig.colors32);
            bws.AddRange(orig.boneWeights);

            for (int s = 0; s < mesh.subMeshCount; s++)
            {
                bool remove = false;
                for (int i = 0; i < submeshesToRemove.Length; i++)
                {
                    if (submeshesToRemove[i] == s)
                    {
                        remove = true;
                    }
                }
                if (!remove)
                {
                    List <int> submesh = new List <int>();
                    submesh.AddRange(mesh.GetTriangles(s));
                    submeshes.Add(submesh);
                }
            }

            LODMaker.RemoveUnusedVertices(vs, ns, uv1s, uv2s, uv3s, uv4s, colors32, bws, submeshes);

                        #if UNITY_4_3
                        #elif UNITY_4_4
                        #elif UNITY_4_5
                        #elif UNITY_4_6
                        #else
            mesh.uv4 = null;
            mesh.uv3 = null;
                        #endif
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = vs.ToArray();
            if (ns.Count > 0)
            {
                mesh.normals = ns.ToArray();
            }
            if (uv1s.Count > 0)
            {
                mesh.uv = uv1s.ToArray();
            }
            if (uv2s.Count > 0)
            {
                mesh.uv2 = uv2s.ToArray();
            }
                        #if UNITY_4_3
                        #elif UNITY_4_4
                        #elif UNITY_4_5
                        #elif UNITY_4_6
                        #else
            if (uv3s.Count > 0)
            {
                mesh.uv3 = uv3s.ToArray();
            }
            if (uv4s.Count > 0)
            {
                mesh.uv4 = uv4s.ToArray();
            }
                        #endif
            if (colors32.Count > 0)
            {
                mesh.colors32 = colors32.ToArray();
            }
            if (bws.Count > 0)
            {
                mesh.boneWeights = bws.ToArray();
            }

            mesh.subMeshCount = submeshes.Count;
            for (int s = 0; s < submeshes.Count; s++)
            {
                mesh.SetTriangles(submeshes[s].ToArray(), s);
            }

            if (ns == null || ns.Count <= 0)
            {
                mesh.RecalculateNormals();
            }
            RecalculateTangents(mesh);
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 6
0
        public static Mesh CopyAndRemoveHiddenTriangles(this Mesh orig, int subMeshIdx, Matrix4x4 localToWorldMatrix, Mesh[] hidingMeshes, int[] hidingSubMeshes, Matrix4x4[] hidingLocalToWorldMatrices, float maxRemoveDistance = 0.01f)
        {
            if (subMeshIdx >= orig.subMeshCount)
            {
                return(null);
            }
            if (hidingMeshes.Length <= 0)
            {
                return(null);
            }
            if (hidingMeshes.Length != hidingSubMeshes.Length || hidingMeshes.Length != hidingLocalToWorldMatrices.Length)
            {
                return(null);
            }
            // copy orig mesh and get everything in Lists
            Mesh mesh = (Mesh)Mesh.Instantiate(orig);
            List <List <int> > submeshes = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     vs        = new List <Vector3>(orig.vertices);
            List <Vector3>     ns        = new List <Vector3>(orig.vertexCount);
            List <Vector2>     uv1s      = new List <Vector2>(orig.vertexCount);
            List <Vector2>     uv2s      = new List <Vector2>();
            List <Vector2>     uv3s      = new List <Vector2>();
            List <Vector2>     uv4s      = new List <Vector2>();
            List <Color32>     colors32  = new List <Color32>();
            List <BoneWeight>  bws       = new List <BoneWeight>();

            ns.AddRange(orig.normals);
            if (ns == null || ns.Count <= 0)
            {
                orig.RecalculateNormals();
                ns.AddRange(orig.normals);
            }
            uv1s.AddRange(orig.uv);
            uv2s.AddRange(orig.uv2);
                        #if UNITY_4_3
                        #elif UNITY_4_4
                        #elif UNITY_4_5
                        #elif UNITY_4_6
                        #else
            uv3s.AddRange(orig.uv3);
            uv4s.AddRange(orig.uv4);
                        #endif
            colors32.AddRange(orig.colors32);
            bws.AddRange(orig.boneWeights);
            for (int s = 0; s < orig.subMeshCount; s++)
            {
                List <int> submesh = new List <int>();
                submesh.AddRange(orig.GetTriangles(s));
                submeshes.Add(submesh);
            }
            List <int> ts = submeshes[subMeshIdx];

            // Make combined arrays for triangles/vertices/normals in hiding meshes
            List <Vector3> hidingVs     = new List <Vector3>();
            List <int>     hidingTs     = new List <int>();
            Mesh           prevMesh     = null;
            int            triOffset    = 0;
            int            prevHvsCount = 0;
            for (int h = 0; h < hidingMeshes.Length; h++)
            {
                Mesh hidingMesh = hidingMeshes[h];

                int[] hts = hidingMesh.GetTriangles(hidingSubMeshes[h]);
                if (prevMesh != hidingMesh)
                {
                    triOffset += prevHvsCount;
                }
                for (int i = 0; i < hts.Length; i++)
                {
                    hidingTs.Add(hts[i] + triOffset);
                }

                if (prevMesh != hidingMesh)
                {
                    Matrix4x4 hidingMatrix = hidingLocalToWorldMatrices[h];
                    Vector3[] hvs          = hidingMesh.vertices;
                    for (int i = 0; i < hvs.Length; i++)
                    {
                        hidingVs.Add(hidingMatrix.MultiplyPoint3x4(hvs[i]));                          // vertex in world coordinates
                    }
                    prevHvsCount = hvs.Length;
                    prevMesh     = hidingMesh;
                }
            }

            // get bounding box per triangle
            List <Vector3> triMinCorners = new List <Vector3>();
            List <Vector3> triMaxCorners = new List <Vector3>();
            for (int t = 0; t < hidingTs.Count; t += 3)
            {
                Vector3 v0 = hidingVs[hidingTs[t]];
                Vector3 v1 = hidingVs[hidingTs[t + 1]];
                Vector3 v2 = hidingVs[hidingTs[t + 2]];
                triMinCorners.Add(new Vector3(Mathf.Min(Mathf.Min(v0.x, v1.x), v2.x), Mathf.Min(Mathf.Min(v0.y, v1.y), v2.y), Mathf.Min(Mathf.Min(v0.z, v1.z), v2.z)));
                triMaxCorners.Add(new Vector3(Mathf.Max(Mathf.Max(v0.x, v1.x), v2.x), Mathf.Max(Mathf.Max(v0.y, v1.y), v2.y), Mathf.Max(Mathf.Max(v0.z, v1.z), v2.z)));
            }

            List <int> newTs = new List <int>();
            for (int t = 0; t < ts.Count; t += 3)
            {
                // we use vertices in world coordinates
                Vector3 v0 = localToWorldMatrix.MultiplyPoint3x4(vs[ts[t]]);
                Vector3 v1 = localToWorldMatrix.MultiplyPoint3x4(vs[ts[t + 1]]);
                Vector3 v2 = localToWorldMatrix.MultiplyPoint3x4(vs[ts[t + 2]]);
                if (!IsTriangleHidden(v0, v1, v2, maxRemoveDistance, triMinCorners, triMaxCorners, hidingVs, hidingTs))
                {
                    newTs.Add(ts[t]);
                    newTs.Add(ts[t + 1]);
                    newTs.Add(ts[t + 2]);
                }
            }

            // remove unused if anything deleted
            submeshes[subMeshIdx] = newTs;
            LODMaker.RemoveUnusedVertices(vs, ns, uv1s, uv2s, uv3s, uv4s, colors32, bws, submeshes);

            // Fill new mesh
                        #if UNITY_4_3
                        #elif UNITY_4_4
                        #elif UNITY_4_5
                        #elif UNITY_4_6
                        #else
            mesh.uv4 = null;
            mesh.uv3 = null;
                        #endif
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = vs.ToArray();
            if (ns.Count > 0)
            {
                mesh.normals = ns.ToArray();
            }
            if (uv1s.Count > 0)
            {
                mesh.uv = uv1s.ToArray();
            }
            if (uv2s.Count > 0)
            {
                mesh.uv2 = uv2s.ToArray();
            }
                        #if UNITY_4_5
                        #elif UNITY_4_6
                        #else
            if (uv3s.Count > 0)
            {
                mesh.uv3 = uv3s.ToArray();
            }
            if (uv4s.Count > 0)
            {
                mesh.uv4 = uv4s.ToArray();
            }
                        #endif
            if (colors32.Count > 0)
            {
                mesh.colors32 = colors32.ToArray();
            }
            if (bws.Count > 0)
            {
                mesh.boneWeights = bws.ToArray();
            }

            mesh.subMeshCount = submeshes.Count;
            for (int s = 0; s < submeshes.Count; s++)
            {
                if (s == subMeshIdx)
                {
                    mesh.SetTriangles(newTs.ToArray(), s);
                }
                else
                {
                    mesh.SetTriangles(submeshes[s].ToArray(), s);
                }
            }

            if (ns == null || ns.Count <= 0)
            {
                mesh.RecalculateNormals();
            }
            RecalculateTangents(mesh);
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 7
0
        public static IEnumerator MakeLODMeshInBackground(this Mesh mesh, float maxWeight, bool recalcNormals, float removeSmallParts, System.Action <Mesh> result)
        {
            Hashtable lodInfo = new Hashtable();

            lodInfo["maxWeight"]        = maxWeight;
            lodInfo["removeSmallParts"] = removeSmallParts;
            Vector3[] vs = mesh.vertices;
            if (vs.Length <= 0)
            {
                throw new ApplicationException("Mesh was empty");
            }
            Vector3[] ns = mesh.normals;
            if (ns.Length == 0)               // mesh has no normals
            {
                mesh.RecalculateNormals();
                ns = mesh.normals;
            }
            Vector2[] uv1s = mesh.uv;
            Vector2[] uv2s = mesh.uv2;
                        #if UNITY_4_3
            Vector2[] uv3s = new Vector2[0];
            Vector2[] uv4s = new Vector2[0];
                        #elif UNITY_4_4
            Vector2[] uv3s = new Vector2[0];
            Vector2[] uv4s = new Vector2[0];
                        #elif UNITY_4_5
            Vector2[] uv3s = new Vector2[0];
            Vector2[] uv4s = new Vector2[0];
                        #elif UNITY_4_6
            Vector2[] uv3s = new Vector2[0];
            Vector2[] uv4s = new Vector2[0];
                        #else
            Vector2[] uv3s = mesh.uv3;
            Vector2[] uv4s = mesh.uv4;
                        #endif
            Color32[]    colors32       = mesh.colors32;
            int[]        ts             = mesh.triangles;
            Matrix4x4[]  bindposes      = mesh.bindposes;
            BoneWeight[] bws            = mesh.boneWeights;
            int[]        subMeshOffsets = new int[mesh.subMeshCount];
            if (mesh.subMeshCount > 1)                // read triangles of submeshes 1 by 1 because I dont know the internal order of the mesh
            {
                for (int s = 0; s < mesh.subMeshCount; s++)
                {
                    int[] subTs = mesh.GetTriangles(s);
                    int   t     = 0;
                    for (; t < subTs.Length; t++)
                    {
                        ts[subMeshOffsets[s] + t] = subTs[t];
                    }
                    if (s + 1 < mesh.subMeshCount)
                    {
                        subMeshOffsets[s + 1] = subMeshOffsets[s] + t;
                    }
                }
            }
            Bounds meshBounds = mesh.bounds;
            lodInfo["vertices"]       = vs;
            lodInfo["normals"]        = ns;
            lodInfo["uv1s"]           = uv1s;
            lodInfo["uv2s"]           = uv2s;
            lodInfo["uv3s"]           = uv3s;
            lodInfo["uv4s"]           = uv4s;
            lodInfo["colors32"]       = colors32;
            lodInfo["triangles"]      = ts;
            lodInfo["bindposes"]      = bindposes;
            lodInfo["boneWeights"]    = bws;
            lodInfo["subMeshOffsets"] = subMeshOffsets;
            lodInfo["meshBounds"]     = meshBounds;

            Thread thread = new Thread(LODMaker.MakeLODMeshInBackground);
            thread.Start(lodInfo);
            while (!lodInfo.ContainsKey("ready"))
            {
                yield return(new WaitForSeconds(0.2f));
            }
            result(LODMaker.CreateNewMesh((Vector3[])lodInfo["vertices"], (Vector3[])lodInfo["normals"], (Vector2[])lodInfo["uv1s"], (Vector2[])lodInfo["uv2s"], (Vector2[])lodInfo["uv3s"], (Vector2[])lodInfo["uv4s"], (Color32[])lodInfo["colors32"], (int[])lodInfo["triangles"], (BoneWeight[])lodInfo["boneWeights"], (Matrix4x4[])lodInfo["bindposes"], (int[])lodInfo["subMeshOffsets"], recalcNormals));
        }
Ejemplo n.º 8
0
 public static Mesh MakeLODMesh(this Mesh orig, float aMaxWeight, bool recalcNormals, float removeSmallParts = 1f, float protectNormals = 1f, float protectUvs = 1f, float protectSubMeshesAndSharpEdges = 1f, float smallTrianglesFirst = 1f)
 {
     return(LODMaker.MakeLODMesh(orig, aMaxWeight, removeSmallParts, protectNormals, protectUvs, protectSubMeshesAndSharpEdges, smallTrianglesFirst, recalcNormals));
 }
Ejemplo n.º 9
0
        public static Mesh CopyAndRemoveHiddenTriangles(this Mesh orig, int subMeshIdx, Matrix4x4 localToWorldMatrix, Mesh[] hidingMeshes, int[] hidingSubMeshes, Matrix4x4[] hidingLocalToWorldMatrices, float maxRemoveDistance = 0.01f)
        {
            if (subMeshIdx >= orig.subMeshCount)
            {
                return(null);
            }
            if (hidingMeshes.Length <= 0)
            {
                return(null);
            }
            if (hidingMeshes.Length != hidingSubMeshes.Length || hidingMeshes.Length != hidingLocalToWorldMatrices.Length)
            {
                return(null);
            }
            Mesh mesh = UnityEngine.Object.Instantiate <Mesh>(orig);
            List <List <int> > list  = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     list2 = new List <Vector3>(orig.vertices);
            List <Vector3>     list3 = new List <Vector3>(orig.vertexCount);
            List <Vector2>     list4 = new List <Vector2>(orig.vertexCount);
            List <Vector2>     list5 = new List <Vector2>();
            List <Vector2>     list6 = new List <Vector2>();
            List <Vector2>     list7 = new List <Vector2>();
            List <Color32>     list8 = new List <Color32>();
            List <BoneWeight>  list9 = new List <BoneWeight>();

            list3.AddRange(orig.normals);
            if (list3 == null || list3.Count <= 0)
            {
                orig.RecalculateNormals();
                list3.AddRange(orig.normals);
            }
            list4.AddRange(orig.uv);
            list5.AddRange(orig.uv2);
            list6.AddRange(orig.uv3);
            list7.AddRange(orig.uv4);
            list8.AddRange(orig.colors32);
            list9.AddRange(orig.boneWeights);
            for (int i = 0; i < orig.subMeshCount; i++)
            {
                List <int> list10 = new List <int>();
                list10.AddRange(orig.GetTriangles(i));
                list.Add(list10);
            }
            List <int>     list11 = list[subMeshIdx];
            List <Vector3> list12 = new List <Vector3>();
            List <int>     list13 = new List <int>();
            Mesh           x      = null;
            int            num    = 0;
            int            num2   = 0;

            for (int j = 0; j < hidingMeshes.Length; j++)
            {
                Mesh  mesh2     = hidingMeshes[j];
                int[] triangles = mesh2.GetTriangles(hidingSubMeshes[j]);
                if (x != mesh2)
                {
                    num += num2;
                }
                for (int k = 0; k < triangles.Length; k++)
                {
                    list13.Add(triangles[k] + num);
                }
                if (x != mesh2)
                {
                    Matrix4x4 matrix4x = hidingLocalToWorldMatrices[j];
                    Vector3[] vertices = mesh2.vertices;
                    for (int l = 0; l < vertices.Length; l++)
                    {
                        list12.Add(matrix4x.MultiplyPoint3x4(vertices[l]));
                    }
                    num2 = vertices.Length;
                    x    = mesh2;
                }
            }
            List <Vector3> list14 = new List <Vector3>();
            List <Vector3> list15 = new List <Vector3>();

            for (int m = 0; m < list13.Count; m += 3)
            {
                Vector3 vector  = list12[list13[m]];
                Vector3 vector2 = list12[list13[m + 1]];
                Vector3 vector3 = list12[list13[m + 2]];
                list14.Add(new Vector3(Mathf.Min(Mathf.Min(vector.x, vector2.x), vector3.x), Mathf.Min(Mathf.Min(vector.y, vector2.y), vector3.y), Mathf.Min(Mathf.Min(vector.z, vector2.z), vector3.z)));
                list15.Add(new Vector3(Mathf.Max(Mathf.Max(vector.x, vector2.x), vector3.x), Mathf.Max(Mathf.Max(vector.y, vector2.y), vector3.y), Mathf.Max(Mathf.Max(vector.z, vector2.z), vector3.z)));
            }
            List <int> list16 = new List <int>();

            for (int n = 0; n < list11.Count; n += 3)
            {
                Vector3 v  = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n]]);
                Vector3 v2 = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n + 1]]);
                Vector3 v3 = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n + 2]]);
                if (!MeshExtensions.IsTriangleHidden(v, v2, v3, maxRemoveDistance, list14, list15, list12, list13))
                {
                    list16.Add(list11[n]);
                    list16.Add(list11[n + 1]);
                    list16.Add(list11[n + 2]);
                }
            }
            list[subMeshIdx] = list16;
            LODMaker.RemoveUnusedVertices(list2, list3, list4, list5, list6, list7, list8, list9, list);
            mesh.uv4         = null;
            mesh.uv3         = null;
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = list2.ToArray();
            if (list3.Count > 0)
            {
                mesh.normals = list3.ToArray();
            }
            if (list4.Count > 0)
            {
                mesh.uv = list4.ToArray();
            }
            if (list5.Count > 0)
            {
                mesh.uv2 = list5.ToArray();
            }
            if (list6.Count > 0)
            {
                mesh.uv3 = list6.ToArray();
            }
            if (list7.Count > 0)
            {
                mesh.uv4 = list7.ToArray();
            }
            if (list8.Count > 0)
            {
                mesh.colors32 = list8.ToArray();
            }
            if (list9.Count > 0)
            {
                mesh.boneWeights = list9.ToArray();
            }
            mesh.subMeshCount = list.Count;
            for (int num3 = 0; num3 < list.Count; num3++)
            {
                if (num3 == subMeshIdx)
                {
                    mesh.SetTriangles(list16.ToArray(), num3);
                }
                else
                {
                    mesh.SetTriangles(list[num3].ToArray(), num3);
                }
            }
            if (list3 == null || list3.Count <= 0)
            {
                mesh.RecalculateNormals();
            }
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 10
0
        public static Mesh CopyAndRemoveSubmeshes(this Mesh orig, int[] submeshesToRemove)
        {
            Mesh mesh = UnityEngine.Object.Instantiate <Mesh>(orig);
            List <List <int> > list  = new List <List <int> >(mesh.subMeshCount);
            List <Vector3>     list2 = new List <Vector3>(orig.vertices);
            List <Vector3>     list3 = new List <Vector3>(orig.vertexCount);
            List <Vector2>     list4 = new List <Vector2>(orig.vertexCount);
            List <Vector2>     list5 = new List <Vector2>();
            List <Vector2>     list6 = new List <Vector2>();
            List <Vector2>     list7 = new List <Vector2>();
            List <Color32>     list8 = new List <Color32>();
            List <BoneWeight>  list9 = new List <BoneWeight>();

            list3.AddRange(orig.normals);
            list4.AddRange(orig.uv);
            list5.AddRange(orig.uv2);
            list6.AddRange(orig.uv3);
            list7.AddRange(orig.uv4);
            list8.AddRange(orig.colors32);
            list9.AddRange(orig.boneWeights);
            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                bool flag = false;
                for (int j = 0; j < submeshesToRemove.Length; j++)
                {
                    if (submeshesToRemove[j] == i)
                    {
                        flag = true;
                    }
                }
                if (!flag)
                {
                    List <int> list10 = new List <int>();
                    list10.AddRange(mesh.GetTriangles(i));
                    list.Add(list10);
                }
            }
            LODMaker.RemoveUnusedVertices(list2, list3, list4, list5, list6, list7, list8, list9, list);
            mesh.uv4         = null;
            mesh.uv3         = null;
            mesh.uv2         = null;
            mesh.uv2         = null;
            mesh.boneWeights = null;
            mesh.colors32    = null;
            mesh.normals     = null;
            mesh.tangents    = null;
            mesh.triangles   = null;
            mesh.vertices    = list2.ToArray();
            if (list3.Count > 0)
            {
                mesh.normals = list3.ToArray();
            }
            if (list4.Count > 0)
            {
                mesh.uv = list4.ToArray();
            }
            if (list5.Count > 0)
            {
                mesh.uv2 = list5.ToArray();
            }
            if (list6.Count > 0)
            {
                mesh.uv3 = list6.ToArray();
            }
            if (list7.Count > 0)
            {
                mesh.uv4 = list7.ToArray();
            }
            if (list8.Count > 0)
            {
                mesh.colors32 = list8.ToArray();
            }
            if (list9.Count > 0)
            {
                mesh.boneWeights = list9.ToArray();
            }
            mesh.subMeshCount = list.Count;
            for (int k = 0; k < list.Count; k++)
            {
                mesh.SetTriangles(list[k].ToArray(), k);
            }
            if (list3 == null || list3.Count <= 0)
            {
                mesh.RecalculateNormals();
            }
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 11
0
        public static IEnumerator MakeLODMeshInBackground(this Mesh mesh, float maxWeight, bool recalcNormals, float removeSmallParts, Action <Mesh> result)
        {
            Hashtable lodInfo = new Hashtable();

            lodInfo["maxWeight"]        = maxWeight;
            lodInfo["removeSmallParts"] = removeSmallParts;
            Vector3[] vs = mesh.vertices;
            if (vs.Length <= 0)
            {
                throw new ApplicationException("Mesh was empty");
            }
            Vector3[] ns = mesh.normals;
            if (ns.Length == 0)
            {
                mesh.RecalculateNormals();
                ns = mesh.normals;
            }
            Vector2[]    uv1s           = mesh.uv;
            Vector2[]    uv2s           = mesh.uv2;
            Vector2[]    uv3s           = mesh.uv3;
            Vector2[]    uv4s           = mesh.uv4;
            Color32[]    colors32       = mesh.colors32;
            int[]        ts             = mesh.triangles;
            Matrix4x4[]  bindposes      = mesh.bindposes;
            BoneWeight[] bws            = mesh.boneWeights;
            int[]        subMeshOffsets = new int[mesh.subMeshCount];
            if (mesh.subMeshCount > 1)
            {
                for (int i = 0; i < mesh.subMeshCount; i++)
                {
                    int[] triangles = mesh.GetTriangles(i);
                    int   j;
                    for (j = 0; j < triangles.Length; j++)
                    {
                        ts[subMeshOffsets[i] + j] = triangles[j];
                    }
                    if (i + 1 < mesh.subMeshCount)
                    {
                        subMeshOffsets[i + 1] = subMeshOffsets[i] + j;
                    }
                }
            }
            Bounds meshBounds = mesh.bounds;

            lodInfo["vertices"]       = vs;
            lodInfo["normals"]        = ns;
            lodInfo["uv1s"]           = uv1s;
            lodInfo["uv2s"]           = uv2s;
            lodInfo["uv3s"]           = uv3s;
            lodInfo["uv4s"]           = uv4s;
            lodInfo["colors32"]       = colors32;
            lodInfo["triangles"]      = ts;
            lodInfo["bindposes"]      = bindposes;
            lodInfo["boneWeights"]    = bws;
            lodInfo["subMeshOffsets"] = subMeshOffsets;
            lodInfo["meshBounds"]     = meshBounds;
            if (MeshExtensions.< > f__mg$cache0 == null)
            {
                MeshExtensions.< > f__mg$cache0 = new ParameterizedThreadStart(LODMaker.MakeLODMeshInBackground);
            }
            Thread thread = new Thread(MeshExtensions.< > f__mg$cache0);

            thread.Start(lodInfo);
            while (!lodInfo.ContainsKey("ready"))
            {
                yield return(new WaitForSeconds(0.2f));
            }
            result(LODMaker.CreateNewMesh((Vector3[])lodInfo["vertices"], (Vector3[])lodInfo["normals"], (Vector2[])lodInfo["uv1s"], (Vector2[])lodInfo["uv2s"], (Vector2[])lodInfo["uv3s"], (Vector2[])lodInfo["uv4s"], (Color32[])lodInfo["colors32"], (int[])lodInfo["triangles"], (BoneWeight[])lodInfo["boneWeights"], (Matrix4x4[])lodInfo["bindposes"], (int[])lodInfo["subMeshOffsets"], recalcNormals));
            yield break;
        }