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
+60 -3
View File
@@ -5,7 +5,17 @@ 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 { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, block_variant, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
import {
type BakedQuad,
block_item_quads,
block_item_texture,
block_variant,
cube_face_texture,
cube_face_tint,
ITEM_FACE_SHADE,
item_tint,
type ModelJson,
} from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
import { get_state_value, item_texture } from "$/common/utils.ts";
import { ID_MASK, TEXTURE_SIZE } from "$/common/constants.ts";
@@ -174,10 +184,46 @@ export function item_block(item: ItemStack): BlockRegistry | 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). tint multiplies its
// colors, like the light it's in
// a block's item_model quads, baked the first time they're drawn
export function item_model_quads(block: BlockRegistry): BakedQuad[] | undefined {
if (block.item_model === undefined) return undefined;
let quads = baked_items.get(block);
if (!quads) {
quads = block_item_quads(block, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"))) ?? [];
baked_items.set(block, quads);
}
return quads;
}
const baked_items = new WeakMap<BlockRegistry, BakedQuad[]>();
// an item's model: its block's item_model or a cube for blocks, its sprite made 3d for everything else (see
// item_model.ts). tint multiplies its colors, like the light it's in
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform, [tr, tg, tb]: Vec3 = [1, 1, 1]) {
const block = item_block(item);
const model = block && item_model_quads(block);
if (block && model) {
for (const quad of model) {
const region = get_sprite_region(quad.texture);
const [r, g, b] = item_tint(block, 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 [px, py, pz] = transform(p[i * 3] - 0.5, p[i * 3 + 1], p[i * 3 + 2] - 0.5);
push_vertex(
px,
py,
pz,
sprite_u(atlas, region, quad.uvs[i * 2]),
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
shade * r * tr,
shade * g * tg,
shade * b * tb,
1,
);
}
}
return;
}
if (block) {
for (const face of CUBE_FACES) {
const region = get_sprite_region(cube_face_texture(block, face.texture));
@@ -201,6 +247,17 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
}
}
// where a model's uv in pixels (0-16) is in the atlas, kept off the sprite's edge
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.width, UV_PAD), region.width - UV_PAD);
return (region.x + t) / atlas.width;
}
export function sprite_v(atlas: Texture, region: SpriteRegion, pixels: number) {
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.height, UV_PAD), region.height - UV_PAD);
return (region.y + t) / atlas.height;
}
function push_textured_quad(atlas: Texture, region: SpriteRegion, corners: number[][], color: number[]) {
const u0 = (region.x + UV_PAD) / atlas.width;
const v0 = (region.y + UV_PAD) / atlas.height;