streaming assets
This commit is contained in:
72
engine.js
72
engine.js
@@ -97,7 +97,11 @@ export class Engine {
|
||||
|
||||
this.atlasTexture = gl.createTexture();
|
||||
|
||||
this.cache = new Cache('assets');
|
||||
this.cache = new Cache('assets', () => {
|
||||
for (const frame of this.observedFrames) this.dirtyFrames.add(frame);
|
||||
for (const diorama of this.dioramas) diorama.dirty = true;
|
||||
this.requestUpdate();
|
||||
});
|
||||
this.atlas = new TextureHandler(256);
|
||||
this.cache.register('blockstates', new BlockstateHandler());
|
||||
this.cache.register('models', new ModelHandler());
|
||||
@@ -120,7 +124,7 @@ export class Engine {
|
||||
if (!this.updateRequested) {
|
||||
this.updateRequested = true;
|
||||
Promise.resolve().then(async () => {
|
||||
await this.updateAll();
|
||||
this.updateAll();
|
||||
this.updateRequested = false;
|
||||
});
|
||||
}
|
||||
@@ -142,11 +146,12 @@ export class Engine {
|
||||
|
||||
if (!this.observedFrames.has(diorama.frame)) {
|
||||
this.observedFrames.add(diorama.frame);
|
||||
this.dirtyFrames.add(diorama.frame); // Force initial build
|
||||
this.dirtyFrames.add(diorama.frame);
|
||||
|
||||
this.preloadWorld(diorama.frame.world);
|
||||
|
||||
diorama.frame.subscribe(() => {
|
||||
this.dirtyFrames.add(diorama.frame);
|
||||
// Mark any diorama observing this shared timeline as dirty
|
||||
for (const d of this.dioramas) {
|
||||
if (d.frame === diorama.frame) d.dirty = true;
|
||||
}
|
||||
@@ -157,35 +162,59 @@ export class Engine {
|
||||
}
|
||||
}
|
||||
|
||||
async updateAll() {
|
||||
async preloadWorld(world) {
|
||||
const blocks = world.getUniqueBlockStates();
|
||||
|
||||
// 1. Await all Blockstates
|
||||
const uniqueIds = new Set(blocks.map(b => b.id));
|
||||
await Promise.all(Array.from(uniqueIds).map(id => this.cache.getAsync(id, 'blockstates')));
|
||||
|
||||
// 2. Resolve permutations and await all Models
|
||||
const uniqueModels = new Set();
|
||||
for (const block of blocks) {
|
||||
const stateDef = this.cache.getSync(block.id, 'blockstates');
|
||||
const parts = stateDef.resolveParts(block.state);
|
||||
for (const p of parts) uniqueModels.add(p.model);
|
||||
}
|
||||
await Promise.all(Array.from(uniqueModels).map(id => this.cache.getAsync(id, 'models')));
|
||||
|
||||
// 3. Scan geometry and await all Textures
|
||||
const textureTasks = [];
|
||||
for (const modelId of uniqueModels) {
|
||||
const blockModel = this.cache.getSync(modelId, 'models');
|
||||
for (const el of blockModel.elements) {
|
||||
for (const face of Object.values(el.faces || {})) {
|
||||
const texPath = blockModel.resolveTexture(face.texture);
|
||||
if (texPath && texPath !== ':missing') {
|
||||
textureTasks.push(this.cache.getAsync(texPath, 'textures'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await Promise.all(textureTasks);
|
||||
}
|
||||
|
||||
updateAll() {
|
||||
if (this.dirtyFrames.size === 0) return;
|
||||
|
||||
// Isolate only the frames that mutated
|
||||
const framesToUpdate = Array.from(this.dirtyFrames);
|
||||
this.dirtyFrames.clear();
|
||||
|
||||
const uniqueBlockIds = new Set();
|
||||
for (const frame of framesToUpdate) {
|
||||
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 parsedFrames = new Map();
|
||||
const uniqueModelIds = new Set();
|
||||
|
||||
for (const frame of framesToUpdate) {
|
||||
const parsedBlocks = [];
|
||||
for (const block of frame.blocks.values()) {
|
||||
const state = await this.cache.get(block.id, 'blockstates');
|
||||
// Sync grab (returns fallback instantly if loading)
|
||||
const state = this.cache.getSync(block.id, 'blockstates');
|
||||
const parts = state.resolveParts(block.state);
|
||||
parsedBlocks.push({block, parts});
|
||||
for (const p of parts) uniqueModelIds.add(p.model);
|
||||
}
|
||||
parsedFrames.set(frame, parsedBlocks);
|
||||
}
|
||||
await Promise.all(Array.from(uniqueModelIds).map(id => this.cache.get(id, 'models')));
|
||||
|
||||
const textureTasks = [];
|
||||
const framePools = new Map();
|
||||
|
||||
for (const frame of framesToUpdate) {
|
||||
const instancePool = new Map();
|
||||
for (const {block, parts} of parsedFrames.get(frame)) {
|
||||
@@ -210,12 +239,12 @@ export class Engine {
|
||||
}
|
||||
|
||||
for (const pool of instancePool.values()) {
|
||||
const blockModel = await this.cache.get(pool.partDef.model, 'models');
|
||||
const blockModel = this.cache.getSync(pool.partDef.model, 'models');
|
||||
for (const el of blockModel.elements) {
|
||||
for (const face of Object.values(el.faces || {})) {
|
||||
const texPath = blockModel.resolveTexture(face.texture);
|
||||
if (texPath) {
|
||||
textureTasks.push(this.cache.get(texPath, 'textures', 'png'));
|
||||
this.cache.getSync(texPath, 'textures');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,11 +252,9 @@ export class Engine {
|
||||
framePools.set(frame, instancePool);
|
||||
}
|
||||
|
||||
await Promise.all(textureTasks);
|
||||
this.updateAtlasTexture();
|
||||
|
||||
for (const frame of framesToUpdate) {
|
||||
// Memory Cleanup: Free old buffers from the GPU before creating new ones
|
||||
const oldMeshes = this.frameMeshes.get(frame);
|
||||
if (oldMeshes) {
|
||||
for (const mesh of oldMeshes) {
|
||||
@@ -238,7 +265,7 @@ export class Engine {
|
||||
|
||||
const newMeshes = [];
|
||||
for (const pool of framePools.get(frame).values()) {
|
||||
const blockModel = await this.cache.get(pool.partDef.model, 'models');
|
||||
const blockModel = this.cache.getSync(pool.partDef.model, 'models');
|
||||
const geometry = blockModel.buildGeometry(this.atlas.uvmap, pool.partDef);
|
||||
|
||||
const matrixArray = new Float32Array(pool.matrices);
|
||||
@@ -250,7 +277,6 @@ export class Engine {
|
||||
this.frameMeshes.set(frame, newMeshes);
|
||||
}
|
||||
|
||||
// Guarantee that dioramas relying on these newly built meshes are flagged for drawing
|
||||
for (const d of this.dioramas) {
|
||||
if (framesToUpdate.includes(d.frame)) d.dirty = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user