This commit is contained in:
2026-10-03 16:28:09 -03:00
parent 24fe7cdede
commit 91655c28c5
53 changed files with 1752 additions and 114 deletions
+82 -2
View File
@@ -4,12 +4,24 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
import { AssetManager } from "$/client/assets.ts";
import { ItemStack } from "$/common/inventory.ts";
import { item_texture } from "$/common/utils.ts";
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.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, 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";
@@ -75,7 +87,10 @@ export function draw_item(item: ItemStack, x: number, y: number) {
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
: undefined;
// blocks that aren't cubes, like plants, show a flat sprite
if (block_info && block_item_texture(block_info) === undefined) {
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);
@@ -129,6 +144,71 @@ 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) {
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<Texture>("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");