111 lines
4.6 KiB
TypeScript
111 lines
4.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// 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 { push_vertex, type Texture, use_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) {
|
|
use_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);
|
|
}
|
|
}
|
|
}
|