Skip to content

Commit 727c3cd

Browse files
authored
fix(voxel-renderer): drain empty VoxelLayer (#239)
1 parent 934aee7 commit 727c3cd

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

.changeset/tame-camels-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@jolly-pixel/voxel.renderer": patch
3+
---
4+
5+
Drain and remove empty VoxelLayer

packages/voxel-renderer/src/world/VoxelLayer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class VoxelLayer {
7373
#visible: boolean;
7474
#chunks = new Map<string, VoxelChunk>();
7575
#chunkSize: number;
76+
#pendingRemoval: VoxelChunk[] = [];
7677

7778
constructor(
7879
options: VoxelLayerOptions
@@ -235,6 +236,7 @@ export class VoxelLayer {
235236
this.#chunks.delete(
236237
this.#createChunkKey(cx, cy, cz)
237238
);
239+
this.#pendingRemoval.push(chunk);
238240
}
239241
}
240242

@@ -320,6 +322,12 @@ export class VoxelLayer {
320322
yield* this.#chunks.values();
321323
}
322324

325+
* drainPendingRemovals(): IterableIterator<VoxelChunk> {
326+
while (this.#pendingRemoval.length > 0) {
327+
yield this.#pendingRemoval.pop()!;
328+
}
329+
}
330+
323331
get chunkCount(): number {
324332
return this.#chunks.size;
325333
}

packages/voxel-renderer/src/world/VoxelWorld.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ export class VoxelWorld {
399399
yield { layer, chunk };
400400
}
401401
} while (this.#layersToRemove.length > 0);
402+
403+
// Drain individual chunks that became empty this frame.
404+
// Their Three.js meshes must be removed even though the layer is still active.
405+
for (const layer of this.#layers) {
406+
for (const chunk of layer.drainPendingRemovals()) {
407+
yield { layer, chunk };
408+
}
409+
}
402410
}
403411

404412
clear(): void {

0 commit comments

Comments
 (0)