File size: 16,901 Bytes
4d559be
 
 
 
c58db7d
 
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
 
4d559be
 
c58db7d
4d559be
c58db7d
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
 
 
 
 
 
 
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
 
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55311f3
4d559be
 
c58db7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624ddb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
 
624ddb0
 
 
 
 
c58db7d
 
 
 
 
 
 
 
55311f3
c58db7d
 
 
 
4d559be
 
 
c58db7d
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c58db7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
 
 
 
55311f3
4d559be
 
 
 
 
 
c58db7d
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// A tiny 3D "film camera" for the highway: perspective projection with yaw,
// near-plane clipping, a road with lamps, and car sprites chosen by viewing
// angle (rear / front / side). Lets us shoot the duels like a director:
// POV through the windscreen, chase cams, a camera that spins with the car.
import { carRear, carSide, heroCarFront, rivalCarFront, roadsterFront, roadsterRear, roadsterSide, truckRear, truckSide, type CarKind } from './cars'
import { PULSE, glowBlob } from './draw'
import type { SceneCtx } from './types'
import { PALETTE, clamp, hash } from './util'

type C2D = CanvasRenderingContext2D

export interface Cam {
  x: number
  y: number
  z: number
  yaw: number
  f: number
  hy: number
  roll: number
  /** true downward tilt in radians (for aerial / drone shots) */
  tilt: number
}

export function makeCam(s: SceneCtx, x: number, y: number, z: number, yaw: number, pitch = 0, fov = 1, roll = 0, tilt = 0): Cam {
  const f = s.W * 0.62 * fov
  return { x, y, z, yaw, f, hy: s.H * 0.5 + pitch * f, roll, tilt }
}

interface P3 {
  x: number
  y: number
  z: number
}

function toCam(c: Cam, X: number, Y: number, Z: number): P3 {
  const dx = X - c.x
  const dz = Z - c.z
  const cs = Math.cos(c.yaw)
  const sn = Math.sin(c.yaw)
  const xr = dx * cs - dz * sn
  const zr = dx * sn + dz * cs
  const yr = Y - c.y
  if (!c.tilt) return { x: xr, y: yr, z: zr }
  const ct = Math.cos(c.tilt)
  const st = Math.sin(c.tilt)
  return { x: xr, y: yr * ct + zr * st, z: -yr * st + zr * ct }
}

export function proj(c: Cam, s: SceneCtx, X: number, Y: number, Z: number) {
  const p = toCam(c, X, Y, Z)
  if (p.z < 0.3) return null
  const k = c.f / p.z
  return { x: s.W / 2 + p.x * k, y: c.hy - p.y * k, z: p.z, k }
}

const NEAR = 0.35

/** fill a world-space polygon with near-plane clipping */
export function poly3(ctx: C2D, c: Cam, s: SceneCtx, pts: [number, number, number][], fill: string) {
  const cam = pts.map(([X, Y, Z]) => toCam(c, X, Y, Z))
  const out: P3[] = []
  for (let i = 0; i < cam.length; i++) {
    const a = cam[i]
    const b = cam[(i + 1) % cam.length]
    const ain = a.z >= NEAR
    const bin = b.z >= NEAR
    if (ain) out.push(a)
    if (ain !== bin) {
      const t = (NEAR - a.z) / (b.z - a.z)
      out.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, z: NEAR })
    }
  }
  if (out.length < 3) return
  ctx.fillStyle = fill
  ctx.beginPath()
  out.forEach((p, i) => {
    const k = c.f / p.z
    const x = s.W / 2 + p.x * k
    const y = c.hy - p.y * k
    if (i) ctx.lineTo(x, y)
    else ctx.moveTo(x, y)
  })
  ctx.closePath()
  ctx.fill()
}

// ------------------------------------------------------------------ the world
export interface WorldOpts {
  /** colour of the sky near the horizon */
  glow?: string
  /** police lights far away */
  police?: boolean
}

export function drawHighway(s: SceneCtx, c: Cam, o: WorldOpts = {}) {
  const { ctx, W, H, t } = s
  ctx.save()
  if (c.roll) {
    ctx.translate(W / 2, H / 2)
    ctx.rotate(c.roll)
    ctx.translate(-W / 2, -H / 2)
  }
  const hy = c.hy - Math.tan(c.tilt || 0) * c.f
  // sky
  const g = ctx.createLinearGradient(0, hy - H * 0.6, 0, hy)
  g.addColorStop(0, '#04050c')
  g.addColorStop(0.7, '#141a36')
  g.addColorStop(1, o.glow ?? '#3a1530')
  ctx.fillStyle = g
  ctx.fillRect(-W, -H, W * 3, hy + H)
  glowBlob(ctx, W / 2, hy, Math.max(W, H) * 0.55, '#ff4a6a', 0.16)
  // city panorama at infinity (rotates correctly with yaw)
  const fovH = Math.atan(W / 2 / c.f) + 0.3
  for (let i = 0; i < 240; i++) {
    const az = (i / 240) * Math.PI * 2
    let rel = az - c.yaw
    rel = Math.atan2(Math.sin(rel), Math.cos(rel))
    if (Math.abs(rel) > fovH) continue
    const x = W / 2 + Math.tan(rel) * c.f
    const bw = c.f * 0.03
    const band = i % 3 === 0 ? PULSE.low : i % 3 === 1 ? PULSE.mid : PULSE.high
    const bh = H * (0.03 + 0.12 * Math.pow(hash(i * 3.1), 2)) * (1 + (0.3 * band + 0.2 * PULSE.kick) * (0.4 + 0.6 * hash(i * 5.3)))
    ctx.fillStyle = '#07070e'
    ctx.fillRect(x - bw / 2, hy - bh, bw, bh + 2)
    ctx.fillStyle = hash(i * 7.7) > 0.5 ? PALETTE.sodium : '#7fc8d8'
    ctx.globalAlpha = 0.5
    for (let w = 0; w < 4; w++) if (hash(i * 13 + w) > 0.5) ctx.fillRect(x - bw * 0.3 + (w % 2) * bw * 0.35, hy - bh + bh * (0.2 + 0.18 * Math.floor(w / 2)), bw * 0.15, bh * 0.08)
    ctx.globalAlpha = 1
  }
  // ground
  ctx.fillStyle = '#07080b'
  ctx.fillRect(-W, hy, W * 3, H * 2)
  // road segments around the camera
  const z0 = Math.floor((c.z - 90) / 4) * 4
  for (let z = z0; z < c.z + 260; z += 4) {
    poly3(ctx, c, s, [
      [-7.2, 0, z],
      [7.2, 0, z],
      [7.2, 0, z + 4.05],
      [-7.2, 0, z + 4.05],
    ], z % 8 === 0 ? '#15171c' : '#17191e')
  }
  for (let z = z0; z < c.z + 260; z += 4) {
    for (const ex of [-5.55, 5.4]) poly3(ctx, c, s, [
      [ex, 0.01, z],
      [ex + 0.15, 0.01, z],
      [ex + 0.15, 0.01, z + 4.05],
      [ex, 0.01, z + 4.05],
    ], 'rgba(235,230,215,0.8)')
  }
  const d0 = Math.floor((c.z - 90) / 12) * 12
  for (let z = d0; z < c.z + 260; z += 12) {
    for (const lx of [-1.8, 1.8]) poly3(ctx, c, s, [
      [lx - 0.08, 0.01, z],
      [lx + 0.08, 0.01, z],
      [lx + 0.08, 0.01, z + 5],
      [lx - 0.08, 0.01, z + 5],
    ], 'rgba(235,230,215,0.85)')
  }
  // lamp posts + light pools + guard rails
  const l0 = Math.floor((c.z - 90) / 30) * 30
  const lamps: { x: number; y: number; k: number; z: number }[] = []
  for (let z = l0; z < c.z + 260; z += 30) {
    for (const sd of [-1, 1]) {
      poly3(ctx, c, s, [
        [sd * 2.5, 0.02, z - 7],
        [sd * 7.5, 0.02, z - 7],
        [sd * 7.5, 0.02, z + 7],
        [sd * 2.5, 0.02, z + 7],
      ], 'rgba(255,179,92,0.07)')
      const base = proj(c, s, sd * 8.2, 0, z)
      const top = proj(c, s, sd * 8.2, 8, z)
      const head = proj(c, s, sd * 5.8, 8, z)
      if (base && top && head) {
        ctx.strokeStyle = '#1c1f26'
        ctx.lineWidth = Math.max(1, 0.25 * base.k)
        ctx.beginPath()
        ctx.moveTo(base.x, base.y)
        ctx.lineTo(top.x, top.y)
        ctx.lineTo(head.x, head.y)
        ctx.stroke()
        lamps.push({ x: head.x, y: head.y, k: head.k, z: head.z })
      }
    }
  }
  for (const L of lamps) glowBlob(ctx, L.x, L.y, Math.max(4, 2.2 * L.k), PALETTE.sodium, 0.95)
  for (const sd of [-1, 1]) {
    for (let z = z0; z < c.z + 260; z += 4)
      poly3(ctx, c, s, [
        [sd * 7.6, 0.6, z],
        [sd * 7.6, 0.85, z],
        [sd * 7.6, 0.85, z + 4.05],
        [sd * 7.6, 0.6, z + 4.05],
      ], '#3a3f48')
  }
  // police lights far away
  if (o.police) policeLights(s, c, t)
  ctx.restore()
}

export function policeLights(s: SceneCtx, c: Cam, t: number) {
  const { ctx } = s
  ctx.save()
  ctx.globalCompositeOperation = 'lighter'
  for (let i = 0; i < 3; i++) {
    const p = proj(c, s, -3.6 + i * 3.6, 1.6, c.z + 220 + i * 25)
    if (!p) continue
    const ph = Math.floor(t * 6 + i) % 2
    glowBlob(ctx, p.x - p.k * 0.5, p.y, Math.max(6, 5 * p.k), '#ff2040', ph ? 1 : 0.25)
    glowBlob(ctx, p.x + p.k * 0.5, p.y, Math.max(6, 5 * p.k), '#2a6aff', ph ? 0.25 : 1)
  }
  ctx.restore()
}

// ------------------------------------------------------------------ actors
export interface Actor {
  kind: CarKind
  x: number
  y?: number
  z: number
  h: number
  head?: number
  high?: number
  brake?: number
  pop?: number
  look?: number
  pitch?: number
  jet?: { power: number; angle: number }
  jetKit?: boolean
  shield?: number
  lit?: number
}

const LEN: Record<CarKind, number> = { hero: 4.0, rival: 4.5, roadster: 4.3, truck: 16 }
const WID: Record<CarKind, number> = { hero: 1.75, rival: 1.9, roadster: 2.0, truck: 2.5 }

/** headlight beams as light thrown onto the road, along each car's heading */
export function drawBeams(s: SceneCtx, c: Cam, actors: Actor[]) {
  const { ctx } = s
  ctx.save()
  ctx.globalCompositeOperation = 'lighter'
  for (const a of actors) {
    const head = a.head ?? 1
    const high = a.high ?? 0
    if (head < 0.05 && high < 0.05) continue
    const fx = Math.sin(a.h)
    const fz = Math.cos(a.h)
    const rx = Math.cos(a.h)
    const rz = -Math.sin(a.h)
    const half = LEN[a.kind] / 2
    const ox = a.x + fx * half
    const oz = a.z + fz * half
    const y = Math.max(0.02, (a.y ?? 0) > 0.3 ? -1 : 0.03)
    if (y < 0) continue // airborne: no beam on the road
    // the beam as light on the asphalt: many short segments, falling off with
    // distance from the lamp, and faded out where it passes under the lens
    const len = 26 + 48 * high
    const segs = 14
    for (let k = 0; k < segs; k++) {
      const d0 = (k / segs) * len
      const d1 = ((k + 1) / segs) * len
      const w0 = 0.8 + (d0 / len) * (3.5 + 3 * high)
      const w1 = 0.8 + (d1 / len) * (3.5 + 3 * high)
      const fall = 1 / (1 + d0 / (6 + 10 * high))
      const mx = ox + fx * (d0 + d1) / 2
      const mz = oz + fz * (d0 + d1) / 2
      const depth = toCam(c, mx, 0, mz).z
      const nearFade = clamp((depth - 2) / 7)
      const al = (0.055 * head + 0.11 * high) * fall * nearFade
      if (al <= 0.003) continue
      poly3(ctx, c, s, [
        [ox + fx * d0 - rx * w0, y, oz + fz * d0 - rz * w0],
        [ox + fx * d0 + rx * w0, y, oz + fz * d0 + rz * w0],
        [ox + fx * d1 + rx * w1, y, oz + fz * d1 + rz * w1],
        [ox + fx * d1 - rx * w1, y, oz + fz * d1 - rz * w1],
      ], `rgba(255,240,210,${al})`)
    }
    // glare at the lamps when they point roughly at the lens
    for (const sd of [-1, 1]) {
      const p = proj(c, s, ox + rx * 0.62 * sd, 0.7, oz + rz * 0.62 * sd)
      if (!p) continue
      const toCamX = c.x - ox
      const toCamZ = c.z - oz
      const facing = (toCamX * fx + toCamZ * fz) / (Math.hypot(toCamX, toCamZ) || 1)
      if (facing > 0.2) glowBlob(ctx, p.x, p.y, Math.min(s.W * 0.1, p.k * (0.5 + 1.2 * high)), '#fff4dc', (0.35 + 0.45 * high) * facing * clamp((p.z - 1.5) / 4))
    }
  }
  ctx.restore()
}

export function drawActors(s: SceneCtx, c: Cam, actors: Actor[]) {
  const { ctx, t } = s
  drawBeams(s, c, actors)
  const sorted = actors
    .map((a) => ({ a, d: toCam(c, a.x, 0, a.z).z }))
    .filter((e) => e.d > 0.8)
    .sort((p, q) => q.d - p.d)
  for (const { a } of sorted) {
    const g = proj(c, s, a.x, a.y ?? 0, a.z)
    if (!g) continue
    const az = Math.atan2(a.x - c.x, a.z - c.z)
    let diff = a.h - az
    diff = Math.atan2(Math.sin(diff), Math.cos(diff))
    const w = WID[a.kind] * g.k
    // ground shadow when airborne
    if ((a.y ?? 0) > 0.05) {
      const gg = proj(c, s, a.x, 0, a.z)
      if (gg) {
        ctx.fillStyle = `rgba(0,0,0,${0.45 - Math.min(0.3, (a.y ?? 0) * 0.08)})`
        ctx.beginPath()
        ctx.ellipse(gg.x, gg.y, WID[a.kind] * gg.k * 0.7, WID[a.kind] * gg.k * 0.15, 0, 0, Math.PI * 2)
        ctx.fill()
      }
    }
    if (a.kind === 'roadster' || a.kind === 'truck') {
      if (Math.abs(diff) < 0.7) {
        if (a.kind === 'roadster') roadsterRear(ctx, g.x, g.y, w, { brake: a.brake, jet: a.jet && a.jet.angle > 2.4 ? a.jet.power : 0, t })
        else truckRear(ctx, g.x, g.y, w)
      } else if (Math.abs(diff) > Math.PI - 0.7) {
        if (a.kind === 'roadster') roadsterFront(ctx, g.x, g.y, w, { head: a.head ?? 1, t })
        else truckRear(ctx, g.x, g.y, w)
      } else {
        const L = LEN[a.kind] * g.k
        const fs = Math.abs(Math.sin(diff))
        ctx.save()
        ctx.translate(g.x, g.y)
        ctx.scale((diff > 0 ? 1 : -1) * Math.max(0.25, fs), 1)
        if (a.kind === 'roadster') roadsterSide(ctx, 0, 0, L, a.pitch ?? 0, { spin: t * 40, head: a.head ?? 1, brake: a.brake, t, nozzle: a.jetKit ? 1 : 0 })
        else truckSide(ctx, 0, 0, L, t * 20)
        ctx.restore()
      }
      continue
    }
    if (Math.abs(diff) < 0.7) {
      carRear(ctx, a.kind, g.x, g.y, w, { brake: a.brake, pop: a.pop, jet: a.jet && a.jet.angle > 2.4 ? a.jet.power : 0, t })
    } else if (Math.abs(diff) > Math.PI - 0.7) {
      if (a.kind === 'hero') heroCarFront(ctx, g.x, g.y, w, { head: a.head ?? 1, high: a.high ?? 0, t })
      else rivalCarFront(ctx, g.x, g.y, w, { head: a.head ?? 1, t, shield: a.shield ?? 0, lit: a.lit ?? 0 })
    } else {
      const L = LEN[a.kind] * g.k
      const fs = Math.abs(Math.sin(diff))
      ctx.save()
      ctx.translate(g.x, g.y)
      ctx.scale((diff > 0 ? 1 : -1) * Math.max(0.25, fs), 1)
      carSide(ctx, a.kind, 0, 0, L, a.pitch ?? 0, { spin: t * 40, head: a.head ?? 1, brake: a.brake, t, look: a.look, jetKit: a.jetKit, jet: a.jet, noBeam: true })
      ctx.restore()
    }
  }
}

// ------------------------------------------------------------------ gas / smoke
export interface Puff {
  x: number
  y: number
  z: number
  r: number
  a: number
  col: [number, number, number]
}

export function drawPuffs(s: SceneCtx, c: Cam, puffs: Puff[]) {
  const { ctx } = s
  const list = puffs
    .map((p) => ({ p, q: proj(c, s, p.x, p.y, p.z) }))
    .filter((e) => e.q)
    .sort((a, b) => b.q!.z - a.q!.z)
  for (const { p, q } of list) {
    const r = Math.min(p.r * q!.k, s.W * 0.32)
    if (r < 0.5) continue
    const near = clamp((q!.z - 2) / 6) / (1 + (p.r * q!.k) / (s.W * 0.25))
    if (near <= 0.01) continue
    const g = ctx.createRadialGradient(q!.x, q!.y, 0, q!.x, q!.y, r)
    const [R, G, B] = p.col
    g.addColorStop(0, `rgba(${R},${G},${B},${clamp(p.a * near)})`)
    g.addColorStop(1, `rgba(${R},${G},${B},0)`)
    ctx.fillStyle = g
    ctx.fillRect(q!.x - r, q!.y - r, r * 2, r * 2)
  }
}

/** a deterministic stream of puffs from an emitter whose position is a function of time */
export function emit(
  out: Puff[],
  tNow: number,
  from: number,
  to: number,
  rate: number,
  pos: (te: number) => { x: number; y: number; z: number } | null,
  o: { spread: number; rise: number; grow: number; life: number; r0: number; a0: number; col: [number, number, number]; drift?: { x: number; z: number } },
) {
  const dt = 1 / rate
  const start = Math.max(from, tNow - o.life)
  const end = Math.min(to, tNow)
  for (let te = Math.ceil(start / dt) * dt; te <= end; te += dt) {
    const age = tNow - te
    const p = pos(te)
    if (!p) continue
    const hx = hash(te * 91.7) - 0.5
    const hz = hash(te * 37.3) - 0.5
    const k = age / o.life
    out.push({
      x: p.x + hx * o.spread * (0.3 + age) + (o.drift?.x ?? 0) * age,
      y: p.y + o.rise * age,
      z: p.z + hz * o.spread * (0.3 + age) + (o.drift?.z ?? 0) * age,
      r: o.r0 + o.grow * age,
      a: o.a0 * (1 - k) * (1 - k),
      col: o.col,
    })
  }
}

/** a jet plume drawn in world space (start point, direction, length) */
export function plume3(s: SceneCtx, c: Cam, X: number, Y: number, Z: number, dx: number, dy: number, dz: number, len: number, w0: number, t: number) {
  const { ctx } = s
  const a = proj(c, s, X, Y, Z)
  // never let the plume reach into the lens (keeps it readable, avoids blow-out)
  let L2 = len
  for (let i = 0; i < 10; i++) {
    const d = toCam(c, X + dx * L2, Y + dy * L2, Z + dz * L2).z
    if (d >= 5) break
    L2 *= 0.75
  }
  const b = proj(c, s, X + dx * L2, Math.max(0, Y + dy * L2), Z + dz * L2)
  ctx.save()
  ctx.globalCompositeOperation = 'lighter'
  if (a) glowBlob(ctx, a.x, a.y, Math.min(s.W * 0.25, Math.max(6, w0 * 3 * a.k)), '#8fdcff', 0.8 * clamp((a.z - 1.5) / 4))
  if (a && b) {
    const flick = 0.85 + 0.15 * Math.sin(t * 53)
    ctx.globalAlpha = clamp((b.z - 2) / 8) * 0.7 + 0.3 * clamp(1 - Math.abs(dz))
    for (const [col, wk] of [
      ['rgba(70,150,255,0.45)', 1],
      ['rgba(140,225,255,0.65)', 0.6],
      ['rgba(255,255,255,0.9)', 0.28],
    ] as [string, number][]) {
      const wa = w0 * a.k * wk
      const wb = w0 * 2.4 * b.k * wk * flick
      const ang = Math.atan2(b.y - a.y, b.x - a.x)
      const px = Math.cos(ang + Math.PI / 2)
      const py = Math.sin(ang + Math.PI / 2)
      ctx.fillStyle = col
      ctx.beginPath()
      ctx.moveTo(a.x + px * wa, a.y + py * wa)
      ctx.lineTo(b.x + px * wb, b.y + py * wb)
      ctx.arc(b.x, b.y, wb, ang + Math.PI / 2, ang - Math.PI / 2, true)
      ctx.lineTo(a.x - px * wa, a.y - py * wa)
      ctx.closePath()
      ctx.fill()
    }
  }
  ctx.restore()
}

/** render the world again into the rear-view mirror (mirrored, looking backwards) */
export function rearMirror(s: SceneCtx, c: Cam, actors: Actor[], rect: { x: number; y: number; w: number; h: number }) {
  const { ctx, W } = s
  ctx.save()
  ctx.beginPath()
  ctx.roundRect(rect.x, rect.y, rect.w, rect.h, rect.h * 0.25)
  ctx.clip()
  ctx.translate(rect.x + rect.w / 2, rect.y + rect.h / 2)
  const sc = rect.w / W
  ctx.scale(-sc, sc)
  ctx.translate(-W / 2, -s.H / 2)
  const back: Cam = { ...c, yaw: c.yaw + Math.PI, hy: s.H * 0.5 }
  drawHighway(s, back)
  drawActors(s, back, actors)
  ctx.restore()
  ctx.strokeStyle = '#0b0c10'
  ctx.lineWidth = Math.max(2, rect.h * 0.08)
  ctx.beginPath()
  ctx.roundRect(rect.x, rect.y, rect.w, rect.h, rect.h * 0.25)
  ctx.stroke()
}