import {mat4Identity, mat4RotateX, mat4RotateY, mat4Translate} from "./math.js"; const MISSING_MODEL_JSON = { "textures": {"missing": ":missing"}, "elements": [ { "from": [0, 0, 0], "to": [16, 16, 16], "faces": { "down": {"texture": "#missing"}, "up": {"texture": "#missing"}, "north": {"texture": "#missing"}, "south": {"texture": "#missing"}, "west": {"texture": "#missing"}, "east": {"texture": "#missing"} } } ] }; export class ModelHandler { async process(cache, id, url) { if (id === ':missing') return new BlockModel(MISSING_MODEL_JSON); try { const res = await fetch(url + '.json'); if (!res.ok) throw new Error(`HTTP ${res.status}`); const json = await res.json(); if (json.parent) { const parent = await cache.getAsync(json.parent, 'models'); json.textures = {...parent.textures, ...json.textures}; if (!json.elements && parent.elements) { json.elements = parent.elements; } } return new BlockModel(json); } catch (e) { console.warn(`Model missing/broken (${id}), falling back to :missing`); return new BlockModel(MISSING_MODEL_JSON); } } getFallback(id) { return new BlockModel(MISSING_MODEL_JSON); } } const CUBOID_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]]} }; export class BlockModel { constructor(json) { this.elements = json.elements || []; this.textures = json.textures || {}; this._geometryCache = new Map(); } resolveTexture(ref) { if (!ref) return ':missing'; let val = ref; if (ref[0] === '#') { let key = ref.slice(1); val = this.textures[key]; while (typeof val === 'string' && val[0] === '#') { key = val.slice(1); val = this.textures[key]; } if (val && typeof val === 'object' && val.sprite) val = val.sprite; } if (typeof val === 'string') { return val.includes(':') ? val : `minecraft:${val}`; } return ':missing'; } 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]; } } buildGeometry(uvmap, variant = {}) { const cacheKey = `${variant.x || 0},${variant.y || 0},${!!variant.uvlock}`; if (this._geometryCache.has(cacheKey)) { return this._geometryCache.get(cacheKey); } const pos = [], norm = [], uv = [], tint = [], shade = [], idx = []; let vOffset = 0; const blockMatrix = mat4Identity(new Float32Array(16)); mat4Translate(blockMatrix, blockMatrix, [0.5, 0.5, 0.5]); 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 this.elements) { 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]; const elementShade = el.shade === false ? 0.0 : 1.0; for (const [name, face] of Object.entries(el.faces || {})) { const tmpl = CUBOID_FACES[name]; const texPath = this.resolveTexture(face.texture); const a = texPath ? uvmap.get(texPath) : null; if (!a) continue; const rawUV = face.uv || this.calculateDefaultUV(name, el.from, el.to); let u1 = rawUV[0] / 16, v1 = rawUV[1] / 16; let u2 = rawUV[2] / 16, v2 = rawUV[3] / 16; let texRot = face.rotation || 0; 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; let faceUVs; if (texRot === 90) faceUVs = [au2, av2, au2, av1, au1, av1, au1, av2]; else if (texRot === 180) faceUVs = [au2, av1, au1, av1, au1, av2, au2, av2]; else if (texRot === 270) faceUVs = [au1, av1, au1, av2, au2, av2, au2, av1]; else 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]; let vx = fx + cx * size[0], vy = fy + cy * size[1], vz = fz + cz * size[2]; 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); 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(...faceUVs); idx.push(vOffset, vOffset + 1, vOffset + 2, vOffset, vOffset + 2, vOffset + 3); vOffset += 4; } } const bakedGeometry = { positions: new Float32Array(pos), normals: new Float32Array(norm), uvs: new Float32Array(uv), tints: new Float32Array(tint), shades: new Float32Array(shade), indices: new Uint16Array(idx) }; this._geometryCache.set(cacheKey, bakedGeometry); return bakedGeometry; } }