Item rendering
This commit is contained in:
@@ -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