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
+89 -21
View File
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { mat4, vec3 } from "gl-matrix";
import type { SpriteRegion } from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
@@ -9,14 +10,17 @@ import type { ItemEntity } from "$/client/entity/item_entity.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
import {
bake_model,
type BakedQuad,
block_item_quads,
block_item_texture,
block_variant,
cube_face_texture,
cube_face_tint,
type DisplayTransform,
find_model,
ITEM_FACE_SHADE,
item_tint,
type ModelDisplay,
type ModelJson,
} from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
@@ -59,6 +63,8 @@ function copies(amount: number) {
// copies for bigger stacks are this far apart, and at most this far off the middle
const SHOWN_ITEM_SCALE = 0.6;
const SHOWN_BLOCK_SCALE = 0.4;
// a model with its own fixed transform is sized by it on top of this. a block's usually halves it, to SHOWN_BLOCK_SCALE
const SHOWN_MODEL_SCALE = 0.8;
const SHOWN_COPY_GAP = 0.03;
const SHOWN_COPY_SPREAD = 0.075;
@@ -77,13 +83,20 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const scale = item_block(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
const scale = drawn_as_model(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
const ground = item_model_of(entity.item)?.display.ground;
const tint = entity_tint(entity, partial_tick);
for (let i = 0; i < copies(entity.item.amount); i++) {
const [ox, oy, oz] = COPY_OFFSETS[i];
const bx = x + ox * scale;
const by = y + bob + oy * scale * 0.5;
const bz = z + oz * scale;
if (ground) {
// lifted by a quarter of its height and spun, then its own transform
const base = mat4.fromTranslation(mat4.create(), [bx, by + 0.25 * (ground.scale?.[1] ?? 1), bz]);
push_item(atlas, entity.item, display_transform(mat4.rotateY(base, base, -angle), ground), tint);
continue;
}
push_item(atlas, entity.item, (lx, ly, lz) => [
bx + (lx * cos - lz * sin) * scale,
by + ly * scale,
@@ -134,7 +147,8 @@ function push_shown_item(
[bx, bz] = [-bz, bx];
}
const is_block = item_block(item) !== undefined;
const is_block = drawn_as_model(item);
const fixed = item_model_of(item)?.display.fixed;
const scale = is_block ? SHOWN_BLOCK_SCALE : SHOWN_ITEM_SCALE;
const surface = y + block.item_display!.surface / TEXTURE_SIZE;
const random = seeded_random(item.type_id);
@@ -146,7 +160,13 @@ function push_shown_item(
const cx = x + 0.5 + ox;
const cz = z + 0.5 + oz;
const bottom = surface + SHOWN_COPY_GAP * (i + 1);
if (is_block) {
if (fixed) {
// standing like a block, its middle where a block's would be and its front towards whoever it faces
const base = mat4.fromTranslation(mat4.create(), [cx, bottom + scale / 2, cz]);
mat4.rotateY(base, base, Math.PI - turns * Math.PI / 2);
mat4.scale(base, base, [SHOWN_MODEL_SCALE, SHOWN_MODEL_SCALE, SHOWN_MODEL_SCALE]);
push_item(atlas, item, display_transform(base, fixed), tint);
} else if (is_block) {
// standing, its front towards whoever it faces
push_item(atlas, item, (lx, ly, lz) => [
cx + (rx * lx - bx * lz) * scale,
@@ -192,27 +212,74 @@ export function item_block(item: ItemStack): BlockRegistry | undefined {
return block && block_item_texture(block) === undefined ? block : undefined;
}
// a block's item_model quads, baked the first time they're drawn
export function item_model_quads(block: BlockRegistry): BakedQuad[] | undefined {
if (block.item_model === undefined) return undefined;
let quads = baked_items.get(block);
if (!quads) {
quads = block_item_quads(block, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"))) ?? [];
baked_items.set(block, quads);
}
return quads;
// the model an item is drawn with, when it has one: its quads, its display transforms, and the block whose textures and
// tint fill it in
export interface ItemModel {
quads: BakedQuad[];
display: ModelDisplay;
block?: BlockRegistry;
}
const baked_items = new WeakMap<BlockRegistry, BakedQuad[]>();
// an item's model: its block's item_model or a cube for blocks, its sprite made 3d for everything else (see
// item_model.ts). tint multiplies its colors, like the light it's in
// an item's own model, or its block's item_model with the block's textures, baked the first time it's drawn. undefined
// for items drawn as a cube or as their sprite
export function item_model_of(item: ItemStack): ItemModel | undefined {
const info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
if (info?.model !== undefined) return baked_item_model(info, info.model);
const block = placed_block(item);
if (block?.item_model !== undefined) return baked_item_model(block, block.item_model, block);
return undefined;
}
function baked_item_model(key: ItemRegistry | BlockRegistry, id: string, block?: BlockRegistry): ItemModel {
let model = baked_items.get(key);
if (!model) {
const json = find_model(id, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")));
model = { quads: json ? bake_model(json, block?.textures) : [], display: json?.display ?? {}, block };
baked_items.set(key, model);
}
return model;
}
const baked_items = new WeakMap<ItemRegistry | BlockRegistry, ItemModel>();
// whether an item is drawn as a block or a model rather than its sprite, which decides how big it's drawn and how it's
// held when its model has no display transform for that
export function drawn_as_model(item: ItemStack) {
return item_block(item) !== undefined || item_model_of(item) !== undefined;
}
// a display transform after base: from a model's points (0 to 1 across the block) to base's, with the model's middle at
// base's origin before it's moved, turned and scaled
export function display_matrix(base: mat4, display: DisplayTransform): mat4 {
const matrix = mat4.clone(base);
const [tx, ty, tz] = display.translation ?? [0, 0, 0];
mat4.translate(matrix, matrix, [tx / 16, ty / 16, tz / 16]);
const [rx, ry, rz] = (display.rotation ?? [0, 0, 0]).map((degrees) => degrees * Math.PI / 180);
mat4.rotateX(matrix, matrix, rx);
mat4.rotateY(matrix, matrix, ry);
mat4.rotateZ(matrix, matrix, rz);
mat4.scale(matrix, matrix, display.scale ?? [1, 1, 1]);
mat4.translate(matrix, matrix, [-0.5, -0.5, -0.5]);
return matrix;
}
// display_matrix as an ItemTransform, whose points are centered on x and z
export function display_transform(base: mat4, display: DisplayTransform): ItemTransform {
const matrix = display_matrix(base, display);
const point = vec3.create();
return (x, y, z) => {
vec3.transformMat4(point, [x + 0.5, y, z + 0.5], matrix);
return [point[0], point[1], point[2]];
};
}
// an item's model: its own model or its block's item_model, a cube for other blocks, its sprite made 3d for everything
// else (see item_model.ts). tint multiplies its colors, like the light it's in
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform, [tr, tg, tb]: Vec3 = [1, 1, 1]) {
const block = item_block(item);
const model = block && item_model_quads(block);
if (block && model) {
for (const quad of model) {
const model = item_model_of(item);
if (model) {
for (const quad of model.quads) {
const region = get_sprite_region(quad.texture);
const [r, g, b] = item_tint(block, quad.variables);
const [r, g, b] = model.block ? item_tint(model.block, quad.variables) : [1, 1, 1];
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
for (const i of [0, 1, 2, 0, 2, 3]) {
const p = quad.positions;
@@ -232,6 +299,7 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
}
return;
}
const block = item_block(item);
if (block) {
for (const face of CUBE_FACES) {
const region = get_sprite_region(cube_face_texture(block, face.texture));