working blockstates
This commit is contained in:
141
geometry.js
141
geometry.js
@@ -1,88 +1,134 @@
|
||||
// geometry.js
|
||||
import {mat4Identity, mat4RotateY, mat4Translate} from './math.js'; // Assuming you have mat4RotateX too
|
||||
|
||||
const FACES = {
|
||||
down: { n: [0, -1, 0], c: [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]] },
|
||||
up: { n: [0, 1, 0], c: [[0, 1, 1], [1, 1, 1], [1, 1, 0], [0, 1, 0]] },
|
||||
north: { n: [0, 0, -1], c: [[1, 0, 0], [0, 0, 0], [0, 1, 0], [1, 1, 0]] },
|
||||
south: { n: [0, 0, 1], c: [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]] },
|
||||
west: { n: [-1, 0, 0], c: [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]] },
|
||||
east: { n: [1, 0, 0], c: [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]] }
|
||||
down: {n: [0, -1, 0], c: [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]},
|
||||
up: {n: [0, 1, 0], c: [[0, 1, 1], [1, 1, 1], [1, 1, 0], [0, 1, 0]]},
|
||||
north: {n: [0, 0, -1], c: [[1, 0, 0], [0, 0, 0], [0, 1, 0], [1, 1, 0]]},
|
||||
south: {n: [0, 0, 1], c: [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]]},
|
||||
west: {n: [-1, 0, 0], c: [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]]},
|
||||
east: {n: [1, 0, 0], c: [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]]}
|
||||
};
|
||||
|
||||
// Calculates missing UVs by projecting the element bounds onto the face plane
|
||||
function calculateDefaultUV(faceName, from, to) {
|
||||
switch (faceName) {
|
||||
case 'up':
|
||||
return [from[0], from[2], to[0], to[2]];
|
||||
case 'down':
|
||||
return [from[0], 16 - to[2], to[0], 16 - from[2]];
|
||||
case 'north':
|
||||
return [16 - to[0], 16 - to[1], 16 - from[0], 16 - from[1]];
|
||||
case 'south':
|
||||
return [from[0], 16 - to[1], to[0], 16 - from[1]];
|
||||
case 'west':
|
||||
return [from[2], 16 - to[1], to[2], 16 - from[1]];
|
||||
case 'east':
|
||||
return [16 - to[2], 16 - to[1], 16 - from[2], 16 - from[1]];
|
||||
default:
|
||||
return [0, 0, 16, 16];
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveTexture(model, ref) {
|
||||
if (!ref) return null;
|
||||
if (ref[0] !== '#') return ref;
|
||||
|
||||
let key = ref.slice(1);
|
||||
let val = model.textures?.[key];
|
||||
|
||||
// Follow variable chain
|
||||
while (typeof val === 'string' && val[0] === '#') {
|
||||
key = val.slice(1);
|
||||
val = model.textures?.[key];
|
||||
}
|
||||
|
||||
// Handle modern object-style textures
|
||||
if (val && typeof val === 'object' && val.sprite) {
|
||||
val = val.sprite;
|
||||
}
|
||||
|
||||
if (val && typeof val === 'object' && val.sprite) val = val.sprite;
|
||||
return val || null;
|
||||
}
|
||||
|
||||
export function buildGeometry(model, atlas) {
|
||||
const pos = [];
|
||||
const norm = [];
|
||||
const uv = [];
|
||||
const tint = [];
|
||||
const idx = [];
|
||||
|
||||
// Takes the blockstate variant object to apply y/x rotations and uvlock
|
||||
export function buildGeometry(model, atlas, variant = {}) {
|
||||
const pos = [], norm = [], uv = [], tint = [], shade = [], idx = [];
|
||||
let vOffset = 0;
|
||||
|
||||
// 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);
|
||||
mat4Translate(blockMatrix, blockMatrix, [-0.5, -0.5, -0.5]);
|
||||
|
||||
for (const el of model.elements || []) {
|
||||
// Map 0-16 voxel scale to 0.0-1.0 world scale
|
||||
const [fx, fy, fz] = el.from.map(x => x / 16);
|
||||
const [tx, ty, tz] = el.to.map(x => x / 16);
|
||||
const size = [tx - fx, ty - fy, tz - fz];
|
||||
|
||||
// Element-level shading flag. Used to make repeater torches/redstone dust emissive.
|
||||
const elementShade = el.shade === false ? 0.0 : 1.0;
|
||||
|
||||
for (const [name, face] of Object.entries(el.faces || {})) {
|
||||
const tmpl = FACES[name];
|
||||
const texPath = resolveTexture(model, face.texture);
|
||||
const a = texPath ? atlas.map.get(texPath) : null;
|
||||
|
||||
// Skip faces with missing textures in the atlas
|
||||
if (!a) continue;
|
||||
|
||||
// Basic UV fallback if omitted: this is a placeholder.
|
||||
// Real implementation needs mapping based on face direction and element bounds.
|
||||
const u1 = (face.uv?.[0] ?? 0) / 16;
|
||||
const v1 = (face.uv?.[1] ?? 0) / 16;
|
||||
const u2 = (face.uv?.[2] ?? 16) / 16;
|
||||
const v2 = (face.uv?.[3] ?? 16) / 16;
|
||||
// DYNAMIC UV PROJECTION (remains the same)
|
||||
const rawUV = face.uv || calculateDefaultUV(name, el.from, el.to);
|
||||
let u1 = rawUV[0] / 16, v1 = rawUV[1] / 16;
|
||||
let u2 = rawUV[2] / 16, v2 = rawUV[3] / 16;
|
||||
|
||||
const au1 = a.u + u1 * a.du;
|
||||
const au2 = a.u + u2 * a.du;
|
||||
// WebGL wants V flipped natively compared to Three.js canvas
|
||||
const av1 = a.v + v1 * a.dv;
|
||||
const av2 = a.v + v2 * a.dv;
|
||||
let texRot = face.rotation || 0;
|
||||
|
||||
// uvlock compensation
|
||||
if (variant.uvlock && (name === 'up' || name === 'down') && variant.y) {
|
||||
texRot = (texRot - variant.y + 360) % 360;
|
||||
}
|
||||
|
||||
const au1 = a.u + u1 * a.du, au2 = a.u + u2 * a.du;
|
||||
const av1 = a.v + v1 * a.dv, av2 = a.v + v2 * a.dv;
|
||||
|
||||
// CORRECTED UV ARRAY MAPPING
|
||||
// Corner Order: 0:Bottom-Left, 1:Bottom-Right, 2:Top-Right, 3:Top-Left
|
||||
let faceUVs;
|
||||
if (texRot === 90) {
|
||||
// Shift UVs clockwise by 1 corner
|
||||
faceUVs = [au2, av2, au2, av1, au1, av1, au1, av2];
|
||||
} else if (texRot === 180) {
|
||||
// Shift UVs by 2 corners
|
||||
faceUVs = [au2, av1, au1, av1, au1, av2, au2, av2];
|
||||
} else if (texRot === 270) {
|
||||
// Shift UVs by 3 corners
|
||||
faceUVs = [au1, av1, au1, av2, au2, av2, au2, av1];
|
||||
} else {
|
||||
// 0 degrees: av2 (bottom) goes to corners 0 and 1. av1 (top) goes to corners 2 and 3.
|
||||
faceUVs = [au1, av2, au2, av2, au2, av1, au1, av1];
|
||||
}
|
||||
|
||||
const tintable = face.tintindex !== undefined ? 1.0 : 0.0;
|
||||
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const [cx, cy, cz] = tmpl.c[i];
|
||||
|
||||
// TODO: Apply el.rotation matrix here later
|
||||
pos.push(fx + cx * size[0], fy + cy * size[1], fz + cz * size[2]);
|
||||
norm.push(...tmpl.n);
|
||||
let vx = fx + cx * size[0];
|
||||
let vy = fy + cy * size[1];
|
||||
let vz = fz + cz * size[2];
|
||||
|
||||
// Apply Blockstate Rotation
|
||||
let wx = blockMatrix[0] * vx + blockMatrix[4] * vy + blockMatrix[8] * vz + blockMatrix[12];
|
||||
let wy = blockMatrix[1] * vx + blockMatrix[5] * vy + blockMatrix[9] * vz + blockMatrix[13];
|
||||
let wz = blockMatrix[2] * vx + blockMatrix[6] * vy + blockMatrix[10] * vz + blockMatrix[14];
|
||||
|
||||
pos.push(wx, wy, wz);
|
||||
|
||||
// Note: Normal rotation should technically use the inverse-transpose of the matrix,
|
||||
// but since we only have pure rotations, direct multiplication works fine here.
|
||||
let nx = blockMatrix[0] * tmpl.n[0] + blockMatrix[4] * tmpl.n[1] + blockMatrix[8] * tmpl.n[2];
|
||||
let ny = blockMatrix[1] * tmpl.n[0] + blockMatrix[5] * tmpl.n[1] + blockMatrix[9] * tmpl.n[2];
|
||||
let nz = blockMatrix[2] * tmpl.n[0] + blockMatrix[6] * tmpl.n[1] + blockMatrix[10] * tmpl.n[2];
|
||||
|
||||
norm.push(nx, ny, nz);
|
||||
tint.push(tintable);
|
||||
shade.push(elementShade);
|
||||
}
|
||||
|
||||
uv.push(au1, av1, au2, av1, au2, av2, au1, av2);
|
||||
|
||||
// Push 2 triangles per quad
|
||||
idx.push(
|
||||
vOffset, vOffset + 1, vOffset + 2,
|
||||
vOffset, vOffset + 2, vOffset + 3
|
||||
);
|
||||
uv.push(...faceUVs);
|
||||
idx.push(vOffset, vOffset + 1, vOffset + 2, vOffset, vOffset + 2, vOffset + 3);
|
||||
vOffset += 4;
|
||||
}
|
||||
}
|
||||
@@ -92,6 +138,7 @@ export function buildGeometry(model, atlas) {
|
||||
normals: new Float32Array(norm),
|
||||
uvs: new Float32Array(uv),
|
||||
tints: new Float32Array(tint),
|
||||
shades: new Float32Array(shade),
|
||||
indices: new Uint16Array(idx)
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user