// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { SLOT_SIZE } from "$/common/constants.ts"; import { get_sprite_region } from "$/client/sprites.ts"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; 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, 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 { draw_rect, draw_text, draw_texture_region, draw_texture_region_skewed, flush_batch, get_current_texture, push_vertex, set_current_texture, Texture, } from "$/client/renderer/mod.ts"; // the corners keep their size (times scale), the edges stretch along and the middle fills the rest, like minecraft's // nine slice gui sprites. borders are in source pixels export function draw_nine_slice( image: Texture, source_x: number, source_y: number, source_width: number, source_height: number, left: number, right: number, top: number, bottom: number, x: number, y: number, width: number, height: number, color = [1, 1, 1, 1], scale = 1, ) { // start and size of each column and row, in the source and on screen const source_columns = [[source_x, left], [source_x + left, source_width - left - right], [ source_x + source_width - right, right, ]]; const source_rows = [[source_y, top], [source_y + top, source_height - top - bottom], [ source_y + source_height - bottom, bottom, ]]; const columns = [[x, left * scale], [x + left * scale, width - (left + right) * scale], [ x + width - right * scale, right * scale, ]]; const rows = [[y, top * scale], [y + top * scale, height - (top + bottom) * scale], [ y + height - bottom * scale, bottom * scale, ]]; for (let row = 0; row < 3; row++) { for (let column = 0; column < 3; column++) { const [sx, sw] = source_columns[column]; const [sy, sh] = source_rows[row]; const [dx, dw] = columns[column]; const [dy, dh] = rows[row]; if (sw > 0 && sh > 0 && dw > 0 && dh > 0) { const [r, g, b, a] = color; draw_texture_region(image, sx, sy, sw, sh, dx, dy, dw, dh, false, false, r, g, b, a); } } } } export function draw_item(item: ItemStack, x: number, y: number) { const item_info = EverythingRegistry.get("items", item.type_id); if (!item_info) { 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) { draw_item_block(block_info, x, y); } else { draw_item_item(item, item_info, x, y); } if (item.max_amount !== 1) { draw_text( String(item.amount), x + SLOT_SIZE + 2 - 11 * 2, y + SLOT_SIZE + 2 - 11 * 2, 1, [63 / 255, 63 / 255, 63 / 255], ); draw_text( String(item.amount), x + SLOT_SIZE - 11 * 2, y + SLOT_SIZE - 11 * 2, 1, ); } // durability bar, only on worn tools: 13 by 2 pixels near the bottom, going from green to red const left = item.durability(); if (left !== undefined && item_info.tool && left < item_info.tool.durability) { const fraction = Math.max(0, left / item_info.tool.durability); const pixel = SLOT_SIZE / 18; draw_rect(x + 3 * pixel, y + 14 * pixel, 13 * pixel, 2 * pixel, [0, 0, 0, 1]); draw_rect(x + 3 * pixel, y + 14 * pixel, Math.round(13 * fraction) * pixel, pixel, [ 1 - fraction, fraction, 0, 1, ]); } } // ey its not a bad name function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) { const texture_id = item_texture(item, item_info); const sprite_region = get_sprite_region(texture_id); draw_texture_region( AssetManager.instance.get("bworld:textures"), sprite_region.x, sprite_region.y, sprite_region.width, sprite_region.height, x + 3, y + 3, SLOT_SIZE - 6, SLOT_SIZE - 6, ); } // 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) { 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 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) { const atlas = AssetManager.instance.get("bworld:textures"); if (get_current_texture() !== atlas.tex) { flush_batch(); set_current_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; }) .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) }; }) .sort((a, b) => a.depth - b.depth); for (const { quad } of visible) { const region = get_sprite_region(quad.texture); const [r, g, b] = item_tint(block_info, quad.variables); 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, 0, sprite_u(atlas, region, quad.uvs[i * 2]), sprite_v(atlas, region, quad.uvs[i * 2 + 1]), shade * r, shade * g, shade * b, 1, ); } } } function draw_item_block(block_info: BlockRegistry, x: number, y: number) { const top_texture = cube_face_texture(block_info, "top"); const front_texture = cube_face_texture(block_info, "front"); const left_texture = cube_face_texture(block_info, "side"); const atlas = AssetManager.instance.get("bworld:textures"); const top = get_sprite_region(top_texture); const front = get_sprite_region(front_texture); const left = get_sprite_region(left_texture); const padding = 4; const size = (SLOT_SIZE - padding * 2) / 2; const cx = x + SLOT_SIZE / 2; const cy = y + SLOT_SIZE / 2 - 10; const half_w = size; const half_h = size / 2; const height = size; draw_texture_region_skewed( atlas, top.x, top.y, top.width, top.height, [ { x: cx, y: cy - half_h }, { x: cx + half_w, y: cy }, { x: cx, y: cy + half_h }, { x: cx - half_w, y: cy }, ], ...cube_face_tint(block_info, "top"), ); draw_texture_region_skewed( atlas, left.x, left.y, left.width, left.height, [ { x: cx - half_w, y: cy }, { x: cx, y: cy + half_h }, { x: cx, y: cy + half_h + height }, { x: cx - half_w, y: cy + height }, ], ...cube_face_tint(block_info, "side"), ); draw_texture_region_skewed( atlas, front.x, front.y, front.width, front.height, [ { x: cx + half_w, y: cy }, { x: cx, y: cy + half_h }, { x: cx, y: cy + half_h + height }, { x: cx + half_w, y: cy + height }, ], ...cube_face_tint(block_info, "front"), ); }