Item rendering
This commit is contained in:
+54
-13
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user