コード例 #1
0
ファイル: World.cs プロジェクト: senlinms/ironVoxel
        void Update()
        {
            if (AsyncService.Loading())
            {
                return;
            }

            AsyncService.StartFrameTimer();


            if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_NEW_CHUNKS_DEADLINE))
            {
                Chunk newChunk = GeneratorService.GenerateNewChunk();
                if (newChunk != null)
                {
                    ChunkRepository.AddToProcessingChunkList(newChunk);
                }
            }

            int i = 0;

            while (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_START_WORK_DEADLINE) &&
                   i < ChunkRepository.GetProcessingChunkListSize())
            {
                Chunk chunk = ChunkRepository.GetProcessingChunk(i);
                if (chunk == null)
                {
                    continue;
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_FLUSH_MODIFICATIONS_DEADLINE))
                {
                    chunk.FlushModifications();
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_FINISH_MESH_DEADLINE))
                {
                    RenderService.FinishMeshGeneration(chunk);
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_MESH_DEADLINE) &&
                    AsyncService.ThreadQueueSize() < Configuration.PERFORMANCE_MAX_THREAD_QUEUE_SIZE)
                {
                    RenderService.GenerateMeshes(chunk); // Threaded
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_MARK_CHUNKS_FOR_MESH_UPDATE_DEADLINE))
                {
                    RenderService.MarkSurroundingChunksForMeshUpdate(chunk);
                }

                if (AsyncService.FrameElapsedPercentageIsNotExceeded(Configuration.PERFORMANCE_GENERATE_BLOCKS_DEADLINE) &&
                    AsyncService.ThreadQueueSize() < Configuration.PERFORMANCE_MAX_THREAD_QUEUE_SIZE)
                {
                    chunk.GenerateBlocks(GetSeed()); // Threaded
                }

                if (chunk.IsFinishedProcessing())
                {
                    ChunkRepository.RemoveFromProcessingChunkList(chunk);
                }

                i++;
            }

            GeneratorService.UnloadDeadChunks();
            RenderService.CullChunks();

            int numberOfChunks = ChunkRepository.NumberOfChunks();

            for (int chunkIndex = 0; chunkIndex < numberOfChunks; chunkIndex++)
            {
                Chunk chunk = ChunkRepository.GetChunkAtIndex(chunkIndex);
                if (chunk.IsFinishedProcessing())
                {
                    Vector3 position;
                    position.x = chunk.WorldPosition().x *Chunk.SIZE;
                    position.y = chunk.WorldPosition().y *Chunk.SIZE;
                    position.z = chunk.WorldPosition().z *Chunk.SIZE;

                    float distance = Vector3.Distance(position, Camera.main.transform.position);
                    if (distance > fogDistance)
                    {
                        fogDistance = distance;
                    }
                }
            }
            SendMessage("UpdateFogDistance", fogDistance, SendMessageOptions.DontRequireReceiver);

            GeneratorService.CleanupOldChunks();
            AsyncService.RePrioritizeCPUMediatorWork();
            ChunkRepository.RePrioritizeSortProcessingChunkList();
            ChunkRepository.FlushProcessingChunkListModifications();

            if (Input.GetKeyDown(KeyCode.F12))
            {
                showDebugMenu = !showDebugMenu;
            }

            if (Input.GetKeyDown(KeyCode.F5))
            {
                SaveScreenshot();
            }

            if (Input.GetKeyDown(KeyCode.F9))
            {
                ChunkRepository.DumpProcessingChunkListDebugData();
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
        }