Esempio n. 1
0
    /// <summary>
    /// Called to get the main thread to start generating chunks.
    /// </summary>
    private void InternalThreadLoop()
    {
        bool         shouldLive = true;
        LoadedChunk2 toLoad;
        Vec2i        position;

        //Form loop that lasts while we generate all chunks
        while (shouldLive)
        {
            UnityEngine.Profiling.Profiler.BeginSample("chunk_pre_load");
            Debug.BeginDeepProfile("chunk_pre_load");
            //thread safety
            lock (ChunkToLoadLock)
            {
                //If the count is 0, then the thread has finished for now.

                //Debug.Log("Chunks To Load: " + ChunksToLoad2.Count);
                if (ChunksToLoad2.Count == 0)
                {
                    return;
                }
                toLoad = ChunksToLoad2.Dequeue();



                position = toLoad.Position;
            }
            Debug.Log("[ChunkLoader] Chunk Loader starting to generate chunk " + toLoad.Position, Debug.CHUNK_LOADING);

            //Create pre-generated chunk
            PreLoadedChunk preLoaded = GeneratePreLoadedChunk(toLoad.Chunk, toLoad.LOD);


            preLoaded.SetLoadedChunk(toLoad);

            Debug.Log("[ChunkLoader] Finished creating PreChunk: " + toLoad.Position, Debug.CHUNK_LOADING);

            //Thread safe add the preloaded chunk
            lock (PreLoadedChunkLock)
            {
                PreLoadedChunks.Add(preLoaded);
            }

            Debug.EndDeepProfile("chunk_pre_load");
            UnityEngine.Profiling.Profiler.EndSample();

            //If we have requested a force load, we exit the thread.
            if (ForceLoad)
            {
                Debug.Log("[ChunkLoader] Force load is starting, need to finish loading chunk " + toLoad.Position, Debug.CHUNK_LOADING);
                return;
            }
        }

        /*
         * bool shouldLive = true;
         * ChunkData toLoad;
         * Vec2i position;
         * //Form loop that lasts while we generate all chunks
         * while (shouldLive)
         * {
         *  UnityEngine.Profiling.Profiler.BeginSample("chunk_pre_load");
         *  Debug.BeginDeepProfile("chunk_pre_load");
         *  //thread safety
         *  lock (ChunkToLoadLock)
         *  {
         *
         *      //If the count is 0, then the thread has finished for now.
         *      if (ChunksToLoad.Count == 0)
         *          return;
         *      //otherwise, we set the object
         *      toLoad = ChunksToLoad[0];
         *      ChunksToLoad.RemoveAt(0);
         *      //We get the lowest priority chunk
         *
         *
         *
         *      position = new Vec2i(toLoad.X, toLoad.Z);
         *  }
         *  Debug.Log("[ChunkLoader] Chunk Loader starting to generate chunk " + toLoad, Debug.CHUNK_LOADING);
         *
         *  //Create pre-generated chunk
         *  PreLoadedChunk preLoaded = GeneratePreLoadedChunk(toLoad);
         *  Debug.Log("[ChunkLoader] Finished creating PreChunk: " + toLoad, Debug.CHUNK_LOADING);
         *
         *  //Thread safe add the preloaded chunk
         *  lock (PreLoadedChunkLock)
         *  {
         *      PreLoadedChunks.Add(preLoaded);
         *  }
         *
         *  Debug.EndDeepProfile("chunk_pre_load");
         *  UnityEngine.Profiling.Profiler.EndSample();
         *
         *  //If we have requested a force load, we exit the thread.
         *  if (ForceLoad)
         *  {
         *      Debug.Log("[ChunkLoader] Force load is starting, need to finish loading chunk " + new Vec2i(toLoad.X, toLoad.Z), Debug.CHUNK_LOADING);
         *      return;
         *  }
         *
         * }  */
    }
Esempio n. 2
0
    /// <summary>
    /// Calling this function forces the chunk loader to load all chunks currently in its list.
    /// This must be called from the main thread.
    /// </summary>
    public void ForceLoadAll()
    {
        ForceLoad = true;
        //Wait for the thread to finish
        MainThread?.Join();

        Debug.BeginDeepProfile("force_chunk_load");
        //All threads have stopped now, so we are not required to be thread safe.
        //TODO - add some thread generation here to improve performance?
        Debug.Log("[ChunkLoader] Force load starting - " + ChunksToLoad.Count + " chunks to load", Debug.CHUNK_LOADING);

        foreach (KeyValuePair <LoadedChunk2, float> kvp in ChunksToLoad2.GetAllElements())
        {
            PreLoadedChunk plc = GeneratePreLoadedChunk(kvp.Key.Chunk, kvp.Key.LOD);
            plc.SetLoadedChunk(kvp.Key);
            PreLoadedChunks.Add(plc);
        }
        ChunksToLoad2.ClearQue();


        ChunksToLoad.Clear();
        Debug.Log("[ChunkLoader] Force load - " + PreLoadedChunks.Count + " chunks to create", Debug.CHUNK_LOADING);

        foreach (PreLoadedChunk plc in PreLoadedChunks)
        {
            //If for some reason this has already been generated, don't bother
            if (ChunkRegionManager.LoadedChunks.ContainsKey(plc.Position))
            {
                Debug.Log("[ChunkLoader] Loaded chunk at position " + plc.Position + " has already generated", Debug.CHUNK_LOADING);
                continue;
            }
            CreateChunk(plc);
            //ChunkRegionManager.LoadedChunks.Add(lc.Position, lc);
        }
        PreLoadedChunks.Clear();
        ChunksToLoadPositions.Clear();
        ForceLoad = false;
        Debug.EndDeepProfile("force_chunk_load");
        AstarPath.active.Scan();

        /*
         * //We set the force load variable to true.
         * //this forces the chunk loader thread to stop when it can
         * ForceLoad = true;
         * //Wait for the thread to finish
         * MainThread?.Join();
         *
         * Debug.BeginDeepProfile("force_chunk_load");
         * //All threads have stopped now, so we are not required to be thread safe.
         * //TODO - add some thread generation here to improve performance?
         * Debug.Log("[ChunkLoader] Force load starting - " + ChunksToLoad.Count + " chunks to load", Debug.CHUNK_LOADING);
         * foreach(ChunkData cd in ChunksToLoad)
         * {
         *  PreLoadedChunks.Add(GeneratePreLoadedChunk(cd));
         *
         * }
         * ChunksToLoad.Clear();
         * Debug.Log("[ChunkLoader] Force load - " + PreLoadedChunks.Count + " chunks to create", Debug.CHUNK_LOADING);
         *
         * foreach (PreLoadedChunk plc in PreLoadedChunks)
         * {
         *  //If for some reason this has already been generated, don't bother
         *  if (ChunkRegionManager.LoadedChunks.ContainsKey(plc.Position))
         *  {
         *      Debug.Log("[ChunkLoader] Loaded chunk at position " + plc.Position + " has already generated", Debug.CHUNK_LOADING);
         *      continue;
         *  }
         *
         *  LoadedChunk2 lc = CreateChunk(plc);
         *  ChunkRegionManager.LoadedChunks.Add(lc.Position, lc);
         * }
         * PreLoadedChunks.Clear();
         * ChunksToLoadPositions.Clear();
         * ForceLoad = false;
         * Debug.EndDeepProfile("force_chunk_load");
         * AstarPath.active.Scan();
         */
    }