diff --git a/geometry.js b/geometry.js index 9a8737b7..bea5b105 100644 --- a/geometry.js +++ b/geometry.js @@ -1,5 +1,5 @@ // geometry.js -import {mat4Identity, mat4RotateY, mat4Translate} from './math.js'; // Assuming you have mat4RotateX too +import {mat4Identity, mat4RotateX, mat4RotateY, mat4Translate} from './math.js'; const FACES = { down: {n: [0, -1, 0], c: [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]}, @@ -51,8 +51,8 @@ export function buildGeometry(model, atlas, variant = {}) { // 1. Pre-calculate the blockstate rotation matrix (around block center) const blockMatrix = mat4Identity(new Float32Array(16)); mat4Translate(blockMatrix, blockMatrix, [0.5, 0.5, 0.5]); - // NOTE: Vanilla blockstates also support `x` rotation. You'd add mat4RotateX here if needed. - if (variant.y) mat4RotateY(blockMatrix, blockMatrix, -variant.y * Math.PI / 180); + if (variant.x) mat4RotateX(blockMatrix, blockMatrix, variant.x * Math.PI / 180); + if (variant.y) mat4RotateY(blockMatrix, blockMatrix, variant.y * Math.PI / 180); mat4Translate(blockMatrix, blockMatrix, [-0.5, -0.5, -0.5]); for (const el of model.elements || []) { diff --git a/index.html b/index.html index 54b369ac..50a7c578 100644 --- a/index.html +++ b/index.html @@ -57,12 +57,28 @@ open: 'false', half: 'bottom' }); - world.setBlock('minecraft:glass', -1, 0, 0); - world.setBlock('minecraft:glass', 0, 0, -1); + + world.setBlock('minecraft:coal_block', 0, -1, -3); // north + world.setBlock('minecraft:gold_block', 0, -1, 3); // south + world.setBlock('minecraft:diamond_block', 3, -1, 0); // east + world.setBlock('minecraft:iron_block', -3, -1, 0); // west + + // world.setBlock('minecraft:glass', 1, 0, 0); + // world.setBlock('minecraft:glass', 0, 0, 1); world.setBlock('minecraft:coal_block', 0, -1, 0); - world.setBlock('minecraft:repeater', 1, -1, 0, {facing: 'west', delay: 1, locked: false, powered: false}); + world.setBlock('minecraft:repeater', 1, -1, 0, {facing: 'east', delay: 1, locked: false, powered: false}); world.setBlock('minecraft:comparator', 0, -1, 1, {facing: 'north', mode: 'compare', powered: false}); + world.setBlock('minecraft:piston', 3, 0, 0, {facing: "west", extended: false}) + world.setBlock('minecraft:piston', -3, 0, 0, {facing: "east", extended: false}) + world.setBlock('minecraft:piston', 0, 0, 3, {facing: "south", extended: false}) + world.setBlock('minecraft:piston', 0, 0, -3, {facing: "north", extended: false}) + + world.setBlock('minecraft:repeater', 3, 1, 0, {facing: "west", delay: 2, locked: true, powered: false}) + world.setBlock('minecraft:repeater', -3, 1, 0, {facing: "east", delay: 1, locked: true, powered: false}) + world.setBlock('minecraft:repeater', 0, 1, 3, {facing: "south", delay: 3, locked: true, powered: false}) + world.setBlock('minecraft:repeater', 0, 1, -3, {facing: "north", delay: 4, locked: true, powered: false}) + await renderer.updateWorld(world); // Render Loop (Static, just drawing the current buffers) diff --git a/math.js b/math.js index 0e792537..4009f6cd 100644 --- a/math.js +++ b/math.js @@ -15,28 +15,37 @@ export function mat4Multiply(out, a, b) { let a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; - out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; - b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; - out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + b0 = b[4]; + b1 = b[5]; + b2 = b[6]; + b3 = b[7]; + out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; - b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; - out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + b0 = b[8]; + b1 = b[9]; + b2 = b[10]; + b3 = b[11]; + out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; - b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; - out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; - out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; - out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; - out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; + b0 = b[12]; + b1 = b[13]; + b2 = b[14]; + b3 = b[15]; + out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; return out; } @@ -58,23 +67,38 @@ export function mat4Ortho(out, left, right, bottom, top, near, far) { export function mat4LookAt(out, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) { let z0 = eyeX - centerX, z1 = eyeY - centerY, z2 = eyeZ - centerZ; let len = 1 / Math.hypot(z0, z1, z2); - z0 *= len; z1 *= len; z2 *= len; + z0 *= len; + z1 *= len; + z2 *= len; let x0 = upY * z2 - upZ * z1, x1 = upZ * z0 - upX * z2, x2 = upX * z1 - upY * z0; len = 1 / Math.hypot(x0, x1, x2); - x0 *= len; x1 *= len; x2 *= len; + x0 *= len; + x1 *= len; + x2 *= len; let y0 = z1 * x2 - z2 * x1, y1 = z2 * x0 - z0 * x2, y2 = z0 * x1 - z1 * x0; - out[0] = x0; out[1] = y0; out[2] = z0; out[3] = 0; - out[4] = x1; out[5] = y1; out[6] = z1; out[7] = 0; - out[8] = x2; out[9] = y2; out[10] = z2; out[11] = 0; + out[0] = x0; + out[1] = y0; + out[2] = z0; + out[3] = 0; + out[4] = x1; + out[5] = y1; + out[6] = z1; + out[7] = 0; + out[8] = x2; + out[9] = y2; + out[10] = z2; + out[11] = 0; out[12] = -(x0 * eyeX + x1 * eyeY + x2 * eyeZ); out[13] = -(y0 * eyeX + y1 * eyeY + y2 * eyeZ); out[14] = -(z0 * eyeX + z1 * eyeY + z2 * eyeZ); out[15] = 1; return out; -}export function mat4Translate(out, a, v) { +} + +export function mat4Translate(out, a, v) { let x = v[0], y = v[1], z = v[2]; if (a === out) { out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; @@ -93,8 +117,14 @@ export function mat4RotateY(out, a, rad) { let a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; if (a !== out) { - out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7]; - out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; } out[0] = a00 * c - a20 * s; @@ -106,4 +136,31 @@ export function mat4RotateY(out, a, rad) { out[10] = a02 * s + a22 * c; out[11] = a03 * s + a23 * c; return out; +} + +export function mat4RotateX(out, a, rad) { + let s = Math.sin(rad), c = Math.cos(rad); + let a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; + let a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; + + if (a !== out) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + out[4] = a10 * c + a20 * s; + out[5] = a11 * c + a21 * s; + out[6] = a12 * c + a22 * s; + out[7] = a13 * c + a23 * s; + out[8] = a20 * c - a10 * s; + out[9] = a21 * c - a11 * s; + out[10] = a22 * c - a12 * s; + out[11] = a23 * c - a13 * s; + return out; } \ No newline at end of file