static int TotalPrefabMesh(GameObject prefab)
    {
        if (prefab == null)
        {
            return(0);
        }

        var renderers = EB.Util.FindAllComponents <MeshRenderer>(prefab);

        int collapsedVerts = 0;

        for (int i = 0; i < renderers.Length; ++i)
        {
            var renderer = renderers[i];

            EditorUtility.DisplayProgressBar("Processing mesh", (int)i + " / " + renderers.Length + " - " + renderer.name, (float)i / (float)renderers.Length);

            var filter = renderer.GetComponent <MeshFilter>();

            if (filter == null || filter.sharedMesh == null)
            {
                continue;
            }

            var mesh          = filter.sharedMesh;
            var optimizedMesh = EBMeshUtils.Optimize(mesh);

            collapsedVerts += mesh.vertexCount - optimizedMesh.vertexCount;
        }

        EditorUtility.ClearProgressBar();

        return(collapsedVerts);
    }
Exemple #2
0
    private int MergePartitions(Dictionary <EBWorldPainterData.Region, Partition> partitions, Dictionary <int, Texture2D> lightmaps, GameObject world)
    {
        char slash = System.IO.Path.DirectorySeparatorChar;
        var  dir   = GetMergeGeometryPath();

        System.IO.Directory.CreateDirectory(dir);

        var merged = EB.Util.GetObjectExactMatch(world, "z_merged");

        DestroyImmediate(merged, false);

        merged = new GameObject("z_merged");
        merged.transform.parent = world.transform;
        merged.isStatic         = false;

        var saved = 0;
        var i     = 0;

        foreach (var kv in partitions)
        {
            var region    = kv.Key;
            var partition = kv.Value;

            EB.Debug.Log("Merging " + i);

            ShowProgressBar(PROGRESS_BAR_STAGE.Merging, (float)i / (float)partitions.Count);

            //Need to figure out lightmaps
            //MergeLightmaps(partition, region, lightmaps);

            var groupings = GroupPartition(partition, lightmaps);

            GameObject parent = new GameObject("Partition_" + region.id);

            parent.transform.parent = merged.transform;

            bool firstMeshOfRegion = true;

            foreach (Grouping grouping in groupings.Values)
            {
                var layername = LayerMask.LayerToName(grouping.layer);
                var layer     = EB.Util.GetObjectExactMatch(parent, layername);
                if (!layer)
                {
                    layer                  = new GameObject(layername);
                    layer.layer            = grouping.layer;
                    layer.transform.parent = parent.transform;
                }

                var name = GenerateName(partition, grouping.material, grouping.layer);
                var go   = EB.Util.GetObjectExactMatch(merged, name);
                if (go == null)
                {
                    go = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer));
                    go.transform.parent = layer.transform;
                    go.layer            = grouping.layer;
                    go.isStatic         = false;
                }

                var mesh = new Mesh();
                mesh.CombineMeshes(grouping.instances.ToArray(), true);

                EBMeshUtils.Channels channels = GetChannelMap(grouping.material.shader);
                var optimizedMesh             = EBMeshUtils.Optimize(EBMeshUtils.StripStreams(mesh, channels));

                AssetDatabase.CreateAsset(optimizedMesh, dir + slash + name + ".asset");

                var mat          = new Material(grouping.material);
                var hiddenShader = Shader.Find(mat.shader.name.Replace("EBG/", "Hidden/"));
                if (hiddenShader != null)
                {
                    mat.shader = hiddenShader;
                    if (mat.HasProperty("_lm"))
                    {
                        mat.SetTexture("_lm", partition.lightmap);
                    }
                }
                AssetDatabase.CreateAsset(mat, dir + slash + name + ".mat");

                go.GetComponent <MeshFilter>().sharedMesh = optimizedMesh;
                var mr = go.GetComponent <MeshRenderer> ();
                mr.sharedMaterial      = mat;
                mr.lightmapIndex       = -1;
                mr.lightmapScaleOffset = new Vector4(1, 1, 0, 0);

                if (firstMeshOfRegion)
                {
                    region.bounds     = optimizedMesh.bounds;
                    firstMeshOfRegion = false;
                }
                else
                {
                    region.bounds.Encapsulate(optimizedMesh.bounds);
                }

                saved += grouping.instances.Count - 1;
            }

            i++;
        }
        EB.Debug.Log("Merge end");
        return(saved);
    }