Player rendering in the inventory
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user