Files
bworld/client/rendering/render_utils.ts
T
2026-10-28 11:49:56 -03:00

339 lines
9.9 KiB
TypeScript

// 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";
import { AssetManager } from "$/client/assets.ts";
import { ItemStack } from "$/common/inventory.ts";
import type { FluidVariant } from "$/common/mod_api/shared.ts";
import { item_texture } from "$/common/utils.ts";
import {
cube_face_texture,
cube_face_tint,
type DisplayTransform,
ITEM_FACE_SHADE,
item_tint,
} from "$/common/block_models.ts";
import {
display_matrix,
fluid_look,
item_block,
item_layers,
item_model_of,
type ItemModel,
sprite_u,
sprite_v,
} from "./item_renderer.ts";
import {
draw_rect,
draw_text,
draw_texture_region,
draw_texture_region_skewed,
push_vertex,
Texture,
use_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);
}
}
}
}
// show_count is for drawing an item where its count means nothing, like one shown on a screen
export function draw_item(item: ItemStack, x: number, y: number, show_count = item.max_amount !== 1) {
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
if (!item_info) {
throw Error(`Didn't find item registry for '${item.type_id}'`);
}
// blocks that aren't cubes, like plants, show a flat sprite
const model = item_model_of(item);
const block_info = item_block(item);
if (model) {
draw_item_model(model, x, y);
} else if (block_info) {
draw_item_block(block_info, x, y);
} else {
draw_item_item(item, item_info, x, y);
}
if (show_count) {
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 draw = (texture_id: string, [r, g, b] = [1, 1, 1]) => {
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,
false,
false,
r,
g,
b,
);
};
draw(item_texture(item, item_info));
for (const layer of item_layers(item)) draw(layer.texture, layer.color);
}
// a fluid as a slot shows it, its texture tinted in its color over the slot's inside where an item's sprite goes.
// filled (0 to 1) is how much of it there is from the bottom up, like a tank's level: the texture is cut, not squashed
export function draw_fluid(fluid: FluidVariant, x: number, y: number, filled = 1) {
const { texture, tint: [r, g, b] } = fluid_look(fluid);
// nothing for NaN too, like amount / capacity before there's a capacity
const done = filled > 0 ? Math.min(1, filled) : 0;
if (done === 0) return;
const region = get_sprite_region(texture);
const size = SLOT_SIZE - 6;
draw_texture_region(
AssetManager.instance.get("bworld:textures"),
region.x,
region.y + region.height * (1 - done),
region.width,
region.height * done,
x + 3,
y + 3 + size * (1 - done),
size,
size * done,
false,
false,
r,
g,
b,
);
}
// 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;
// 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);
}
// each model's icon in a slot at 0, 0, as x y u v r g b per vertex. a slot's place only moves the icon (see
// icon_matrix), so it's worked out once per model and moved to each slot, rather than every slot every frame
const model_icons = new WeakMap<ItemModel, Float32Array>();
// an item's model in a slot: its faces that face the viewer, back to front
function draw_item_model(model: ItemModel, x: number, y: number) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
use_texture(atlas.tex);
let icon = model_icons.get(model);
if (!icon) {
icon = model_icon(model, atlas);
model_icons.set(model, icon);
}
for (let i = 0; i < icon.length; i += 7) {
push_vertex(
x + icon[i],
y + icon[i + 1],
0,
icon[i + 2],
icon[i + 3],
icon[i + 4],
icon[i + 5],
icon[i + 6],
1,
);
}
}
function model_icon(model: ItemModel, atlas: Texture): Float32Array {
const matrix = icon_matrix(0, 0, model.display.gui);
// 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 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);
const vertices: number[] = [];
for (const { quad, corners } of visible) {
const region = get_sprite_region(quad.texture);
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]) {
vertices.push(
corners[i][0],
corners[i][1],
sprite_u(atlas, region, quad.uvs[i * 2]),
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
shade * r,
shade * g,
shade * b,
);
}
}
return new Float32Array(vertices);
}
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<Texture>("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"),
);
}