private void ProcessGeneratorJobs(NativeList <int3> jobsToRemove) { NativeKeyValueArrays <int3, ChunkJobData> jobs = generateJobs.GetKeyValueArrays(Allocator.Temp); for (int i = 0; i < jobs.Keys.Length; i++) { ChunkJobData data = jobs.Values[i]; if (data.urgent || data.job.IsCompleted || data.frameCounter >= maxJobFrames) { data.job.Complete(); Chunk chunk = chunks[data.position]; chunk.CompleteGenerating(); if (!chunk.RequestedRemoval && chunk.render) { TryToQueueChunkRender(chunk, data.priority); } jobsToRemove.Add(jobs.Keys[i]); } else { data.frameCounter++; generateJobs[jobs.Keys[i]] = data; } } jobs.Dispose(); for (int i = 0; i < jobsToRemove.Length; i++) { generateJobs.Remove(jobsToRemove[i]); } }
private bool AddChunkToRenderList(Chunk chunk, float priority = 0) { if (renderJobs.ContainsKey(chunk.position) || chunk.RequestedRemoval) { return(false); } JobHandle job = chunk.ScheduleRenderJob(); ChunkJobData data = new ChunkJobData(chunk.position, job, priority, false); renderJobs.Add(chunk.position, data); return(true); }
private void ProcessRenderJobs(NativeList <int3> jobsToRemove) { NativeKeyValueArrays <int3, ChunkJobData> jobs = renderJobs.GetKeyValueArrays(Allocator.Temp); for (int i = 0; i < jobs.Keys.Length; i++) { ChunkJobData data = jobs.Values[i]; if (data.urgent || data.job.IsCompleted || data.frameCounter >= maxJobFrames) { data.job.Complete(); Chunk chunk = chunks[data.position]; if (useRendererPrefab) { CreateChunkRenderer(data.position, chunk.CompleteMeshUpdate(chunk.mesh)); } else { chunk.CompleteMeshUpdate(chunk.mesh); } if (!chunk.RequestedRemoval) { AddToQueue(colliderQueue, data.position, data.priority); } jobsToRemove.Add(jobs.Keys[i]); } else { data.frameCounter++; renderJobs[jobs.Keys[i]] = data; } } jobs.Dispose(); for (int i = 0; i < jobsToRemove.Length; i++) { renderJobs.Remove(jobsToRemove[i]); } }
private void ProcessColliderJobs(NativeList <int3> jobsToRemove) { NativeKeyValueArrays <int3, ChunkJobData> jobs = colliderJobs.GetKeyValueArrays(Allocator.Temp); for (int i = 0; i < jobs.Keys.Length; i++) { ChunkJobData data = jobs.Values[i]; if (data.urgent || data.job.IsCompleted || data.frameCounter >= maxJobFrames) { data.job.Complete(); Chunk chunk = chunks[data.position]; if (!chunkColliders.TryGetValue(data.position, out MeshCollider collider)) { collider = GetChunkCollider(); chunkColliders.Add(data.position, collider); #if DEBUG collider.gameObject.name = "Collider [" + data.position.x + "," + data.position.y + "," + data.position.z + "]"; #endif } collider.sharedMesh = chunk.CompleteColliderMeshUpdate(collider.sharedMesh); jobsToRemove.Add(jobs.Keys[i]); } else { data.frameCounter++; colliderJobs[jobs.Keys[i]] = data; } } jobs.Dispose(); for (int i = 0; i < jobsToRemove.Length; i++) { colliderJobs.Remove(jobsToRemove[i]); } }
private void LateUpdateChunks() { if (generatingChunks) { generatingChunksFrame++; if (generateChunksJob.IsCompleted || generatingChunksFrame >= maxJobFrames) { FinishGeneratingChunks(); } } int numChunks = 0; while (generateQueue.Count > 0) { if (generator == null) { break; } if (numChunks > maxGenerateJobs) { break; } ChunkNode node = generateQueue.Dequeue(); if (chunks.TryGetValue(node.position, out Chunk chunk)) { if (generateJobs.ContainsKey(chunk.position) || !chunk.NeedsTerrain || chunk.RequestedRemoval || chunk.GeneratingTerrain) { continue; } chunk.StartGenerating(new NativeArray <int>(Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE, Allocator.TempJob)); JobHandle job = generator.GenerateChunk(chunk.temporaryBlocks, chunk.position); ChunkJobData data = new ChunkJobData(node.position, job, node.Priority, false); generateJobs.Add(chunk.position, data); numChunks++; } } numChunks = 0; while (renderQueue.Count > 0) { if (numChunks > maxRenderJobs) { break; } ChunkNode node = renderQueue.Dequeue(); if (chunks.TryGetValue(node.position, out Chunk chunk)) { if (AddChunkToRenderList(chunk, node.Priority)) { numChunks++; } } } numChunks = 0; while (colliderQueue.Count > 0) { if (numChunks > maxColliderJobs) { break; } ChunkNode node = colliderQueue.Dequeue(); if (chunks.TryGetValue(node.position, out Chunk chunk)) { if (colliderJobs.ContainsKey(node.position) || chunk.RequestedRemoval) { continue; } JobHandle job = chunk.ScheduleColliderJob(); colliderJobs.Add(node.position, new ChunkJobData(node.position, job, node.Priority, false)); numChunks++; } } }