Player rendering in the inventory
This commit is contained in:
@@ -7,6 +7,11 @@ import { display_name } from "$/common/utils.ts";
|
||||
import { ClientMessage, PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
|
||||
import { player_menu } from "../menu.ts";
|
||||
import type { ModGraphics } from "./mod_graphics.ts";
|
||||
import { draw_gui_player } from "../rendering/gui_player.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import type { Texture } from "../renderer/mod.ts";
|
||||
import { GUI_SCALE } from "./gui_sprites.ts";
|
||||
|
||||
const WIDTH = 176;
|
||||
const HEIGHT = 166;
|
||||
@@ -18,9 +23,16 @@ const ICON = 16;
|
||||
const ICON_GAP = 2;
|
||||
const ICON_MARGIN = 4;
|
||||
const TIME_COLOR: [number, number, number, number] = [0.66, 0.66, 0.66, 1];
|
||||
// the box the player stands in, left of the crafting grid, like minecraft's
|
||||
const PLAYER_BOX = { x: MARGIN, y: MARGIN, size: 64 };
|
||||
const PLAYER_BOX_COLOR: [number, number, number, number] = [0, 0, 0, 0.6];
|
||||
// gui pixels to a skin pixel, so the player's 32 fit in the box with room to look around
|
||||
const PLAYER_PIXEL = 1.6;
|
||||
// the feet, above the box's bottom
|
||||
const PLAYER_FEET = 4;
|
||||
|
||||
// menu 0's screen: the crafting grid over the inventory, and any slots mods added, with the player's effects above.
|
||||
// the recipes live on the server, which fills in the result
|
||||
// menu 0's screen: the player looking at the mouse beside the crafting grid, over the inventory, and any slots mods
|
||||
// added, with the player's effects above. the recipes live on the server, which fills in the result
|
||||
export class GuiPlayerInventory extends GuiMenuScreen {
|
||||
#player: LocalPlayer;
|
||||
|
||||
@@ -31,6 +43,7 @@ export class GuiPlayerInventory extends GuiMenuScreen {
|
||||
|
||||
render_background(g: ModGraphics): void {
|
||||
g.panel(0, 0, WIDTH, HEIGHT);
|
||||
this.#render_player(g);
|
||||
this.#render_effects(g);
|
||||
// the slots mods added outside the panel get one of their own, around them with the same margin
|
||||
const outside = this.menu.slots.slice(PLAYER_MENU_EXTRA)
|
||||
@@ -44,6 +57,25 @@ export class GuiPlayerInventory extends GuiMenuScreen {
|
||||
}
|
||||
}
|
||||
|
||||
// the player in their box, in their skin and what they wear, turned toward the mouse
|
||||
#render_player(g: ModGraphics) {
|
||||
const { x, y, size } = PLAYER_BOX;
|
||||
g.rect(x, y, size, size, PLAYER_BOX_COLOR);
|
||||
const skin = this.#player.skin ?? AssetManager.instance.get<Texture>("bworld:skin");
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
draw_gui_player(
|
||||
skin,
|
||||
this.#player.slim,
|
||||
this.x + (x + size / 2) * GUI_SCALE,
|
||||
this.y + (y + size - PLAYER_FEET) * GUI_SCALE,
|
||||
PLAYER_PIXEL * GUI_SCALE,
|
||||
GUI_SCALE,
|
||||
mouse.x,
|
||||
mouse.y,
|
||||
this.#player.worn,
|
||||
);
|
||||
}
|
||||
|
||||
// the ones with an icon, left to right in the order they were added. hovering one says what it is
|
||||
#render_effects(g: ModGraphics) {
|
||||
const shown = this.#player.effects.flatMap((effect) => {
|
||||
@@ -68,7 +100,7 @@ export class GuiPlayerInventory extends GuiMenuScreen {
|
||||
}
|
||||
|
||||
render_labels(g: ModGraphics): void {
|
||||
g.text(this.menu.title, 26, 6);
|
||||
g.text(this.menu.title, 79, 6);
|
||||
g.text("Inventory", 8, 73);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -77,10 +77,11 @@ export function player_menu(inventories: ClientInventories): ClientMenu {
|
||||
const row = Math.floor(i / 9);
|
||||
slots.push(slot(inventories.inventory, i, 7 + (i % 9) * 18, i < 9 ? 141 : 83 + (row - 1) * 18));
|
||||
}
|
||||
// the crafting grid on the right, the player drawn left of it, see GuiPlayerInventory
|
||||
for (let i = 0; i < CRAFTING_RESULT_SLOT; i++) {
|
||||
slots.push(slot(inventories.crafting, i, 25 + (i % 3) * 18, 16 + Math.floor(i / 3) * 18));
|
||||
slots.push(slot(inventories.crafting, i, 79 + (i % 3) * 18, 16 + Math.floor(i / 3) * 18));
|
||||
}
|
||||
slots.push(slot(inventories.crafting, CRAFTING_RESULT_SLOT, 115, 34, true));
|
||||
slots.push(slot(inventories.crafting, CRAFTING_RESULT_SLOT, 151, 34, true));
|
||||
if (slots.length !== PLAYER_MENU_EXTRA) throw new Error("menu 0 has the wrong slots");
|
||||
for (const [i, info] of inventories.extra_slots.entries()) {
|
||||
slots.push({ ...slot(inventories.extra, i, info.x, info.y, info.output === true), icon: info.icon });
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
// the player drawn in their inventory, like minecraft's InventoryScreen.renderEntityInInventoryFollowsMouse: standing,
|
||||
// facing out of the screen, the body turned a little toward the mouse and the head turned and tilted more, with what
|
||||
// they wear, like their curios. the gui has no depth buffer, so the faces are drawn back to front with the ones facing
|
||||
// away left out, like 3d items in slots
|
||||
import { flush_batch, get_current_texture, push_vertex, set_current_texture, type Texture } from "../renderer/mod.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { rotation_around } from "./model_math.ts";
|
||||
import { part_point, PARTS, type PlayerPose, pose_at, pose_to_world, SLIM_PARTS } from "./player_model.ts";
|
||||
import { push_cube, type VertexSink } from "./player_renderer.ts";
|
||||
import { push_worn } from "./worn_renderer.ts";
|
||||
|
||||
// how far away the mouse is, in gui pixels, for the player to turn about 45 degrees toward it, like minecraft's 40
|
||||
const LOOK_DISTANCE = 40;
|
||||
// how far they turn for that, like minecraft's 20 degrees for the body, and 40 for the head
|
||||
const BODY_TURN = 20 * Math.PI / 180;
|
||||
const HEAD_TURN = 40 * Math.PI / 180;
|
||||
const HEAD_TILT = 20 * Math.PI / 180;
|
||||
// the eyes, in skin pixels above the feet: what the mouse is measured from
|
||||
export const EYES = 28;
|
||||
|
||||
// a face: its 6 vertices as [x, y, z, u, v, r, g, b], in blocks around the feet, the viewer at +z, and the texture
|
||||
// they're in, the skin or the atlas for what's worn
|
||||
export interface Face {
|
||||
vertices: number[][];
|
||||
texture: Texture;
|
||||
}
|
||||
|
||||
// the player facing +z, out of the screen, turned toward a mouse dx right of and dy below their eyes, in gui pixels
|
||||
export function gui_player_pose(slim: boolean, dx: number, dy: number): PlayerPose {
|
||||
const look_x = Math.atan(dx / LOOK_DISTANCE);
|
||||
const look_y = Math.atan(dy / LOOK_DISTANCE);
|
||||
const pose = pose_at({
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0,
|
||||
yaw: Math.PI + look_x * BODY_TURN,
|
||||
pitch: 0,
|
||||
slim,
|
||||
walk: { position: 0, speed: 0 },
|
||||
});
|
||||
// the head turns the rest of the way on top of the body, and tilts down for a mouse below
|
||||
pose.parts.head = rotation_around(PARTS[0].pivot, -look_y * HEAD_TILT, look_x * (HEAD_TURN - BODY_TURN), 0);
|
||||
return pose;
|
||||
}
|
||||
|
||||
// the player's faces turned toward the mouse (see gui_player_pose), with what they wear, back to front, without the
|
||||
// ones facing away
|
||||
export function gui_player_faces(skin: Texture, slim: boolean, dx: number, dy: number, worn: ItemStack[] = []): Face[] {
|
||||
const pose = gui_player_pose(slim, dx, dy);
|
||||
const faces: Face[] = [];
|
||||
// every 6 vertices are a face, in the texture they're collected for
|
||||
let vertices: number[][] = [];
|
||||
const collect = (texture: Texture): VertexSink => (x, y, z, u, v, r = 1, g = 1, b = 1) => {
|
||||
vertices.push([x, y, z, u, v, r, g, b]);
|
||||
if (vertices.length === 6) {
|
||||
faces.push({ vertices, texture });
|
||||
vertices = [];
|
||||
}
|
||||
};
|
||||
|
||||
const to_world = pose_to_world(pose);
|
||||
const emit_skin = collect(skin);
|
||||
for (const part of slim ? SLIM_PARTS : PARTS) {
|
||||
const transform = (x: number, y: number, z: number) => to_world(...part_point(pose, part, x, y, z));
|
||||
push_cube(skin, part, part.texture, 0, transform, undefined, emit_skin);
|
||||
push_cube(skin, part, part.overlay, part.inflate, transform, undefined, emit_skin);
|
||||
}
|
||||
if (worn.length > 0) {
|
||||
push_worn(worn, pose, undefined, collect(AssetManager.instance.get<Texture>("bworld:textures")));
|
||||
}
|
||||
|
||||
return faces
|
||||
.filter(({ vertices: [a, b, c] }) => {
|
||||
// toward +z, the viewer
|
||||
const normal_z = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
||||
return normal_z > 1e-9;
|
||||
})
|
||||
.map((face) => ({ face, depth: face.vertices.reduce((sum, v) => sum + v[2], 0) }))
|
||||
.sort((a, b) => a.depth - b.depth)
|
||||
.map(({ face }) => face);
|
||||
}
|
||||
|
||||
// the player standing with their feet's middle at x, feet_y on the screen, pixel screen pixels to a skin pixel,
|
||||
// looking at the mouse at mouse_x, mouse_y, wearing worn. gui_scale is the screen pixels to a gui pixel
|
||||
export function draw_gui_player(
|
||||
skin: Texture,
|
||||
slim: boolean,
|
||||
x: number,
|
||||
feet_y: number,
|
||||
pixel: number,
|
||||
gui_scale: number,
|
||||
mouse_x: number,
|
||||
mouse_y: number,
|
||||
worn: ItemStack[] = [],
|
||||
) {
|
||||
const eyes_y = feet_y - EYES * pixel;
|
||||
const faces = gui_player_faces(skin, slim, (mouse_x - x) / gui_scale, (mouse_y - eyes_y) / gui_scale, worn);
|
||||
// blocks to skin pixels to the screen, y down
|
||||
const scale = 16 * pixel;
|
||||
for (const { vertices, texture } of faces) {
|
||||
if (get_current_texture() !== texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(texture.tex);
|
||||
}
|
||||
for (const [vx, vy, , u, v, r, g, b] of vertices) {
|
||||
push_vertex(x + vx * scale, feet_y - vy * scale, 0, u, v, r, g, b, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@ import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { apply, bone_matrices, type Matrix, type Vec3 } from "./model_math.ts";
|
||||
import { push_box } from "./player_renderer.ts";
|
||||
import { push_box, type VertexSink } from "./player_renderer.ts";
|
||||
|
||||
// pose is each bone's turn before its parent's, like a head looking or a limb swinging. to_world takes the model's
|
||||
// pixels, after the bones are turned, to the world. the texture is region of the atlas, stretched to the model's
|
||||
// texture size. only picks the bones drawn, all of them when not given
|
||||
// texture size. only picks the bones drawn, all of them when not given. emit gets the vertices, the batch by default
|
||||
export function push_model(
|
||||
model: EntityModelJson,
|
||||
pose: (bone: EntityBoneJson) => Matrix,
|
||||
@@ -16,6 +16,7 @@ export function push_model(
|
||||
to_world: (x: number, y: number, z: number) => Vec3,
|
||||
tint?: Vec3,
|
||||
only?: (bone: EntityBoneJson) => boolean,
|
||||
emit?: VertexSink,
|
||||
) {
|
||||
const to_uv = (u: number, v: number): [number, number] => [
|
||||
(region.x + u * region.width / model.texture_width) / atlas.width,
|
||||
@@ -33,7 +34,7 @@ export function push_model(
|
||||
// bedrock models are mirrored on x from the engine's, like blockbench shows them: +x is the model's right
|
||||
const low: Vec3 = [-(ox + w) - inflate, oy - inflate, oz - inflate];
|
||||
const high: Vec3 = [-ox + inflate, oy + h + inflate, oz + d + inflate];
|
||||
push_box(cube.size, low, high, cube.uv, to_uv, transform, tint);
|
||||
push_box(cube.size, low, high, cube.uv, to_uv, transform, tint, emit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,9 @@ export function render_player(skin: Texture, pose: PlayerPose, tint?: Vec3) {
|
||||
}
|
||||
}
|
||||
|
||||
// what a box's vertices go to: the batch, or something collecting them, like the player in the inventory
|
||||
export type VertexSink = typeof push_vertex;
|
||||
|
||||
// one layer of a part's box, its texture at uv and grown by inflate. transform takes points around the part's pivot
|
||||
export function push_cube(
|
||||
skin: Texture,
|
||||
@@ -90,10 +93,11 @@ export function push_cube(
|
||||
inflate: number,
|
||||
transform: (x: number, y: number, z: number) => Vec3,
|
||||
tint?: Vec3,
|
||||
emit: VertexSink = push_vertex,
|
||||
) {
|
||||
const low = part.from.map((n) => n - inflate) as Vec3;
|
||||
const high = part.from.map((n, i) => n + part.size[i] + inflate) as Vec3;
|
||||
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint);
|
||||
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint, emit);
|
||||
}
|
||||
|
||||
// a box of a model, from low to high, textured like minecraft's box uv from [u, v] for a box of size (in texture
|
||||
@@ -106,6 +110,7 @@ export function push_box(
|
||||
to_uv: (u: number, v: number) => [number, number],
|
||||
transform: (x: number, y: number, z: number) => Vec3,
|
||||
tint: Vec3 = [1, 1, 1],
|
||||
emit: VertexSink = push_vertex,
|
||||
) {
|
||||
const [w, h, d] = size;
|
||||
for (const face of FACES) {
|
||||
@@ -121,7 +126,7 @@ export function push_box(
|
||||
cy ? high[1] : low[1],
|
||||
cz ? high[2] : low[2],
|
||||
);
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, r, g, b, 1);
|
||||
emit(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, r, g, b, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,26 +3,42 @@
|
||||
// TransformCopyingHumanoidModel. see docs/modding/items/index.md
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { Player } from "$/client/entity/player.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { IDENTITY, type Vec3 } from "./model_math.ts";
|
||||
import { push_model } from "./model_renderer.ts";
|
||||
import type { VertexSink } from "./player_renderer.ts";
|
||||
import { on_part, type PlayerPose, pose_to_world, right_arm, worn_bone_pose, worn_look } from "./player_model.ts";
|
||||
|
||||
// the atlas has to be the current texture. tint is the player's, for light and flashing red
|
||||
export function render_worn(player: Player, pose: PlayerPose, tint?: Vec3) {
|
||||
if (player.worn.length === 0) return;
|
||||
push_worn(player.worn, pose, tint);
|
||||
}
|
||||
|
||||
// what's worn on a player in that pose, its vertices to emit, the batch by default, like the player in the inventory
|
||||
export function push_worn(worn_items: ItemStack[], pose: PlayerPose, tint?: Vec3, emit?: VertexSink) {
|
||||
if (worn_items.length === 0) return;
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const to_world = pose_to_world(pose);
|
||||
for (const stack of player.worn) {
|
||||
for (const stack of worn_items) {
|
||||
const worn = EverythingRegistry.get<ItemRegistry>("items", stack.type_id)?.worn;
|
||||
if (!worn) continue;
|
||||
const look = worn_look(worn, pose.slim);
|
||||
const model = EverythingRegistry.get<EntityModelJson>("entity_models", look.model);
|
||||
if (!model) continue;
|
||||
push_model(model, worn_bone_pose(pose), atlas, get_sprite_region(look.texture), to_world, tint);
|
||||
push_model(
|
||||
model,
|
||||
worn_bone_pose(pose),
|
||||
atlas,
|
||||
get_sprite_region(look.texture),
|
||||
to_world,
|
||||
tint,
|
||||
undefined,
|
||||
emit,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// the player in the inventory turns to look at the mouse
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { gui_player_faces, gui_player_pose } from "$/client/rendering/gui_player.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
import { part_point, PARTS, pose_to_world } from "$/client/rendering/player_model.ts";
|
||||
|
||||
// where a point of the head or body is, in blocks, the viewer at +z with +x on their right and +y up
|
||||
function where(part_name: "head" | "body", dx: number, dy: number, local: [number, number, number]) {
|
||||
const pose = gui_player_pose(false, dx, dy);
|
||||
const part = PARTS.find((p) => p.name === part_name)!;
|
||||
return pose_to_world(pose)(...part_point(pose, part, ...local));
|
||||
}
|
||||
// the middle of the front of the head (its face) and of the body, and their middles. the front is the low z side
|
||||
const head_front = (dx: number, dy: number) => where("head", dx, dy, [0, 4, -4]);
|
||||
const head_middle = (dx: number, dy: number) => where("head", dx, dy, [0, 4, 0]);
|
||||
const body_front = (dx: number, dy: number) => where("body", dx, dy, [0, -6, -2]);
|
||||
const body_middle = (dx: number, dy: number) => where("body", dx, dy, [0, -6, 0]);
|
||||
|
||||
Deno.test("with the mouse on the eyes, the player faces the viewer", () => {
|
||||
const front = head_front(0, 0);
|
||||
const middle = head_middle(0, 0);
|
||||
assert(front[2] > middle[2] + 0.2, `${front} isn't toward the viewer from ${middle}`);
|
||||
assert(Math.abs(front[0] - middle[0]) < 1e-9 && Math.abs(front[1] - middle[1]) < 1e-9);
|
||||
});
|
||||
|
||||
Deno.test("the head and body turn toward the mouse, the head more", () => {
|
||||
for (const dx of [40, -40]) {
|
||||
const head = head_front(dx, 0)[0] - head_middle(dx, 0)[0];
|
||||
const body = body_front(dx, 0)[0] - body_middle(dx, 0)[0];
|
||||
assert(Math.sign(head) === Math.sign(dx), `head turns ${head} for a mouse at ${dx}`);
|
||||
assert(Math.sign(body) === Math.sign(dx), `body turns ${body} for a mouse at ${dx}`);
|
||||
// both are 4 pixels from the middle, the body's 2: compare their angles
|
||||
assert(Math.abs(head) / 0.25 > Math.abs(body) / 0.125, `the head turns less than the body for ${dx}`);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("the head tilts toward the mouse, down when it's below", () => {
|
||||
assert(head_front(0, 40)[1] < head_middle(0, 40)[1], "doesn't look down");
|
||||
assert(head_front(0, -40)[1] > head_middle(0, -40)[1], "doesn't look up");
|
||||
});
|
||||
|
||||
Deno.test("what the player wears is drawn on them too, with the atlas, in among their faces back to front", async () => {
|
||||
await test_game("mods");
|
||||
const skin = { width: 64, height: 64, tex: "skin" } as unknown as Texture;
|
||||
const atlas = { width: 256, height: 256, tex: "atlas" } as unknown as Texture;
|
||||
AssetManager.instance.assets["bworld:textures"] = atlas as never;
|
||||
// where each texture is in the atlas, none known: the renderer falls back to a spot of its own
|
||||
AssetManager.instance.assets["bworld:textures_info"] = {} as never;
|
||||
const plain = gui_player_faces(skin, false, 0, 0);
|
||||
const gloved = gui_player_faces(skin, false, 0, 0, [new ItemStack("artifacts:power_glove", 1)]);
|
||||
assert(plain.every((face) => face.texture === skin));
|
||||
const glove = gloved.filter((face) => face.texture === atlas);
|
||||
assert(glove.length > 0, "no glove");
|
||||
assertEquals(gloved.length - glove.length, plain.length);
|
||||
// sorted together with the body's faces, back to front
|
||||
const depth = (face: { vertices: number[][] }) => face.vertices.reduce((sum, v) => sum + v[2], 0);
|
||||
const depths = gloved.map(depth);
|
||||
assertEquals(depths, [...depths].sort((a, b) => a - b));
|
||||
// and in among them, not all over or under the body
|
||||
assert(gloved.indexOf(glove[0]) > 0 && gloved.indexOf(glove.at(-1)!) < gloved.length - 1);
|
||||
});
|
||||
Reference in New Issue
Block a user