Item rendering
This commit is contained in:
@@ -16,7 +16,7 @@ import {
|
||||
Texture,
|
||||
use_texture,
|
||||
} from "$/client/renderer/mod.ts";
|
||||
import { item_block, push_item } from "./item_renderer.ts";
|
||||
import { display_transform, drawn_as_model, item_model_of, push_item } from "./item_renderer.ts";
|
||||
import { push_cube, type Vec3 } from "./player_renderer.ts";
|
||||
import { right_arm } from "./player_model.ts";
|
||||
import { render_worn_arm } from "./worn_renderer.ts";
|
||||
@@ -66,7 +66,11 @@ export class HandRenderer {
|
||||
if (item) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
set_current_texture(atlas.tex);
|
||||
push_item(atlas, item, pose(item, (1 - equip) * 0.6, Math.sin(swing_progress * Math.PI)), tint);
|
||||
const display = item_model_of(item)?.display.firstperson_righthand;
|
||||
const transform = display
|
||||
? display_transform(held_pose(1 - equip, swing_progress), display)
|
||||
: pose(item, (1 - equip) * 0.6, Math.sin(swing_progress * Math.PI));
|
||||
push_item(atlas, item, transform, tint);
|
||||
} else {
|
||||
const skin = player.skin ?? AssetManager.instance.get<Texture>("bworld:skin");
|
||||
const arm = right_arm(player.slim);
|
||||
@@ -119,9 +123,30 @@ export function arm_pose(pivot: Vec3, lower: number, attack: number) {
|
||||
};
|
||||
}
|
||||
|
||||
// from the item's model to in front of the camera. lower drops it out of sight, swing tips it forward and in
|
||||
// where a held model's firstperson_righthand transform starts from: low on the right in front of the camera, dropped
|
||||
// out of sight by lower and swung by attack like arm_pose. it's where the display format's transforms are made from, so
|
||||
// ones made for it elsewhere look the same here
|
||||
export function held_pose(lower: number, attack: number) {
|
||||
const degrees = Math.PI / 180;
|
||||
const sqrt_attack = Math.sqrt(attack);
|
||||
const matrix = mat4.create();
|
||||
mat4.translate(matrix, matrix, [
|
||||
-0.4 * Math.sin(sqrt_attack * Math.PI) + 0.56,
|
||||
0.2 * Math.sin(sqrt_attack * Math.PI * 2) - 0.52 - lower * 0.6,
|
||||
-0.2 * Math.sin(attack * Math.PI) - 0.72,
|
||||
]);
|
||||
const swing = Math.sin(sqrt_attack * Math.PI);
|
||||
mat4.rotateY(matrix, matrix, (45 - Math.sin(attack * attack * Math.PI) * 20) * degrees);
|
||||
mat4.rotateZ(matrix, matrix, swing * -20 * degrees);
|
||||
mat4.rotateX(matrix, matrix, swing * -80 * degrees);
|
||||
mat4.rotateY(matrix, matrix, -45 * degrees);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
// from the item's model to in front of the camera, for one without a display transform for it. lower drops it out of
|
||||
// sight, swing tips it forward and in
|
||||
function pose(item: ItemStack, lower: number, swing: number) {
|
||||
const p = item_block(item) ? BLOCK_POSE : ITEM_POSE;
|
||||
const p = drawn_as_model(item) ? BLOCK_POSE : ITEM_POSE;
|
||||
const matrix = mat4.create();
|
||||
mat4.translate(matrix, matrix, [p.x - swing * 0.25, p.y - lower + swing * 0.1, p.z - swing * 0.2]);
|
||||
mat4.rotateX(matrix, matrix, p.pitch - swing * SWING_ANGLE);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { mat4, vec3 } from "gl-matrix";
|
||||
import { SLOT_SIZE } from "$/common/constants.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
@@ -8,14 +9,13 @@ import { AssetManager } from "$/client/assets.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { item_texture } from "$/common/utils.ts";
|
||||
import {
|
||||
type BakedQuad,
|
||||
block_item_texture,
|
||||
cube_face_texture,
|
||||
cube_face_tint,
|
||||
type DisplayTransform,
|
||||
ITEM_FACE_SHADE,
|
||||
item_tint,
|
||||
} from "$/common/block_models.ts";
|
||||
import { item_model_quads, placed_block, sprite_u, sprite_v } from "./item_renderer.ts";
|
||||
import { display_matrix, item_block, item_model_of, type ItemModel, sprite_u, sprite_v } from "./item_renderer.ts";
|
||||
import {
|
||||
draw_rect,
|
||||
draw_text,
|
||||
@@ -84,12 +84,12 @@ export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
throw Error(`Didn't find item registry for '${item.type_id}'`);
|
||||
}
|
||||
|
||||
const block_info = placed_block(item);
|
||||
// blocks that aren't cubes, like plants, show a flat sprite
|
||||
const model = block_info && item_model_quads(block_info);
|
||||
if (block_info && model) {
|
||||
draw_item_model(block_info, model, x, y);
|
||||
} else if (block_info && block_item_texture(block_info) === undefined) {
|
||||
const model = item_model_of(item);
|
||||
const block_info = item_block(item);
|
||||
if (model) {
|
||||
draw_item_model(model, icon_matrix(x, y, model.display.gui));
|
||||
} else if (block_info) {
|
||||
draw_item_block(block_info, x, y);
|
||||
} else {
|
||||
draw_item_item(item, item_info, x, y);
|
||||
@@ -143,56 +143,51 @@ function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y:
|
||||
);
|
||||
}
|
||||
|
||||
// where the corners of the cube icon are, so models are drawn at the same size and angle: seen from above the
|
||||
// north west corner, west on the left and north on the right
|
||||
function item_projection(x: number, y: number) {
|
||||
// from a model's points to a slot, as screen x and y and depth toward the viewer. with a gui transform, the transform
|
||||
// starts from the model's 16 pixels across the slot's middle, y up. without one it's the cube icon's corners (see
|
||||
// draw_item_block), so models are drawn at the same size and angle: seen from above the north west corner, west on the
|
||||
// left and north on the right
|
||||
function icon_matrix(x: number, y: number, gui?: DisplayTransform): mat4 {
|
||||
if (gui) {
|
||||
const unit = SLOT_SIZE * 16 / 18;
|
||||
const base = mat4.fromTranslation(mat4.create(), [x + SLOT_SIZE / 2, y + SLOT_SIZE / 2, 0]);
|
||||
return display_matrix(mat4.scale(base, base, [unit, -unit, unit]), gui);
|
||||
}
|
||||
const size = (SLOT_SIZE - 4 * 2) / 2;
|
||||
const cx = x + SLOT_SIZE / 2;
|
||||
const cy = y + SLOT_SIZE / 2 - 10;
|
||||
return (px: number, py: number, pz: number) => [
|
||||
cx + (px - pz) * size,
|
||||
cy + size / 2 - (px + pz) * size / 2 + (1 - py) * size,
|
||||
];
|
||||
// toward the viewer is -x, +y, -z
|
||||
return mat4.fromValues(size, -size / 2, -1, 0, 0, -size, 1, 0, -size, -size / 2, -1, 0, cx, cy + size * 1.5, 0, 1);
|
||||
}
|
||||
|
||||
// toward whoever looks at an icon, see item_projection
|
||||
const ICON_VIEW = [-1, 1, -1];
|
||||
|
||||
// a block's item_model in a slot: its faces that face the viewer, back to front
|
||||
function draw_item_model(block_info: BlockRegistry, quads: BakedQuad[], x: number, y: number) {
|
||||
// an item's model in a slot: its faces that face the viewer, back to front
|
||||
function draw_item_model(model: ItemModel, matrix: mat4) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
use_texture(atlas.tex);
|
||||
const project = item_projection(x, y);
|
||||
const toward_viewer = (p: number[]) => p[0] * ICON_VIEW[0] + p[1] * ICON_VIEW[1] + p[2] * ICON_VIEW[2];
|
||||
const visible = quads
|
||||
.filter((quad) => {
|
||||
const p = quad.positions;
|
||||
const e1 = [p[3] - p[0], p[4] - p[1], p[5] - p[2]];
|
||||
const e2 = [p[9] - p[0], p[10] - p[1], p[11] - p[2]];
|
||||
const normal = [
|
||||
e1[1] * e2[2] - e1[2] * e2[1],
|
||||
e1[2] * e2[0] - e1[0] * e2[2],
|
||||
e1[0] * e2[1] - e1[1] * e2[0],
|
||||
];
|
||||
return toward_viewer(normal) > 1e-6;
|
||||
})
|
||||
// a mirroring matrix (like the y flip to the screen's y down) turns which way a face's corners go round
|
||||
const flip = Math.sign(mat4.determinant(matrix));
|
||||
const visible = model.quads
|
||||
.map((quad) => {
|
||||
const p = quad.positions;
|
||||
const center = [0, 1, 2].map((axis) => (p[axis] + p[3 + axis] + p[6 + axis] + p[9 + axis]) / 4);
|
||||
return { quad, depth: toward_viewer(center) };
|
||||
const corners = [0, 1, 2, 3].map((i) =>
|
||||
vec3.transformMat4(vec3.create(), quad.positions.slice(i * 3, i * 3 + 3) as vec3, matrix)
|
||||
);
|
||||
const e1 = vec3.subtract(vec3.create(), corners[1], corners[0]);
|
||||
const e2 = vec3.subtract(vec3.create(), corners[3], corners[0]);
|
||||
const facing = (e1[0] * e2[1] - e1[1] * e2[0]) * flip;
|
||||
const depth = corners.reduce((sum, corner) => sum + corner[2], 0);
|
||||
return { quad, corners, facing, depth };
|
||||
})
|
||||
.filter(({ facing }) => facing > 1e-6)
|
||||
.sort((a, b) => a.depth - b.depth);
|
||||
|
||||
for (const { quad } of visible) {
|
||||
for (const { quad, corners } of visible) {
|
||||
const region = get_sprite_region(quad.texture);
|
||||
const [r, g, b] = item_tint(block_info, 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;
|
||||
const [sx, sy] = project(p[i * 3], p[i * 3 + 1], p[i * 3 + 2]);
|
||||
push_vertex(
|
||||
sx,
|
||||
sy,
|
||||
corners[i][0],
|
||||
corners[i][1],
|
||||
0,
|
||||
sprite_u(atlas, region, quad.uvs[i * 2]),
|
||||
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
|
||||
|
||||
Reference in New Issue
Block a user