working blockstates

This commit is contained in:
2026-07-03 22:32:48 -04:00
parent 2fa76da8bc
commit a702916de3
7 changed files with 380 additions and 178 deletions

48
scene.js Normal file
View File

@@ -0,0 +1,48 @@
// scene.js
import { resolveBlock } from './blockstate.js';
import { mat4Identity, mat4Translate } from './math.js';
// ... imports for loading assets ...
export class SceneBuilder {
constructor(renderer, atlas) {
this.renderer = renderer;
this.atlas = atlas;
// Cache parsed geometries by model ID so we only build them once
this.geometryCache = new Map();
}
async buildFromWorld(world) {
// 1. Group all instances by their resolved Model ID
const instancePool = new Map(); // "block/stone" -> [ matrix1, matrix2, ... ]
for (const block of world.blocks.values()) {
const stateJSON = await loadBlockstate(block.id);
const parts = resolveBlock(stateJSON, block.props);
for (const part of parts) {
if (!instancePool.has(part.model)) {
instancePool.set(part.model, {
parts: [],
modelId: part.model
});
}
// Calculate the world position matrix
const matrix = mat4Identity(new Float32Array(16));
mat4Translate(matrix, matrix, [block.x, block.y, block.z]);
instancePool.get(part.model).parts.push({
matrix: matrix,
variant: part // Pass variant so geometry builder handles local rotation
});
}
}
// 2. We now have a list of models and every place they appear.
// The next step is to ensure geometry is built, and pack those matrices
// into Float32Arrays for the WebGL instanced buffers.
return instancePool;
}
}