Пример #1
0
        //TODO: Use Request Async maybe for new chunks
        private void LateUpdate()
        {
            while (chunksToInitialize.Count > 0 && FrameTimer.HasTimeLeftInFrame)
            {
                ChunkInitializeTask task = chunksToInitialize.Pop();
                task.chunk.InitializeWithMeshData(task.chunk.MeshData);
                task.onChunkDone(task.chunk);
            }

            while (readyExchangeChunks.Count > 0 && FrameTimer.HasTimeLeftInFrame)
            {
                ReadyChunkExchange change;
                lock (MarchingCubeChunkHandler.exchangeLocker)
                {
                    change = readyExchangeChunks.Pop();
                }

                List <CompressedMarchingCubeChunk> chunk = change.chunks;
                List <CompressedMarchingCubeChunk> olds  = change.old;

                if (olds.Count == 0)
                {
                    Debug.LogError("old is 0!");
                    continue;
                }

                if (olds[0].IsSpawner)
                {
                    chunkHandler.FindNeighbourOfChunk(chunk[0]);
                }
                for (int i = 0; i < olds.Count; i++)
                {
                    olds[i].DestroyChunk();
                }
                for (int i = 0; i < chunk.Count; i++)
                {
                    if (chunk[i].IsEmpty && !chunk[i].IsSpawner)
                    {
                        chunk[i].DestroyChunk();
                    }
                    else
                    {
                        chunk[i].BuildEnvironmentForChunk();
                    }
                }
            }


            List <CompressedMarchingCubeChunk> chunks = new List <CompressedMarchingCubeChunk>();

            isInIncreasingChunkIteration = true;
            foreach (CompressedMarchingCubeChunk chunk in increaseChunkLods)
            {
                if (FrameTimer.HasTimeLeftInFrame)
                {
                    if (chunk.IsReady || chunk.IsSpawner)
                    {
                        chunkHandler.IncreaseChunkLod(chunk, chunk.TargetLODPower);
                        chunks.Add(chunk);
                    }
                }
                else
                {
                    break;
                }
            }
            isInIncreasingChunkIteration = false;
            foreach (var c in chunks)
            {
                increaseChunkLods.Remove(c);
            }
            chunks.Clear();

            //TODO: Potential race condition? maybe lock something
            isInDecreasingChunkIteration = true;
            foreach (var item in lowerChunkLods)
            {
                if (FrameTimer.HasTimeLeftInFrame)
                {
                    if (item.IsReady && !removedLowerChunkLodsBuffer.Contains(item))
                    {
                        chunkHandler.DecreaseChunkLod(item, item.TargetLODPower);
                    }
                }
                else
                {
                    break;
                }
            }
            isInDecreasingChunkIteration = false;
            foreach (var c in removedLowerChunkLodsBuffer)
            {
                lowerChunkLods.Remove(c);
            }
            removedLowerChunkLodsBuffer.Clear();

            FrameTimer.RestartWatch();
        }