import type { SpriteRegion } 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 { light_color, push_vertex, Texture } from "$/client/renderer/mod.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"; import type { ClientLevel } from "$/client/level/client_level.ts"; import { entity_tint } from "./entity_light.ts"; import type { Vec3 } from "./player_renderer.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; } // 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; 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]; // the atlas has to be the current texture export function render_item_entity(entity: ItemEntity, partial_tick: number) { const atlas = AssetManager.instance.get("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; 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; push_item(atlas, entity.item, (lx, ly, lz) => [ bx + (lx * cos - lz * sin) * scale, by + ly * scale, bz + (lx * sin + lz * cos) * scale, ], tint); } } // every item blocks with item_display show on top of them, see BlockRegistry.item_display. the atlas has to be the // current texture export function render_block_items(level: ClientLevel) { const atlas = AssetManager.instance.get("bworld:textures"); const blocks = EverythingRegistry.get_registry("blocks"); for (const items of level.block_items.values()) { for (const { x, y, z, item } of items.values()) { // 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; // 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 = {}; 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 = item_block(item) !== undefined; 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 (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("items", item.type_id); if (!item_info?.block_id || item_info.block_id !== item.type_id) return undefined; return EverythingRegistry.get("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; } // 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("models"))) ?? []; baked_items.set(block, quads); } return quads; } const baked_items = new WeakMap(); // 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)); 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); push_textured_quad(atlas, region, corners, [face.shade * r * tr, face.shade * g * tg, face.shade * b * tb]); } return; } const texture_id = item_texture(item); const region = get_sprite_region(texture_id); const u = (pixels: number) => (region.x + pixels) / atlas.width; const v = (pixels: number) => (region.y + 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 * tr, quad.shade * tg, quad.shade * tb, 1); } } } // 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; const u1 = (region.x + region.width - UV_PAD) / atlas.width; const v1 = (region.y + region.height - 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, color[0], color[1], color[2], 1); } }