private void Update() { if (killswitch) { return; } if (activeChunks.Count > 64) { killswitch = true; Debug.LogError("Unexpectedly high amount of active rain chunks. Disabling rain."); foreach (var activeChunk in activeChunks) { pool.Release(activeChunk.Value); } activeChunks.Clear(); return; } if (trackedEntities.Count == 0) { return; } if (Mathf.Approximately(currentIntensity, 0f)) { foreach (var activeChunk in activeChunks) { pool.Release(activeChunk.Value); } activeChunks.Clear(); return; } var chunkBoundaryOffsetVector = new Vector2(cullDistance, cullDistance); var sqrDist = cullDistance * cullDistance; foreach (var entity in trackedEntities) { var worldPos3d = entity.localPosition; var worldPos = new Vector2(worldPos3d.x, worldPos3d.z); var min = GetChunkPosition(worldPos - chunkBoundaryOffsetVector); var max = GetChunkPosition(worldPos + chunkBoundaryOffsetVector); for (var x = min.x; x <= max.x; ++x) { for (var y = min.y; y <= max.y; ++y) { var chunkCenter = new Vector2(x + 0.5f, y + 0.5f) * chunkSize; if ((chunkCenter - worldPos).sqrMagnitude > sqrDist) { continue; } toSpawn.Add(new Vector2Int(x, y)); } } } foreach (var activeChunk in activeChunks) { if (toSpawn.Contains(activeChunk.Key)) { toSpawn.Remove(activeChunk.Key); } else { toRelease.Add(activeChunk.Key); } } foreach (var key in toRelease) { reusable.Push(activeChunks[key]); activeChunks.Remove(key); } foreach (var key in toSpawn) { var effect = reusable.Count > 0 ? reusable.Pop() : pool.Get(); FitToChunk(effect, key); activeChunks[key] = effect; } while (reusable.Count > 0) { pool.Release(reusable.Pop()); } toSpawn.Clear(); toRelease.Clear(); }