split 'world', 'frame'.
This commit is contained in:
44
engine.js
44
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);
|
||||
|
||||
Reference in New Issue
Block a user