/// This option has a far longer preprocessing time at startup but leads to better runtime performance.
        public void Combine()
        {
            // Debug.Log("ddd");
            if(!Application.isEditor&&!inEditor)
            {
                DisableCombine();
            }

            currentMeshes = new List<GameObject>();
            allRenderers = new List<Renderer>();

            Component[] filters  = GetComponentsInChildren(typeof(MeshFilter));
            Matrix4x4 myTransform = transform.worldToLocalMatrix;
            Hashtable materialToMesh= new Hashtable();

            MeshFilter f = GetComponent<MeshFilter>();

            for (int i=0;i<filters.Length;i++)
            {
                MeshFilter filter = (MeshFilter)filters[i];

                if(f != null)
                {
                    if(f == filter) continue;
                }

                if(removeColliders)
                {
                    if(filter.collider != null)
                    {
                        if(Application.isEditor)
                        {
                            DestroyImmediate(filter.collider);
                        }
                        else
                        {
                            Destroy(filter.collider);
                        }
                    }
                }

                Renderer curRenderer  = filters[i].renderer;
                MeshCombineUtility.MeshInstance instance = new MeshCombineUtility.MeshInstance ();
                instance.mesh = filter.sharedMesh;
                if (curRenderer != null && curRenderer.enabled && instance.mesh != null)
                {
                    instance.transform = myTransform * filter.transform.localToWorldMatrix;

                    Material[] materials = curRenderer.sharedMaterials;
                    for (int m=0;m<materials.Length;m++) {
                        instance.subMeshIndex = System.Math.Min(m, instance.mesh.subMeshCount - 1);

                        ArrayList objects = (ArrayList)materialToMesh[materials[m]];
                        if (objects != null) {
                            objects.Add(instance);
                        }
                        else
                        {
                            objects = new ArrayList ();
                            objects.Add(instance);
                            materialToMesh.Add(materials[m], objects);
                        }
                    }

                    allRenderers.Add(curRenderer);
                    curRenderer.enabled = false;
                    DestroyImmediate(curRenderer);
                    // Destroy(curRenderer);
                }
            }

            Transform parent=(new GameObject("Combined meshes output")).transform;
            foreach (DictionaryEntry de  in materialToMesh) {
                ArrayList elements = (ArrayList)de.Value;
                MeshCombineUtility.MeshInstance[] instances = (MeshCombineUtility.MeshInstance[])elements.ToArray(typeof(MeshCombineUtility.MeshInstance));
                int count=0;
                ArrayList insss=new ArrayList();
                ArrayList inss=new ArrayList();
                foreach(MeshCombineUtility.MeshInstance ins in instances){
                    if(count>60000){
                        count=0;
                        insss.Add(inss);
                        inss=new ArrayList();
                    }
                    inss.Add(ins);
                    count+=ins.mesh.vertexCount;
                }
                insss.Add(inss);
                foreach(ArrayList arr in insss){
                    GameObject go = new GameObject("Combined mesh");
                    currentMeshes.Add(go);
                    go.transform.parent =parent;
                    go.transform.localScale = Vector3.one;
                    go.transform.localRotation = Quaternion.identity;
                    go.transform.localPosition = Vector3.zero;
                    go.AddComponent(typeof(MeshFilter));
                    go.AddComponent("MeshRenderer");
                    go.renderer.material = (Material)de.Key;
                    MeshFilter filter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
                    filter.mesh = MeshCombineUtility.Combine((MeshCombineUtility.MeshInstance[])arr.ToArray(typeof(MeshCombineUtility.MeshInstance)), generateTriangleStrips);
                }
            }
        }
Exemplo n.º 2
0
        /// This option has a far longer preprocessing time at startup but leads to better runtime performance.
        public void Combine()
        {
            // Debug.Log("ddd");
            if (!Application.isEditor && !inEditor)
            {
                DisableCombine();
            }

            currentMeshes = new List <GameObject>();
            allRenderers  = new List <Renderer>();

            Component[] filters        = GetComponentsInChildren(typeof(MeshFilter));
            Matrix4x4   myTransform    = transform.worldToLocalMatrix;
            Hashtable   materialToMesh = new Hashtable();

            MeshFilter f = GetComponent <MeshFilter>();

            for (int i = 0; i < filters.Length; i++)
            {
                MeshFilter filter = (MeshFilter)filters[i];

                if (f != null)
                {
                    if (f == filter)
                    {
                        continue;
                    }
                }

                if (removeColliders)
                {
                    if (filter.collider != null)
                    {
                        if (Application.isEditor)
                        {
                            DestroyImmediate(filter.collider);
                        }
                        else
                        {
                            Destroy(filter.collider);
                        }
                    }
                }

                Renderer curRenderer = filters[i].renderer;
                MeshCombineUtility.MeshInstance instance = new MeshCombineUtility.MeshInstance();
                instance.mesh = filter.sharedMesh;
                if (curRenderer != null && curRenderer.enabled && instance.mesh != null)
                {
                    instance.transform = myTransform * filter.transform.localToWorldMatrix;

                    Material[] materials = curRenderer.sharedMaterials;
                    for (int m = 0; m < materials.Length; m++)
                    {
                        instance.subMeshIndex = System.Math.Min(m, instance.mesh.subMeshCount - 1);

                        ArrayList objects = (ArrayList)materialToMesh[materials[m]];
                        if (objects != null)
                        {
                            objects.Add(instance);
                        }
                        else
                        {
                            objects = new ArrayList();
                            objects.Add(instance);
                            materialToMesh.Add(materials[m], objects);
                        }
                    }

                    allRenderers.Add(curRenderer);
                    curRenderer.enabled = false;
                    DestroyImmediate(curRenderer);
                    // Destroy(curRenderer);
                }
            }

            Transform parent = (new GameObject("Combined meshes output")).transform;

            foreach (DictionaryEntry de  in materialToMesh)
            {
                ArrayList elements = (ArrayList)de.Value;
                MeshCombineUtility.MeshInstance[] instances = (MeshCombineUtility.MeshInstance[])elements.ToArray(typeof(MeshCombineUtility.MeshInstance));
                int       count = 0;
                ArrayList insss = new ArrayList();
                ArrayList inss  = new ArrayList();
                foreach (MeshCombineUtility.MeshInstance ins in instances)
                {
                    if (count > 60000)
                    {
                        count = 0;
                        insss.Add(inss);
                        inss = new ArrayList();
                    }
                    inss.Add(ins);
                    count += ins.mesh.vertexCount;
                }
                insss.Add(inss);
                foreach (ArrayList arr in insss)
                {
                    GameObject go = new GameObject("Combined mesh");
                    currentMeshes.Add(go);
                    go.transform.parent        = parent;
                    go.transform.localScale    = Vector3.one;
                    go.transform.localRotation = Quaternion.identity;
                    go.transform.localPosition = Vector3.zero;
                    go.AddComponent(typeof(MeshFilter));
                    go.AddComponent("MeshRenderer");
                    go.renderer.material = (Material)de.Key;
                    MeshFilter filter = (MeshFilter)go.GetComponent(typeof(MeshFilter));
                    filter.mesh = MeshCombineUtility.Combine((MeshCombineUtility.MeshInstance[])arr.ToArray(typeof(MeshCombineUtility.MeshInstance)), generateTriangleStrips);
                }
            }
        }