From 6f8ca350ca7a88d67ddd1ad309965ffaf7d839fa Mon Sep 17 00:00:00 2001 From: David Allemang Date: Sun, 5 Jul 2026 20:55:54 -0400 Subject: [PATCH] split 'world', 'frame'. --- diorama.js | 10 ++-- engine.js | 44 +++++++-------- index.html | 144 +++++++++++++++++++----------------------------- world.js | 159 ++++++++++++++++++++++++++--------------------------- 4 files changed, 162 insertions(+), 195 deletions(-) diff --git a/diorama.js b/diorama.js index 8758bb9c..f0d8f83e 100644 --- a/diorama.js +++ b/diorama.js @@ -1,17 +1,17 @@ import {mat4Ortho, mat4LookAt, mat4Multiply} from './math.js'; export class Diorama { - constructor(elementId, world, requestRenderCallback) { + constructor(elementId, frame, requestRenderCallback) { this.element = document.getElementById(elementId); this.canvas = document.createElement('canvas') this.element.appendChild(this.canvas) this.ctx2d = this.canvas.getContext('2d'); - this.world = world; + this.frame = frame; this.requestRender = requestRenderCallback; this.target = [0.5, 0, 0.5]; this.radius = 4; - this.theta = 45; + this.theta = 135; this.phi = 30; this.projMatrix = new Float32Array(16); @@ -73,7 +73,7 @@ export class Diorama { } centerView() { - if (this.world.blocks.size === 0) { + if (this.frame.blocks.size === 0) { this.target = [0.5, 0.5, 0.5]; return; } @@ -81,7 +81,7 @@ export class Diorama { let minX = Infinity, minY = Infinity, minZ = Infinity; let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity; - for (const block of this.world.blocks.values()) { + for (const block of this.frame.blocks.values()) { minX = Math.min(minX, block.pos[0]); minY = Math.min(minY, block.pos[1]); minZ = Math.min(minZ, block.pos[2]); diff --git a/engine.js b/engine.js index 8cac0b69..72cd7412 100644 --- a/engine.js +++ b/engine.js @@ -104,9 +104,9 @@ export class Engine { this.cache.register('textures', this.atlas); this.dioramas = []; - this.worldMeshes = new Map(); + this.frameMeshes = new Map(); this.renderRequested = false; - this.observedWorlds = new Set(); + this.observedFrames = new Set(); this.updateRequested = false; window.addEventListener('resize', () => this.requestRender()); @@ -137,43 +137,43 @@ export class Engine { addDiorama(diorama) { this.dioramas.push(diorama); - if (!this.observedWorlds.has(diorama.world)) { - this.observedWorlds.add(diorama.world); - diorama.world.subscribe(() => this.requestUpdate()); + if (!this.observedFrames.has(diorama.frame)) { + this.observedFrames.add(diorama.frame); + diorama.frame.subscribe(() => this.requestUpdate()); } } async updateAll() { - const uniqueWorlds = new Set(this.dioramas.map(d => d.world)); - if (uniqueWorlds.size === 0) return; + const uniqueFrames = new Set(this.dioramas.map(d => d.frame)); + if (uniqueFrames.size === 0) return; const uniqueBlockIds = new Set(); - for (const world of uniqueWorlds) { - for (const block of world.blocks.values()) uniqueBlockIds.add(block.id); + for (const frame of uniqueFrames) { + for (const block of frame.blocks.values()) uniqueBlockIds.add(block.id); } await Promise.all(Array.from(uniqueBlockIds).map(id => this.cache.get(id, 'blockstates'))); const uniqueModelIds = new Set(); - const parsedWorlds = new Map(); + const parsedFrames = new Map(); - for (const world of uniqueWorlds) { + for (const frame of uniqueFrames) { const parsedBlocks = []; - for (const block of world.blocks.values()) { + for (const block of frame.blocks.values()) { const state = await this.cache.get(block.id, 'blockstates'); const parts = state.resolveParts(block.state); parsedBlocks.push({block, parts}); for (const p of parts) uniqueModelIds.add(p.model); } - parsedWorlds.set(world, parsedBlocks); + parsedFrames.set(frame, parsedBlocks); } await Promise.all(Array.from(uniqueModelIds).map(id => this.cache.get(id, 'models'))); const textureTasks = []; - const worldPools = new Map(); + const framePools = new Map(); - for (const world of uniqueWorlds) { + for (const frame of uniqueFrames) { const instancePool = new Map(); - for (const {block, parts} of parsedWorlds.get(world)) { + for (const {block, parts} of parsedFrames.get(frame)) { for (const part of parts) { const hash = Blockstate.getVariantHash(part); if (!instancePool.has(hash)) { @@ -205,15 +205,15 @@ export class Engine { } } } - worldPools.set(world, instancePool); + framePools.set(frame, instancePool); } await Promise.all(textureTasks); this.updateAtlasTexture(); - for (const world of uniqueWorlds) { + for (const frame of uniqueFrames) { const newMeshes = []; - for (const pool of worldPools.get(world).values()) { + for (const pool of framePools.get(frame).values()) { const blockModel = await this.cache.get(pool.partDef.model, 'models'); const geometry = blockModel.buildGeometry(this.atlas.uvmap, pool.partDef); @@ -223,7 +223,7 @@ export class Engine { newMeshes.push(this.createInstancedMesh(geometry, instanceCount, matrixArray, colorArray)); } - this.worldMeshes.set(world, newMeshes); + this.frameMeshes.set(frame, newMeshes); } this.requestRender(); @@ -317,11 +317,11 @@ export class Engine { const viewProj = diorama.updateMatrices(aspect); gl.uniformMatrix4fv(this.uniforms.viewProj, false, viewProj); - gl.uniform3f(this.uniforms.lightDir, 1.0, 3.0, -2.0); + 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]); gl.uniform3f(this.uniforms.upDir, diorama.upDir[0], diorama.upDir[1], diorama.upDir[2]); - const meshes = this.worldMeshes.get(diorama.world) || []; + const meshes = this.frameMeshes.get(diorama.frame) || []; for (const mesh of meshes) { gl.bindVertexArray(mesh.vao); gl.drawElementsInstanced(gl.TRIANGLES, mesh.indexCount, gl.UNSIGNED_SHORT, 0, mesh.instanceCount); diff --git a/index.html b/index.html index 7431c0b0..51dff18f 100644 --- a/index.html +++ b/index.html @@ -52,77 +52,76 @@