Render a hand
This commit is contained in:
@@ -3,6 +3,7 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
|
||||
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 { push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
|
||||
import { item_model } from "./item_model.ts";
|
||||
@@ -38,85 +39,63 @@ function copies(amount: number) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 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<Texture>("bworld:textures");
|
||||
const item_info = EverythingRegistry.get<ItemRegistry>("items", entity.item.type_id);
|
||||
// blocks that aren't cubes, like plants, are drawn as their item's sprite
|
||||
let block_info = item_info?.block_id
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
|
||||
: undefined;
|
||||
if (block_info && block_item_texture(block_info) !== undefined) block_info = undefined;
|
||||
|
||||
// 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 = block_info ? BLOCK_SCALE : ITEM_SCALE;
|
||||
const scale = item_block(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
|
||||
for (let i = 0; i < copies(entity.item.amount); i++) {
|
||||
const [ox, oy, oz] = COPY_OFFSETS[i];
|
||||
const base = { x: x + ox * scale, y: y + bob + oy * scale * 0.5, z: z + oz * scale };
|
||||
if (block_info) {
|
||||
push_block(atlas, block_info, base, scale, angle);
|
||||
} else {
|
||||
push_sprite(atlas, item_texture(entity, item_info), base, scale, angle);
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
const block = item_info?.block_id ? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id) : 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)
|
||||
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform) {
|
||||
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));
|
||||
push_textured_quad(atlas, region, corners, face.shade);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// a small cube turned around its middle
|
||||
function push_block(
|
||||
atlas: Texture,
|
||||
block: BlockRegistry,
|
||||
base: { x: number; y: number; z: number },
|
||||
size: number,
|
||||
angle: number,
|
||||
) {
|
||||
const sin = Math.sin(angle);
|
||||
const cos = Math.cos(angle);
|
||||
const corner = (cx: number, cy: number, cz: number) => {
|
||||
const lx = (cx - 0.5) * size;
|
||||
const lz = (cz - 0.5) * size;
|
||||
return [base.x + lx * cos - lz * sin, base.y + cy * size, base.z + lx * sin + lz * cos];
|
||||
};
|
||||
|
||||
for (const face of CUBE_FACES) {
|
||||
const region = get_sprite_region(cube_face_texture(block, face.texture));
|
||||
push_textured_quad(atlas, region, face.corners.map(([cx, cy, cz]) => corner(cx, cy, cz)), face.shade);
|
||||
}
|
||||
}
|
||||
|
||||
// a flat item as its 3d model (see item_model.ts), standing up and turning around its middle
|
||||
function push_sprite(
|
||||
atlas: Texture,
|
||||
texture_id: string,
|
||||
base: { x: number; y: number; z: number },
|
||||
size: number,
|
||||
angle: number,
|
||||
) {
|
||||
const texture_id = item_texture(item);
|
||||
const region = get_sprite_region(texture_id);
|
||||
const sin = Math.sin(angle);
|
||||
const cos = Math.cos(angle);
|
||||
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
|
||||
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
|
||||
|
||||
for (const quad of item_model(texture_id)) {
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [lx, ly, lz] = quad.corners[i];
|
||||
const [px, py, pz] = transform(...quad.corners[i]);
|
||||
const [pu, pv] = quad.uvs[i];
|
||||
push_vertex(
|
||||
base.x + (lx * cos - lz * sin) * size,
|
||||
base.y + ly * size,
|
||||
base.z + (lx * sin + lz * cos) * size,
|
||||
u(pu),
|
||||
v(pv),
|
||||
quad.shade,
|
||||
quad.shade,
|
||||
quad.shade,
|
||||
1,
|
||||
);
|
||||
push_vertex(px, py, pz, u(pu), v(pv), quad.shade, quad.shade, quad.shade, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,10 +113,10 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co
|
||||
}
|
||||
}
|
||||
|
||||
function item_texture(entity: ItemEntity, item_info: ItemRegistry | undefined) {
|
||||
const texture_id = item_info?.texture_id;
|
||||
function item_texture(item: ItemStack) {
|
||||
const texture_id = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.texture_id;
|
||||
if (typeof texture_id === "function") {
|
||||
return texture_id(entity.item);
|
||||
return texture_id(item);
|
||||
}
|
||||
return texture_id ?? "engine:missing";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user