443 lines
18 KiB
TypeScript
443 lines
18 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { mat4, vec3 } from "gl-matrix";
|
|
import type { SpriteRegion } from "@/common/constants.ts";
|
|
import { BlockRegistry, EverythingRegistry, type ItemLayer, 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 { contained_fluid, type ItemData, ItemStack } from "@/common/inventory.ts";
|
|
import type { FluidVariant } from "@/common/mod_api/shared.ts";
|
|
import type { FluidJson } from "@/common/mod_data.ts";
|
|
import { light_color, push_vertex, Texture } from "@/client/renderer/mod.ts";
|
|
import {
|
|
bake_model,
|
|
type BakedQuad,
|
|
block_item_texture,
|
|
block_variant,
|
|
cube_face_texture,
|
|
cube_face_tint,
|
|
type DisplayTransform,
|
|
find_model,
|
|
ITEM_FACE_SHADE,
|
|
item_tint,
|
|
type ModelDisplay,
|
|
type ModelJson,
|
|
} from "@/common/block_models.ts";
|
|
import { item_model, type ItemQuad } from "./item_model.ts";
|
|
import { get_state_value, item_texture } from "@/common/utils.ts";
|
|
import { ID_MASK, TEXTURE_SIZE } from "@/common/constants.ts";
|
|
import type { ClientLevel } from "@/client/level/client_level.ts";
|
|
import { entity_tint } from "./entity_light.ts";
|
|
import type { Vec3 } from "./player_renderer.ts";
|
|
import { fluid_rules } from "@/client/fluid_rules.ts";
|
|
import { color_rgb, tint_without_biome, type TintColor } from "@/common/worldgen/biome_colors.ts";
|
|
|
|
// how big items on the ground are drawn, minecraft's ground transform
|
|
const BLOCK_SCALE = 0.25;
|
|
const ITEM_SCALE = 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]];
|
|
|
|
// how far each of a flat item's layers sits out from the one under it, so it's drawn over it
|
|
const LAYER_GAP = 0.001;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// items shown on blocks, like farmer's delight's CuttingBoardRenderer: items lie flat, blocks stand. the extra
|
|
// copies for bigger stacks are this far apart, and at most this far off the middle
|
|
const SHOWN_ITEM_SCALE = 0.6;
|
|
const SHOWN_BLOCK_SCALE = 0.4;
|
|
// a model with its own fixed transform is sized by it on top of this. a block's usually halves it, to SHOWN_BLOCK_SCALE
|
|
const SHOWN_MODEL_SCALE = 0.8;
|
|
const SHOWN_COPY_GAP = 0.03;
|
|
const SHOWN_COPY_SPREAD = 0.075;
|
|
|
|
// 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];
|
|
|
|
// where push_item's vertices go: the batch, or anything else that takes them the same way, like a visual's model
|
|
export type VertexSink = typeof push_vertex;
|
|
|
|
// an item the size it's drawn on the ground, with its middle at the middle of the block. for a visual's item, which
|
|
// is placed from there, like create's items in a basin
|
|
export function ground_transform(item: ItemStack): ItemTransform {
|
|
const ground = item_model_of(item)?.display.ground;
|
|
if (ground) return display_transform(mat4.fromTranslation(mat4.create(), [0.5, 0.5, 0.5]), ground);
|
|
const scale = drawn_as_model(item) ? BLOCK_SCALE : ITEM_SCALE;
|
|
return (x, y, z) => [0.5 + x * scale, 0.5 + (y - 0.5) * scale, 0.5 + z * scale];
|
|
}
|
|
|
|
// the atlas has to be the current texture
|
|
export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
|
const atlas = AssetManager.instance.get<Texture>("cubi: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 = drawn_as_model(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
|
|
const ground = item_model_of(entity.item)?.display.ground;
|
|
const tint = entity_tint(entity, partial_tick);
|
|
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;
|
|
if (ground) {
|
|
// lifted by a quarter of its height and spun, then its own transform
|
|
const base = mat4.fromTranslation(mat4.create(), [bx, by + 0.25 * (ground.scale?.[1] ?? 1), bz]);
|
|
push_item(atlas, entity.item, display_transform(mat4.rotateY(base, base, -angle), ground), tint);
|
|
continue;
|
|
}
|
|
push_item(atlas, entity.item, (lx, ly, lz) => [
|
|
bx + (lx * cos - lz * sin) * scale,
|
|
by + ly * scale,
|
|
bz + (lx * sin + lz * cos) * scale,
|
|
], tint);
|
|
}
|
|
}
|
|
|
|
// the stacks shown items are drawn as, by their "item" client data, which is replaced whenever it changes
|
|
const shown_stacks = new WeakMap<ItemData, ItemStack>();
|
|
|
|
// every item blocks with item_display show on top of them, their "item" client data, see BlockRegistry.item_display.
|
|
// the atlas has to be the current texture
|
|
export function render_block_items(level: ClientLevel) {
|
|
const atlas = AssetManager.instance.get<Texture>("cubi:textures");
|
|
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
|
for (const in_chunk of level.block_data.values()) {
|
|
for (const { x, y, z, data } of in_chunk.values()) {
|
|
const shown = data.item as ItemData | undefined;
|
|
if (!shown) continue;
|
|
// gone already when this client predicted breaking it
|
|
const value = level.get_block_value(x, y, z);
|
|
const block = blocks[value & ID_MASK];
|
|
if (!block?.item_display) continue;
|
|
let item = shown_stacks.get(shown);
|
|
if (!item) {
|
|
item = ItemStack.from_data(shown);
|
|
shown_stacks.set(shown, item);
|
|
}
|
|
// the block's own light, or the one above for blocks light doesn't get into, since the items sit on top
|
|
const here = level.get_light(x, y, z);
|
|
const above = level.get_light(x, y + 1, z);
|
|
const tint = light_color((Math.max(here >> 4, above >> 4) << 4) | Math.max(here & 15, above & 15));
|
|
push_shown_item(atlas, block, value, item, x, y, z, tint);
|
|
}
|
|
}
|
|
}
|
|
|
|
function push_shown_item(
|
|
atlas: Texture,
|
|
block: BlockRegistry,
|
|
value: number,
|
|
item: ItemStack,
|
|
x: number,
|
|
y: number,
|
|
z: number,
|
|
tint: Vec3,
|
|
) {
|
|
const states: Record<string, number> = {};
|
|
for (const state of block.states ?? []) states[state.name] = get_state_value(value, block, state.name)!;
|
|
// the way the block's model is turned: towards the player who placed it, like the cutting board's facing
|
|
const turns = Math.round(block_variant(block, states).y / 90) % 4;
|
|
// to the right of whoever it faces, and away from them. unturned it faces north (-z)
|
|
let [rx, rz] = [-1, 0];
|
|
let [bx, bz] = [0, 1];
|
|
for (let t = 0; t < turns; t++) {
|
|
[rx, rz] = [-rz, rx];
|
|
[bx, bz] = [-bz, bx];
|
|
}
|
|
|
|
const is_block = drawn_as_model(item);
|
|
const fixed = item_model_of(item)?.display.fixed;
|
|
const scale = is_block ? SHOWN_BLOCK_SCALE : SHOWN_ITEM_SCALE;
|
|
const surface = y + block.item_display!.surface / TEXTURE_SIZE;
|
|
const random = seeded_random(item.type_id);
|
|
// one, then more the fuller the stack is, like the java mod
|
|
const count = item.amount > 1 ? 1 + Math.ceil((item.amount / item.max_amount) * 4) : 1;
|
|
for (let i = 0; i < count; i++) {
|
|
const ox = count > 1 ? (random() * 2 - 1) * SHOWN_COPY_SPREAD : 0;
|
|
const oz = count > 1 ? (random() * 2 - 1) * SHOWN_COPY_SPREAD : 0;
|
|
const cx = x + 0.5 + ox;
|
|
const cz = z + 0.5 + oz;
|
|
const bottom = surface + SHOWN_COPY_GAP * (i + 1);
|
|
if (fixed) {
|
|
// standing like a block, its middle where a block's would be and its front towards whoever it faces
|
|
const base = mat4.fromTranslation(mat4.create(), [cx, bottom + scale / 2, cz]);
|
|
mat4.rotateY(base, base, Math.PI - turns * Math.PI / 2);
|
|
mat4.scale(base, base, [SHOWN_MODEL_SCALE, SHOWN_MODEL_SCALE, SHOWN_MODEL_SCALE]);
|
|
push_item(atlas, item, display_transform(base, fixed), tint);
|
|
} else if (is_block) {
|
|
// standing, its front towards whoever it faces
|
|
push_item(atlas, item, (lx, ly, lz) => [
|
|
cx + (rx * lx - bx * lz) * scale,
|
|
bottom + ly * scale,
|
|
cz + (rz * lx - bz * lz) * scale,
|
|
], tint);
|
|
} else {
|
|
// lying face up, its top away from whoever it faces so they read it the right way round
|
|
const half = scale / TEXTURE_SIZE / 2;
|
|
push_item(atlas, item, (lx, ly, lz) => [
|
|
cx + (rx * lx + bx * (ly - 0.5)) * scale,
|
|
bottom + half + lz * scale,
|
|
cz + (rz * lx + bz * (ly - 0.5)) * scale,
|
|
], tint);
|
|
}
|
|
}
|
|
}
|
|
|
|
// the same copies in the same places every frame, by item
|
|
function seeded_random(seed: string) {
|
|
let state = 0;
|
|
for (let i = 0; i < seed.length; i++) state = (Math.imul(state, 31) + seed.charCodeAt(i)) | 0;
|
|
return () => {
|
|
// mulberry32
|
|
state = (state + 0x6d2b79f5) | 0;
|
|
let t = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
// the block a block's own item looks like. other items that place a block, like seeds or a water bucket, look like
|
|
// their own texture
|
|
export function placed_block(item: ItemStack): BlockRegistry | undefined {
|
|
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
|
if (!item_info?.block_id || item_info.block_id !== item.type_id) return undefined;
|
|
return EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id);
|
|
}
|
|
|
|
// 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 block = placed_block(item);
|
|
return block && block_item_texture(block) === undefined ? block : undefined;
|
|
}
|
|
|
|
// the model an item is drawn with, when it has one: its quads, its display transforms, and the block whose textures and
|
|
// tint fill it in
|
|
export interface ItemModel {
|
|
quads: BakedQuad[];
|
|
display: ModelDisplay;
|
|
block?: BlockRegistry;
|
|
}
|
|
|
|
// an item's own model, or its block's item_model with the block's textures, baked the first time it's drawn. undefined
|
|
// for items drawn as a cube or as their sprite
|
|
export function item_model_of(item: ItemStack): ItemModel | undefined {
|
|
const info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
|
if (info?.model !== undefined) return baked_item_model(info, info.model);
|
|
const block = placed_block(item);
|
|
if (block?.item_model !== undefined) return baked_item_model(block, block.item_model, block);
|
|
return undefined;
|
|
}
|
|
|
|
function baked_item_model(key: ItemRegistry | BlockRegistry, id: string, block?: BlockRegistry): ItemModel {
|
|
let model = baked_items.get(key);
|
|
if (!model) {
|
|
const json = find_model(id, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")));
|
|
model = { quads: json ? bake_model(json, block?.textures) : [], display: json?.display ?? {}, block };
|
|
baked_items.set(key, model);
|
|
}
|
|
return model;
|
|
}
|
|
const baked_items = new WeakMap<ItemRegistry | BlockRegistry, ItemModel>();
|
|
|
|
// whether an item is drawn as a block or a model rather than its sprite, which decides how big it's drawn and how it's
|
|
// held when its model has no display transform for that
|
|
export function drawn_as_model(item: ItemStack) {
|
|
return item_block(item) !== undefined || item_model_of(item) !== undefined;
|
|
}
|
|
|
|
// a display transform after base: from a model's points (0 to 1 across the block) to base's, with the model's middle at
|
|
// base's origin before it's moved, turned and scaled
|
|
export function display_matrix(base: mat4, display: DisplayTransform): mat4 {
|
|
const matrix = mat4.clone(base);
|
|
const [tx, ty, tz] = display.translation ?? [0, 0, 0];
|
|
mat4.translate(matrix, matrix, [tx / 16, ty / 16, tz / 16]);
|
|
const [rx, ry, rz] = (display.rotation ?? [0, 0, 0]).map((degrees) => degrees * Math.PI / 180);
|
|
mat4.rotateX(matrix, matrix, rx);
|
|
mat4.rotateY(matrix, matrix, ry);
|
|
mat4.rotateZ(matrix, matrix, rz);
|
|
mat4.scale(matrix, matrix, display.scale ?? [1, 1, 1]);
|
|
mat4.translate(matrix, matrix, [-0.5, -0.5, -0.5]);
|
|
return matrix;
|
|
}
|
|
|
|
// display_matrix as an ItemTransform, whose points are centered on x and z
|
|
export function display_transform(base: mat4, display: DisplayTransform): ItemTransform {
|
|
const matrix = display_matrix(base, display);
|
|
const point = vec3.create();
|
|
return (x, y, z) => {
|
|
vec3.transformMat4(point, [x + 0.5, y, z + 0.5], matrix);
|
|
return [point[0], point[1], point[2]];
|
|
};
|
|
}
|
|
|
|
// an item's model: its own model or its block's item_model, a cube for other blocks, its sprite made 3d for everything
|
|
// else (see item_model.ts). tint multiplies its colors, like the light it's in. two triangles a quad, its corners 0 1 2
|
|
// and 0 2 3
|
|
export function push_item(
|
|
atlas: Texture,
|
|
item: ItemStack,
|
|
transform: ItemTransform,
|
|
[tr, tg, tb]: Vec3 = [1, 1, 1],
|
|
vertex: VertexSink = push_vertex,
|
|
) {
|
|
const model = item_model_of(item);
|
|
if (model) {
|
|
for (const quad of model.quads) {
|
|
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]) {
|
|
const p = quad.positions;
|
|
const [px, py, pz] = transform(p[i * 3] - 0.5, p[i * 3 + 1], p[i * 3 + 2] - 0.5);
|
|
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;
|
|
}
|
|
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));
|
|
const [r, g, b] = cube_face_tint(block, face.texture);
|
|
const color = [face.shade * r * tr, face.shade * g * tg, face.shade * b * tb];
|
|
push_textured_quad(atlas, region, corners, color, vertex);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// its sprite, then its layers over it: only their fronts and backs, the first two quads, each a little further out
|
|
const texture_id = item_texture(item);
|
|
push_sprite(atlas, texture_id, item_model(texture_id), [tr, tg, tb], 0, transform, vertex);
|
|
item_layers(item).forEach(({ texture, color: [r, g, b] }, i) => {
|
|
const color: Vec3 = [r * tr, g * tg, b * tb];
|
|
push_sprite(atlas, texture, item_model(texture).slice(0, 2), color, (i + 1) * LAYER_GAP, transform, vertex);
|
|
});
|
|
}
|
|
|
|
// a flat item's sprite made 3d (see item_model.ts), its front and back gap further out than the item's
|
|
function push_sprite(
|
|
atlas: Texture,
|
|
texture_id: string,
|
|
quads: ItemQuad[],
|
|
[r, g, b]: Vec3,
|
|
gap: number,
|
|
transform: ItemTransform,
|
|
vertex: VertexSink,
|
|
) {
|
|
const region = get_sprite_region(texture_id);
|
|
for (const quad of quads) {
|
|
// each corner transformed once, though corners 0 and 2 are in both triangles
|
|
const points = quad.corners.map(([cx, cy, cz]) => transform(cx, cy, cz + Math.sign(cz) * gap));
|
|
for (const i of QUAD_CORNERS) {
|
|
const point = points[i];
|
|
const uv = quad.uvs[i];
|
|
const u = (region.x + uv[0]) / atlas.width;
|
|
const v = (region.y + uv[1]) / atlas.height;
|
|
vertex(point[0], point[1], point[2], u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
// a quad's corners as two triangles
|
|
const QUAD_CORNERS = [0, 1, 2, 0, 2, 3];
|
|
|
|
// the layers drawn over a flat item's texture (see ItemLayer), each in its tint's color, without the ones whose tint
|
|
// has no color for this stack
|
|
export function item_layers(item: ItemStack): { texture: string; color: Vec3 }[] {
|
|
const layers = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.layers ?? [];
|
|
return layers.flatMap(({ texture, tint }) => {
|
|
const color = layer_color(item, tint);
|
|
return color ? [{ texture, color }] : [];
|
|
});
|
|
}
|
|
|
|
function layer_color(item: ItemStack, tint: ItemLayer["tint"]): Vec3 | undefined {
|
|
if (tint === undefined) return [1, 1, 1];
|
|
if (tint !== "fluid") return color_rgb(tint_without_biome(tint));
|
|
const fluid = contained_fluid(item.type_id, item.data);
|
|
return fluid && fluid_rgb(fluid);
|
|
}
|
|
|
|
// a fluid's color by its rules, as r g b from 0 to 1
|
|
export function fluid_rgb(fluid: FluidVariant): Vec3 {
|
|
return color_rgb(tint_without_biome(fluid_rules.color(fluid) as TintColor));
|
|
}
|
|
|
|
// a fluid's texture and its color by its rules, for drawing it in a box or a slot. throws for a fluid no mod defines
|
|
export function fluid_look(fluid: FluidVariant): { texture: string; tint: Vec3 } {
|
|
const json = EverythingRegistry.get<FluidJson>("fluids", fluid?.id);
|
|
if (!json) throw new Error(`there's no fluid ${fluid?.id}`);
|
|
return { texture: json.texture ?? "cubi:missing", tint: fluid_rgb(structuredClone(fluid)) };
|
|
}
|
|
|
|
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
|
|
// lookups that round past its edge on its own pixels
|
|
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
|
|
return (region.x + pixels / TEXTURE_SIZE * region.width) / atlas.width;
|
|
}
|
|
|
|
export function sprite_v(atlas: Texture, region: SpriteRegion, pixels: number) {
|
|
return (region.y + pixels / TEXTURE_SIZE * region.height) / atlas.height;
|
|
}
|
|
|
|
function push_textured_quad(
|
|
atlas: Texture,
|
|
region: SpriteRegion,
|
|
corners: number[][],
|
|
color: number[],
|
|
vertex: VertexSink,
|
|
) {
|
|
const u0 = region.x / atlas.width;
|
|
const v0 = region.y / atlas.height;
|
|
const u1 = (region.x + region.width) / atlas.width;
|
|
const v1 = (region.y + region.height) / atlas.height;
|
|
|
|
for (const i of [0, 1, 2, 0, 2, 3]) {
|
|
const [px, py, pz] = corners[i];
|
|
const [cu, cv] = CORNER_UVS[i];
|
|
vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1);
|
|
}
|
|
}
|