/// This option has a far longer preprocessing time at startup but leads to better runtime performance.
    void Start()
    {
        Component[] filters        = GetComponentsInChildren(typeof(MeshFilter));
        Matrix4x4   myTransform    = transform.worldToLocalMatrix;
        Hashtable   materialToMesh = new Hashtable();

        for (int i = 0; i < filters.Length; i++)
        {
            MeshFilter filter      = (MeshFilter)filters[i];
            Renderer   curRenderer = filters[i].renderer;
            MeshCombineUtility2.MeshInstance instance = new MeshCombineUtility2.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);
                    }
                }

                curRenderer.enabled = false;
            }
        }

        foreach (DictionaryEntry de  in materialToMesh)
        {
            ArrayList elements = (ArrayList)de.Value;
            MeshCombineUtility2.MeshInstance[] instances = (MeshCombineUtility2.MeshInstance[])elements.ToArray(typeof(MeshCombineUtility2.MeshInstance));

            // We have a maximum of one material, so just attach the mesh to our own game object
            if (materialToMesh.Count == 1)
            {
                // Make sure we have a mesh filter & renderer
                if (GetComponent(typeof(MeshFilter)) == null)
                {
                    gameObject.AddComponent(typeof(MeshFilter));
                }
                if (!GetComponent("MeshRenderer"))
                {
                    gameObject.AddComponent("MeshRenderer");
                }

                MeshFilter filter = (MeshFilter)GetComponent(typeof(MeshFilter));
                filter.mesh       = MeshCombineUtility2.Combine(instances, generateTriangleStrips);
                renderer.material = (Material)de.Key;
                renderer.enabled  = true;
            }
            // We have multiple materials to take care of, build one mesh / gameobject for each material
            // and parent it to this object
            else
            {
                GameObject go = new GameObject("Combined mesh");
                go.transform.parent        = transform;
                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 = MeshCombineUtility2.Combine(instances, generateTriangleStrips);
            }
        }
    }
Beispiel #2
0
    /// This option has a far longer preprocessing time at startup but leads to better runtime performance.
    void Start()
    {
        Component[] filters  = GetComponentsInChildren(typeof(MeshFilter));
        Matrix4x4 myTransform = transform.worldToLocalMatrix;
        Hashtable materialToMesh= new Hashtable();

        for (int i=0;i<filters.Length;i++) {
            MeshFilter filter = (MeshFilter)filters[i];
            Renderer curRenderer  = filters[i].renderer;
            MeshCombineUtility2.MeshInstance instance = new MeshCombineUtility2.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);
                    }
                }

                curRenderer.enabled = false;
            }
        }

        foreach (DictionaryEntry de  in materialToMesh) {
            ArrayList elements = (ArrayList)de.Value;
            MeshCombineUtility2.MeshInstance[] instances = (MeshCombineUtility2.MeshInstance[])elements.ToArray(typeof(MeshCombineUtility2.MeshInstance));

            // We have a maximum of one material, so just attach the mesh to our own game object
            if (materialToMesh.Count == 1)
            {
                // Make sure we have a mesh filter & renderer
                if (GetComponent(typeof(MeshFilter)) == null)
                    gameObject.AddComponent(typeof(MeshFilter));
                if (!GetComponent("MeshRenderer"))
                    gameObject.AddComponent("MeshRenderer");

                MeshFilter filter = (MeshFilter)GetComponent(typeof(MeshFilter));
                filter.mesh = MeshCombineUtility2.Combine(instances, generateTriangleStrips);
                renderer.material = (Material)de.Key;
                renderer.enabled = true;
            }
            // We have multiple materials to take care of, build one mesh / gameobject for each material
            // and parent it to this object
            else
            {
                GameObject go = new GameObject("Combined mesh");
                go.transform.parent = transform;
                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 = MeshCombineUtility2.Combine(instances, generateTriangleStrips);
            }
        }
    }