Bound renderer memory use and configure worker heap ceiling

This commit is contained in:
2026-10-04 19:17:15 +05:00
parent a44e28c99f
commit 3b3cf6997c
4 changed files with 47 additions and 6 deletions

View File

@@ -160,9 +160,12 @@ window.buildTerrain = async (snapshot) => {
const started = performance.now(),
batches = new Map(),
unsupported = new Set();
const occupancy = new Map(
snapshot.blocks.map((b) => [`${b[0]},${b[1]},${b[2]}`, b[3]]),
);
const occupancy = new Map();
const distinctStates = new Set();
for (const b of snapshot.blocks) {
occupancy.set(`${b[0]},${b[1]},${b[2]}`, b[3]);
distinctStates.add(b[3]);
}
const stateCache = new Map();
async function stateModels(s) {
if (stateCache.has(s)) return stateCache.get(s);
@@ -179,7 +182,7 @@ window.buildTerrain = async (snapshot) => {
return variants;
}
// Preload each distinct model once before constructing batched geometry.
for (const s of new Set(snapshot.blocks.map((b) => b[3])))
for (const s of distinctStates)
await stateModels(s);
async function emit(block, m, v = {}) {
const [bx, by, bz, s] = block;
@@ -310,6 +313,7 @@ window.buildTerrain = async (snapshot) => {
}
for (const { m, v } of await stateModels(b[3])) await emit(b, m, v);
}
occupancy.clear();
for (const batch of batches.values()) {
const g = new THREE.BufferGeometry();
g.setAttribute(
@@ -331,6 +335,11 @@ window.buildTerrain = async (snapshot) => {
opacity: batch.tex.image.src.includes("water") ? 0.8 : 1,
});
scene.add(new THREE.Mesh(g, mat));
// Drop the JS number arrays as each GPU geometry is completed.
batch.positions.length = 0;
batch.normals.length = 0;
batch.uvs.length = 0;
batch.indices.length = 0;
}
window.terrainBounds = snapshot.bounds;
return {