Пример #1
0
        public static OCSceneConfig LoadSceneConfig(string projectAssetPath, int index)
        {
            OCSceneConfig ret = new OCSceneConfig();

            try
            {
                var filePath = Path.Combine(projectAssetPath, String.Format("OCSceneConfig{0}.json", index));

                if (!File.Exists(filePath))
                {
                    Debug.LogErrorFormat("batch mode json file {0} does not exist!", filePath);
                    return(ret);
                }

                string jsonContent = Util.LoadJson(filePath);

                ret = JsonUtility.FromJson <OCSceneConfig>(jsonContent);
            }
            catch (Exception e)
            {
                Debug.LogError("batch mode:" + e);
            }

            return(ret);
        }
Пример #2
0
        private static void ConfigGenerator(OCSceneConfig config)
        {
            Config.CellSize            = config.CellSize;
            Config.ScreenHeight        = config.ScreenHeight;
            Config.ScreenWidth         = config.ScreenWidth;
            Config.MaxPlayAreaHeight   = config.MaxPlayerHeight;
            Config.MinPlayAreaHeight   = config.MinPlayerHeight;
            Config.mergeCell           = config.MergeCell;
            Config.CellWeight          = config.MergeCellWeight;
            Config.mergeObjectID       = config.MergeObjectID;
            Config.mergeObjectDistance = config.MergeObjectDistance;
            Config.mergeObjectMaxSize  = config.MergeObjectSize;
            Config.UseComputeShader    = config.UseComputeShader;
            Config.UseVisibleCache     = config.UseVisbileCache;
            Config.ComputePerframe     = config.ComputePerframe;
            Config.PerframeExecCount   = config.PerframeExecCount;
            //Config.IsBatchMode = UnityEditorInternal.InternalEditorUtility.inBatchMode;

            Debug.LogFormat("batch mode {0} Use Compute Shader {1} Use Visible Cache {2}  ComputePerframe {3} PerframeExecCount {4} CellSize {5} MinHeight {6} MaxHeight {7} MergeObjectId {8} MergeCell {9}",
                            Config.IsBatchMode,
                            Config.UseComputeShader, Config.UseVisibleCache,
                            Config.ComputePerframe, Config.PerframeExecCount,
                            Config.CellSize, Config.MinPlayAreaHeight, Config.MaxPlayAreaHeight,
                            Config.mergeObjectID, Config.mergeCell);
        }
Пример #3
0
        public static void MergeStreamSceneOCData(OCSceneConfig config)
        {
            var scene = new MultiScene(config.GetSceneAssetPath(), config.SceneNamePattern, config.TileDimension, config.TileSize);

            scene.MergeOCData(config.TemporaryContainer);
            scene.CopyOCDataTo(config.TemporaryContainer);
        }
Пример #4
0
        //----------------------------

        public static OCSceneConfig GetSceneConfig(string mapName)
        {
            OCSceneConfig ret = new OCSceneConfig();

            try
            {
                var filePath = "Assets/template/OCScenesConfig.json";
                if (UnityEditorInternal.InternalEditorUtility.inBatchMode)
                {
                    if (!File.Exists(filePath))
                    {
                        var otherFilePath = "Assets/CoreRes/template/OCScenesConfig.json";
                        if (!File.Exists(otherFilePath))
                        {
                            Debug.LogErrorFormat("batch mode  Can not found config file: \"OCScenesConfig.json\" from path {0} or {1}", filePath, otherFilePath);
                            return(ret);
                        }

                        filePath = otherFilePath;
                    }
                }
                else
                {
                    if (!File.Exists(filePath))
                    {
                        var otherFilePath = "Assets/Assets/CoreRes/template/OCScenesConfig.json";
                        if (!File.Exists(otherFilePath))
                        {
                            //Debug.LogErrorFormat("Can not found config file: \"OCScenesConfig.json\" from path {0} or {1}", filePath, otherFilePath);
                            //return ret;
                            otherFilePath = "Assets/CoreRes/template/OCScenesConfig.json";
                        }

                        filePath = otherFilePath;
                    }
                }

                string templateContent = Util.LoadJson(filePath);

                var scenesConfig = JsonUtility.FromJson <OCScenesConfig>(templateContent);


                foreach (var sceneConfig in scenesConfig.scenesConfig)
                {
                    if (sceneConfig.MapName == mapName)
                    {
                        ret = sceneConfig;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }

            return(ret);
        }
Пример #5
0
        private static void WriteJsonFile(string path, int index, OCSceneConfig config)
        {
            try
            {
                var fileName = String.Format("OCSceneConfig{0}.json", index);
                var filePath = Path.Combine(path, fileName);

                string jsonText = JsonUtility.ToJson(config, true);

                File.WriteAllText(filePath, jsonText);
            }
            catch (Exception e)
            {
                Debug.LogError("batch mode:" + e);
            }
        }
Пример #6
0
        private static void BakeSingleSceneByConfig(OCSceneConfig config)
        {
            Debug.Log("batch mode Do bake single");
            ConfigGenerator(config);
            if (!Util.IsSceneOpened(config.SceneNamePattern))
            {
                Debug.LogFormat("batch mode Open Scene {0}", config.SceneNamePattern);
                EditorSceneManager.OpenScene(String.Format("{0}/{1}.unity", config.GetSceneAssetPath(), config.SceneNamePattern));
            }


            var scene = new SingleScene(config.GetSceneAssetPath(), config.SceneNamePattern, Index.InValidIndex);

            scene.tempPath = config.TemporaryContainer;
            scene.Bake(config.ComputePerframe, config.TemporaryContainer);
        }
Пример #7
0
        private static void BakeStreamSceneByConfig(OCSceneConfig config)
        {
            Debug.Log("batch mode Do bake stream!");
            ConfigGenerator(config);
            var tiles = config.indices;

            if (tiles != null)
            {
                var multiScene = new MultiScene(config.GetSceneAssetPath(), config.SceneNamePattern, config.TileDimension, config.TileSize);
                multiScene.BakeTiles(tiles, config.ComputePerframe, config.TemporaryContainer);
            }
            else
            {
                Debug.LogErrorFormat("batch mode Can not get bake tiles for map {0}", config.MapName);
                ExitOnBatchMode();
            }
        }
Пример #8
0
        //-----------------------------------------------

        public static OCSceneConfig CreateSceneConfig(string sceneName, string scenePath, bool frameBake = true, bool bStream = false, string sceneNameTemplate = "",
                                                      float cellSize = 2.0f, bool mergeCell = false, float weight = 0.9f, int tileDim = 8, int tileSize = 1000)
        {
            OCSceneConfig config = new OCSceneConfig();

            config.MapName             = sceneName;
            config.CellSize            = cellSize;
            config.MaxPlayerHeight     = 2.0f;
            config.MinPlayerHeight     = 0;
            config.ScreenHeight        = 600;
            config.ScreenWidth         = 600;
            config.MergeCell           = mergeCell;
            config.MergeCellWeight     = weight;
            config.MergeObjectID       = false;
            config.MergeObjectSize     = 1;
            config.MergeObjectDistance = 1;
            config.TileDimension       = tileDim;
            config.TileSize            = tileSize;
            config.IsStreamScene       = bStream;
            config.UseComputeShader    = true;
            config.UseVisbileCache     = true;
            config.SceneAssetPath      = scenePath;
            config.SceneNamePattern    = sceneNameTemplate;
            config.TemporaryContainer  = "D:/OCTemp";
            config.ComputePerframe     = frameBake;
            config.PerframeExecCount   = 1000;
            if (bStream)
            {
                config.indices = new List <Index>();
                for (int i = 0; i < config.TileDimension; i++)
                {
                    for (int j = 0; j < config.TileDimension; j++)
                    {
                        config.indices.Add(new Index(i, j));
                    }
                }
            }
            else
            {
                config.SceneNamePattern = config.MapName;
                config.TileDimension    = 1;
            }

            return(config);
        }
Пример #9
0
        private static void GenerateAllSceneConfigFile(OCSceneConfig config, bool bakeForTile, int processorNum)
        {
            if (config.IsStreamScene)
            {
                var bakeTiles = config.indices;
                var tileCount = bakeTiles.Count;
                processorNum = processorNum > tileCount ? tileCount : processorNum;
                if (processorNum == 0)
                {
                    processorNum = 1;
                }
                var perCountArray = new int[processorNum];
                for (int i = 0; i < tileCount; ++i)
                {
                    perCountArray[i % processorNum] += 1;
                }

                var startTile = 0;
                for (int index = 0; index < processorNum; ++index)
                {
                    Debug.LogFormat("batch mode Baking Tile Count for Processor {0} is {1}", index, perCountArray[index]);
                    if (bakeForTile)
                    {
                        WriteJsonFile("./Assets", index, config);
                    }
                    else
                    {
                        var tiles = GetConfigTiles(config.indices, startTile, perCountArray[index]);

                        OCSceneConfig tempConfig = config;
                        tempConfig.indices = tiles;

                        WriteJsonFile("./Assets", index, tempConfig);
                        startTile += perCountArray[index];
                    }
                }
            }
            else
            {
                WriteJsonFile("./Assets", 0, config);
            }
        }
Пример #10
0
        public void TestPVS()
        {
            //var config = GetSceneConfig(gameObject.scene.name);
            //if (string.IsNullOrEmpty(config.MapName))
            //{
            // config.IsStreamScene = false;
            // config.SceneAssetPath = GetScenePath();
            //config.SceneNamePattern = gameObject.scene.name;
            // }

            InitConfig();
            OCSceneConfig config = new OCSceneConfig();

            config.IsStreamScene    = false;
            config.SceneAssetPath   = GetScenePath();
            config.SceneNamePattern = gameObject.scene.name;
            var testPVS = new PVSTest(Camera.main, config);

            testPVS.Test();
        }
Пример #11
0
        /*public static void OpenScenes(string name)
         * {
         *  OCSceneConfig config = OCGenerator.GetSceneConfig(name);
         *  string path = config.GetSceneAssetPath();
         *  path += "/";
         *
         *
         *  if (config.IsStreamScene)
         *  {
         *      int tileDim = config.TileDimension;
         *      int tileSize = config.TileSize;
         *
         *      var mainScene = SceneManager.GetSceneByName("AdditiveScene.unity");
         *      if(mainScene.isLoaded == false)
         *          EditorSceneManager.OpenScene(path + "AdditiveScene.unity", OpenSceneMode.Additive);
         *
         *      foreach (var index in config.indices)
         *      {
         *          string sceneName = string.Format("{0} {1}x{2}", name, index.x , index.y);
         *          string scenePath = path + sceneName + ".unity";
         *
         *          var scene = SceneManager.GetSceneByName(sceneName);
         *          if (scene.isLoaded == false)
         *              EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
         *      }
         *
         *  }
         *  else
         *  {
         *      EditorSceneManager.OpenScene(path + name +".unity");
         *  }
         * }*/

        public static void GenerateTestStreamScenes(string name)
        {
            OCSceneConfig config = OCGenerator.GetSceneConfig(name);
            string        path   = config.GetSceneAssetPath();

            path += "/";

            int tileDim  = config.TileDimension;
            int tileSize = config.TileSize;

            var template = Resources.Load("root") as GameObject;

            var mainScene = EditorSceneManager.OpenScene(path + "AdditiveScene.unity");

            foreach (var root in mainScene.GetRootGameObjects())
            {
                GameObject.DestroyImmediate(root);
            }
            var mainCamera = Resources.Load("MainCamera") as GameObject;
            var cam        = GameObject.Instantiate(mainCamera);


            for (int i = 0; i < tileDim; i++)
            {
                for (int j = 0; j < tileDim; j++)
                {
                    string sceneName = string.Format("{0} {1}x{2}", name, i, j);
                    string scenePath = path + sceneName + ".unity";
                    var    scene     = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);

                    EditorSceneManager.SetActiveScene(scene);

                    foreach (var root in scene.GetRootGameObjects())
                    {
                        GameObject.DestroyImmediate(root);
                    }

                    var templateGO = GameObject.Instantiate(template);
                    templateGO.transform.localScale = new Vector3(tileSize / 10, tileSize / 10, tileSize / 10);
                    templateGO.transform.position   = new Vector3(i * tileSize, 0, j * tileSize);
                    var     pos         = templateGO.transform.position;
                    float   offsetValue = -tileSize * tileDim * 0.5f;
                    Vector3 offset      = new Vector3(offsetValue, 0, offsetValue);
                    templateGO.transform.position = pos + offset;

                    var coms = templateGO.GetComponentsInChildren <MeshRenderer>();

                    int count = 0;
                    foreach (var com in coms)
                    {
                        var idcom = com.gameObject.GetComponent <GameObjectID>();
                        if (idcom == null)
                        {
                            idcom = com.gameObject.AddComponent <GameObjectID>();
                        }

                        idcom.GUID = count;
                        count++;
                    }

                    EditorSceneManager.SaveScene(scene);
                }
            }

            //EditorSceneManager.SaveOpenScenes();
        }
Пример #12
0
        private void OnGUI()
        {
            GUILayout.BeginHorizontal("Scene");
            sceneName = EditorGUILayout.TextField("场景名字", sceneName);
            assetPath = EditorGUILayout.TextField("场景路径", assetPath);
            if (GUILayout.Button("打开场景"))
            {
                OCGenerator.OpenScene(sceneName, new Index(tileX, tileY));
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal("base setting");
            processNumber = EditorGUILayout.IntField("进程数", processNumber);
            cellSize      = EditorGUILayout.FloatField("Cell大小", cellSize);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal("Screen");
            screenWidth  = EditorGUILayout.IntField("屏幕宽", screenWidth);
            screenHeight = EditorGUILayout.IntField("屏幕高", screenHeight);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal("Player");
            maxPlayAreaHeight = EditorGUILayout.Slider("玩家最大可到达高度", maxPlayAreaHeight, 0, 10);
            minPlayAreaHeight = EditorGUILayout.Slider("玩家最小不可到达高度", minPlayAreaHeight, 0, 10);
            GUILayout.EndHorizontal();

            IsFrameBake    = EditorGUILayout.BeginToggleGroup("分帧烘焙", IsFrameBake);
            frameCellCount = EditorGUILayout.IntField("每帧烘焙cell的个数", frameCellCount);
            EditorGUILayout.EndToggleGroup();

            IsMergeCell     = EditorGUILayout.BeginToggleGroup("是否融合Cell", IsMergeCell);
            cellMergeWeight = EditorGUILayout.Slider("融合相似度", cellMergeWeight, 0, 1);
            EditorGUILayout.EndToggleGroup();

            IsMergeObjectID = EditorGUILayout.BeginToggleGroup("是否融合GUID", IsMergeObjectID);
            GUILayout.BeginHorizontal("Merge Object");
            mergeObjectSize = EditorGUILayout.FloatField("对象大小", mergeObjectSize);
            mergeDistance   = EditorGUILayout.FloatField("对象距离", mergeDistance);
            GUILayout.EndHorizontal();
            EditorGUILayout.EndToggleGroup();


            //----------------
            IsStreamScene = EditorGUILayout.BeginToggleGroup("是否是流式场景", IsStreamScene);

            IsBakeOne = EditorGUILayout.BeginToggleGroup("只烘焙一小块地形(流式)", IsBakeOne);
            GUILayout.BeginHorizontal("tile index");

            tileX = EditorGUILayout.IntField("宽度索引(流式)", tileX);

            tileY = EditorGUILayout.IntField("高度索引(流式)", tileY);
            if (GUILayout.Button("烘焙单块(流式)"))
            {
                if (IsBakeOne)
                {
                    OCGenerator.TestBakeOneTile(sceneName, tileX, tileY);
                }
            }
            GUILayout.EndHorizontal();
            EditorGUILayout.EndToggleGroup(); //end IsBakeOne
            EditorGUILayout.EndToggleGroup(); //end IsStreamScene


            /*for (int i = 0; i < indices.Count; i++)
             * {
             *  var index = indices[i];
             *  Vector2 tile = new Vector2(index.x, index.y);
             *  EditorGUILayout.Vector2Field("bake tile", tile);
             *
             * }*/


            bTest = EditorGUILayout.BeginToggleGroup("测试", bTest);

            if (GUILayout.Button("创建测试所用的场景"))
            {
                OCGenerator.GenerateTestStreamScenes(sceneName);
            }

            GUILayout.BeginHorizontal("Json");

            if (GUILayout.Button("创建Json文件"))
            {
                OCGenerator.TestCreateScensJson();
            }

            if (GUILayout.Button("写Json配置"))
            {
                //OCGenerator.OpenScenes(sceneName);

                OCGenerator.WriteJsonConfig(sceneName, IsStreamScene, screenWidth, screenHeight, maxPlayAreaHeight, minPlayAreaHeight, cellSize,
                                            IsFrameBake, frameCellCount, IsMergeCell, cellMergeWeight, IsMergeObjectID, mergeDistance, mergeObjectSize, indices);
            }
            if (GUILayout.Button("读Json配置"))
            {
                //OCGenerator.OpenScenes(sceneName);
                ReadJsonConfig(sceneName);
            }

            GUILayout.EndHorizontal();



            GUILayout.BeginHorizontal("test");
            if (GUILayout.Button("根据进程数生成配置文件"))
            {
                OCGenerator.TestGenerateOCGenMapConfigFile(sceneName, processNumber);
            }

            if (GUILayout.Button("TestInitConfig(open scenes and generate ID)"))
            {
                OCGenerator.TestInitOCGeneration(sceneName);
            }

            if (GUILayout.Button("程序化自动测试数据"))
            {
                OCSceneConfig config = OCGenerator.GetSceneConfig(sceneName);
                PVSTest       test   = new PVSTest(Camera.main, config);
                test.Test();
            }


            /*if (GUILayout.Button("打开场景并加载OC数据"))
             * {
             *  OCGenerator.OpenScenes(sceneName);
             *  OCSceneConfig config = OCGenerator.GetSceneConfig(sceneName);
             *  if (config.IsStreamScene)
             *  {
             *      var ocDataFilePath = MultiScene.GetOCDataFilePath(config.GetSceneAssetPath(), config.SceneNamePattern);
             *      if (!File.Exists(ocDataFilePath))
             *      {
             *          EditorUtility.DisplayDialog("文件不存在", string.Format("OC 数据文件 {0} 不存在!", ocDataFilePath), "确定");
             *          return;
             *      }
             *      int TileDimension = config.TileDimension;
             *      byte[] data = null;
             *      using (var fileStream = File.Open(ocDataFilePath, FileMode.Open))
             *      {
             *          data = new byte[fileStream.Length];
             *          if (fileStream.Read(data, 0, data.Length) != data.Length)
             *          {
             *              EditorUtility.DisplayDialog("文件读取失败", string.Format("读取 OC 数据文件 {0} 失败!", ocDataFilePath), "确定");
             *              return;
             *          }
             *      }
             *
             *      streamScene = new MultiScene(config.GetSceneAssetPath(), config.SceneNamePattern, TileDimension, config.TileSize, data);
             *      //for (int i = 0; i < TileDimension; i++)
             *          //for (int j = 0; j < TileDimension; j++)
             *              //streamScene.Load(i, j);
             *      foreach(var index in config.indices )
             *      {
             *          streamScene.Load(index.x, index.y);
             *      }
             *
             *  }
             *  else
             *  {
             *
             *      singleScene = new OC.SingleScene(config.GetSceneAssetPath(), config.SceneNamePattern, Index.InValidIndex);
             *      singleScene.TestLoad();
             *  }
             *
             * }*/
            GUILayout.EndHorizontal();

            /*GUILayout.BeginHorizontal("OC");
             * if (GUILayout.Button("打开OC"))
             * {
             *  OCSceneConfig config = OCGenerator.GetSceneConfig(sceneName);
             *  if (config.IsStreamScene)
             *  {
             *      if (streamScene == null)
             *          Debug.LogError("stream scene is null!");
             *      else
             *      {
             *          streamScene.DoCulling(Camera.main.transform.position);
             *      }
             *  }
             *  else
             *  {
             *      if (singleScene == null)
             *          Debug.LogError("stream scene is null!");
             *      else
             *      {
             *          singleScene.DoCulling(Camera.main.transform.position);
             *      }
             *  }
             *
             * }
             * if (GUILayout.Button("关闭OC"))
             * {
             *  OCSceneConfig config = OCGenerator.GetSceneConfig(sceneName);
             *  if (config.IsStreamScene)
             *  {
             *      if (streamScene == null)
             *          Debug.LogError("stream scene is null!");
             *      else
             *      {
             *          streamScene.UndoCulling();
             *      }
             *  }
             *  else
             *  {
             *      if (singleScene == null)
             *          Debug.LogError("stream scene is null!");
             *      else
             *      {
             *          singleScene.UndoCulling();
             *      }
             *  }
             * }
             *
             * GUILayout.EndHorizontal();*/
            if (GUILayout.Button("TestApplyOCData"))
            {
                OCGenerator.TestApplyOCData(sceneName);
            }


            EditorGUILayout.EndToggleGroup();



            GUILayout.Label("烘焙", EditorStyles.boldLabel);

            GUILayout.BeginHorizontal("bake");

            if (GUILayout.Button("产生所有场景ID并保存"))
            {
                OCGenerator.OpenAllScenesAndGernerateGUID(sceneName);
            }

            jsonI = EditorGUILayout.IntField("第几个Json配置文件", jsonI);
            if (GUILayout.Button("烘焙配置文件"))
            {
                //EditorSceneManager.SaveOpenScenes();

                //OCGenerator.WriteJsonConfig(sceneName, IsStreamScene, screenWidth, screenHeight, maxPlayAreaHeight, minPlayAreaHeight, cellSize,
                //IsFrameBake, frameCellCount, IsMergeCell, cellMergeWeight, IsMergeObjectID, mergeDistance, mergeObjectSize, indices);

                OCGenerator.TestGenerateOCData(jsonI);
            }

            GUILayout.EndHorizontal();

            if (GUILayout.Button("烘焙所有"))
            {
                //OCGenerator.WriteJsonConfig(sceneName, IsStreamScene, screenWidth, screenHeight, maxPlayAreaHeight, minPlayAreaHeight, cellSize,
                //IsFrameBake, frameCellCount, IsMergeCell, cellMergeWeight, IsMergeObjectID, mergeDistance, mergeObjectSize, indices);
                //EditorSceneManager.SaveOpenScenes();
                OCGenerator.TestBakeAll(sceneName);
                //OCGenerator.TestMergeOCDataForStreamScene();
            }

            if (GUILayout.Button("融合OC数据"))
            {
                OCGenerator.TestMergeOCDataForStreamScene();
            }
        }