Exemplo n.º 1
0
        public bool RemoveMaterialContainer(BatchMeshRenderer materialContainer)
        {
            if (materialContainer.CurrentMaterial != null && CloneContainer.ContainsKey(materialContainer.CurrentMaterial))
            {
                if (CloneContainer[materialContainer.CurrentMaterial].Remove(materialContainer))
                {
                    BakeNeeded = true;
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public bool AddMaterialContainer(BatchMeshRenderer materialContainer)
        {
            if (materialContainer.CurrentMaterial != null)
            {
                if (!CloneContainer.ContainsKey(materialContainer.CurrentMaterial))
                {
                    CloneContainer.Add(materialContainer.CurrentMaterial, new HashSet <BatchMeshRenderer>());
                }

                if (!CloneContainer[materialContainer.CurrentMaterial].Contains(materialContainer))
                {
                    CloneContainer[materialContainer.CurrentMaterial].Add(materialContainer);
                    BakeNeeded = true;

                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 3
0
 protected virtual void OnDestroy()
 {
     CloneContainer.Clear();
     DestroyBakedObjects();
 }
Exemplo n.º 4
0
        public void Bake()
        {
            UnityEngine.Profiling.Profiler.BeginSample("Bake");

            //Lists that holds mesh data that belongs to each submesh
            List <MaterialMesh> materialMeshes = new List <MaterialMesh>();

            // First let's create our lists of CombineInstances based on the used Material
            foreach (var item in MaterialController.SourceToClone.Values)
            {
                foreach (Material material in item.Values)
                {
                    if (!CloneContainer.ContainsKey(material) || CloneContainer[material].Count == 0)
                    {
                        // We continue on if this material isn't cloned anywhere
                        continue;
                    }

                    List <CombineInstance> combineInstances = new List <CombineInstance>();
                    int vertexCount = 0;
                    foreach (BatchMeshRenderer container in CloneContainer[material])
                    {
                        container.SetRendering(false);

                        foreach (MeshDetail meshDetail in container.MeshDetails)
                        {
                            CombineInstance combine = new CombineInstance();
                            combine.mesh         = meshDetail.mesh;
                            combine.subMeshIndex = meshDetail.subMeshIndex;
                            combine.transform    = meshDetail.transform;

                            if (vertexCount + combine.mesh.vertexCount > MAX_MESH_VERTICES)
                            {
                                Mesh tmpMesh = new Mesh();
                                tmpMesh.CombineMeshes(combineInstances.ToArray(), true, true);
                                materialMeshes.Add(new MaterialMesh(material, tmpMesh));

                                combineInstances.Clear();
                                vertexCount = 0;
                            }

                            vertexCount += combine.mesh.vertexCount;
                            combineInstances.Add(combine);
                        }
                    }

                    Mesh mesh = new Mesh();
                    mesh.CombineMeshes(combineInstances.ToArray(), true, true);
                    materialMeshes.Add(new MaterialMesh(material, mesh));
                }
            }

            // Now let's combine all of these instances into one giant mesh
            {
                List <Material>        materials        = new List <Material>();
                List <CombineInstance> combineInstances = new List <CombineInstance>();
                int vertexCount = 0;

                for (int i = 0; i < materialMeshes.Count; i++)
                {
                    if (vertexCount + materialMeshes[i].mesh.vertexCount > MAX_MESH_VERTICES)
                    {
                        Mesh tmpMesh = new Mesh();
                        tmpMesh.CombineMeshes(combineInstances.ToArray(), false, true);
                        for (int j = 0; j < combineInstances.Count; j++)
                        {
                            Destroy(combineInstances[j].mesh);
                        }

                        GameObject tmp = GetNewCombinedObject();
                        tmp.GetComponent <MeshFilter>().mesh = tmpMesh;
                        tmp.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();

                        combineInstances.Clear();
                        materials.Clear();
                        vertexCount = 0;
                    }

                    materials.Add(materialMeshes[i].material);

                    CombineInstance combine = new CombineInstance();
                    combine.mesh      = materialMeshes[i].mesh;
                    combine.transform = Matrix4x4.identity;// transform.localToWorldMatrix;
                    combineInstances.Add(combine);

                    vertexCount += combine.mesh.vertexCount;
                }

                //Create the final combined mesh
                Mesh combinedMesh = new Mesh();
                combinedMesh.CombineMeshes(combineInstances.ToArray(), false, true);
                for (int i = 0; i < combineInstances.Count; i++)
                {
                    Destroy(combineInstances[i].mesh);
                }

                GameObject combinedObj = GetNewCombinedObject();
                combinedObj.GetComponent <MeshFilter>().mesh = combinedMesh;
                combinedObj.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
            }

            UnityEngine.Profiling.Profiler.EndSample();
        }