Files
bworld/tools/importer/vanilla_models.ts
T
2026-10-12 17:22:50 -03:00

129 lines
5.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the vanilla parent models mods build on, so their models convert without minecraft's own files: cubes, slabs,
// stairs, walls and panes. each is boxes with texture variables for the children to fill in. faces leave out uv, which
// then comes from where the face is on the block, as it does in java
type Side = "down" | "up" | "north" | "south" | "west" | "east";
type Faces = Partial<Record<Side, string>>;
type Model = Record<string, unknown>;
const SIDES: Side[] = ["down", "up", "north", "south", "west", "east"];
// a box whose faces are given as "#texture", with "!" in front for one hidden by a solid neighbor on that side
function box(from: number[], to: number[], faces: Faces, rotations: Partial<Record<Side, number>> = {}) {
return {
from,
to,
faces: Object.fromEntries(
Object.entries(faces).map(([side, texture]) => [side, {
texture: texture.replace("!", ""),
...(texture.startsWith("!") ? { cullface: side } : {}),
...(rotations[side as Side] ? { rotation: rotations[side as Side] } : {}),
}]),
),
};
}
const all = (texture: (side: Side) => string): Faces => Object.fromEntries(SIDES.map((side) => [side, texture(side)]));
const sides = (top: string, bottom: string, side: string) =>
all((s) => s === "up" ? top : s === "down" ? bottom : side);
// a full cube's faces, all hidden by solid neighbors
const cube_faces = all((side) => `!#${side}`);
const slab = box([0, 0, 0], [16, 8, 16], { ...all((side) => `!#side`), down: "!#bottom", up: "#top" });
// the stairs' step is the back (east) half, the way they face with no turn
const step = (from: number[], to: number[], culled: Side[]) =>
box(from, to, {
...Object.fromEntries(
(["up", "north", "south", "west", "east"] as Side[]).map((
side,
) => [side, side === "up" ? "#top" : "#side"]),
),
...Object.fromEntries(culled.map((side) => [side, side === "up" ? "!#top" : "!#side"])),
});
const wall_post = box([4, 0, 4], [12, 16, 12], { ...all(() => "#wall"), down: "!#wall", up: "!#wall" });
const wall_side = (height: number) =>
box([5, 0, 0], [11, height, 8], {
down: "!#wall",
up: height === 16 ? "!#wall" : "#wall",
north: "!#wall",
west: "#wall",
east: "#wall",
});
export const VANILLA_MODELS: Record<string, Model> = {
"minecraft:block/block": {},
"minecraft:block/air": {},
"minecraft:block/cube": { elements: [box([0, 0, 0], [16, 16, 16], cube_faces)] },
"minecraft:block/cube_all": { parent: "block/cube", textures: all(() => "#all") },
"minecraft:block/cube_column": { parent: "block/cube", textures: sides("#end", "#end", "#side") },
"minecraft:block/cube_bottom_top": { parent: "block/cube", textures: sides("#top", "#bottom", "#side") },
"minecraft:block/orientable_with_bottom": {
parent: "block/cube",
textures: { ...sides("#top", "#bottom", "#side"), north: "#front" },
},
"minecraft:block/orientable": { parent: "block/orientable_with_bottom", textures: { bottom: "#top" } },
// a column lying along north to south, its sides turned so their grain runs the same way
"minecraft:block/cube_column_horizontal": {
elements: [box(
[0, 0, 0],
[16, 16, 16],
{ down: "!#side", up: "!#side", north: "!#end", south: "!#end", west: "!#side", east: "!#side" },
{ down: 180, west: 270, east: 90 },
)],
},
"minecraft:block/slab": { elements: [slab] },
"minecraft:block/slab_top": {
elements: [box([0, 8, 0], [16, 16, 16], { ...all(() => "!#side"), down: "#bottom", up: "!#top" })],
},
"minecraft:block/stairs": { elements: [slab, step([8, 8, 0], [16, 16, 16], ["up", "north", "south", "east"])] },
"minecraft:block/inner_stairs": {
elements: [
slab,
step([8, 8, 0], [16, 16, 16], ["up", "north", "south", "east"]),
step([0, 8, 8], [8, 16, 16], ["up", "south", "west"]),
],
},
"minecraft:block/outer_stairs": { elements: [slab, step([8, 8, 8], [16, 16, 16], ["up", "south", "east"])] },
"minecraft:block/template_wall_post": { elements: [wall_post] },
"minecraft:block/template_wall_side": { elements: [wall_side(14)] },
"minecraft:block/template_wall_side_tall": { elements: [wall_side(16)] },
"minecraft:block/wall_inventory": {
elements: [
wall_post,
box([5, 0, 0], [11, 13, 16], { ...all(() => "#wall"), north: "!#wall", south: "!#wall" }),
],
},
"minecraft:block/template_glass_pane_post": {
elements: [box([7, 0, 7], [9, 16, 9], { down: "!#edge", up: "!#edge" })],
},
"minecraft:block/template_glass_pane_side": {
elements: [
box([7, 0, 0], [9, 16, 7], { down: "!#edge", up: "!#edge", north: "!#edge", west: "#pane", east: "#pane" }),
],
},
"minecraft:block/template_glass_pane_side_alt": {
elements: [
box([7, 0, 9], [9, 16, 16], {
down: "!#edge",
up: "!#edge",
south: "!#edge",
west: "#pane",
east: "#pane",
}),
],
},
"minecraft:block/template_glass_pane_noside": { elements: [box([7, 0, 7], [9, 16, 9], { north: "#pane" })] },
"minecraft:block/template_glass_pane_noside_alt": { elements: [box([7, 0, 7], [9, 16, 9], { east: "#pane" })] },
};
// item models that are a flat sprite or drawn by code, not boxes
export const NOT_BOXES: Record<string, string> = {
"minecraft:item/generated": "an item sprite",
"minecraft:item/handheld": "an item sprite",
"minecraft:item/barrier": "an item sprite",
"minecraft:builtin/generated": "an item sprite",
"minecraft:builtin/entity": "drawn by code",
};