示例#1
0
    private void LoadLightmapsDirect(WorldInfoInternal worldInfo)
    {
        List <LightmapData> lightmaps = new List <LightmapData> ();

        int offset = 0;

        foreach (KeyValuePair <string, PieceInfo> piece in worldInfo.pieces)
        {
            piece.Value.lightmapOffset = offset;
            GameObject prefab        = GetPrefabForCode(piece.Key);
            string     lightmapPath  = GetLightmapPathForPieceCode(piece.Key);
            var        lightmapPaths = GeneralUtils.GetFilesRecursive(lightmapPath, "*.exr");

            for (int i = 0; i < lightmapPaths.Count; ++i)
            {
                string path = lightmapPaths [i];
                if (!path.Contains("LightmapFar"))
                {
                    continue; //only far lightmaps are used in forward rendering
                }
                LightmapData lightmapData = new LightmapData();
                lightmapData.lightmapColor = (Texture2D)AssetDatabase.LoadMainAssetAtPath(path);
                lightmaps.Add(lightmapData);
            }

            Renderer[] renderers = EB.Util.FindAllComponents <Renderer> (prefab);
            foreach (var renderer in renderers)
            {
                if (renderer.lightmapIndex >= 0 && renderer.lightmapIndex != 254)
                {
                    renderer.lightmapIndex += offset;
                }
            }

            offset = lightmaps.Count;
        }
        LightmapSettings.lightmaps = lightmaps.ToArray();
    }
示例#2
0
    private Dictionary <int, Texture2D> LoadLightmaps(WorldInfoInternal worldInfo, bool fromGlobalBake)
    {
        var lightmaps = new Dictionary <int, Texture2D> ();

        if (fromGlobalBake)
        {
            string baseLightmapsDir = GetGlobalBakeLightmapPath();
            var    lightmapPaths    = GeneralUtils.GetFilesRecursive(baseLightmapsDir, "*.exr");
            for (int i = 0; i < lightmapPaths.Count; ++i)
            {
                string path = lightmapPaths [i];
                if (!path.Contains("LightmapFar"))
                {
                    continue;
                }
                var ti = (TextureImporter)TextureImporter.GetAtPath(path);
                if (!ti.isReadable || ti.textureFormat != TextureImporterFormat.ARGB32)
                {
                    ti.isReadable    = true;
                    ti.textureFormat = TextureImporterFormat.ARGB32;
                    AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
                }
                var      texture   = (Texture2D)AssetDatabase.LoadMainAssetAtPath(path);
                string[] nameSplit = texture.name.Split('-');
                int      lightmapIndex;
                int.TryParse(nameSplit[nameSplit.Length - 1], out lightmapIndex);
                lightmaps.Add(lightmapIndex, texture);
            }
        }
        else
        {
            int offset = 0;
            foreach (KeyValuePair <string, PieceInfo> piece in worldInfo.pieces)
            {
                piece.Value.lightmapOffset = offset;

                string lightmapPath  = GetLightmapPathForPieceCode(piece.Key);
                var    lightmapPaths = GeneralUtils.GetFilesRecursive(lightmapPath, "*.exr");

                int maxLightmapIndex = 0;

                for (int i = 0; i < lightmapPaths.Count; ++i)
                {
                    string path = lightmapPaths [i];
                    if (!path.Contains("LightmapFar"))
                    {
                        continue; //only far lightmaps are used in forward rendering
                    }
                    var ti = (TextureImporter)TextureImporter.GetAtPath(path);
                    if (!ti.isReadable || ti.textureFormat != TextureImporterFormat.ARGB32)
                    {
                        ti.isReadable    = true;
                        ti.textureFormat = TextureImporterFormat.ARGB32;
                        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
                    }

                    var      texture   = (Texture2D)AssetDatabase.LoadMainAssetAtPath(path);
                    string[] nameSplit = texture.name.Split('-');
                    int      lightmapIndex;
                    int.TryParse(nameSplit[nameSplit.Length - 1], out lightmapIndex);

                    maxLightmapIndex = Mathf.Max(maxLightmapIndex, lightmapIndex);

                    lightmaps.Add(piece.Value.lightmapOffset + lightmapIndex, texture);
                }

                offset += maxLightmapIndex + 1;
            }
        }

        return(lightmaps);
    }
示例#3
0
    private void MergeWorld(WorldInfoInternal worldInfo, Dictionary <int, Texture2D> lightmaps, EBWorldPainterData worldPainterData)
    {
        var world = GameObject.Find("z_track");

        if (world == null)
        {
            return;
        }

        List <MeshFilter> filtersList = new List <MeshFilter> ();
        List <Transform>  transforms  = new List <Transform> ();

        foreach (KeyValuePair <string, PieceInfo> piece in worldInfo.pieces)
        {
            //for each prefab
            GameObject prefab = GetPrefabForCode(piece.Key);
            if (!prefab)
            {
                Debug.LogError("No prefab for code " + piece.Key);
                continue;
            }

            //for each zone in prefab
            EB.Debug.Log("TEST");
            EB.Debug.Log(prefab.name);
            GameObject zones = EB.Util.GetObjectExactMatch(prefab, "Zones");

            for (int i = 0; zones != null && i < zones.transform.childCount; i++)
            {
                //Transform child_transform = zones.transform.GetChild (i);
                //GameObject child = child_transform.gameObject;

                var unmerged = EB.Util.GetObjectExactMatch(prefab, "z_unmerged");
                if (!unmerged)
                {
                    Debug.LogError("No z_unmerged found in prefab " + prefab.name + ". Not merging this block.");
                    continue;
                }

                var meshFilters = EB.Util.FindAllComponents <MeshFilter> (unmerged);

                foreach (var meshFilter in meshFilters)
                {
                    if (!meshFilter.GetComponent <Renderer>() || !RendererHasLightmap(meshFilter.GetComponent <Renderer>()))
                    {
                        continue;
                    }
                    meshFilter.GetComponent <Renderer>().lightmapIndex += piece.Value.lightmapOffset;
                }

                filtersList.AddRange(meshFilters);
                transforms.AddRange(EB.Util.FindAllComponents <Transform>(unmerged));
            }
        }

        EB.Debug.Log(filtersList.Count);

        return;


        ////skip hidden meshes
        //for (int i = filtersList.Count - 1; i >= 0; --i)
        //{
        //    MeshFilter filter = filtersList [i];
        //    if (!filter.gameObject.activeInHierarchy)
        //    {
        //        filtersList.RemoveAt(i);
        //    }
        //}

        //var filters = filtersList.ToArray ();
        //var renderersFull = new MeshRenderer[filters.Length];

        //int x = 0;
        //foreach (var filter in filters)
        //{
        //    var renderer = filter.GetComponent<Renderer>() as MeshRenderer;
        //    if (renderer != null)
        //    {
        //        renderersFull[x] = renderer;
        //        ++x;
        //    }
        //}

        //var renderers = new MeshRenderer[x];
        //System.Array.Copy(renderersFull, renderers, x);

        ////Paritition the meshes based of the World Painter data
        //var partitions = PartitionMeshes (renderers, worldPainterData);

        ////Merge the partitions, creating the new merged geometry and lightmaps
        //int saved = MergePartitions (partitions, lightmaps, world);
        //Debug.Log("Merged " + _worldName + ": saved " + saved + " draw calls");

        ////remove any game objects in the z_unmerged heirarchy that are just meshes or empty transforms
        //int childDepth = 5;
        //for (int i = 0; i < childDepth; ++i)
        //{
        //    foreach (var filter in filters)
        //    {
        //        if (filter == null)
        //        {
        //            continue;
        //        }

        //        if (!RendererIsMergeable(filter.GetComponent<Renderer>()))
        //        {
        //            continue;
        //        }

        //        int childCount = filter.gameObject.transform.childCount;
        //        Component[] components = filter.gameObject.GetComponents<Component> ();
        //        if ((childCount == 0) && (components.Length == 3))
        //        {
        //            //a transform, mesh filter, mesh renderer (which we merged), so kill the parent node
        //            DestroyImmediate(filter.gameObject);
        //        }
        //        else if (i == (childDepth - 1))
        //        {
        //            //must have other things; just kill the mesh filter and mesh renderer
        //            DestroyImmediate(filter.GetComponent<Renderer>());
        //            DestroyImmediate(filter);
        //        }
        //    }

        //    foreach (var transform in transforms)
        //    {
        //        if (transform == null)
        //        {
        //            continue;
        //        }

        //        if ((transform.childCount == 0) && (transform.gameObject.GetComponents<Component>().Length == 1))
        //        {
        //            //empty node
        //            DestroyImmediate(transform.gameObject);
        //        }
        //    }
        //}

        //EditorApplication.SaveAssets();
    }
示例#4
0
    private void MergeSkinnedMeshes(WorldInfoInternal worldInfo, EBWorldPainterData worldPainterData)
    {
        var world = GameObject.Find("z_track");

        if (world == null)
        {
            return;
        }
        Dictionary <EBWorldPainterData.Region, List <MeshFilter> > meshRegionMap = new Dictionary <EBWorldPainterData.Region, List <MeshFilter> > ();

        foreach (KeyValuePair <string, PieceInfo> piece in worldInfo.pieces)
        {
            GameObject prefab = GetPrefabForCode(piece.Key);
            if (!prefab)
            {
                Debug.LogError("No prefab for code " + piece.Key);
                continue;
            }

            //Deal with decision types+
            MergeAdditionalCustom(piece.Key);


            MeshFilter[] meshFilters = GetSkinnedMeshsFor(piece.Key);

            if (meshFilters != null && meshFilters.Length > 0)
            {
                foreach (MeshFilter meshFilter in meshFilters)
                {
                    EBWorldPainterData.Point    point   = new EBWorldPainterData.Point(new Vector2(meshFilter.GetComponent <Renderer>().bounds.center.x, meshFilter.GetComponent <Renderer>().bounds.center.z));
                    EBWorldPainterData.Region[] regions = worldPainterData.RegionsPointIsInside(point);

                    if (regions.Length > 0)
                    {
                        if (!meshRegionMap.ContainsKey(regions[0]))
                        {
                            meshRegionMap[regions[0]] = new List <MeshFilter>();
                        }
                        meshRegionMap[regions[0]].Add(meshFilter);
                    }
                    else
                    {
                        Debug.LogError("Meshfilter " + meshFilter.name + " does not fall into any region");
                    }
                }
            }
        }

        foreach (KeyValuePair <EBWorldPainterData.Region, List <MeshFilter> > meshRegion in meshRegionMap)
        {
            int id = meshRegion.Key.id;

            GameObject partition = GameObject.Find("Partition_" + id);
            if (partition == null)
            {
                Debug.LogError("NO Partition to parent to " + id);
                continue;
            }
            string            savedName             = _worldName + "_" + id;
            List <GameObject> gos                   = SkinnedMeshMerger.Merge(meshRegion.Value.ToArray(), savedName, _worldName);
            GameObject        mergedBreakableMeshes = new GameObject("MergedBreakableMeshes");

            mergedBreakableMeshes.transform.parent = partition.transform;

            foreach (GameObject g in gos)
            {
                g.layer            = LayerMask.NameToLayer("environment");
                g.transform.parent = mergedBreakableMeshes.transform;
            }
        }
    }
示例#5
0
    private void BuildWorld(BuildStages stages)
    {
        ShowProgressBar(PROGRESS_BAR_STAGE.Assembling);

        //WillBuildWorld();

        //int changelist = 0;
        //need to bring this back
        //int changelist = CheckoutFiles(stages, false);

        //do we really want a new scene??
        //EditorApplication.NewScene();

        var       world     = new GameObject("z_track");
        WorldInfo worldInfo = AssembleWorld(world);


        //save early, so that we have a scene name that we can link other data we save to
        //SaveScene();

        //convert to internal representation, so we can carry around extra data that we need
        WorldInfoInternal worldInfoInternal = new WorldInfoInternal();

        worldInfoInternal.pieces = new Dictionary <string, PieceInfo>();
        foreach (var code in worldInfo.pieceCodes)
        {
            EB.Debug.Log(code);
            worldInfoInternal.pieces[code] = new PieceInfo();
        }

        //assemble world
        //RenderSettingsType renderSettings = LoadOrCreateRenderSettings ();

        Dictionary <int, Texture2D> lightMaps = null;

        //need to figure out backed lighting

        /*if (stages.BakeLighting)
         * {
         * ShowProgressBar(PROGRESS_BAR_STAGE.Baking_Lighting);
         * Lightmapping.Bake();
         * }*/

        //need to gigure out the lightmap path

        /*if (stages.Merge)
         * {
         * lightMaps = LoadLightmaps(worldInfoInternal, stages.BakeLighting || stages.UseGlobalBakeLightmaps);
         * }
         * else
         * {
         * //reindex the lightmaps as we are going to directly load them
         * LoadLightmapsDirect(worldInfoInternal);
         * }*/

        //SetupLightProbes(stages.BakeLightProbes, renderSettings);

        if (stages.Merge)
        {
            EBWorldPainterData worldPainterData = EBWorldPainter.DataForCurrentScene();
            MergeWorld(worldInfoInternal, lightMaps, worldPainterData);
            //MergeSkinnedMeshes(worldInfoInternal, worldPainterData);
        }

        return;

        //ShowProgressBar(PROGRESS_BAR_STAGE.Cleaning_Up);

        //DidMergeWorld();

        //SaveScene("Built");

        //checkout anything that we created in between when we started and now
        //CheckoutFiles(stages, true, changelist);

        //DidBuildWorld();

        //P4Connect.Config.PerforceEnabled = true;
    }