public ChunkMachine(int LODs, float MinCellSize, int MaxNumChunksToPrepare, int Resolution, ComputeShader NoiseComputeShader, GameObject ChunkPrefab, Transform ChunkParent) { this.LODs = LODs; this.MinCellSize = MinCellSize; this.Resolution = Resolution; this.ChunkPrefab = ChunkPrefab; this.MaxNumToPrepare = MaxNumChunksToPrepare; this.NoiseComputeShader = NoiseComputeShader; this.ChunkParent = ChunkParent; this.ActiveJob = false; PreparingChunksMode = false; CurrentlyManagingChunks = false; ChunkJobQueuer.Initialize(); this.LoadedChunks = new Hashtable(); this.ResultPool = new CObjectPool <ChunkManageResult>(() => new ChunkManageResult()); UsedInput = new ChunkManageInput(); LoadedChunksCenter = new Vector3(float.MinValue, float.MinValue, float.MinValue); UnloadedChunksCenter = new Vector3(float.MinValue, float.MinValue, float.MinValue); UsedInput.LoadedChunks = LoadedChunks; UsedInput.LODs = LODs; UsedInput.MinSizeOfCell = MinCellSize; UsedInput.Resolution = Resolution; }
public void Update(Vector3 PlayerLocation) { if (PreparingChunksMode) { PrepareChunks(MaxNumToPrepare); return; } else if (CurrentlyManagingChunks) { ChunkManageResult ManageResult = ChunkManager.CheckManageChunks(); if (ManageResult != null) { CurrentlyManagingChunks = false; if (ManageResult.NewState == ChunkWorkState.DoNewJob) { this.ActiveJob = true; LastResult = ManageResult; UnloadedChunksCenter = ManageResult.NewCenter; MostRecentAllChunkJobList = ManageResult.AllChunkJobs; ChunkJobQueuer.QueueTasks(ManageResult.Jobs.ToArray()); } } } else if (UnityEngine.Time.time - LastUpdateTime > 3.0f) { LastUpdateTime = UnityEngine.Time.time; if (this.ActiveJob) { CheckForAndProcessLoadedChunks(); return; } UsedInput.LoadedChunks = LoadedChunks; UsedInput.PlayerLocation = PlayerLocation; UsedInput.CurrentChunksCenter = LoadedChunksCenter; Task.Run(() => ChunkManager.ManageChunks(UsedInput)); CurrentlyManagingChunks = true; return; } }
private void CheckForAndProcessLoadedChunks() { ChunkJobResult[] Results = ChunkJobQueuer.CheckStatus(); if (Results != null) { UnityEngine.Debug.Log("All " + Results.Length + " jobs finished."); PreparingChunksMode = true; ActiveJob = false; LoadedChunksCenter = UnloadedChunksCenter; ResetPrepareCollections(); Hashtable NewChunks = new Hashtable(); // For each currently loaded chunk... foreach (DictionaryEntry c1 in LoadedChunks) { bool shouldDestroy = true; // Check if it's in the most recent chunk job list foreach (ChunkJob c2 in MostRecentAllChunkJobList) { // If it is, don't destroy it and add it to the new chunks list if ((string)c1.Key == c2.Key) { shouldDestroy = false; SavedChunks.Add((Chunk)c1.Value); break; } } // Otherwise, destroy it if (shouldDestroy) { //UnityEngine.Debug.Log("Destroying chunk"); //UnityEngine.Debug.Log("((Chunk)(c1.Value)).Object.name" + ((Chunk)(c1.Value)).Object.name); //UnityEngine.Object.Destroy(((Chunk)(c1.Value)).Object); DestroyList.Add((Chunk)c1.Value); } } long totalProcessingTime = 0; // For each chunk job... foreach (ChunkJobResult JobRes in Results) { if (JobRes == null) { UnityEngine.Debug.Log("JobResult is null."); } totalProcessingTime += JobRes.ProcessingTime; // Load its chunk RealizeList.Enqueue(JobRes); //Chunk c = ChunkManager.RealizeChunk(JobRes, MeshPrefab, MeshParent); //NewChunks.Add(c.Key, c); } UnityEngine.Debug.Log("Average processing time: " + (totalProcessingTime / Results.Length) + "ms."); //LoadedChunks = NewChunks; } }