60 lines
2.9 KiB
TypeScript
60 lines
2.9 KiB
TypeScript
import type { Camera } from "$/client/camera.ts";
|
|
import type { Entity } from "$/client/entity/entity.ts";
|
|
import type { ClientLevel } from "$/client/level/client_level.ts";
|
|
import { RemotePlayer } from "$/client/entity/remote_player.ts";
|
|
import { draw_rect_3d, draw_text_3d, flush_batch, measure_text, type Plane3d } from "$/client/renderer/mod.ts";
|
|
|
|
// how big a font pixel is, in blocks. m6x11's letters are 11 pixels tall, so about a quarter of a block
|
|
const SCALE = 0.022;
|
|
// from the top of the head to the bottom of the plate
|
|
const GAP = 0.3;
|
|
// further than this they're not drawn, like minecraft's 64 blocks
|
|
const MAX_DISTANCE = 64;
|
|
// the plate's height around the text in font pixels: from a pixel above the letters to under their descenders
|
|
const PLATE_TOP = -1;
|
|
const PLATE_BOTTOM = 14;
|
|
const PLATE_PADDING = 2;
|
|
const PLATE_COLOR = [0, 0, 0, 0.25];
|
|
const TEXT_COLOR = [1, 1, 1, 1];
|
|
// the text sits this far in front of its plate, towards the camera, so they don't z-fight
|
|
const TEXT_LIFT = 0.002;
|
|
|
|
// names over other players' heads, always facing the camera like minecraft's. drawn after translucent terrain since
|
|
// the plate is see through. they're full bright, so they can be read in the dark
|
|
export function render_nameplates(level: ClientLevel, camera: Camera, camera_entity: Entity, partial_tick: number) {
|
|
const cos_yaw = Math.cos(camera.yaw);
|
|
const sin_yaw = Math.sin(camera.yaw);
|
|
const cos_pitch = Math.cos(camera.pitch);
|
|
const sin_pitch = Math.sin(camera.pitch);
|
|
// the camera's right, up and back in the world, see update_camera in renderer/core.ts
|
|
const right: [number, number, number] = [cos_yaw, 0, -sin_yaw];
|
|
const up: [number, number, number] = [sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch];
|
|
const back: [number, number, number] = [sin_yaw * cos_pitch, -sin_pitch, cos_yaw * cos_pitch];
|
|
|
|
for (const entity of level.entities.values()) {
|
|
if (entity === camera_entity || !(entity instanceof RemotePlayer) || !entity.name) continue;
|
|
const pos = entity.render_position(partial_tick);
|
|
const dx = pos.x - camera.x;
|
|
const dy = pos.y - camera.y;
|
|
const dz = pos.z - camera.z;
|
|
if (dx * dx + dy * dy + dz * dz > MAX_DISTANCE * MAX_DISTANCE) continue;
|
|
|
|
// the plane's origin is the middle of the plate's top
|
|
const plate_height = (PLATE_BOTTOM - PLATE_TOP) * SCALE;
|
|
const top = pos.y + entity.height + GAP + plate_height;
|
|
const origin: [number, number, number] = [pos.x, top, pos.z];
|
|
const plate: Plane3d = { origin, right, up };
|
|
const text: Plane3d = {
|
|
origin: [origin[0] + back[0] * TEXT_LIFT, origin[1] + back[1] * TEXT_LIFT, origin[2] + back[2] * TEXT_LIFT],
|
|
right,
|
|
up,
|
|
};
|
|
|
|
const width = measure_text(entity.name, SCALE);
|
|
const padding = PLATE_PADDING * SCALE;
|
|
draw_rect_3d(plate, -width / 2 - padding, 0, width + padding * 2, plate_height, PLATE_COLOR);
|
|
draw_text_3d(text, entity.name, -width / 2, -PLATE_TOP * SCALE, SCALE, TEXT_COLOR);
|
|
}
|
|
flush_batch();
|
|
}
|