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

View File

@@ -40,20 +40,13 @@
import {loadBlockstate, loadModel, TextureAtlas} from './assets.js';
import {resolveTexture, buildGeometry} from './geometry.js';
import {Renderer} from './renderer.js';
import {
mat4Identity,
mat4Ortho,
mat4LookAt,
mat4Multiply,
mat4Translate,
mat4RotateY
} from './math.js';
import {mat4Identity, mat4Ortho, mat4LookAt, mat4Multiply, mat4Translate} from './math.js';
import {World} from './world.js';
import {resolveBlock} from './blockstate.js';
// Matches block properties against vanilla variant strings
function resolveVariant(blockstate, properties) {
const propStr = Object.keys(properties).sort().map(k => `${k}=${properties[k]}`).join(',');
let variantDef = blockstate.variants[propStr] || blockstate.variants[""];
return Array.isArray(variantDef) ? variantDef[0] : variantDef;
// Helper to uniquely identify a baked model
function getVariantHash(part) {
return `${part.model}#y=${part.y || 0},x=${part.x || 0},uvlock=${!!part.uvlock}`;
}
async function testPipeline() {
@@ -61,70 +54,103 @@
const atlas = new TextureAtlas(512);
document.getElementById('atlas-container').appendChild(atlas.canvas);
const stateId = 'minecraft:repeater';
const state = await loadBlockstate(stateId);
// 1. Manually Populate World
const world = new World();
const directions = ['north', 'east', 'south', 'west'];
const instancesToDraw = [];
// Draw a redstone line into a repeater, next to a cobblestone wall
world.setBlock('minecraft:redstone_wire', 0, 0, 0, {
east: 'side',
west: 'up',
north: 'none',
south: 'none',
power: '15'
});
world.setBlock('minecraft:redstone_wire', 1, 0, 0, {
east: 'side',
west: 'side',
north: 'none',
south: 'none',
power: '3'
});
world.setBlock('minecraft:repeater', 2, 0, 0, {
facing: 'east',
delay: '1',
locked: 'false',
powered: 'true'
});
world.setBlock('minecraft:cobblestone_wall', 2, 0, -1, {
up: 'true',
north: 'tall',
south: 'low'
});
// Generate a 4x4 grid covering all repeater states
for (let x = 0; x < 4; x++) {
for (let z = 0; z < 4; z++) {
const props = {
facing: directions[x],
delay: (z + 1).toString(),
locked: (x % 2 === 0).toString(),
powered: (z % 2 === 0).toString()
};
// 2. Build Scene Pools
const instancePool = new Map();
const variant = resolveVariant(state, props);
const model = await loadModel(variant.model);
for (const block of world.blocks.values()) {
const stateJSON = await loadBlockstate(block.id);
const parts = resolveBlock(stateJSON, block.props);
// Load textures
for (const el of model.elements || []) {
for (const face of Object.values(el.faces || {})) {
const texPath = resolveTexture(model, face.texture);
if (texPath) await atlas.load(texPath);
}
for (const part of parts) {
const hash = getVariantHash(part);
if (!instancePool.has(hash)) {
instancePool.set(hash, {partDef: part, matrices: [], colors: []});
}
// For now, we rebuild geometry per block.
// Later, we will cache buffers by model ID.
const geometry = buildGeometry(model, atlas);
// Calculate instance matrix
const matrix = mat4Identity(new Float32Array(16));
mat4Translate(matrix, matrix, [block.x, block.y, block.z]);
// 1. Move to grid slot
mat4Translate(matrix, matrix, [x * 1.5, 0, z * 1.5]);
const pool = instancePool.get(hash);
pool.matrices.push(...matrix);
// 2. Minecraft rotates around block center (0.5, 0.5, 0.5)
mat4Translate(matrix, matrix, [0.5, 0.5, 0.5]);
if (variant.y) mat4RotateY(matrix, matrix, -variant.y * Math.PI / 180);
mat4Translate(matrix, matrix, [-0.5, -0.5, -0.5]);
instancesToDraw.push({geometry, matrix});
// Simple redstone tint calculation (if power exists)
if (block.props.power) {
const p = parseInt(block.props.power, 10);
const red = (0x4B + (p * 12)) / 255;
pool.colors.push(red, 0.0, 0.0);
} else {
pool.colors.push(1.0, 1.0, 1.0); // Default white
}
}
}
// 3. Bake Geometries & Create WebGL Buffers
const webglCanvas = document.getElementById('webgl-canvas');
const renderer = new Renderer(webglCanvas);
const renderableMeshes = [];
for (const [hash, pool] of instancePool.entries()) {
const modelJSON = await loadModel(pool.partDef.model);
// Load textures
for (const el of modelJSON.elements || []) {
for (const face of Object.values(el.faces || {})) {
const texPath = resolveTexture(modelJSON, face.texture);
if (texPath) await atlas.load(texPath);
}
}
// Bake geometry with local rotations
const geometry = buildGeometry(modelJSON, atlas, pool.partDef);
// Pack instances
const matrixArray = new Float32Array(pool.matrices);
const colorArray = new Float32Array(pool.colors);
const instanceCount = pool.matrices.length / 16;
const mesh = renderer.createInstancedMesh(geometry, instanceCount, matrixArray, colorArray);
renderableMeshes.push(mesh);
}
renderer.updateAtlas(atlas.canvas);
// Convert raw geometries into WebGL VAOs
const buffers = instancesToDraw.map(inst => renderer.createModelBuffer(inst.geometry));
// Camera setup to fit the grid
// 4. Render Setup
const proj = new Float32Array(16);
const view = new Float32Array(16);
const viewProj = new Float32Array(16);
mat4Ortho(proj, -4, 4, -4, 4, -20, 20);
mat4LookAt(view,
8, 6, 8, // Eye
2.25, 0, 2.25, // Look at center of grid
0, 1, 0
);
mat4Ortho(proj, -3, 3, -3, 3, -20, 20);
mat4LookAt(view, 6, 5, 6, 1, 0, 0, 0, 1, 0);
mat4Multiply(viewProj, proj, view);
function render() {
@@ -133,20 +159,16 @@
gl.clearColor(0.1, 0.1, 0.12, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for (let i = 0; i < instancesToDraw.length; i++) {
renderer.drawSingleInstance(buffers[i], viewProj, instancesToDraw[i].matrix);
for (const mesh of renderableMeshes) {
renderer.drawInstanced(mesh, viewProj);
}
requestAnimationFrame(render);
}
render();
document.getElementById('output').innerHTML = `<strong>Repeater Stress Test Active</strong>`;
} catch (err) {
console.error(err);
document.getElementById('output').textContent = 'Error: ' + err.message;
}
}