Item rendering

This commit is contained in:
2026-10-13 22:29:38 -03:00
parent a18b099206
commit b34e97e487
269 changed files with 12566 additions and 3220 deletions
+54 -13
View File
@@ -39,15 +39,29 @@ export interface ModelElementJson {
faces: Partial<Record<ModelFace, ModelFaceJson>>;
}
// how an item drawn with a model is placed in one of the places it's shown. all around the model's middle: moved by
// translation (pixels), turned by rotation (degrees, around the z axis first, then y, then x) and sized by scale
export interface DisplayTransform {
rotation?: [number, number, number];
translation?: [number, number, number];
scale?: [number, number, number];
}
// where items are shown: in slots, held in first person, dropped, and on blocks with item_display
export const DISPLAY_CONTEXTS = ["gui", "firstperson_righthand", "ground", "fixed"] as const;
export type DisplayContext = typeof DISPLAY_CONTEXTS[number];
export type ModelDisplay = Partial<Record<DisplayContext, DisplayTransform>>;
// a model as a mod writes it in models/. it can start from a parent, like minecraft's: it gets the parent's elements
// unless it has its own, and the parent's texture defaults under its own. one with no elements is only there to be a
// parent, or draws nothing
// unless it has its own, and the parent's texture defaults and display transforms under its own. one with no elements
// is only there to be a parent, or draws nothing
export interface ModelFileJson {
id: string;
parent?: string;
// defaults for texture variables, can point at other variables ("top": "#side")
textures?: Record<string, string>;
elements?: ModelElementJson[];
display?: ModelDisplay;
}
// a model with its parents merged in, which is what blocks are drawn with. see resolve_models
@@ -55,6 +69,7 @@ export interface ModelJson {
id: string;
textures?: Record<string, string>;
elements: ModelElementJson[];
display?: ModelDisplay;
}
// mods' models with their parents merged in. a parent is another of these, or one existing finds (a model of a mod
@@ -88,10 +103,13 @@ export function resolve_models(
if (!parent) problems.push({ id, problem: `its parent ${file.parent} doesn't exist` });
}
const textures = { ...parent?.textures, ...file.textures };
// each place's transform is the model's own or its parent's, whole
const display = { ...parent?.display, ...file.display };
const model: ModelJson = {
id,
...(Object.keys(textures).length > 0 ? { textures } : {}),
elements: file.elements ?? parent?.elements ?? [],
...(Object.keys(display).length > 0 ? { display } : {}),
};
resolved.set(id, model);
return model;
@@ -120,6 +138,9 @@ export interface BlockVariant {
// turns the model around the east-west axis first, like minecraft's: 0, 90, 180 or 270. 90 tips its top to the north,
// so a model pointing up points north, then y turns it to face the rest of the ways
x?: number;
// keeps the textures where they'd be on an unturned block when x or y turn the model, so a turned stair's planks
// still run the same way as the planks next to it
uvlock?: boolean;
// what the player points at and bumps into, as boxes in pixels: [x1, y1, z1, x2, y2, z2]. the model's boxes when
// not set, see common/block_shapes.ts
shape?: number[][];
@@ -137,6 +158,7 @@ export interface BlockPart {
textures?: BlockTextures;
y?: number;
x?: number;
uvlock?: boolean;
shape?: number[][];
collision_shape?: number[][];
}
@@ -147,6 +169,7 @@ export interface PlacedModel {
textures: BlockTextures;
y: number;
x: number;
uvlock: boolean;
shape?: number[][];
collision_shape?: number[][];
}
@@ -250,6 +273,7 @@ export function block_variant(block: BlockRegistry, states?: Record<string, numb
let textures: BlockTextures | undefined;
let y: number | undefined;
let x: number | undefined;
let uvlock: boolean | undefined;
let shape: number[][] | undefined;
let collision_shape: number[][] | undefined;
if (block.variants && states) {
@@ -259,6 +283,7 @@ export function block_variant(block: BlockRegistry, states?: Record<string, numb
textures ??= override.textures;
y ??= override.y;
x ??= override.x;
uvlock ??= override.uvlock;
shape ??= override.shape;
collision_shape ??= override.collision_shape;
}
@@ -268,6 +293,7 @@ export function block_variant(block: BlockRegistry, states?: Record<string, numb
textures: textures ?? cube_textures(block),
y: y ?? 0,
x: x ?? 0,
uvlock: uvlock ?? false,
shape: shape ?? block.shape,
collision_shape: collision_shape ?? block.collision_shape,
};
@@ -285,6 +311,7 @@ export function block_parts(block: BlockRegistry, states?: Record<string, number
textures: part.textures ?? variant.textures,
y: part.y ?? 0,
x: part.x ?? 0,
uvlock: part.uvlock ?? false,
shape: part.shape,
collision_shape: part.collision_shape,
}));
@@ -315,7 +342,7 @@ export function bake_block(block: BlockRegistry, value: number, models: Record<s
const states = looks_by_state(block) ? states_of(block, value) : undefined;
return block_parts(block, states).flatMap((part) => {
const model = find_model(part.model, models) ?? BUILTIN_MODELS[DEFAULT_MODEL];
return bake_model(model, part.textures, part.y, part.x);
return bake_model(model, part.textures, part.y, part.x, part.uvlock);
});
}
@@ -364,14 +391,6 @@ export function item_tint(block: BlockRegistry, variables: string[]): number[] {
return [color >> 16, (color >> 8) & 0xff, color & 0xff].map((c) => c / 255);
}
// the quads of a block's item_model with the block's textures, for its item in inventories, hands and on the ground.
// undefined for blocks without one
export function block_item_quads(block: BlockRegistry, models: Record<string, ModelJson>): BakedQuad[] | undefined {
if (block.item_model === undefined) return undefined;
const model = find_model(block.item_model, models);
return model ? bake_model(model, block.textures) : undefined;
}
// how much darker each side of an item's model is drawn, like the mesher's: top, bottom, south, north, west, east
export const ITEM_FACE_SHADE = [1.0, 0.5, 0.8, 0.8, 0.6, 0.6];
@@ -446,12 +465,13 @@ function default_uv(face: ModelFace, from: number[], to: number[]): [number, num
const EPSILON = 1e-4;
// a model's quads with its textures, turned by x and then y like a block variant's turns
// a model's quads with its textures, turned by x and then y like a block variant's turns, with uvlock or without
export function bake_model(
model: ModelJson,
textures: BlockTextures | undefined,
y_rotation = 0,
x_rotation = 0,
uvlock = false,
): BakedQuad[] {
const quads: BakedQuad[] = [];
const turns = quarters(y_rotation);
@@ -466,6 +486,7 @@ export function bake_model(
const uv = face_json.uv ?? default_uv(face, element.from, element.to);
// each corner takes the uv of the corner a quarter turn along, per 90 degrees
const uv_turns = Math.round((face_json.rotation ?? 0) / 90) % 4;
const lock_turns = uvlock ? uvlock_turns(index, x_turns, turns) : 0;
const positions: number[] = [];
const uvs: number[] = [];
@@ -476,7 +497,10 @@ export function bake_model(
for (let t = 0; t < turns; t++) point = [1 - point[2], point[1], point[0]];
positions.push(...point);
const [cu, cv] = CORNER_UVS[(k + uv_turns) % 4];
uvs.push(cu ? uv[2] : uv[0], cv ? uv[3] : uv[1]);
let [u, v] = [cu ? uv[2] : uv[0], cv ? uv[3] : uv[1]];
// a quarter turn of the texture around its middle, which takes each of CORNER_UVS to the next
for (let t = 0; t < lock_turns; t++) [u, v] = [v, 16 - u];
uvs.push(u, v);
});
let cull = face_json.cullface ? FACE_INDEX[face_json.cullface] : -1;
@@ -499,6 +523,23 @@ export function bake_model(
return quads;
}
// how many corners along a face's corners end up on the face the model's turns take it to, which is how many quarter
// turns its texture needs to sit there like that face's texture on an unturned block. a face's default uvs (see
// default_uv) give its full face's corners CORNER_UVS in order, and turning keeps the order, so one corner says it
function uvlock_turns(face: number, x_turns: number, y_turns: number) {
let point: number[] = [...FACE_CORNERS[face][0]];
let turned = face;
for (let t = 0; t < x_turns; t++) {
point = [point[0], point[2], 1 - point[1]];
turned = TURN_X[turned];
}
for (let t = 0; t < y_turns; t++) {
point = [1 - point[2], point[1], point[0]];
turned = TURN_Y[turned];
}
return FACE_CORNERS[turned].findIndex((corner) => corner.every((end, axis) => end === point[axis]));
}
// turns a point (in block space) by the element's rotation
export function element_rotation(element: ModelElementJson): (point: number[]) => number[] {
const rotation = element.rotation;
+2
View File
@@ -249,6 +249,8 @@ export interface ItemRegistry<T = unknown | undefined> {
texture_id: string | ((item: ItemStack<T>) => string);
// shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins
data_textures?: Record<string, string>;
// a model it's drawn with instead of its texture, with the model's own textures, like a 3d wrench
model?: string;
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
name?: string;
block_id?: string;
+2
View File
@@ -271,6 +271,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
uses(file, texture, "texture");
}
}
if (json.item_model) uses(file, json.item_model, "model");
if (json.drops) uses(file, json.drops, "item");
if (json.loot_table) uses(file, json.loot_table, "loot table");
if (json.waterloggable) {
@@ -310,6 +311,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
errors.push(`${file}: item ${json.id} has no texture, and no block of this mod gives it an item`);
}
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
if (json.model) uses(file, json.model, "model");
if (json.places) uses(file, json.places, "block");
if (json.food?.returns) uses(file, json.food.returns, "item");
if (json.worn) {
+44 -4
View File
@@ -19,7 +19,14 @@ import {
WATERLOGGED,
type WornProperties,
} from "./everything_registry.ts";
import { type BlockPart, type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
import {
type BlockPart,
type BlockVariant,
DISPLAY_CONTEXTS,
type DisplayContext,
MODEL_FACES,
type ModelFace,
} from "./block_models.ts";
import { expand_items, list_matches } from "./tags.ts";
import { GAME_VERSION } from "./constants.ts";
@@ -121,6 +128,8 @@ export interface ItemJson {
texture?: string;
// data key -> texture, see ItemRegistry.data_textures
data_textures?: Record<string, string>;
// see ItemRegistry.model
model?: string;
// tier and speed default to 0 and 2
tool?: Omit<ToolProperties, "tier" | "speed"> & Partial<ToolProperties>;
places?: string;
@@ -389,6 +398,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
}
const json: ItemJson = { id, texture: item.texture_id };
if (item.data_textures !== undefined) json.data_textures = item.data_textures;
if (item.model !== undefined) json.model = item.model;
if (item.tool !== undefined) json.tool = item.tool;
if (item.block_id !== undefined) json.places = item.block_id;
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
@@ -416,6 +426,7 @@ export function add_to_block_item(json: ItemJson, item: ItemRegistry) {
function item_fields(json: ItemJson): Partial<ItemRegistry> {
const item: Partial<ItemRegistry> = {};
if (json.data_textures !== undefined) item.data_textures = json.data_textures;
if (json.model !== undefined) item.model = json.model;
if (json.tool !== undefined) item.tool = { tier: 0, speed: 2, ...json.tool };
if (json.places !== undefined) item.block_id = json.places;
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
@@ -589,6 +600,9 @@ export function validate_block(json: unknown): Problems {
problems.push(`${where}.${axis} must be 0, 90, 180 or 270`);
}
}
if (variant.uvlock !== undefined && typeof variant.uvlock !== "boolean") {
problems.push(`${where}.uvlock must be true or false`);
}
if (variant.shape !== undefined) problems.push(...validate_shape(variant.shape, `${where}.shape`));
if (variant.collision_shape !== undefined) {
problems.push(...validate_shape(variant.collision_shape, `${where}.collision_shape`, 24));
@@ -632,6 +646,9 @@ export function validate_block(json: unknown): Problems {
problems.push(`${where}.${axis} must be 0, 90, 180 or 270`);
}
}
if (part.uvlock !== undefined && typeof part.uvlock !== "boolean") {
problems.push(`${where}.uvlock must be true or false`);
}
if (part.shape !== undefined) problems.push(...validate_shape(part.shape, `${where}.shape`));
if (part.collision_shape !== undefined) {
problems.push(...validate_shape(part.collision_shape, `${where}.collision_shape`, 24));
@@ -781,10 +798,11 @@ export function validate_model(json: unknown): Problems {
const problems = validate_id(json.id, "id");
if (json.parent !== undefined) problems.push(...validate_id(json.parent, "parent"));
if (json.textures !== undefined) problems.push(...validate_textures(json.textures, "textures", true));
// a model can leave its elements to its parent, or be one that only gives texture defaults to its children
if (json.display !== undefined) problems.push(...validate_display(json.display));
// a model can leave its elements to its parent, or be one that only gives defaults to its children
if (json.elements === undefined) {
if (json.parent === undefined && json.textures === undefined) {
problems.push("a model needs elements, a parent, or textures for models that use it as theirs");
if (json.parent === undefined && json.textures === undefined && json.display === undefined) {
problems.push("a model needs elements, a parent, or textures or display for models that use it as theirs");
}
return problems;
}
@@ -844,6 +862,27 @@ export function validate_model(json: unknown): Problems {
return problems;
}
function validate_display(display: unknown): Problems {
if (!is_object(display)) return [`display must map ${DISPLAY_CONTEXTS.join(", ")} to transforms`];
const problems: Problems = [];
for (const [context, transform] of Object.entries(display)) {
if (!DISPLAY_CONTEXTS.includes(context as DisplayContext)) {
problems.push(`display: "${context}" isn't one of ${DISPLAY_CONTEXTS.join(", ")}`);
} else if (!is_object(transform)) {
problems.push(`display.${context} must be { rotation, translation, scale }`);
} else {
for (const [key, value] of Object.entries(transform)) {
if (!["rotation", "translation", "scale"].includes(key)) {
problems.push(`display.${context}: "${key}" isn't rotation, translation or scale`);
} else if (!Array.isArray(value) || value.length !== 3 || !value.every((v) => typeof v === "number")) {
problems.push(`display.${context}.${key} must be [x, y, z]`);
}
}
}
}
return problems;
}
// a texture id, or texture variables mapped to ids. a model's own defaults may also point at variables ("#side")
function validate_textures(value: unknown, field: string, allow_variables = false): Problems {
if (typeof value === "string") return validate_id(value, field);
@@ -869,6 +908,7 @@ export function validate_item(json: unknown): Problems {
}
}
}
if (json.model !== undefined) problems.push(...validate_id(json.model, "model"));
if (json.places !== undefined) problems.push(...validate_id(json.places, "places"));
problems.push(...validate_text(json.name, "name"), ...validate_text(json.lore, "lore"));
if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) {