parsing refactor

This commit is contained in:
2026-07-04 17:43:40 -04:00
parent 88f601501e
commit b5c318de1b
8 changed files with 460 additions and 325 deletions

View File

@@ -1,8 +1,9 @@
// engine.js
import {mat4Identity, mat4Translate} from './math.js';
import {loadBlockstate, loadModel, TextureAtlas} from './assets.js';
import {resolveTexture, buildGeometry} from './geometry.js';
import {resolveBlock, getVariantHash} from './blockstate.js';
import {ResourceCache} from "./resource-cache.js";
import {Blockstate, BlockstateHandler} from "./blockstate.js";
import {ModelHandler} from "./model.js";
import {TextureHandler} from "./texture.js";
const VS_SRC = `#version 300 es
layout(location=0) in vec3 a_position;
@@ -91,7 +92,13 @@ export class Engine {
};
this.atlasTexture = gl.createTexture();
this.atlas = new TextureAtlas(256);
// --- Initialize the new Resource Pipeline ---
this.cache = new ResourceCache('assets');
this.atlas = new TextureHandler(256);
this.cache.register('blockstates', new BlockstateHandler());
this.cache.register('models', new ModelHandler());
this.cache.register('textures', this.atlas);
this.dioramas = [];
this.worldMeshes = new Map();
@@ -111,12 +118,10 @@ export class Engine {
requestUpdate() {
if (!this.updateRequested) {
this.updateRequested = true;
// Promise.resolve().then() executes immediately after the current synchronous
// call stack finishes, ensuring 10 simultaneous setBlock calls only trigger 1 update.
Promise.resolve().then(() => {
// Add 'async' here and 'await' updateAll
Promise.resolve().then(async () => {
await this.updateAll();
this.updateRequested = false;
this.updateAll();
});
}
}
@@ -138,52 +143,48 @@ export class Engine {
addDiorama(diorama) {
this.dioramas.push(diorama);
// Subscribe to the world if we aren't already watching it
if (!this.observedWorlds.has(diorama.world)) {
this.observedWorlds.add(diorama.world);
// Whenever the world changes, queue a single, debounced geometry rebuild
diorama.world.subscribe(() => this.requestUpdate());
}
}
async updateAll() {
// Extract all unique worlds currently being viewed
const uniqueWorlds = new Set(this.dioramas.map(d => d.world));
if (uniqueWorlds.size === 0) return;
// --- SWEEP 1: Collect & Fetch Blockstates ---
const uniqueBlockIds = new Set();
for (const world of uniqueWorlds) {
for (const block of world.blocks.values()) uniqueBlockIds.add(block.id);
}
await Promise.all(Array.from(uniqueBlockIds).map(id => loadBlockstate(id)));
await Promise.all(Array.from(uniqueBlockIds).map(id => this.cache.get(id, 'blockstates')));
// --- SWEEP 2: Resolve Parts & Fetch Models ---
const uniqueModelIds = new Set();
const parsedWorlds = new Map(); // World -> Array of {block, parts}
const parsedWorlds = new Map();
for (const world of uniqueWorlds) {
const parsedBlocks = [];
for (const block of world.blocks.values()) {
const stateJSON = await loadBlockstate(block.id);
const parts = resolveBlock(stateJSON, block.props);
const state = await this.cache.get(block.id, 'blockstates');
const parts = state.resolveParts(block.props);
parsedBlocks.push({block, parts});
for (const p of parts) uniqueModelIds.add(p.model);
}
parsedWorlds.set(world, parsedBlocks);
}
await Promise.all(Array.from(uniqueModelIds).map(id => loadModel(id)));
await Promise.all(Array.from(uniqueModelIds).map(id => this.cache.get(id, 'models')));
// --- SWEEP 3: Pool Instances & Fetch Textures ---
const uniqueTexturePaths = new Set();
const worldPools = new Map(); // World -> instancePool
const textureTasks = [];
const worldPools = new Map();
for (const world of uniqueWorlds) {
const instancePool = new Map();
for (const {block, parts} of parsedWorlds.get(world)) {
for (const part of parts) {
const hash = getVariantHash(part);
const hash = Blockstate.getVariantHash(part);
if (!instancePool.has(hash)) {
instancePool.set(hash, {partDef: part, matrices: [], colors: []});
}
@@ -203,29 +204,34 @@ export class Engine {
}
for (const pool of instancePool.values()) {
const modelJSON = await loadModel(pool.partDef.model);
for (const el of modelJSON.elements || []) {
const blockModel = await this.cache.get(pool.partDef.model, 'models');
for (const el of blockModel.elements) {
for (const face of Object.values(el.faces || {})) {
const texPath = resolveTexture(modelJSON, face.texture);
if (texPath) uniqueTexturePaths.add(texPath);
const texPath = blockModel.resolveTexture(face.texture);
if (texPath) {
// Synchronously allocates UV, asynchronously fetches image
textureTasks.push(this.cache.get(texPath, 'textures', 'png'));
}
}
}
}
worldPools.set(world, instancePool);
}
await Promise.all(Array.from(uniqueTexturePaths).map(path => this.atlas.load(path)));
await Promise.all(textureTasks);
this.updateAtlasTexture();
// --- SWEEP 4: Bake Geometry ---
for (const world of uniqueWorlds) {
const newMeshes = [];
for (const pool of worldPools.get(world).values()) {
const modelJSON = await loadModel(pool.partDef.model);
const geometry = buildGeometry(modelJSON, this.atlas, pool.partDef);
const blockModel = await this.cache.get(pool.partDef.model, 'models');
const geometry = blockModel.buildGeometry(this.atlas.uvmap, pool.partDef);
const matrixArray = new Float32Array(pool.matrices);
const colorArray = new Float32Array(pool.colors);
const instanceCount = pool.matrices.length / 16;
newMeshes.push(this.createInstancedMesh(geometry, instanceCount, matrixArray, colorArray));
}
this.worldMeshes.set(world, newMeshes);
@@ -309,11 +315,10 @@ export class Engine {
const aspect = rect.width / rect.height;
const viewProj = diorama.updateMatrices(aspect);
gl.uniformMatrix4fv(this.uniforms.viewProj, false, viewProj);
gl.uniform3f(this.uniforms.lightDir, 1.0, 3.0, 2);
gl.uniform3f(this.uniforms.viewDir,
diorama.viewDir[0], diorama.viewDir[1], diorama.viewDir[2]
);
gl.uniform3f(this.uniforms.lightDir, 1.0, 3.0, 2.0);
gl.uniform3f(this.uniforms.viewDir, diorama.viewDir[0], diorama.viewDir[1], diorama.viewDir[2]);
const meshes = this.worldMeshes.get(diorama.world) || [];
for (const mesh of meshes) {