Files
bworld/client/rendering/item_renderer.ts
T
2026-09-27 00:18:16 -03:00

123 lines
5.4 KiB
TypeScript

import { TEXTURE_SIZE } from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
const ITEM_SCALE = 0.5;
// keeps texture lookups off the sprite's edge
const UV_PAD = 0.5;
// each face's corners in drawing order (counter clockwise from outside) on a unit cube, with its shade.
// top, bottom, front, back, left, right, like the chunk mesher
const CUBE_FACES = [
{ corners: [[0, 1, 1], [1, 1, 1], [1, 1, 0], [0, 1, 0]], shade: 1.0, texture: "top" },
{ corners: [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], shade: 0.5, texture: "bottom" },
{ corners: [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], shade: 0.8, texture: "front" },
{ corners: [[1, 0, 0], [0, 0, 0], [0, 1, 0], [1, 1, 0]], shade: 0.8, texture: "side" },
{ corners: [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]], shade: 0.6, texture: "side" },
{ corners: [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]], shade: 0.6, texture: "side" },
] as const;
// sprite end each corner gets, u then v
const CORNER_UVS = [[0, 1], [1, 1], [1, 0], [0, 0]] as const;
// where the extra copies of a bigger stack sit, as fractions of the model's size
const COPY_OFFSETS = [[0, 0, 0], [0.35, 0.2, -0.25], [-0.3, 0.4, 0.2], [0.15, 0.6, 0.35], [-0.2, 0.8, -0.3]];
// minecraft draws more copies of the model the bigger the stack is
function copies(amount: number) {
if (amount > 48) return 5;
if (amount > 32) return 4;
if (amount > 16) return 3;
if (amount > 1) return 2;
return 1;
}
// a point of an item's model to where it's drawn. models are 1 across, from y 0 to 1, centered on x and z
export type ItemTransform = (x: number, y: number, z: number) => [number, number, number];
// the atlas has to be the current texture
export function render_item_entity(entity: ItemEntity, partial_tick: number) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
// spinning and bobbing, like minecraft's ItemEntityRenderer
const time = entity.age + partial_tick;
const angle = time / 20 + entity.bob_offset;
const bob = Math.sin(time / 10 + entity.bob_offset) * 0.1 + 0.1;
const { x, y, z } = entity.render_position(partial_tick);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const scale = item_block(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
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;
push_item(atlas, entity.item, (lx, ly, lz) => [
bx + (lx * cos - lz * sin) * scale,
by + ly * scale,
bz + (lx * sin + lz * cos) * scale,
]);
}
}
// the full block an item places, drawn as a cube. blocks that aren't cubes, like plants, are drawn as their sprite
export function item_block(item: ItemStack): BlockRegistry | undefined {
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
const block = item_info?.block_id ? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id) : undefined;
return block && block_item_texture(block) === undefined ? block : undefined;
}
// an item's model: a cube for blocks, its sprite made 3d for everything else (see item_model.ts)
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform) {
const block = item_block(item);
if (block) {
for (const face of CUBE_FACES) {
const region = get_sprite_region(cube_face_texture(block, face.texture));
const corners = face.corners.map(([cx, cy, cz]) => transform(cx - 0.5, cy, cz - 0.5));
push_textured_quad(atlas, region, corners, face.shade);
}
return;
}
const texture_id = item_texture(item);
const region = get_sprite_region(texture_id);
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = transform(...quad.corners[i]);
const [pu, pv] = quad.uvs[i];
push_vertex(px, py, pz, u(pu), v(pv), quad.shade, quad.shade, quad.shade, 1);
}
}
}
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], shade: number) {
const u0 = (region.x * TEXTURE_SIZE + UV_PAD) / atlas.width;
const v0 = (region.y * TEXTURE_SIZE + UV_PAD) / atlas.height;
const u1 = ((region.x + 1) * TEXTURE_SIZE - UV_PAD) / atlas.width;
const v1 = ((region.y + 1) * TEXTURE_SIZE - UV_PAD) / atlas.height;
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = corners[i];
const [cu, cv] = CORNER_UVS[i];
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, shade, shade, shade, 1);
}
}
function item_texture(item: ItemStack) {
const texture_id = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.texture_id;
if (typeof texture_id === "function") {
return texture_id(item);
}
return texture_id ?? "engine:missing";
}