diff --git a/client/renderer/shapes.ts b/client/renderer/shapes.ts index 540edc5..e89ca94 100644 --- a/client/renderer/shapes.ts +++ b/client/renderer/shapes.ts @@ -1,4 +1,4 @@ -import { flush_batch, get_current_texture, push_quad, set_current_texture, white_tex } from "./core.ts"; +import { flush_batch, get_current_texture, push_quad, push_vertex, set_current_texture, white_tex } from "./core.ts"; export function draw_rect( x: number, @@ -32,3 +32,63 @@ export function draw_rect_stroke( // right draw_rect(x + w - thickness, y, thickness, h, color); } + +// a plane in the world to lay 2d drawing on: origin is its top left, x goes along right and y along -up, like the +// screen. for text and rects facing a direction, like nameplates facing the camera +export type Plane3d = { + origin: [number, number, number]; + right: [number, number, number]; + up: [number, number, number]; +}; + +// a quad at x, y on the plane, in its units. wound counter clockwise seen from where right and up point the usual way, +// so it's culled from behind +export function push_plane_quad( + plane: Plane3d, + x: number, + y: number, + w: number, + h: number, + u0: number, + v0: number, + u1: number, + v1: number, + color: readonly number[], +) { + const { origin: o, right: rt, up } = plane; + const corner = (cx: number, cy: number, u: number, v: number) => + push_vertex( + o[0] + rt[0] * cx - up[0] * cy, + o[1] + rt[1] * cx - up[1] * cy, + o[2] + rt[2] * cx - up[2] * cy, + u, + v, + color[0], + color[1], + color[2], + color[3], + ); + const x2 = x + w; + const y2 = y + h; + corner(x, y, u0, v0); + corner(x, y2, u0, v1); + corner(x2, y2, u1, v1); + corner(x, y, u0, v0); + corner(x2, y2, u1, v1); + corner(x2, y, u1, v0); +} + +export function draw_rect_3d( + plane: Plane3d, + x: number, + y: number, + w: number, + h: number, + color: readonly number[] = [1, 1, 1, 1], +) { + if (get_current_texture() !== white_tex) { + flush_batch(); + set_current_texture(white_tex!); + } + push_plane_quad(plane, x, y, w, h, 0, 0, 1, 1, color); +} diff --git a/client/renderer/text.ts b/client/renderer/text.ts index f902aa3..9e42852 100644 --- a/client/renderer/text.ts +++ b/client/renderer/text.ts @@ -1,5 +1,6 @@ import { AssetManager } from "../assets.ts"; import { flush_batch, get_current_texture, push_quad, set_current_texture } from "./core.ts"; +import { type Plane3d, push_plane_quad } from "./shapes.ts"; import { FntChar, FntFont, Texture } from "./types.ts"; let font_texture: Texture; @@ -102,6 +103,52 @@ export function draw_text(str: string, x: number, y: number, scale: number = 1, } } +// text on a plane in the world (see Plane3d), x and y in the plane's units like draw_text's are in pixels. scale is +// how big a font pixel is in them +export function draw_text_3d( + plane: Plane3d, + str: string, + x: number, + y: number, + scale: number, + color: readonly number[] = [1, 1, 1, 1], +) { + if (!font_texture) { + return; + } + + if (get_current_texture() !== font_texture.tex) { + flush_batch(); + set_current_texture(font_texture.tex); + } + + for (const ch of str) { + const glyph = font_fnt.chars[ch.charCodeAt(0)]; + if (!glyph) { + continue; + } + + push_plane_quad( + plane, + x + glyph.xoffset * scale, + y + glyph.yoffset * scale, + glyph.width * scale, + glyph.height * scale, + glyph.x / font_texture.width, + glyph.y / font_texture.height, + (glyph.x + glyph.width) / font_texture.width, + (glyph.y + glyph.height) / font_texture.height, + color, + ); + + x += glyph.xadvance * scale; + } +} + +export function font_line_height() { + return font_fnt?.line_height ?? 0; +} + export function measure_text(str: string, scale: number = 1) { let x = 0; for (const ch of str) { diff --git a/client/rendering/game_renderer.ts b/client/rendering/game_renderer.ts index 641cba8..ba5df6d 100644 --- a/client/rendering/game_renderer.ts +++ b/client/rendering/game_renderer.ts @@ -5,6 +5,7 @@ import { Hud } from "$/client/gui/hud.ts"; import { DebugOverlay } from "$/client/gui/debug_overlay.ts"; import { LevelRenderer } from "./level_renderer.ts"; import { HandRenderer } from "./hand_renderer.ts"; +import { render_nameplates } from "./nameplate_renderer.ts"; import { profiler } from "$/client/profiler.ts"; // the sky at noon and in the middle of the night @@ -49,6 +50,7 @@ export class GameRenderer { level.render_entities(client.level, camera, client.player, partial_tick); }); profiler.section("translucent", () => level.render_translucent(client.level, camera)); + profiler.section("nameplates", () => render_nameplates(client.level, camera, client.player, partial_tick)); end_mode_3d(); profiler.section("hand", () => this.hand.render(client.player, partial_tick)); diff --git a/client/rendering/nameplate_renderer.ts b/client/rendering/nameplate_renderer.ts new file mode 100644 index 0000000..1294c78 --- /dev/null +++ b/client/rendering/nameplate_renderer.ts @@ -0,0 +1,59 @@ +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(); +}