Proper cutting board
This commit is contained in:
@@ -5,9 +5,11 @@ 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 { push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import { block_item_texture, block_variant, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import { item_model } from "./item_model.ts";
|
||||
import { item_texture } from "$/common/utils.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";
|
||||
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
const BLOCK_SCALE = 0.25;
|
||||
@@ -40,6 +42,13 @@ function copies(amount: number) {
|
||||
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];
|
||||
|
||||
@@ -69,6 +78,87 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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<Texture>("bworld:textures");
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("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;
|
||||
push_shown_item(atlas, block, value, item, x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function push_shown_item(
|
||||
atlas: Texture,
|
||||
block: BlockRegistry,
|
||||
value: number,
|
||||
item: ItemStack,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
) {
|
||||
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 = 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,
|
||||
]);
|
||||
} 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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 item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { MultiPlayerGameMode } from "$/client/game_mode.ts";
|
||||
import type { Entity } from "$/client/entity/entity.ts";
|
||||
import { RemotePlayer } from "$/client/entity/remote_player.ts";
|
||||
import { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { render_item_entity } from "./item_renderer.ts";
|
||||
import { render_block_items, render_item_entity } from "./item_renderer.ts";
|
||||
import { render_player } from "./player_renderer.ts";
|
||||
import { render_mod_entity } from "./entity_renderer.ts";
|
||||
import { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
@@ -117,6 +117,7 @@ export class LevelRenderer {
|
||||
render_mod_entity(entity, camera, partial_tick);
|
||||
}
|
||||
}
|
||||
render_block_items(level);
|
||||
flush_batch();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user