first pass
This commit is contained in:
105
assets.js
Normal file
105
assets.js
Normal file
@@ -0,0 +1,105 @@
|
||||
// assets.js
|
||||
const jsonCache = new Map();
|
||||
|
||||
/**
|
||||
* Resolves a namespace:path ID to a file path.
|
||||
* Defaults to the 'minecraft' namespace if none is provided.
|
||||
*/
|
||||
export function resolveResourceLocation(id, type, ext = 'json') {
|
||||
const parts = id.split(':');
|
||||
const namespace = parts.length > 1 ? parts[0] : 'minecraft';
|
||||
const path = parts.length > 1 ? parts[1] : parts[0];
|
||||
return `assets/${namespace}/${type}/${path}.${ext}`;
|
||||
}
|
||||
|
||||
async function fetchJSON(url) {
|
||||
if (jsonCache.has(url)) return jsonCache.get(url);
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error(`Failed to load asset: ${url}`);
|
||||
const data = await res.json();
|
||||
jsonCache.set(url, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function loadBlockstate(id) {
|
||||
const url = resolveResourceLocation(id, 'blockstates');
|
||||
return fetchJSON(url);
|
||||
}
|
||||
|
||||
export async function loadModel(id) {
|
||||
const url = resolveResourceLocation(id, 'models');
|
||||
const model = await fetchJSON(url);
|
||||
|
||||
// Recursively resolve and merge parent models
|
||||
if (model.parent) {
|
||||
// Parents sometimes use block/ or item/ prefixes directly
|
||||
const parentId = model.parent.includes(':') ? model.parent : `minecraft:${model.parent}`;
|
||||
const parent = await loadModel(parentId);
|
||||
|
||||
// Merge textures
|
||||
model.textures = { ...parent.textures, ...model.textures };
|
||||
// Inherit elements if child doesn't override them
|
||||
if (!model.elements) model.elements = parent.elements;
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
export class TextureAtlas {
|
||||
constructor(size = 512) {
|
||||
this.canvas = document.createElement('canvas');
|
||||
this.canvas.width = size;
|
||||
this.canvas.height = size;
|
||||
this.ctx = this.canvas.getContext('2d', { willReadFrequently: true });
|
||||
this.ctx.imageSmoothingEnabled = false;
|
||||
|
||||
// Magenta background for missing textures
|
||||
this.ctx.fillStyle = '#ff00ff';
|
||||
this.ctx.fillRect(0, 0, size, size);
|
||||
|
||||
this.map = new Map();
|
||||
this.cellSize = 16;
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.rowHeight = 0;
|
||||
}
|
||||
|
||||
async load(id) {
|
||||
if (this.map.has(id)) return this.map.get(id);
|
||||
|
||||
const url = resolveResourceLocation(id, 'textures', 'png');
|
||||
|
||||
try {
|
||||
const img = await new Promise((resolve, reject) => {
|
||||
const i = new Image();
|
||||
i.crossOrigin = 'anonymous';
|
||||
i.onload = () => resolve(i);
|
||||
i.onerror = () => reject(new Error(url));
|
||||
i.src = url;
|
||||
});
|
||||
|
||||
if (this.x + this.cellSize > this.canvas.width) {
|
||||
this.x = 0;
|
||||
this.y += this.rowHeight;
|
||||
this.rowHeight = 0;
|
||||
}
|
||||
|
||||
this.ctx.drawImage(img, this.x, this.y, this.cellSize, this.cellSize);
|
||||
|
||||
const uvData = {
|
||||
u: this.x / this.canvas.width,
|
||||
v: this.y / this.canvas.height,
|
||||
du: this.cellSize / this.canvas.width,
|
||||
dv: this.cellSize / this.canvas.height
|
||||
};
|
||||
|
||||
this.map.set(id, uvData);
|
||||
this.x += this.cellSize;
|
||||
this.rowHeight = Math.max(this.rowHeight, this.cellSize);
|
||||
|
||||
return uvData;
|
||||
} catch (e) {
|
||||
console.warn(`Texture missing: ${id}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user