Worm bin model

This commit is contained in:
2026-10-12 18:58:38 -03:00
parent 99bdcaeffc
commit 038b6d2caa
16 changed files with 447 additions and 39 deletions
+25 -12
View File
@@ -44,7 +44,6 @@ const FACE_NAMES: Record<string, ModelFace> = {
west: "west",
east: "east",
};
const ANGLES = [-45, -22.5, 0, 22.5, 45];
// item models that are a flat sprite or drawn by code, not boxes, and so is anything that has one as a parent
const NOT_BOXES: Record<string, string> = {
@@ -74,7 +73,7 @@ export function import_models(input: string, options: ImportOptions = {}): Impor
const remaps = options.remaps ?? DEFAULT_REMAPS;
const single = is_file(input);
if (!single && options.output === undefined) throw new Error("--output is needed to convert a folder");
const files = input_files(input, "models", ".json", options.namespace);
const files = input_files(input, "models", ".json", options.namespace, options.output);
const read = model_source([
...new Set(files.map(({ root }) => root)),
...(options.parents ?? []).flatMap((dir) => find_roots(dir, "models")),
@@ -87,7 +86,8 @@ export function import_models(input: string, options: ImportOptions = {}): Impor
const where = location_of(root, file, ".json");
const location = location_string(where);
if (skips(location, remaps, report)) continue;
const result = convert_model(location, read, remaps);
// a loose file's bare texture names are its own, like blockbench writes them
const result = convert_model(location, read, remaps, root.loose ? root.namespace : "minecraft");
if (result.kind === "skipped") report.skipped.push({ location, reason: result.reason });
else if (result.kind === "error") report.errors.push({ location, problems: result.problems });
else if (claim(result.id, location)) {
@@ -124,7 +124,8 @@ export function model_source(roots: ResourceRoot[]): ModelSource {
};
}
export function convert_model(location: string, read: ModelSource, remaps: Remaps): ModelResult {
// bare is the namespace of textures named without one
export function convert_model(location: string, read: ModelSource, remaps: Remaps, bare = "minecraft"): ModelResult {
const json = read(location_string(parse_location(location)));
if (!json) return { kind: "error", problems: [`${location} wasn't found`] };
const made = made_by(location, read);
@@ -137,7 +138,7 @@ export function convert_model(location: string, read: ModelSource, remaps: Remap
}
const texture = (value: string, where: string) => {
if (value.startsWith("#")) return value;
const id = to_id(value, remaps.textures);
const id = to_id(value, remaps.textures, bare);
if (!is_valid_id(id)) problems.push(`${where}: "${value}" doesn't make a valid texture id (${id})`);
return id;
};
@@ -208,16 +209,28 @@ function convert_element(
};
const out = { from: point("from"), to: point("to") } as ModelElementJson;
// { axis, angle }, or blockbench's newer { x, y, z } with a turn around each
const rotation = element.rotation as Record<string, unknown> | undefined;
if (rotation && rotation.angle !== 0) {
if (!ANGLES.includes(rotation.angle as number)) {
problems.push(`${where}.rotation: an angle of ${rotation.angle} isn't one of ${ANGLES.join(", ")}`);
const turns = rotation === undefined
? []
: typeof rotation.axis === "string"
? [[rotation.axis, rotation.angle]]
: ["x", "y", "z"].map((axis) => [axis, rotation[axis] ?? 0]);
const turned = turns.filter(([, angle]) => angle !== 0);
if (turned.length > 1) {
problems.push(
`${where}.rotation turns around ${turned.map(([axis]) => axis).join(" and ")} at once, bworld only one`,
);
} else if (turned.length === 1) {
const [[axis, angle]] = turned;
if (typeof angle !== "number" || Math.abs(angle) > 180) {
problems.push(`${where}.rotation: ${angle} isn't an angle from -180 to 180`);
}
out.rotation = {
origin: rotation.origin as [number, number, number],
axis: rotation.axis as "x" | "y" | "z",
angle: rotation.angle as number,
...(rotation.rescale === true ? { rescale: true } : {}),
origin: rotation!.origin as [number, number, number],
axis: axis as "x" | "y" | "z",
angle: angle as number,
...(rotation!.rescale === true ? { rescale: true } : {}),
};
}
if (element.shade === false) out.shade = false;