Split The code is public; the app answers only to the crew.
Who is that: a name over the player you can see, health when they are yours, and a `project.ts` that can place it #119
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/91-who-is-that-a-name-over-the-player-you-c → main 1 commit fork
6 files
changed +385
−10
README.md +15 −1
| @@ -301,6 +301,17 @@ it is planted, and both sites marked A and B — never the other team, because | ||
| 301 | 301 | a radar that shows the enemy is a wallhack. Its map is baked once and turned |
| 302 | 302 | with the canvas as you turn; nobody re-walks the grid to draw it. |
| 303 | 303 | |
| 304 | Over the head of every player you can actually see is their name — your | |
| 305 | teammates in your side's colour with their health under it, an enemy's name and | |
| 306 | nothing more. "Can actually see" is the same geometry the bots shoot by: in | |
| 307 | front of the eye, inside the field of view, traced clear of the level eye to | |
| 308 | eye, and alive. A name through a wall would be a wallhack, so there is one | |
| 309 | trace per player per frame and no plate without it. The plates fade with | |
| 310 | distance and are clamped in size, so the far side of the map is a hint rather | |
| 311 | than a billboard; while you are dead they are the watched teammate's view, and | |
| 312 | a held <kbd>Tab</kbd> takes them off the screen. The nine traces and the | |
| 313 | projections they feed cost 0.04 ms of a 16.7 ms frame, measured. | |
| 314 | ||
| 304 | 315 | ## Shape |
| 305 | 316 | |
| 306 | 317 | - `src/client/` — the game, in modules named for one thing each: `math`, `map` |
| @@ -318,7 +329,10 @@ with the canvas as you turn; nobody re-walks the grid to draw it. | ||
| 318 | 329 | `quality` (the render scale the frame time buys), |
| 319 | 330 | `spectate` (whose eyes you borrow when you are dead), `bodies` (the dead of |
| 320 | 331 | this round, where they fell), `radar` (the blips the |
| 321 | HUD's minimap draws, and nothing it draws them with), `summary` (the end of | |
| 332 | HUD's minimap draws, and nothing it draws them with), | |
| 333 | `project` (a world point as a screen point, from the same camera the | |
| 334 | renderer draws with, or null when there is no pixel for it), | |
| 335 | `summary` (the end of | |
| 322 | 336 | the match as numbers), `ears` (a sound's pan and volume, as plain data), |
| 323 | 337 | `hearing` (what a sound is worth to someone standing somewhere else: how loud |
| 324 | 338 | it arrives through however many walls, and where they think it came from), |
src/client/hud.test.ts +143 −4
| @@ -8,17 +8,21 @@ | ||
| 8 | 8 | import { expect, test } from "bun:test"; |
| 9 | 9 | import { drawHud } from "./hud.ts"; |
| 10 | 10 | import { SHORT } from "./match.ts"; |
| 11 | import { createWorld, type Player, type World } from "./world.ts"; | |
| 11 | import type { Camera } from "./renderer.ts"; | |
| 12 | import { createWorld, eyePos, type Player, type World } from "./world.ts"; | |
| 12 | 13 | |
| 13 | type Call = { name: string; args: number[]; text: string }; | |
| 14 | type Call = { name: string; args: number[]; text: string; alpha: number }; | |
| 14 | 15 | |
| 15 | 16 | function recorder(): { ctx: CanvasRenderingContext2D; calls: Call[] } { |
| 16 | 17 | const calls: Call[] = []; |
| 18 | // The alpha in force when the call was made, snapshotted: a fade is a | |
| 19 | // property and not an argument, so it is the only way to see one. | |
| 17 | 20 | const log = (name: string) => (...args: unknown[]): void => { |
| 18 | 21 | calls.push({ |
| 19 | 22 | name, |
| 20 | 23 | args: args.filter((a) => typeof a === "number") as number[], |
| 21 | 24 | text: args.filter((a) => typeof a === "string").join(" "), |
| 25 | alpha: ctx.globalAlpha, | |
| 22 | 26 | }); |
| 23 | 27 | }; |
| 24 | 28 | const ctx = { |
| @@ -30,7 +34,7 @@ function recorder(): { ctx: CanvasRenderingContext2D; calls: Call[] } { | ||
| 30 | 34 | drawImage: log("drawImage"), closePath: log("closePath"), |
| 31 | 35 | measureText: () => ({ width: 10 }), |
| 32 | 36 | createRadialGradient: (...args: unknown[]) => { |
| 33 | calls.push({ name: "createRadialGradient", args: args as number[], text: "" }); | |
| 37 | calls.push({ name: "createRadialGradient", args: args as number[], text: "", alpha: 1 }); | |
| 34 | 38 | return { addColorStop: (): void => {} }; |
| 35 | 39 | }, |
| 36 | 40 | globalAlpha: 1, lineWidth: 1, fillStyle: "", strokeStyle: "", |
| @@ -41,9 +45,13 @@ function recorder(): { ctx: CanvasRenderingContext2D; calls: Call[] } { | ||
| 41 | 45 | |
| 42 | 46 | const W = 1280, H = 720; |
| 43 | 47 | |
| 48 | /** The camera `main.ts` builds for the player being drawn, punch and all aside. */ | |
| 49 | const cameraFor = (view: Player): Camera => | |
| 50 | ({ pos: eyePos(view), yaw: view.yaw, pitch: view.pitch, fov: view.scoped ? 40 : 90 }); | |
| 51 | ||
| 44 | 52 | /** One frame of the HUD, drawing `view` — the person unless a spectator is watching. */ |
| 45 | 53 | const draw = (into: { ctx: CanvasRenderingContext2D }, world: World, view: Player = world.me, |
| 46 | scoreboard = false): void => drawHud(into.ctx, world, view, W, H, scoreboard); | |
| 54 | scoreboard = false): void => drawHud(into.ctx, world, view, cameraFor(view), W, H, scoreboard); | |
| 47 | 55 | |
| 48 | 56 | /** Where the middle of a stroked wedge lands on the screen. */ |
| 49 | 57 | const wedgePoint = (call: Call): { x: number; y: number } => { |
| @@ -285,3 +293,134 @@ test("the HUD says what is in the pocket, and counts it where the ammo goes", () | ||
| 285 | 293 | draw(without, world); |
| 286 | 294 | expect(without.calls.some((c) => c.text.includes("HE"))).toBe(false); |
| 287 | 295 | }); |
| 296 | ||
| 297 | /** | |
| 298 | * A world with nothing solid in it, the person at the origin looking east, and | |
| 299 | * one live teammate and one live enemy to put wherever a test needs them. The | |
| 300 | * boxes are replaced rather than emptied in place: `world.boxes` starts out as | |
| 301 | * the shared map's own list, which no test may edit. | |
| 302 | */ | |
| 303 | function plateWorld(): { world: World; me: Player; mate: Player; enemy: Player } { | |
| 304 | const world = createWorld(SHORT, "me", "ct", 5, 61); | |
| 305 | const me = world.me; | |
| 306 | me.pos = [0, 0, 0]; | |
| 307 | me.yaw = 0; | |
| 308 | me.pitch = 0; | |
| 309 | const mates = world.players.filter((p) => p.team === "ct" && p !== me); | |
| 310 | const enemies = world.players.filter((p) => p.team === "t"); | |
| 311 | for (const p of [...mates.slice(1), ...enemies.slice(1)]) p.alive = false; | |
| 312 | const mate = mates[0] as Player, enemy = enemies[0] as Player; | |
| 313 | mate.pos = [600, 60, 0]; | |
| 314 | enemy.pos = [600, -60, 0]; | |
| 315 | world.boxes = []; | |
| 316 | return { world, me, mate, enemy }; | |
| 317 | } | |
| 318 | ||
| 319 | /** A wall across the view at `x`, tall enough to hide anyone standing behind it. */ | |
| 320 | const wallAt = (x: number): { min: [number, number, number]; max: [number, number, number]; mat: number } => | |
| 321 | ({ min: [x, -400, 0], max: [x + 16, 400, 200], mat: 0 }); | |
| 322 | ||
| 323 | const written = (calls: Call[]): string[] => calls.filter((c) => c.name === "fillText").map((c) => c.text); | |
| 324 | ||
| 325 | test("a name over the player in front of you, with health when they are yours", () => { | |
| 326 | const { world, mate, enemy } = plateWorld(); | |
| 327 | mate.health = 64; | |
| 328 | enemy.health = 37; | |
| 329 | const frame = recorder(); | |
| 330 | draw(frame, world); | |
| 331 | const texts = written(frame.calls); | |
| 332 | ||
| 333 | expect(texts).toContain(mate.name); | |
| 334 | expect(texts).toContain(enemy.name); | |
| 335 | expect(texts).toContain("64"); // the teammate's health, under their name | |
| 336 | expect(texts).not.toContain("37"); // an enemy's health is nobody's business | |
| 337 | }); | |
| 338 | ||
| 339 | test("no wallhack: a player behind a box has no plate, and gets one again in the open", () => { | |
| 340 | const { world, mate, enemy } = plateWorld(); | |
| 341 | world.boxes = [wallAt(300)]; | |
| 342 | const hidden = recorder(); | |
| 343 | draw(hidden, world); | |
| 344 | expect(written(hidden.calls)).not.toContain(mate.name); | |
| 345 | expect(written(hidden.calls)).not.toContain(enemy.name); | |
| 346 | ||
| 347 | // Same frame, same players, with the wall gone. | |
| 348 | world.boxes = []; | |
| 349 | const open = recorder(); | |
| 350 | draw(open, world); | |
| 351 | expect(written(open.calls)).toContain(mate.name); | |
| 352 | expect(written(open.calls)).toContain(enemy.name); | |
| 353 | }); | |
| 354 | ||
| 355 | test("a plate needs a live player in front of the eye, and no scoreboard over it", () => { | |
| 356 | const { world, mate, enemy } = plateWorld(); | |
| 357 | enemy.alive = false; // a body is not a player | |
| 358 | mate.pos = [-600, 0, 0]; // behind the camera, which looks east | |
| 359 | const away = recorder(); | |
| 360 | draw(away, world); | |
| 361 | expect(written(away.calls)).not.toContain(mate.name); | |
| 362 | expect(written(away.calls)).not.toContain(enemy.name); | |
| 363 | ||
| 364 | // In front but off to the side past the 90° frustum: still nothing. | |
| 365 | mate.pos = [600, 1400, 0]; | |
| 366 | const wide = recorder(); | |
| 367 | draw(wide, world); | |
| 368 | expect(written(wide.calls)).not.toContain(mate.name); | |
| 369 | ||
| 370 | // In front, alive, and visible — but a held Tab is showing the scoreboard, | |
| 371 | // which names everyone anyway, so the plates stay out of it. | |
| 372 | mate.pos = [600, 0, 0]; | |
| 373 | const held = recorder(); | |
| 374 | draw(held, world, world.me, true); | |
| 375 | expect(written(held.calls).filter((t) => t === mate.name).length).toBe(0); | |
| 376 | const plain = recorder(); | |
| 377 | draw(plain, world); | |
| 378 | expect(written(plain.calls)).toContain(mate.name); | |
| 379 | }); | |
| 380 | ||
| 381 | test("the plates are the watched player's while spectating, not the corpse's", () => { | |
| 382 | const { world, me, mate, enemy } = plateWorld(); | |
| 383 | me.alive = false; | |
| 384 | me.health = 0; | |
| 385 | mate.pos = [600, 0, 0]; | |
| 386 | mate.yaw = 0; | |
| 387 | enemy.pos = [1200, 0, 0]; // clear of the teammate, blocked from the corpse | |
| 388 | world.boxes = [wallAt(300)]; | |
| 389 | ||
| 390 | const watching = recorder(); | |
| 391 | draw(watching, world, mate); | |
| 392 | expect(written(watching.calls)).toContain(enemy.name); | |
| 393 | expect(written(watching.calls)).not.toContain(mate.name); // never the player you are | |
| 394 | ||
| 395 | // From the corpse's own eye the wall is in the way, which is the difference. | |
| 396 | const corpse = recorder(); | |
| 397 | draw(corpse, world, me); | |
| 398 | expect(written(corpse.calls)).not.toContain(enemy.name); | |
| 399 | }); | |
| 400 | ||
| 401 | test("a plate across the map is a hint: fainter, and clamped small", () => { | |
| 402 | const { world, mate, enemy } = plateWorld(); | |
| 403 | mate.alive = false; // one plate in the frame, and no health bar under it | |
| 404 | enemy.pos = [500, 0, 0]; | |
| 405 | const near = recorder(); | |
| 406 | draw(near, world); | |
| 407 | enemy.pos = [4000, 0, 0]; | |
| 408 | const far = recorder(); | |
| 409 | draw(far, world); | |
| 410 | ||
| 411 | const plate = (calls: Call[]): Call => calls.find((c) => c.name === "fillText" && c.text === enemy.name) as Call; | |
| 412 | // The backing behind the name is the only small fillRect in the frame; its | |
| 413 | // height is the font size the plate was drawn at. | |
| 414 | const backing = (calls: Call[]): Call => calls.find((c) => c.name === "fillRect" && c.args[2] < 30) as Call; | |
| 415 | ||
| 416 | expect(plate(near.calls)).toBeDefined(); | |
| 417 | expect(plate(far.calls)).toBeDefined(); | |
| 418 | expect(plate(far.calls).alpha).toBeLessThan(plate(near.calls).alpha * 0.5); | |
| 419 | expect(plate(far.calls).alpha).toBeGreaterThan(0.1); // faint, never invisible | |
| 420 | expect(backing(far.calls).args[3]).toBeLessThan(backing(near.calls).args[3]); | |
| 421 | // Clamped at both ends: a player in your face is not twice the size either. | |
| 422 | enemy.pos = [120, 0, 0]; | |
| 423 | const close = recorder(); | |
| 424 | draw(close, world); | |
| 425 | expect(backing(close.calls).args[3]).toBeCloseTo(backing(near.calls).args[3], 6); | |
| 426 | }); |
src/client/hud.ts +84 −4
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * The HUD, drawn on a 2D canvas over the scene: crosshair, health, ammo, the |
| 3 | * round clock, the kill feed and the scoreboard. It reads the world and draws; | |
| 4 | * it never changes anything. | |
| 3 | * round clock, the kill feed, the names over the players you can see, and the | |
| 4 | * scoreboard. It reads the world and draws; it never changes anything. | |
| 5 | 5 | * |
| 6 | 6 | * It is handed the player it is drawing rather than reaching for `world.me`: |
| 7 | 7 | * while the person is dead the camera is on a teammate, and the numbers under |
| @@ -10,12 +10,14 @@ | ||
| 10 | 10 | |
| 11 | 11 | import { angleDelta, clamp, rad } from "./math.ts"; |
| 12 | 12 | import { walkable, type MapDef } from "./map.ts"; |
| 13 | import { MOVE } from "./physics.ts"; | |
| 13 | import { hullHeight, MOVE } from "./physics.ts"; | |
| 14 | import { project, type Projected } from "./project.ts"; | |
| 14 | 15 | import { buildRadar } from "./radar.ts"; |
| 16 | import type { Camera } from "./renderer.ts"; | |
| 15 | 17 | import { drawReport } from "./reportPanel.ts"; |
| 16 | 18 | import { deathNotice } from "./spectate.ts"; |
| 17 | 19 | import { inaccuracy, WEAPONS } from "./weapons.ts"; |
| 18 | import { currentWeapon, eyePos, offeredPickup, useProgress, type Player, type World } from "./world.ts"; | |
| 20 | import { canSee, currentWeapon, eyePos, offeredPickup, useProgress, type Player, type World } from "./world.ts"; | |
| 19 | 21 | |
| 20 | 22 | const T_COLOR = "#e2b04a"; |
| 21 | 23 | const CT_COLOR = "#6fa8e8"; |
| @@ -211,10 +213,83 @@ function drawRadar( | ||
| 211 | 213 | ctx.stroke(); |
| 212 | 214 | } |
| 213 | 215 | |
| 216 | /** A plate is full strength this close and at its faintest by here. */ | |
| 217 | const PLATE_NEAR = 400; | |
| 218 | const PLATE_FAR = 3000; | |
| 219 | /** How far above the top of the hull the plate floats, in world units. */ | |
| 220 | const PLATE_LIFT = 14; | |
| 221 | ||
| 222 | /** | |
| 223 | * A name over every player the eye can actually see: in front of it, inside | |
| 224 | * the frustum, traced clear of the level's boxes, and alive. That list is the | |
| 225 | * whole point — a name drawn through a wall is a wallhack — so the test that | |
| 226 | * puts a box in the way is the one that matters, and the trace is `canSee`, | |
| 227 | * the same eye-to-eye geometry the bots shoot by. | |
| 228 | * | |
| 229 | * Teammates carry their health, in your side's colour; an enemy is a name and | |
| 230 | * nothing else. Both fade with distance and are clamped in size, so the far | |
| 231 | * side of the map is a hint rather than a billboard. The geometry is | |
| 232 | * `project.ts`'s: this function only decides what a plate looks like. | |
| 233 | */ | |
| 234 | function drawPlates( | |
| 235 | ctx: CanvasRenderingContext2D, | |
| 236 | world: World, | |
| 237 | view: Player, | |
| 238 | camera: Camera, | |
| 239 | width: number, | |
| 240 | height: number, | |
| 241 | scale: number, | |
| 242 | ): void { | |
| 243 | const plates: { player: Player; at: Projected }[] = []; | |
| 244 | for (const p of world.players) { | |
| 245 | if (p === view || !p.alive) continue; | |
| 246 | if (!canSee(world, view, p)) continue; // one trace per player per frame | |
| 247 | const head: [number, number, number] = [p.pos[0], p.pos[1], p.pos[2] + hullHeight(p) + PLATE_LIFT]; | |
| 248 | const at = project(head, camera, width, height); | |
| 249 | if (at !== null) plates.push({ player: p, at }); | |
| 250 | } | |
| 251 | // Furthest first, so the near one is the one you read where two overlap. | |
| 252 | plates.sort((a, b) => b.at.depth - a.at.depth); | |
| 253 | ||
| 254 | ctx.textAlign = "center"; | |
| 255 | const teamColor = view.team === "t" ? T_COLOR : CT_COLOR; | |
| 256 | for (const { player, at } of plates) { | |
| 257 | // 15px at 900 units, shrinking with the perspective divide the way the | |
| 258 | // player under it does — but clamped both ways, so a plate is never a | |
| 259 | // headline in your face nor a line of pixels you cannot read. | |
| 260 | const size = Math.round(clamp((15 * 900 * scale) / at.depth, 9 * scale, 17 * scale)); | |
| 261 | const mate = player.team === view.team; | |
| 262 | ctx.globalAlpha = clamp(1 - (at.depth - PLATE_NEAR) / (PLATE_FAR - PLATE_NEAR), 0.16, 1); | |
| 263 | ctx.font = `${size}px system-ui, sans-serif`; | |
| 264 | const nameWidth = ctx.measureText(player.name).width; | |
| 265 | ctx.fillStyle = "rgba(10, 12, 16, 0.42)"; | |
| 266 | ctx.fillRect(at.x - nameWidth / 2 - size * 0.3, at.y - size, nameWidth + size * 0.6, size * 1.25); | |
| 267 | ctx.fillStyle = mate ? teamColor : "#eef0f5"; | |
| 268 | ctx.fillText(player.name, at.x, at.y); | |
| 269 | if (mate) { | |
| 270 | // Health as a bar under the name: a number to read and a length to | |
| 271 | // glance at, which is what "is this one worth trading for" asks. | |
| 272 | const fraction = clamp(player.health / 100, 0, 1); | |
| 273 | const barW = Math.round(size * 3), barH = Math.max(2, Math.round(size * 0.18)); | |
| 274 | const barY = at.y + size * 0.4; | |
| 275 | ctx.fillStyle = "rgba(10, 12, 16, 0.5)"; | |
| 276 | ctx.fillRect(at.x - barW / 2, barY, barW, barH); | |
| 277 | ctx.fillStyle = fraction > 0.3 ? "#7ddc8a" : "#ff6b5b"; | |
| 278 | ctx.fillRect(at.x - barW / 2, barY, barW * fraction, barH); | |
| 279 | // The number keeps the bar's colour: one glance says how much and | |
| 280 | // whether it is the kind of much you go and help with. | |
| 281 | ctx.font = `${Math.max(8, Math.round(size * 0.78))}px system-ui, sans-serif`; | |
| 282 | ctx.fillText(String(Math.max(0, Math.round(player.health))), at.x, barY + barH + size * 0.78); | |
| 283 | } | |
| 284 | } | |
| 285 | ctx.globalAlpha = 1; | |
| 286 | } | |
| 287 | ||
| 214 | 288 | export function drawHud( |
| 215 | 289 | ctx: CanvasRenderingContext2D, |
| 216 | 290 | world: World, |
| 217 | 291 | view: Player, |
| 292 | camera: Camera, | |
| 218 | 293 | width: number, |
| 219 | 294 | height: number, |
| 220 | 295 | showScoreboard: boolean, |
| @@ -288,6 +363,11 @@ export function drawHud( | ||
| 288 | 363 | ctx.fillRect(x, y, w, h); |
| 289 | 364 | }; |
| 290 | 365 | |
| 366 | // --- who is that: names over the players the eye can see, drawn under the | |
| 367 | // rest of the HUD so a plate never covers the clock or the ammo, and not at | |
| 368 | // all while the scoreboard — which says the same thing better — is up. | |
| 369 | if (!showScoreboard) drawPlates(ctx, world, view, camera, width, height, scale); | |
| 370 | ||
| 291 | 371 | // --- radar: bottom-left, clear of the money line above and the health and |
| 292 | 372 | // armour numbers below, at 360px of a 1280px-wide HUD — about 28% of the |
| 293 | 373 | // width, the same share at any size since it scales with everything else. |
src/client/main.ts +3 −1
| @@ -743,7 +743,9 @@ function boot(): void { | ||
| 743 | 743 | hudCanvas.width = w; |
| 744 | 744 | hudCanvas.height = h; |
| 745 | 745 | } |
| 746 | drawHud(hudCtx, world, view, w, h, controls.scoreboard); | |
| 746 | // The same camera the scene was drawn with: the HUD's name plates have to | |
| 747 | // agree with the pixels the renderer just put on the screen. | |
| 748 | drawHud(hudCtx, world, view, camera, w, h, controls.scoreboard); | |
| 747 | 749 | // The fps is the frame time and nothing else; the scale is an aside, shown |
| 748 | 750 | // only when it is costing you pixels, so a slow machine says why. |
| 749 | 751 | const note = renderScale < MAX_SCALE ? ` · ${Math.round(renderScale * 100)}% res` : ""; |
src/client/project.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | /** | |
| 2 | * The projection, checked the way a player would notice it being wrong: the | |
| 3 | * thing dead ahead in the middle, the thing behind you nowhere, and left, | |
| 4 | * right, above and below on the side of the screen they are on. | |
| 5 | */ | |
| 6 | ||
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { project, FAR, NEAR } from "./project.ts"; | |
| 9 | import type { Camera } from "./renderer.ts"; | |
| 10 | ||
| 11 | const W = 1280, H = 720; | |
| 12 | ||
| 13 | /** An eye at the origin's head height, looking east at the game's own 90°. */ | |
| 14 | const eye = (yaw = 0, pitch = 0): Camera => ({ pos: [0, 0, 64], yaw, pitch, fov: 90 }); | |
| 15 | ||
| 16 | test("a point dead ahead lands in the middle of the screen", () => { | |
| 17 | const at = project([500, 0, 64], eye(), W, H); | |
| 18 | expect(at).not.toBeNull(); | |
| 19 | expect(at!.x).toBeCloseTo(W / 2, 3); | |
| 20 | expect(at!.y).toBeCloseTo(H / 2, 3); | |
| 21 | expect(at!.depth).toBeCloseTo(500, 3); | |
| 22 | }); | |
| 23 | ||
| 24 | test("a point behind the camera is null, however close to the eye it is", () => { | |
| 25 | expect(project([-500, 0, 64], eye(), W, H)).toBeNull(); | |
| 26 | expect(project([-1, 0, 64], eye(), W, H)).toBeNull(); | |
| 27 | // Inside the near plane counts as behind: there is no pixel for it either. | |
| 28 | expect(project([NEAR / 2, 0, 64], eye(), W, H)).toBeNull(); | |
| 29 | // And turning round puts the same point back in the middle. | |
| 30 | expect(project([-500, 0, 64], eye(180), W, H)!.x).toBeCloseTo(W / 2, 3); | |
| 31 | }); | |
| 32 | ||
| 33 | test("left, right, above and below land on the side of the screen they are on", () => { | |
| 34 | const ahead = 500; | |
| 35 | // Yaw grows anticlockwise, so north (+y) is the left of an eye facing east. | |
| 36 | const left = project([ahead, 120, 64], eye(), W, H); | |
| 37 | const right = project([ahead, -120, 64], eye(), W, H); | |
| 38 | const above = project([ahead, 0, 64 + 120], eye(), W, H); | |
| 39 | const below = project([ahead, 0, 64 - 120], eye(), W, H); | |
| 40 | for (const at of [left, right, above, below]) expect(at).not.toBeNull(); | |
| 41 | ||
| 42 | expect(left!.x).toBeLessThan(W / 2 - 10); | |
| 43 | expect(right!.x).toBeGreaterThan(W / 2 + 10); | |
| 44 | expect(Math.abs(left!.y - H / 2)).toBeLessThan(1); | |
| 45 | expect(Math.abs(right!.y - H / 2)).toBeLessThan(1); | |
| 46 | ||
| 47 | expect(above!.y).toBeLessThan(H / 2 - 10); // up the screen is a smaller y | |
| 48 | expect(below!.y).toBeGreaterThan(H / 2 + 10); | |
| 49 | expect(Math.abs(above!.x - W / 2)).toBeLessThan(1); | |
| 50 | expect(Math.abs(below!.x - W / 2)).toBeLessThan(1); | |
| 51 | ||
| 52 | // Left and right are mirror images about the middle, as is up and down. | |
| 53 | expect(W - right!.x).toBeCloseTo(left!.x, 3); | |
| 54 | expect(H - below!.y).toBeCloseTo(above!.y, 3); | |
| 55 | }); | |
| 56 | ||
| 57 | test("a point off the frustum is null even when it is in front", () => { | |
| 58 | // 90° horizontal means the edge is at 45°: a point further out than its own | |
| 59 | // distance ahead is past it. | |
| 60 | expect(project([500, 700, 64], eye(), W, H)).toBeNull(); | |
| 61 | expect(project([500, -700, 64], eye(), W, H)).toBeNull(); | |
| 62 | expect(project([500, 0, 64 + 900], eye(), W, H)).toBeNull(); | |
| 63 | expect(project([FAR + 100, 0, 64], eye(), W, H)).toBeNull(); | |
| 64 | }); | |
| 65 | ||
| 66 | test("pitch is positive downwards, so looking down brings the floor up the screen", () => { | |
| 67 | const flat = project([500, 0, 0], eye(), W, H); | |
| 68 | const down = project([500, 0, 0], eye(0, 30), W, H); | |
| 69 | expect(flat).not.toBeNull(); | |
| 70 | expect(down).not.toBeNull(); | |
| 71 | expect(down!.y).toBeLessThan(flat!.y); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the horizontal field of view is fixed whatever shape the screen is", () => { | |
| 75 | const wide = project([500, 200, 64], eye(), 1280, 400); | |
| 76 | const tall = project([500, 200, 64], eye(), 1280, 1280); | |
| 77 | expect(wide).not.toBeNull(); | |
| 78 | expect(tall).not.toBeNull(); | |
| 79 | // The horizontal field of view is fixed, so x in fractions of the width is | |
| 80 | // the same whatever the height is. | |
| 81 | expect(tall!.x / 1280).toBeCloseTo(wide!.x / 1280, 3); | |
| 82 | expect(project([500, 0, 64], eye(), 800, 800)!.x).toBeCloseTo(400, 3); | |
| 83 | }); |
src/client/project.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * World point to screen point: the one thing the client could not do. The | |
| 3 | * renderer builds this same matrix on the GPU's side of the wire — same | |
| 4 | * horizontal field of view turned vertical by the aspect, same near and far | |
| 5 | * — so a point handed to `project` lands where the pixel the renderer drew | |
| 6 | * for it is. It is pure: no DOM, no GL, nothing kept between calls. | |
| 7 | * | |
| 8 | * Screen axes are the canvas's: x right from the left edge, y down from the | |
| 9 | * top, both in the pixels `width`/`height` are given in. Off the frustum or | |
| 10 | * behind the eye is `null` rather than a clamped edge, because a plate at the | |
| 11 | * edge of the screen for something behind you is a lie about where it is. | |
| 12 | * | |
| 13 | * `Camera` is `renderer.ts`'s, imported as a type only: the eye is one shape | |
| 14 | * in this game and the renderer is where it is built, but nothing of the | |
| 15 | * renderer's is here at runtime. | |
| 16 | */ | |
| 17 | ||
| 18 | import { multiply, perspective, viewMatrix, type Vec3 } from "./math.ts"; | |
| 19 | import type { Camera } from "./renderer.ts"; | |
| 20 | ||
| 21 | /** The near and far planes the renderer draws with; nearer than near is not on the screen. */ | |
| 22 | export const NEAR = 6; | |
| 23 | export const FAR = 12000; | |
| 24 | ||
| 25 | export type Projected = { | |
| 26 | x: number; | |
| 27 | y: number; | |
| 28 | /** How far in front of the eye, along the view axis: the perspective divide. */ | |
| 29 | depth: number; | |
| 30 | }; | |
| 31 | ||
| 32 | /** | |
| 33 | * `point` on a `width`×`height` screen seen by `camera`, or null when it is | |
| 34 | * behind the eye, nearer than the near plane, past the far plane, or outside | |
| 35 | * the frustum sideways or vertically. | |
| 36 | */ | |
| 37 | export function project(point: Vec3, camera: Camera, width: number, height: number): Projected | null { | |
| 38 | const aspect = width / Math.max(1, height); | |
| 39 | // A CS field of view is horizontal; the projection wants the vertical one — | |
| 40 | // the same conversion renderer.draw does, for the same reason. | |
| 41 | const fovY = (2 * Math.atan(Math.tan((camera.fov * Math.PI) / 360) / aspect) * 180) / Math.PI; | |
| 42 | const clip = multiply(perspective(fovY, aspect, NEAR, FAR), viewMatrix(camera.pos, camera.yaw, camera.pitch)); | |
| 43 | ||
| 44 | const [px, py, pz] = point; | |
| 45 | const w = (clip[3] as number) * px + (clip[7] as number) * py + (clip[11] as number) * pz + (clip[15] as number); | |
| 46 | if (w < NEAR) return null; // behind the eye, or inside the near plane | |
| 47 | const cx = (clip[0] as number) * px + (clip[4] as number) * py + (clip[8] as number) * pz + (clip[12] as number); | |
| 48 | const cy = (clip[1] as number) * px + (clip[5] as number) * py + (clip[9] as number) * pz + (clip[13] as number); | |
| 49 | ||
| 50 | const ndcX = cx / w, ndcY = cy / w; | |
| 51 | if (ndcX < -1 || ndcX > 1 || ndcY < -1 || ndcY > 1 || w > FAR) return null; | |
| 52 | return { | |
| 53 | x: (ndcX * 0.5 + 0.5) * width, | |
| 54 | y: (0.5 - ndcY * 0.5) * height, | |
| 55 | depth: w, | |
| 56 | }; | |
| 57 | } |
new above
offline