<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
<title>Patrol</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Silkscreen:wght@400;700&family=IBM+Plex+Mono:wght@400;600&display=swap" rel="stylesheet">
<style>
html, body { margin: 0; height: 100%; overflow: hidden; background: #1d1a3a; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
canvas { position: fixed; left: 0; top: 0; image-rendering: pixelated; image-rendering: crisp-edges; display: block; }
#ui { pointer-events: none; }
body.playing { cursor: none; }
#title { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: #ffe6a8; transition: opacity .9s steps(6); pointer-events: none; padding-bottom: 6vh; }
#title.gone { opacity: 0; }
#title .logo { font-family: 'Silkscreen', monospace; font-weight: 700; font-size: clamp(56px, 13vw, 136px); line-height: .9; letter-spacing: .06em; color: #fff4d6;
text-shadow: 0 .06em 0 #e0643a, 0 .12em 0 #7c1f2c, .05em .17em 0 rgba(29, 26, 58, .55); -webkit-font-smoothing: none; }
#title .sub { font-family: 'IBM Plex Mono', monospace; font-size: 13px; letter-spacing: .38em; text-transform: uppercase; color: #f6d27e; text-shadow: 1px 1px 0 #1d1a3a; margin-top: 12px; background: rgba(29, 26, 58, .62); padding: 5px 6px 5px 12px; }
#bar { width: min(300px, 70vw); height: 12px; border: 2px solid #ffe6a8; padding: 2px; box-shadow: 3px 3px 0 #1d1a3a; margin-top: 18px; }
#barFill { height: 100%; width: 0; background: repeating-linear-gradient(90deg, #f0a13a 0 8px, #d8642a 8px 10px); }
#status { font-family: 'IBM Plex Mono', monospace; font-size: 12px; color: #c9a7c7; letter-spacing: .08em; min-height: 16px; }
#go { display: none; font-family: 'Silkscreen', monospace; font-size: 20px; color: #fff4d6; letter-spacing: .14em; text-shadow: 2px 2px 0 #1d1a3a; margin-top: 22px; background: rgba(29, 26, 58, .62); padding: 6px 14px 4px; animation: blink 1.1s steps(1) infinite; }
#go .t { display: none; } body.touch #go .t { display: inline; } body.touch #go .m { display: none; }
@keyframes blink { 50% { opacity: .25; } }
#hints { position: fixed; left: 0; right: 0; bottom: 0; height: 30px; display: flex; align-items: center; gap: 22px; padding: 0 14px; box-sizing: border-box;
background: rgba(18, 16, 23, .58); color: rgba(255, 244, 214, .62); font-family: 'IBM Plex Mono', monospace; font-size: 12px; letter-spacing: .04em; white-space: nowrap; overflow: hidden; z-index: 3;
opacity: 0; transition: opacity .6s steps(4); }
body.ready #hints { opacity: 1; }
#hints b { color: #fff4d6; font-weight: 600; }
#hints .sp { flex: 1; }
#msl { color: #fff4d6; display: flex; align-items: center; gap: 3px; }
#msl i { display: inline-block; width: 5px; height: 11px; margin-left: 1px; background: rgba(255, 244, 214, .14); }
#msl i.a { background: #fff4d6; } #msl i.r { background: #f0a13a; animation: blink .5s steps(1) infinite; }
#snd { font: inherit; color: rgba(255, 244, 214, .7); background: none; border: 1px solid rgba(255, 244, 214, .35); padding: 2px 8px; cursor: pointer; letter-spacing: .08em; }
#snd.on { color: #1d1a3a; background: #f6d27e; border-color: #f6d27e; }
#hints .tm { display: none; } body.touch #hints .tm { display: inline; } body.touch #hints .mo { display: none; }
@media (max-width: 760px) { #hints .wide { display: none; } }
#touch { display: none; position: fixed; inset: 0 0 30px 0; pointer-events: none; z-index: 2; }
body.touch.playing #touch { display: block; }
#touch [data-t] { position: absolute; pointer-events: auto; width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; box-sizing: border-box;
font-family: 'Silkscreen', monospace; font-size: 13px; color: #fff4d6; background: rgba(29, 26, 58, .55); border: 3px solid rgba(255, 244, 214, .7); box-shadow: 3px 3px 0 rgba(29, 26, 58, .6); }
#touch [data-t].down { background: #f0a13a; color: #1d1a3a; border-color: #ffe6a8; }
#touch [data-t="fire"] { right: 18px; bottom: 26px; width: 84px; height: 84px; border-color: #e04a3a; }
#touch [data-t="ab"] { right: 116px; bottom: 26px; }
#touch [data-t="roll"] { right: 30px; bottom: 126px; }
#touch [data-t="view"] { right: 16px; top: 16px; width: 70px; height: 40px; }
</style>
</head>
<body>
<canvas id="gl"></canvas>
<canvas id="ui"></canvas>
<div id="title">
<div class="logo">PATROL</div>
<div class="sub">lake country · last light</div>
<div id="bar"><div id="barFill"></div></div>
<div id="status">Raising hills, filling lakes</div>
<div id="go"><span class="m">Click to fly</span><span class="t">Tap to fly</span></div>
</div>
<div id="touch">
<div data-t="fire">FIRE</div><div data-t="ab">BURN</div><div data-t="roll">ROLL</div><div data-t="view">VIEW</div>
</div>
<div id="hints">
<span class="mo"><b>MOUSE</b> steer</span><span class="mo"><b>CLICK</b> roll</span><span class="mo"><b>HOLD</b> afterburner</span>
<span class="mo"><b>RIGHT</b>/<b>F</b> missile</span><span class="mo"><b>SPACE</b> view <span id="viewName" class="wide">TOP</span></span>
<span class="tm"><b>DRAG</b> steer · buttons for the rest</span>
<span class="sp"></span>
<span id="msl">MSL</span>
<button id="snd">SOUND OFF</button>
</div>
<script>
'use strict';
// ===== Terrain / cloud generator. Runs inside Web Workers (stringified), or on the main thread as a fallback.
function GEN_WORKER(self) {
'use strict';
// ---- periodic gradient noise -------------------------------------------------
const PERM = new Uint8Array(512);
const GX = new Float32Array(256), GY = new Float32Array(256);
(function init(seed) {
let s = seed >>> 0;
const rnd = () => { s = (s + 0x6D2B79F5) >>> 0; let t = s; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
const p = []; for (let i = 0; i < 256; i++) p.push(i);
for (let i = 255; i > 0; i--) { const j = (rnd() * (i + 1)) | 0; const t = p[i]; p[i] = p[j]; p[j] = t; }
for (let i = 0; i < 512; i++) PERM[i] = p[i & 255];
for (let i = 0; i < 256; i++) { const a = rnd() * Math.PI * 2; GX[i] = Math.cos(a); GY[i] = Math.sin(a); }
})(1337);
function pn(x, y, P) { // gradient noise, period P (integer) in lattice units, range ~[-0.7,0.7]
const xf = Math.floor(x), yf = Math.floor(y);
const fx = x - xf, fy = y - yf;
let x0 = xf % P; if (x0 < 0) x0 += P; let x1 = x0 + 1; if (x1 >= P) x1 = 0;
let y0 = yf % P; if (y0 < 0) y0 += P; let y1 = y0 + 1; if (y1 >= P) y1 = 0;
x0 &= 255; x1 &= 255; y0 &= 255; y1 &= 255;
const h00 = PERM[PERM[x0] + y0], h10 = PERM[PERM[x1] + y0], h01 = PERM[PERM[x0] + y1], h11 = PERM[PERM[x1] + y1];
const n00 = GX[h00] * fx + GY[h00] * fy;
const n10 = GX[h10] * (fx - 1) + GY[h10] * fy;
const n01 = GX[h01] * fx + GY[h01] * (fy - 1);
const n11 = GX[h11] * (fx - 1) + GY[h11] * (fy - 1);
const u = fx * fx * fx * (fx * (fx * 6 - 15) + 10), v = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
const a = n00 + (n10 - n00) * u, b = n01 + (n11 - n01) * u;
return (a + (b - a) * v) * 1.4;
}
function fbm(u, v, cells, oct, gain) {
let s = 0, a = 1, n = 0, c = cells;
for (let o = 0; o < oct; o++) { s += a * pn(u * c, v * c, c); n += a; a *= gain; c *= 2; }
return s / n;
}
function ridged(u, v, cells, oct) {
let s = 0, a = 0.5, w = 1, c = cells, n = 0;
for (let o = 0; o < oct; o++) {
let r = 1 - Math.abs(pn(u * c, v * c, c)); r *= r; r *= w;
w = Math.min(1, Math.max(0, r * 2));
s += r * a; n += a; a *= 0.5; c *= 2;
}
return s / n;
}
const sstep = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
// ---- terrain height (metres) at tile coords u,v in [0,1) ------------------------
function height(u, v) {
const wu = u + 0.045 * fbm(u + 0.37, v + 0.11, 4, 4, 0.5);
const wv = v + 0.045 * fbm(u + 0.71, v + 0.53, 4, 4, 0.5);
const base = fbm(wu, wv, 3, 6, 0.5);
const region = fbm(u + 0.13, v + 0.77, 2, 3, 0.5);
const mMask = sstep(0.02, 0.32, region);
const ridge = ridged(wu + 0.29, wv + 0.61, 5, 6);
const hills = fbm(wu + 0.5, wv + 0.25, 14, 4, 0.5);
let h = 30 + base * 70 + hills * 16 + mMask * Math.pow(ridge, 1.6) * 300;
// lakes: carved basins, fewer in the mountains
const lk = fbm(wu + 0.21, wv + 0.43, 6, 5, 0.5);
const lk2 = fbm(wu + 0.83, wv + 0.17, 13, 3, 0.5);
const carve = sstep(-0.06, 0.24, lk + 0.35 * lk2) * 80 * (1 - 0.7 * mMask);
h -= carve;
// soften the shore: flatten a beach shelf around the waterline
if (h > 0) h = h < 6 ? h * (0.35 + 0.65 * h / 6) : h;
else h = -Math.pow(Math.min(-h, 90) / 70, 1.5) * 70;
return h;
}
function cloud(u, v) {
const wu = u + 0.03 * fbm(u + 0.4, v + 0.9, 6, 3, 0.5);
const wv = v + 0.03 * fbm(u + 0.2, v + 0.6, 6, 3, 0.5);
const c = fbm(wu, wv, 9, 5, 0.52);
const big = fbm(u + 0.55, v + 0.35, 3, 2, 0.5);
const d = c + big * 0.25 - 0.17;
if (d <= 0) return 0;
const b = 1 - Math.abs(pn(wu * 46, wv * 46, 46));
const b2 = 1 - Math.abs(pn(wu * 92 + 0.5, wv * 92, 92));
const dome = Math.sqrt(sstep(0, 0.22, d));
return Math.min(1, dome * (0.62 + 0.26 * b + 0.12 * b2));
}
self.onmessage = function (e) {
const m = e.data;
if (m.job === 'height') {
const N = m.N, out = new Float32Array((m.y1 - m.y0) * N);
for (let y = m.y0; y < m.y1; y++) {
const v = y / N;
for (let x = 0; x < N; x++) out[(y - m.y0) * N + x] = height(x / N, v);
if ((y & 7) === 7) self.postMessage({ type: 'progress', job: 'height', n: 8 });
}
self.postMessage({ type: 'progress', job: 'height', n: (m.y1 - m.y0) & 7 });
self.postMessage({ type: 'done', job: 'height', y0: m.y0, y1: m.y1, data: out }, [out.buffer]);
} else if (m.job === 'cloud') {
const N = m.N, out = new Uint8Array((m.y1 - m.y0) * N);
for (let y = m.y0; y < m.y1; y++) {
for (let x = 0; x < N; x++) out[(y - m.y0) * N + x] = Math.round(cloud(x / N, y / N) * 255);
if ((y & 7) === 7) self.postMessage({ type: 'progress', job: 'cloud', n: 8 });
}
self.postMessage({ type: 'progress', job: 'cloud', n: (m.y1 - m.y0) & 7 });
self.postMessage({ type: 'done', job: 'cloud', y0: m.y0, y1: m.y1, data: out }, [out.buffer]);
} else if (m.job === 'setH') {
self._bake = m;
} else if (m.job === 'bake') {
// shadow (towards a fixed low sun), ambient occlusion, forest density
const S = self._bake;
const N = S.N, H = S.height, MG = S.maxGrid, GB = S.gridBlock, GN = N / GB, texel = S.texel;
const sx = S.sun[0], sy = S.sun[1], sz = S.sun[2];
const hl = Math.hypot(sx, sz), dx = sx / hl, dz = sz / hl, rise = sy / hl * texel; // metres risen per texel moved
const maxH = S.maxH;
const mask = N - 1;
const hs = (x, z) => { // bilinear, wrapped, water surface clamped
const xf = Math.floor(x), zf = Math.floor(z), fx = x - xf, fz = z - zf;
const x0 = xf & mask, x1 = (xf + 1) & mask, z0 = zf & mask, z1 = (zf + 1) & mask;
const a = H[z0 * N + x0], b = H[z0 * N + x1], c = H[z1 * N + x0], d = H[z1 * N + x1];
const h = (a + (b - a) * fx) * (1 - fz) + (c + (d - c) * fx) * fz;
return h > 0 ? h : 0;
};
const hr = (x, z) => H[(z & mask) * N + (x & mask)];
const out = new Float32Array((m.y1 - m.y0) * N * 4);
for (let y = m.y0; y < m.y1; y++) {
for (let x = 0; x < N; x++) {
const h0 = hr(x, y), hsurf = h0 > 0 ? h0 : 0;
// --- shadow march
let px = x, pz = y, ry = hsurf + 0.6, dist = 0, sh = 1;
for (let i = 0; i < 900; i++) {
if (ry > maxH) break;
const bx = (Math.floor(px) & mask) / GB | 0, bz = (Math.floor(pz) & mask) / GB | 0;
const bm = MG[bz * GN + bx];
let step;
if (ry > bm + 0.5) step = GB * 0.9;
else {
const th = hs(px, pz);
if (dist > 0.5) {
const s = (ry - th) / (0.03 * dist * texel + 1.2);
if (s < sh) sh = s;
if (sh <= 0) { sh = 0; break; }
}
step = 1.25;
}
px += dx * step; pz += dz * step; ry += rise * step; dist += step;
}
sh = Math.max(0, Math.min(1, sh));
// --- ambient occlusion: compare to local averages
let s1 = 0, s2 = 0;
for (let k = -2; k <= 2; k++) { s1 += hr(x + k * 2, y) + hr(x, y + k * 2); }
for (let k = -3; k <= 3; k++) { s2 += hr(x + k * 6, y + k * 3) + hr(x - k * 3, y + k * 6); }
const hh = Math.max(h0, -2);
const cav = (hh - Math.max(s1 / 10, -2)) * 0.6 + (hh - Math.max(s2 / 14, -2)) * 0.4;
const ao = Math.max(0.45, Math.min(1.2, 1 + cav * 0.018));
// --- forest density
const gx = (hr(x + 1, y) - hr(x - 1, y)) / (2 * texel), gz = (hr(x, y + 1) - hr(x, y - 1)) / (2 * texel);
const slope = Math.hypot(gx, gz);
const u = x / N, v = y / N;
let f = fbm(u + 0.44, v + 0.11, 10, 5, 0.55) * 1.7 + 0.1 + Math.min(slope, 0.8) * 0.7;
f -= sstep(5, 0.5, h0) * 1.2; // no forest on the beach
f -= sstep(150, 215, h0 + fbm(u, v, 24, 2, 0.5) * 40) * 1.6; // treeline
f -= sstep(0.9, 1.3, slope) * 1.5; // cliffs
const forest = h0 <= 0.3 ? 0 : sstep(0.0, 0.3, f);
const o = ((y - m.y0) * N + x) * 4;
out[o] = h0; out[o + 1] = sh; out[o + 2] = ao; out[o + 3] = forest;
}
if ((y & 7) === 7) self.postMessage({ type: 'progress', job: 'bake', n: 8 });
}
self.postMessage({ type: 'progress', job: 'bake', n: (m.y1 - m.y0) & 7 });
self.postMessage({ type: 'done', job: 'bake', y0: m.y0, y1: m.y1, data: out }, [out.buffer]);
}
};
}
const PALETTE = [[0.114,0.102,0.227],[0.7,0.6,0.79],[0.86,0.68,0.72],[0.98,0.8,0.5],[1,0.9,0.66],[0.62,0.52,0.76],[0.51,0.44,0.71],[0.4,0.35,0.64],[0.3,0.27,0.54],[0.2,0.19,0.42],[0.77,0.59,0.73],[0.63,0.5,0.71],[0.48,0.41,0.67],[0.33,0.29,0.56],[0.97,0.71,0.55],[0.87,0.62,0.64],[0.64,0.49,0.67],[0.42,0.35,0.6],[0.22,0.2,0.44],[0.8,0.7,0.86],[0.95,0.78,0.78],[1,0.88,0.62],[1,0.94,0.74],[0.93,0.7,0.78],[1,0.98,0.88],[0.13,0.1,0.14],[0.27,0.22,0.3],[1,0.8,0.4],[0.95,0.4,0.16],[0.92,0.98,0.95],[0.66,0.9,0.82],[0.42,0.78,0.72],[0.24,0.6,0.63],[0.18,0.43,0.56],[0.14,0.28,0.48],[0.11,0.17,0.35],[0.07,0.09,0.23],[0.42,0.62,0.7],[0.28,0.48,0.62],[0.18,0.36,0.54],[0.12,0.19,0.4],[0.09,0.12,0.3],[0.06,0.07,0.2],[0.97,0.78,0.66],[0.68,0.56,0.74],[1,1,1],[0.09,0.15,0.16],[0.16,0.25,0.22],[0.27,0.38,0.3],[0.52,0.2,0.24],[0.85,0.39,0.16],[0.97,0.63,0.25],[0.38,0.11,0.2],[0.72,0.18,0.19],[0.91,0.33,0.24],[0.52,0.38,0.22],[0.82,0.6,0.17],[0.97,0.81,0.3],[0.58,0.5,0.6],[0.85,0.74,0.54],[0.95,0.87,0.64],[0.62,0.6,0.8],[0.88,0.85,0.93],[1,0.96,0.94],[0.3,0.26,0.4],[0.48,0.43,0.56],[0.68,0.62,0.7],[0.89,0.77,0.46],[0.84,0.81,0.53],[0.74,0.62,0.35],[0.66,0.67,0.4],[0.4,0.38,0.47],[0.7,0.73,0.45],[0.53,0.58,0.36],[0.2,0.15,0.22],[0.33,0.24,0.26],[0.5,0.38,0.3],[0.55,0.47,0.68],[0.43,0.37,0.59],[1,0.94,0.86],[0.5,0.43,0.65],[0.33,0.35,0.42],[0.55,0.57,0.63],[0.79,0.78,0.78],[0.22,0.23,0.3],[0.38,0.4,0.48],[0.1,0.09,0.13],[0.16,0.15,0.19],[0.3,0.28,0.33],[0.5,0.33,0.13],[0.84,0.6,0.22],[1,0.88,0.55],[0.47,0.13,0.16],[0.78,0.2,0.18],[0.95,0.42,0.3],[0.21,0.2,0.25],[0.33,0.32,0.38],[0.85,0.3,0.12],[0.52,0.52,0.6],[0.78,0.78,0.82],[0.96,0.94,0.92],[0.05,0.05,0.09]];
// ===== GLSL ================================================================================
const GLSL_COMMON = `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
const float TILE = 5120.0;
const float CB = 190.0; // cloud base
const float CT = 88.0; // max cloud thickness
const float MAXH = 345.0; // above every terrain point
uniform vec3 uSun;
uniform vec3 uCamPos, uCamR, uCamU, uCamF;
uniform float uTime;
float bayer4(ivec2 p) {
int i = (p.y & 3) * 4 + (p.x & 3);
const float m[16] = float[16](0.,8.,2.,10.,12.,4.,14.,6.,3.,11.,1.,9.,15.,7.,13.,5.);
return (m[i] + 0.5) / 16.0;
}
float hash12(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * .1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }
vec4 hash42(vec2 p) { vec4 p4 = fract(vec4(p.xyxy) * vec4(.1031, .1030, .0973, .1099)); p4 += dot(p4, p4.wzxy + 33.33); return fract((p4.xxyz + p4.yzzw) * p4.zywx); }
// Haze: gold toward the sun, lilac away from it (a stepped ramp of palette colours)
vec3 fogRamp(vec3 rd, float th) {
vec2 h = normalize(rd.xz + vec2(1e-5));
float s = dot(h, normalize(uSun.xz)) * 0.5 + 0.5;
float g = max(dot(rd, uSun), 0.0);
float p = smoothstep(0.18, 0.6, s) + smoothstep(0.6, 0.95, s) + pow(g, 14.0) * 1.3;
float i = floor(p + th);
return i < 0.5 ? vec3(0.70, 0.60, 0.79) : (i < 1.5 ? vec3(0.86, 0.68, 0.72) : (i < 2.5 ? vec3(0.98, 0.80, 0.50) : vec3(1.0, 0.90, 0.66)));
}
// exponential height fog + thin distance haze
float fogAmt(float y0, vec3 rd, float t) {
const float a = 0.00050, b = 0.0042;
float k = b * rd.y * t;
float f = abs(k) < 1e-3 ? a * exp(-b * y0) * t : a * exp(-b * y0) * (1.0 - exp(-k)) / (b * rd.y);
f += t * 0.000045;
return 1.0 - exp(-f);
}
vec3 applyFog(vec3 col, vec3 rd, float y0, float t, float th1, float th2) {
float f = (fogAmt(y0, rd, t) - 0.15) / 0.85;
return f > th1 ? fogRamp(rd, th2) : col;
}
`;
const VS_QUAD = `#version 300 es
in vec2 aP; out vec2 vP;
void main() { vP = aP; gl_Position = vec4(aP, 0.0, 1.0); }`;
// ----- the world: sky, terrain, water, clouds -----------------------------------------------
const FS_WORLD = GLSL_COMMON + `
uniform sampler2D uTer; // r height, g baked sun visibility, b ao, a forest
uniform sampler2D uHgt; // height only (cheap fetches for the ray march)
uniform sampler2D uMaxH; // dilated block max height (80 m blocks)
uniform sampler2D uMaxH2; // dilated block max height (320 m blocks)
uniform sampler2D uCld; // cloud thickness
uniform sampler2D uNoise; // random rgba
uniform vec2 uTan, uRes;
uniform float uNear, uFar, uPixAng;
uniform vec2 uCloudOff;
uniform vec3 uJetPos, uJetR, uJetU, uJetF, uJetSun;
uniform float uJetVis;
uniform vec4 uImp[8]; // x, z, t0, type (1 land, 2 water)
uniform vec4 uImpB[8]; // y, size, 0, 0
uniform vec4 uWake[24]; // x, z, t0, strength
uniform int uWakeN;
uniform vec4 uMsl[4]; // missile flames: x,y,z,intensity
in vec2 vP;
out vec4 oCol;
float B1, B2, B3; // three decorrelated ordered-dither thresholds for this pixel
vec4 ter(vec2 xz) { return texture(uTer, xz / TILE); }
float surf(vec2 xz) { return max(texture(uHgt, xz / TILE).r, 0.0); }
float nz(vec2 p) { return texture(uNoise, p / 256.0).r; }
float nz2(vec2 p) { return texture(uNoise, p / 256.0).g; }
vec3 dpick(vec3 a, vec3 b, float t, float th) { return t > th ? b : a; }
// cumulus: flat base, cauliflower tops
float cth(vec2 xz) {
vec2 q = xz + uCloudOff;
float c = texture(uCld, q / TILE).r;
if (c <= 0.0) return 0.0;
float b1 = texture(uNoise, q / (256.0 * 34.0)).r;
float b2 = texture(uNoise, q / (256.0 * 13.0) + 0.37).g;
float k = smoothstep(0.0, 0.3, c);
// rounded "cauliflower" puffs rather than crags
float puff = (1.0 - (1.0 - b1) * (1.0 - b1)) * 30.0 + (1.0 - (1.0 - b2) * (1.0 - b2)) * 7.0 - 22.0;
return max(0.0, c * CT + puff * k - (1.0 - k) * 6.0);
}
float marchTerrain(vec3 ro, vec3 rd, float tmax, out bool budget) {
budget = false;
float t = 0.0;
if (ro.y > MAXH) { if (rd.y >= -1e-4) return -1.0; t = (ro.y - MAXH) / -rd.y; }
float tp = t;
float hxz = max(length(rd.xz), 1e-4);
for (int i = 0; i < 190; i++) {
if (t > tmax) return -1.0;
vec3 p = ro + rd * t;
if (p.y > MAXH && rd.y >= 0.0) return -1.0;
float h = surf(p.xz);
float d = p.y - h;
if (d < 0.0) {
float a = tp, b = t;
for (int k = 0; k < 7; k++) { float m = 0.5 * (a + b); vec3 q = ro + rd * m; if (q.y < surf(q.xz)) b = m; else a = m; }
return b;
}
float st = max(d * 0.42, 0.2 + t * 0.011);
float cm = texture(uMaxH2, p.xz / TILE).r;
if (p.y > cm + 0.3) {
float s = 300.0 / hxz;
if (rd.y < 0.0) s = min(s, (p.y - cm) / -rd.y);
st = max(st, s);
} else {
float bm = texture(uMaxH, p.xz / TILE).r;
if (p.y > bm + 0.3) {
float s = 72.0 / hxz;
if (rd.y < 0.0) s = min(s, (p.y - bm) / -rd.y);
st = max(st, s);
}
}
tp = t; t += st;
}
budget = true;
return t;
}
float marchCloud(vec3 ro, vec3 rd, float tmax) {
float t0, t1;
const float TOP = CB + CT + 22.0;
if (abs(rd.y) < 1e-5) { if (ro.y < CB || ro.y > TOP) return -1.0; t0 = 0.0; t1 = tmax; }
else { float ta = (CB - ro.y) / rd.y, tb = (TOP - ro.y) / rd.y; t0 = max(min(ta, tb), 0.0); t1 = min(max(ta, tb), tmax); }
if (t0 >= t1) return -1.0;
t1 = min(t1, t0 + 4500.0);
float t = t0, tp = t0;
for (int i = 0; i < 80; i++) {
vec3 p = ro + rd * t;
float th = cth(p.xz);
float d = p.y - (CB + th);
if (th > 0.6 && d < 0.0 && p.y > CB - 0.05) {
float a = tp, b = t;
for (int k = 0; k < 6; k++) { float m = 0.5 * (a + b); vec3 q = ro + rd * m; float tq = cth(q.xz); if (tq > 0.6 && q.y < CB + tq && q.y > CB - 0.05) b = m; else a = m; }
return b;
}
float st = clamp(abs(d) * 0.45, 1.5 + t * 0.005, 45.0);
tp = t; t += st;
if (t > t1) break;
}
return -1.0;
}
// ---- sky: banded ramps, dithered between neighbouring bands ---------------------------------------
const vec3 SKA[6] = vec3[6](vec3(0.70, 0.60, 0.79), vec3(0.62, 0.52, 0.76), vec3(0.51, 0.44, 0.71), vec3(0.40, 0.35, 0.64), vec3(0.30, 0.27, 0.54), vec3(0.20, 0.19, 0.42));
const vec3 SKM[6] = vec3[6](vec3(0.86, 0.68, 0.72), vec3(0.77, 0.59, 0.73), vec3(0.63, 0.50, 0.71), vec3(0.48, 0.41, 0.67), vec3(0.33, 0.29, 0.56), vec3(0.20, 0.19, 0.42));
const vec3 SKT[6] = vec3[6](vec3(0.98, 0.80, 0.50), vec3(0.97, 0.71, 0.55), vec3(0.87, 0.62, 0.64), vec3(0.64, 0.49, 0.67), vec3(0.42, 0.35, 0.60), vec3(0.22, 0.20, 0.44));
vec3 sky(vec3 rd, bool streaks) {
float y = max(rd.y, 0.0);
vec2 h = normalize(rd.xz + vec2(1e-5));
float s = dot(h, normalize(uSun.xz)) * 0.5 + 0.5;
float sd = dot(rd, uSun);
float band = clamp(sqrt(y / 0.9) * 5.0, 0.0, 5.0);
int bi = int(clamp(floor(band + B1), 0.0, 5.0));
float colp = smoothstep(0.18, 0.6, s) + smoothstep(0.6, 0.95, s);
int ci = int(clamp(floor(colp + B3), 0.0, 2.0));
vec3 c = ci == 0 ? SKA[bi] : (ci == 1 ? SKM[bi] : SKT[bi]);
if (bi == 0) {
float g = max(dot(rd, uSun), 0.0);
float p = smoothstep(0.18, 0.6, s) + smoothstep(0.6, 0.95, s) + pow(g, 14.0) * 1.3;
float i = floor(p + B3);
c = i < 0.5 ? vec3(0.80, 0.70, 0.86) : (i < 1.5 ? vec3(0.95, 0.78, 0.78) : (i < 2.5 ? vec3(1.0, 0.88, 0.62) : vec3(1.0, 0.94, 0.74)));
}
if (streaks && y > 0.012) {
// thin high streaks, sampled in angular space so they stay a few pixels wide
vec2 q = rd.xz / (y + 0.04);
vec2 sd2 = normalize(uSun.xz);
float along = dot(q, vec2(-sd2.y, sd2.x)), across = dot(q, sd2);
float n = texture(uNoise, vec2(along * 0.004 + uTime * 0.0002, across * 0.12)).b;
float n2 = texture(uNoise, vec2(along * 0.0013 + 0.3, across * 0.035)).a;
float vis = abs(dot(normalize(rd.xz + vec2(1e-5)), sd2));
float v = n * 0.65 + n2 * 0.35 - (1.0 - smoothstep(0.012, 0.06, y)) * 0.25 - smoothstep(0.22, 0.55, y) * 0.3 - (1.0 - smoothstep(0.3, 0.75, vis)) * 0.35;
if (v > 0.715 + (B2 - 0.5) * 0.02) c = ci == 2 ? vec3(0.97, 0.71, 0.55) : (ci == 1 ? vec3(0.93, 0.70, 0.78) : vec3(0.77, 0.59, 0.73));
}
// stepped glow rings and a large banded sun disc
if (sd > 0.9920 && B2 < 0.5) c = vec3(0.97, 0.71, 0.55);
if (sd > 0.9955 && B2 < 0.75) c = vec3(1.0, 0.86, 0.62);
if (sd > 0.99895) c = vec3(1.0, 0.86, 0.62);
if (sd > 0.99915) c = vec3(1.0, 0.94, 0.74);
if (sd > 0.99945) c = vec3(1.0, 0.98, 0.88);
return c;
}
// ---- jet shadow (silhouette projected along a steeper "shadow sun") ------------------------
float jetShadow(vec3 p) {
if (uJetVis < 0.5) return 1.0;
vec3 rel = uJetPos - p;
if (dot(rel, rel) > 1600.0 * 1600.0) return 1.0;
vec3 L = uJetSun;
float den = dot(L, uJetU);
if (abs(den) > 0.06) {
float s = dot(rel, uJetU) / den;
if (s > 0.0) {
vec3 q = p + L * s - uJetPos;
float x = abs(dot(q, uJetR)), z = dot(q, uJetF);
bool wing = x < 6.9 && z > -6.1 && z < mix(2.4, -3.7, clamp((x - 1.0) / 5.9, 0.0, 1.0));
float fw = z > 6.0 ? (9.6 - z) * 0.26 : (z > 1.5 ? 0.95 + (6.0 - z) * 0.2 : 1.95);
bool fus = z < 9.6 && z > -8.9 && x < fw;
bool stab = x < 4.1 && z < -6.0 && z > -8.7 && z < -6.0 - (x - 1.6) * 0.45;
if (wing || fus || stab) return 0.0;
}
}
vec3 a = uJetPos - uJetF * 8.5, b = uJetPos + uJetF * 9.0;
vec3 w0 = p - a, u = b - a;
float uu = dot(u, u), uv = dot(u, L);
float du = dot(u, w0), dv = dot(L, w0);
float den2 = uu - uv * uv;
if (den2 > 1e-4) {
float sc = clamp((du - uv * dv) / den2, 0.0, 1.0);
float tc = max(uv * sc - dv, 0.0);
if (length(a + u * sc - (p + L * tc)) < 1.3) return 0.0;
}
return 1.0;
}
float cloudShadow(vec3 p) {
float s = (CB + 22.0 - p.y) / uSun.y;
if (s < 0.0) return 1.0;
float th = cth(p.xz + uSun.xz * s);
return 1.0 - smoothstep(3.0, 10.0, th);
}
// stepped tone picker with dithered boundaries
vec3 tone3(vec3 d, vec3 m, vec3 l, float L) {
float x = L + (B1 - 0.5) * 0.16;
return x > 0.64 ? l : (x > 0.32 ? m : d);
}
vec3 impactGround(vec3 col, vec3 p, bool water, inout float lightAdd) {
for (int i = 0; i < 8; i++) {
vec4 im = uImp[i];
if (im.w < 0.5) continue;
vec2 d = p.xz - im.xy;
float r = length(d);
float a = uTime - im.z;
if (a < 0.0 || r > 260.0) continue;
float sz = uImpB[i].y;
float jag = nz(p.xz * 1.7 + im.xy) * 0.6 + nz2(p.xz * 0.6) * 0.4;
if (!water) {
float R = sz * (1.05 + jag * 0.55);
float fade = 1.0 - smoothstep(35.0, 50.0, a);
if (r < R) {
float k = smoothstep(R, R * 0.55, r);
vec3 sc = k > 0.5 + (B2 - 0.5) * 0.5 ? vec3(0.13, 0.10, 0.14) : vec3(0.27, 0.22, 0.30);
col = dpick(col, sc, fade * (0.75 + 0.25 * k), B3);
vec2 cell = floor(p.xz * 0.8);
float hsh = hash12(cell + floor(uTime * 5.0 + hash12(cell) * 7.0) * 0.17);
float ember = (1.0 - smoothstep(4.0, 24.0, a)) * k;
if (hsh > 1.0 - 0.22 * ember) col = hsh > 1.0 - 0.07 * ember ? vec3(1.0, 0.80, 0.40) : vec3(0.95, 0.40, 0.16);
} else if (r < R * 1.8) {
float k = (1.0 - smoothstep(R, R * 1.8, r)) * (1.0 - smoothstep(25.0, 50.0, a));
col = dpick(col, vec3(0.27, 0.22, 0.30), k * 0.6, B3);
}
} else {
float fade = 1.0 - smoothstep(6.0, 12.0, a);
float R = sz * (1.2 + a * 0.25) * (0.8 + jag * 0.5);
if (r < R && jag > 0.35 + a * 0.04) col = dpick(col, vec3(0.92, 0.98, 0.95), fade, B3);
float rr = r - a * 14.0;
if (rr < 0.0 && r > R * 0.8 && fract(rr / 11.0) < 0.22 && jag > 0.3) col = dpick(col, vec3(0.92, 0.98, 0.95), fade * 0.9 * (1.0 - smoothstep(60.0, 140.0, r)), B3);
}
// shock ring racing outward
if (a < 1.3) {
float R = sz * (1.0 + 16.0 * pow(a, 0.65));
float w = 1.6 + a * 2.5;
if (abs(r - R) < w) col = dpick(col, water ? vec3(0.92, 0.98, 0.95) : vec3(1.0, 0.94, 0.74), clamp(1.4 - a, 0.0, 1.0), B2);
}
if (a < 1.6) lightAdd += (1.0 - a / 1.6) * max(0.0, 1.0 - r / (sz * 9.0)) * 1.4;
}
return col;
}
// water: depth bands (lit / shaded pairs)
const vec3 WL[7] = vec3[7](vec3(0.66, 0.90, 0.82), vec3(0.42, 0.78, 0.72), vec3(0.24, 0.60, 0.63), vec3(0.18, 0.43, 0.56), vec3(0.14, 0.28, 0.48), vec3(0.11, 0.17, 0.35), vec3(0.07, 0.09, 0.23));
const vec3 WS[7] = vec3[7](vec3(0.42, 0.62, 0.70), vec3(0.28, 0.48, 0.62), vec3(0.18, 0.36, 0.54), vec3(0.15, 0.27, 0.48), vec3(0.12, 0.19, 0.40), vec3(0.09, 0.12, 0.30), vec3(0.06, 0.07, 0.20));
vec3 shadeWater(vec3 p, vec3 rd, float t, float depth, float lit, float fp) {
float d = depth + (B1 - 0.5) * (0.6 + depth * 0.12);
int bi = d < 1.1 ? 0 : (d < 3.2 ? 1 : (d < 7.5 ? 2 : (d < 14.0 ? 3 : (d < 24.0 ? 4 : (d < 40.0 ? 5 : 6)))));
vec3 c = lit > B2 * 0.9 + 0.05 ? WL[bi] : WS[bi];
// ripples
float lod = clamp(fp * 0.6, 0.0, 1.0);
vec2 u1 = p.xz * 0.9 + uTime * vec2(2.1, 1.3);
vec2 u2 = p.xz * 2.3 - uTime * vec2(1.7, 3.1);
vec2 g = (texture(uNoise, u1 / 256.0).xy - 0.5) * 0.55 + (texture(uNoise, u2 / 256.0).zw - 0.5) * 0.45;
g *= mix(1.0, 0.45, lod);
vec3 n = normalize(vec3(g.x, 1.0, g.y));
// sky reflection at grazing angles
float cosv = max(dot(-rd, n), 0.0);
float F = 0.03 + 0.97 * pow(1.0 - cosv, 5.0);
vec3 r = reflect(rd, n); r.y = abs(r.y);
if (smoothstep(0.1, 0.55, F) > B3) {
vec3 rc = sky(r, false);
if (r.y > 0.01 && p.y < CB) {
float s = (CB + 25.0 - p.y) / r.y;
float th = cth(p.xz + r.xz * s);
if (th > 8.0) rc = dot(normalize(r.xz), normalize(uSun.xz)) > 0.2 ? vec3(0.97, 0.78, 0.66) : vec3(0.68, 0.56, 0.74);
}
c = rc;
}
// glitter path under the sun
float sp = dot(r, uSun);
float gl = smoothstep(0.984, 0.994, sp);
if (gl > B2 && lit > 0.5) c = sp > 0.9975 ? vec3(1.0, 0.98, 0.88) : vec3(1.0, 0.86, 0.62);
// short white glints
vec2 cell = floor(p.xz / vec2(3.0, 1.2));
float gh = hash12(cell * 1.37 + floor(uTime * 2.5 + hash12(cell) * 13.0));
if (gh > 0.9985 && fp < 1.2 && lit > 0.5) c = vec3(1.0, 1.0, 1.0);
// animated shore foam
float jag = nz(p.xz * 0.35);
float sw = depth + jag * 1.2;
float wave = fract(sw * 0.5 - uTime * 0.22);
if (depth < 0.12 + jag * 0.22) c = vec3(0.92, 0.98, 0.95);
else if (sw < 3.4 && wave < 0.16 * (1.0 - sw / 3.4) + 0.02 && jag > 0.3) c = dpick(c, vec3(0.92, 0.98, 0.95), 0.85, B3);
// wake behind a low jet
for (int i = 0; i < 23; i++) {
if (i >= uWakeN - 1) break;
vec4 A = uWake[i], Bw = uWake[i + 1];
if (Bw.w <= 0.0 || A.w <= 0.0) continue;
vec2 ab = Bw.xy - A.xy, ap = p.xz - A.xy;
float L2 = dot(ab, ab);
if (L2 < 1e-3) continue;
float s = clamp(dot(ap, ab) / L2, 0.0, 1.0);
float dist = length(ap - ab * s);
float age = uTime - mix(A.z, Bw.z, s);
float str = mix(A.w, Bw.w, s) * (1.0 - smoothstep(4.0, 9.0, age));
float wdt = 1.6 + age * 3.2;
if (dist < wdt && str > 0.0) {
float edge = smoothstep(wdt * 0.62, wdt * 0.9, dist);
float churn = nz(p.xz * 1.4 + vec2(uTime * 3.0, 0.0));
float f = max(edge * 0.95, (1.0 - smoothstep(0.0, 3.0, age)) * step(0.45, churn));
if (f * str > 0.42 + (B1 - 0.5) * 0.3) c = vec3(0.92, 0.98, 0.95);
}
}
return c;
}
vec3 crownCol(int kind, float L) {
if (kind == 0) return tone3(vec3(0.09, 0.15, 0.16), vec3(0.16, 0.25, 0.22), vec3(0.27, 0.38, 0.30), L);
if (kind == 1) return tone3(vec3(0.52, 0.20, 0.24), vec3(0.85, 0.39, 0.16), vec3(0.97, 0.63, 0.25), L);
if (kind == 2) return tone3(vec3(0.38, 0.11, 0.20), vec3(0.72, 0.18, 0.19), vec3(0.91, 0.33, 0.24), L);
return tone3(vec3(0.52, 0.38, 0.22), vec3(0.82, 0.60, 0.17), vec3(0.97, 0.81, 0.30), L);
}
int hueKind(float h) { return h < 0.38 ? 1 : (h < 0.64 ? 2 : 3); }
vec3 shadeLand(vec3 p, vec4 T, vec3 n, float t, float lit, float fp) {
float h = T.r, ao = T.b, forest = T.a;
float slope = 1.0 - n.y;
float ndl = dot(n, uSun);
float L = smoothstep(-0.12, 0.42, ndl) * lit;
L *= mix(1.0, ao, 0.8);
L += (fract(h / 9.0) - 0.5) * 0.10 * smoothstep(0.04, 0.2, slope); // subtle terraces
float m1 = nz(p.xz * 0.08), m2 = nz2(p.xz * 0.5), m3 = nz(p.xz * 2.1);
float lodA = 1.0 - smoothstep(0.35, 1.2, fp), lodB = 1.0 - smoothstep(1.5, 5.0, fp);
m3 = mix(0.5, m3, lodA); m2 = mix(nz2(p.xz * 0.04), m2, lodB);
float region = texture(uNoise, p.xz / 256.0 * 0.0035).b;
vec3 col;
float snowLine = 205.0 + (m1 - 0.5) * 50.0;
float beach = 1.3 + m2 * 1.2;
if (h < beach && slope < 0.35) {
col = tone3(vec3(0.58, 0.50, 0.60), vec3(0.85, 0.74, 0.54), vec3(0.95, 0.87, 0.64), L + 0.15 - (m3 > 0.8 ? 0.2 : 0.0));
} else if (h > snowLine && slope < 0.42 + m3 * 0.1) {
col = tone3(vec3(0.62, 0.60, 0.80), vec3(0.88, 0.85, 0.93), vec3(1.0, 0.96, 0.94), L + 0.08);
} else if (slope > 0.30 + m2 * 0.12 || (h > 160.0 && slope > 0.17)) {
float strata = fract(h / 5.5 + m3 * 0.3);
col = tone3(vec3(0.30, 0.26, 0.40), vec3(0.48, 0.43, 0.56), vec3(0.68, 0.62, 0.70), L + (strata < 0.3 ? -0.12 : 0.0) + (m3 - 0.5) * 0.15);
} else {
// meadows: pale straw and faded green, mottled
float mv = m1 * 0.6 + m2 * 0.4;
bool straw = region + (m3 - 0.5) * 0.08 > 0.55;
vec3 ml = straw ? vec3(0.89, 0.77, 0.46) : vec3(0.84, 0.81, 0.53);
vec3 mm = straw ? vec3(0.74, 0.62, 0.35) : vec3(0.66, 0.67, 0.40);
vec3 md = vec3(0.40, 0.38, 0.47);
if (mv > 0.62) { ml = vec3(0.70, 0.73, 0.45); mm = vec3(0.53, 0.58, 0.36); }
col = tone3(md, mm, ml, L + (m3 - 0.5) * 0.12);
if (forest > 0.02) {
float pv = texture(uNoise, p.xz / 256.0 * 0.012).r * 0.7 + texture(uNoise, p.xz / 256.0 * 0.04).g * 0.3;
float conP = 0.12 + smoothstep(70.0, 170.0, h) * 0.55;
if (fp < 3.2) {
// individual crowns on a jittered grid
vec2 g = p.xz / 7.0, gi = floor(g);
float bestH = -1.0; vec3 crown = vec3(0.0); bool shad = false;
vec2 sOff = -normalize(uSun.xz);
for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {
vec2 c = gi + vec2(i, j);
vec4 r = hash42(c);
vec2 cpos = (c + 0.15 + 0.7 * r.xy) * 7.0;
float fd = ter(cpos).a;
if (r.w > fd * 1.1) continue;
bool con = fract(r.z * 7.13) < conP;
float rad = con ? 2.4 + 0.8 * r.z : 3.1 + 1.4 * r.z;
vec2 dd = p.xz - cpos;
float q = dot(dd, dd) / (rad * rad);
if (q < 1.0 && rad - sqrt(q) * rad > bestH) {
bestH = rad - sqrt(q) * rad;
vec3 cn = normalize(vec3(dd.x / rad, sqrt(1.0 - q) * 0.9 + 0.1, dd.y / rad));
float cl = smoothstep(-0.25, 0.55, dot(cn, uSun)) * lit;
float hue = fract(pv * 1.7 + (r.x > 0.9 ? 0.33 : 0.0) + (r.y > 0.95 ? 0.66 : 0.0));
crown = crownCol(con ? 0 : hueKind(hue), cl);
}
vec2 ds = dd - sOff * rad * 1.7;
if (dot(ds, ds) < rad * rad * 1.05) shad = true;
}
if (bestH >= 0.0) col = crown;
else {
vec3 fl = tone3(vec3(0.20, 0.15, 0.22), vec3(0.33, 0.24, 0.26), vec3(0.50, 0.38, 0.30), L * (shad ? 0.35 : 1.0));
col = dpick(col, fl, smoothstep(0.1, 0.4, forest), B3);
}
} else {
// far: mottled canopy
float hue = fract(pv * 1.7);
vec3 fc = crownCol(nz2(p.xz * 0.11) < conP * 0.7 ? 0 : hueKind(hue), L + (nz(p.xz * 0.15) - 0.5) * 0.3);
if (forest > 0.2 + (B2 - 0.5) * 0.3) col = fc;
}
}
}
return col;
}
vec3 shadeCloud(vec3 p, vec3 rd, float t) {
float e = 7.0;
float th = cth(p.xz);
float hx = cth(p.xz + vec2(e, 0.0)) - cth(p.xz - vec2(e, 0.0));
float hz = cth(p.xz + vec2(0.0, e)) - cth(p.xz - vec2(0.0, e));
vec3 n = normalize(vec3(-hx, 2.0 * e * 0.8, -hz));
bool under = p.y < CB + 0.6;
float ndl = dot(n, uSun);
float occ = 0.0;
for (int k = 1; k <= 3; k++) {
vec3 q = p + uSun * (float(k) * 14.0);
float tq = cth(q.xz);
if (q.y < CB + tq && q.y > CB) occ += 0.33;
}
float L = clamp(ndl * 1.25 + 0.42, 0.0, 1.0) * (1.0 - occ * 0.55) + (p.y - CB - 30.0) * 0.004;
float fs = dot(normalize(rd.xz), normalize(uSun.xz));
vec3 col;
if (under) {
float x = th + (B1 - 0.5) * 8.0;
col = x < 10.0 ? vec3(0.68, 0.56, 0.74) : (x < 30.0 ? vec3(0.55, 0.47, 0.68) : vec3(0.43, 0.37, 0.59));
if (th < 12.0 && fs > 0.3) col = vec3(0.97, 0.71, 0.55);
} else {
float x = L + (B1 - 0.5) * 0.14;
if (x > 0.84) col = vec3(1.0, 0.94, 0.86);
else if (x > 0.66) col = vec3(0.97, 0.78, 0.66);
else if (x > 0.46) col = vec3(0.86, 0.66, 0.72);
else if (x > 0.26) col = vec3(0.68, 0.56, 0.74);
else col = vec3(0.50, 0.43, 0.65);
// golden rim on thin edges when looking towards the sun
if (th < 9.0 && fs > 0.4) col = vec3(1.0, 0.86, 0.62);
}
if (jetShadow(p) < 0.5) col = under ? vec3(0.43, 0.37, 0.59) : vec3(0.50, 0.43, 0.65);
return col;
}
float depthOf(float t, vec3 rd) {
float z = max(t * dot(rd, uCamF), uNear);
float ndc = (uFar + uNear) / (uFar - uNear) - 2.0 * uFar * uNear / ((uFar - uNear) * z);
return clamp(ndc * 0.5 + 0.5, 0.0, 1.0);
}
void main() {
ivec2 ip = ivec2(gl_FragCoord.xy);
B1 = bayer4(ip); B2 = bayer4(ip.yx + ivec2(1, 2)); B3 = bayer4(ip + ivec2(2, 1));
vec2 ndc = gl_FragCoord.xy / uRes * 2.0 - 1.0;
vec3 rd = normalize(uCamF + ndc.x * uTan.x * uCamR + ndc.y * uTan.y * uCamU);
vec3 ro = uCamPos;
const float TMAX = 9000.0;
bool budget;
float tT = marchTerrain(ro, rd, TMAX, budget);
float tLim = tT > 0.0 ? tT : TMAX;
float tC = marchCloud(ro, rd, tLim);
if (tC > 0.0) {
// never let a cloud hide the jet, and fade clouds right next to the camera
vec3 toJet = uJetPos - ro; float dj = length(toJet);
float ang = acos(clamp(dot(rd, toJet / dj), -1.0, 1.0));
float hole = 0.035 + 26.0 / dj;
float k = (tC < dj) ? smoothstep(hole, hole * 0.45, ang) : 0.0;
float nearF = 1.0 - smoothstep(25.0, 85.0, tC);
if (max(k * 0.85, nearF) > B1) tC = -1.0;
}
float th0 = cth(ro.xz);
bool inCloud = ro.y > CB && ro.y < CB + th0;
vec3 col; float t; float depth = 1.0;
if (tC > 0.0 && (tT < 0.0 || tC < tT)) {
t = tC;
vec3 p = ro + rd * t;
col = shadeCloud(p, rd, t);
col = applyFog(col, rd, ro.y, t * 0.85, B2, B3);
depth = depthOf(t, rd);
} else if (tT > 0.0 && !budget) {
t = tT;
vec3 p = ro + rd * t;
vec4 T = ter(p.xz);
float fp = t * uPixAng;
float lit = T.g * cloudShadow(p) * jetShadow(p);
float lightAdd = 0.0;
if (T.r < 0.0) {
col = shadeWater(p, rd, t, -T.r, lit, fp);
col = impactGround(col, p, true, lightAdd);
} else {
float e = 5.0;
vec3 n = normalize(vec3(surf(p.xz - vec2(e, 0.0)) - surf(p.xz + vec2(e, 0.0)), 2.0 * e, surf(p.xz - vec2(0.0, e)) - surf(p.xz + vec2(0.0, e))));
col = shadeLand(p, T, n, t, lit, fp);
col = impactGround(col, p, false, lightAdd);
}
for (int i = 0; i < 4; i++) {
if (uMsl[i].w <= 0.0) continue;
lightAdd += uMsl[i].w * max(0.0, 1.0 - length(p - uMsl[i].xyz) / 45.0) * 0.8;
}
if (lightAdd > 0.0) col = dpick(col, lightAdd > 1.2 ? vec3(1.0, 0.80, 0.40) : vec3(0.97, 0.63, 0.25), clamp(lightAdd * 0.5, 0.0, 0.75), B2);
col = applyFog(col, rd, ro.y, t, B2, B3);
depth = depthOf(t, rd);
} else if (tT > 0.0 && budget) {
col = fogRamp(rd, B3);
t = tT; depth = depthOf(t, rd);
} else if (rd.y < 0.0) {
// beyond draw distance: hazy far ground, a shade apart from the sky so the horizon reads
col = fogRamp(rd, B3);
t = TMAX;
} else {
col = sky(rd, true);
t = TMAX;
}
if (inCloud) { col = dpick(col, B1 > 0.5 ? vec3(1.0, 0.94, 0.86) : vec3(0.86, 0.66, 0.72), 0.85, B2); depth = depthOf(2.0, rd); }
oCol = vec4(col, 0.0);
gl_FragDepth = depth;
}
`;
// ----- aircraft & missiles: flat-shaded, three tones ------------------------------------------
const VS_MESH = `#version 300 es
in vec3 aPos; in vec3 aNrm; in vec2 aMat;
uniform mat4 uVP; uniform mat4 uM; uniform vec4 uSlot; uniform float uGlow;
out vec3 vN; out vec3 vObj; out vec3 vW; flat out int vMat;
void main() {
vec3 p = aPos;
int slot = int(aMat.y + 0.5) - 1;
if (slot >= 0) {
float s = uSlot[slot];
if (s <= 0.0) p = vec3(0.0); else p.z -= (1.0 - s) * 3.5;
}
vec4 w = uM * vec4(p, 1.0);
vW = w.xyz; vObj = aPos;
vN = mat3(uM) * aNrm;
vMat = int(aMat.x + 0.5);
gl_Position = uVP * w;
}`;
const FS_MESH = GLSL_COMMON + `
in vec3 vN; in vec3 vObj; in vec3 vW; flat in int vMat;
uniform float uGlow; uniform float uMask;
out vec4 oCol;
float camo(vec3 q) {
float id = 0.0;
id += step(0.0, dot(q.xz, vec2(0.8, 0.6)) - 1.1);
id += 2.0 * step(0.0, dot(q.xz, vec2(-0.5, 0.86)) + 0.9);
id += 4.0 * step(0.0, dot(q.xz, vec2(0.95, -0.31)) + 2.4);
id += 8.0 * step(0.0, dot(q.xz, vec2(0.28, 0.96)) - 2.6);
id += 16.0 * step(0.0, dot(q.xz, vec2(-0.9, -0.44)) - 2.2);
id += 32.0 * step(0.0, q.z + q.x * 0.7 + 5.0);
return fract(sin(id * 12.9898 + 3.1) * 43758.5453);
}
void main() {
vec3 n = normalize(vN);
float L = dot(n, uSun) * 0.75 + 0.36 + n.y * 0.22;
int k = L > 0.66 ? 2 : (L > 0.30 ? 1 : 0);
vec3 d, m, l;
if (vMat == 0) {
if (camo(vObj) > 0.5) { d = vec3(0.33, 0.35, 0.42); m = vec3(0.55, 0.57, 0.63); l = vec3(0.79, 0.78, 0.78); }
else { d = vec3(0.22, 0.23, 0.30); m = vec3(0.38, 0.40, 0.48); l = vec3(0.56, 0.58, 0.64); }
} else if (vMat == 1) { d = vec3(0.10, 0.09, 0.13); m = vec3(0.16, 0.15, 0.19); l = vec3(0.30, 0.28, 0.33); }
else if (vMat == 2) { d = vec3(0.50, 0.33, 0.13); m = vec3(0.84, 0.60, 0.22); l = vec3(1.0, 0.88, 0.55); }
else if (vMat == 3) { d = vec3(0.47, 0.13, 0.16); m = vec3(0.78, 0.20, 0.18); l = vec3(0.95, 0.42, 0.30); }
else if (vMat == 4) { d = vec3(0.12, 0.11, 0.15); m = vec3(0.21, 0.20, 0.25); l = vec3(0.33, 0.32, 0.38); }
else if (vMat == 5) { vec3 g = mix(vec3(0.85, 0.30, 0.12), vec3(1.0, 0.90, 0.62), uGlow); d = g; m = g; l = g; }
else if (vMat == 6) { d = vec3(0.52, 0.52, 0.60); m = vec3(0.78, 0.78, 0.82); l = vec3(0.96, 0.94, 0.92); }
else { d = vec3(0.10, 0.09, 0.13); m = vec3(0.16, 0.15, 0.19); l = vec3(0.30, 0.28, 0.33); }
vec3 c = k == 2 ? l : (k == 1 ? m : d);
float t = length(vW - uCamPos);
vec3 rd = (vW - uCamPos) / t;
ivec2 ip = ivec2(gl_FragCoord.xy);
c = applyFog(c, rd, uCamPos.y, t, bayer4(ip.yx + ivec2(1, 2)), bayer4(ip + ivec2(2, 1)));
oCol = vec4(c, uMask);
}`;
// ----- sprites: discs, puffs, rings; dithered transparency ------------------------------------
const VS_SPRITE = `#version 300 es
in vec2 aC; in vec4 aP; in vec4 aCol; in vec4 aCore; in vec4 aK;
uniform mat4 uV, uP;
out vec2 vC; out vec4 vCol; out vec4 vCore; out vec4 vK; out vec3 vW;
void main() {
vec4 vc = uV * vec4(aP.xyz, 1.0);
float a = aK.y;
vec2 c = mat2(cos(a), sin(a), -sin(a), cos(a)) * aC;
vc.xy += c * aP.w;
vC = aC; vCol = aCol; vCore = aCore; vK = aK; vW = aP.xyz;
gl_Position = uP * vc;
}`;
const FS_SPRITE = GLSL_COMMON + `
in vec2 vC; in vec4 vCol; in vec4 vCore; in vec4 vK; in vec3 vW;
out vec4 oCol;
void main() {
float r = length(vC);
int type = int(vK.x + 0.5);
float bz = bayer4(ivec2(gl_FragCoord.xy));
float a = vCol.a;
vec3 c = vCol.rgb;
if (type == 0) { if (r > 1.0) discard; if (r < vCore.a) c = vCore.rgb; }
else if (type == 1) { if (r > 1.0) discard; a *= 1.0 - r * r * 0.55; float lit = dot(normalize(vec3(vC, 0.6)), vec3(-0.6, 0.55, 0.6)); c = lit > 0.25 ? vCore.rgb : c; }
else if (type == 2) { if (r > 1.0 || r < 1.0 - vCore.a) discard; }
else { if (!(abs(vC.x) < 0.22 || abs(vC.y) < 0.22) || r > 1.0) discard; if (r < 0.4) c = vCore.rgb; }
if (a < bz) discard;
float t = length(vW - uCamPos);
vec3 rd = (vW - uCamPos) / max(t, 1e-3);
ivec2 ip = ivec2(gl_FragCoord.xy);
if (fogAmt(uCamPos.y, rd, t) * vK.z > bayer4(ip.yx + ivec2(1, 2))) c = fogRamp(rd, bayer4(ip + ivec2(2, 1)));
oCol = vec4(c, 0.0);
}`;
// ----- thin lines (vapour trails) ----------------------------------------------------------------
const VS_LINE = `#version 300 es
in vec4 aP; uniform mat4 uVP; out float vA; out vec3 vW;
void main() { vA = aP.w; vW = aP.xyz; gl_Position = uVP * vec4(aP.xyz, 1.0); }`;
const FS_LINE = GLSL_COMMON + `
in float vA; in vec3 vW; out vec4 oCol;
void main() {
float bz = bayer4(ivec2(gl_FragCoord.xy));
if (vA < bz) discard;
float t = length(vW - uCamPos);
vec3 rd = (vW - uCamPos) / max(t, 1e-3);
ivec2 ip = ivec2(gl_FragCoord.xy);
vec3 c = applyFog(vec3(1.0, 0.98, 0.88), rd, uCamPos.y, t, bayer4(ip.yx + ivec2(1, 2)), bayer4(ip + ivec2(2, 1)));
oCol = vec4(c, 0.0);
}`;
// ----- post: outline + palette quantisation with ordered dithering --------------------------
const FS_POST = `#version 300 es
precision highp float;
precision highp int;
uniform sampler2D uScene;
uniform vec3 uPal[PALN];
uniform float uFlash;
uniform vec2 uRes;
out vec4 oCol;
float bayer4(ivec2 p) {
int i = (p.y & 3) * 4 + (p.x & 3);
const float m[16] = float[16](0.,8.,2.,10.,12.,4.,14.,6.,3.,11.,1.,9.,15.,7.,13.,5.);
return (m[i] + 0.5) / 16.0;
}
void main() {
ivec2 ip = ivec2(gl_FragCoord.xy);
ivec2 mx = ivec2(uRes) - 1;
vec4 c = texelFetch(uScene, ip, 0);
if (c.a < 0.5) {
float m = max(max(texelFetch(uScene, min(ip + ivec2(1, 0), mx), 0).a, texelFetch(uScene, max(ip - ivec2(1, 0), ivec2(0)), 0).a),
max(texelFetch(uScene, min(ip + ivec2(0, 1), mx), 0).a, texelFetch(uScene, max(ip - ivec2(0, 1), ivec2(0)), 0).a));
if (m > 0.5) { oCol = vec4(uPal[0], 1.0); return; }
}
vec3 col = c.rgb;
col = mix(col, vec3(1.0, 0.95, 0.85), uFlash);
const vec3 W = vec3(0.30, 0.50, 0.20);
float d1 = 1e9, d2 = 1e9; int i1 = 0, i2 = 0;
for (int i = 0; i < PALN; i++) {
vec3 e = (col - uPal[i]);
float d = dot(e * e, W);
if (d < d1) { d2 = d1; i2 = i1; d1 = d; i1 = i; }
else if (d < d2) { d2 = d; i2 = i; }
}
vec3 a = uPal[i1], b = uPal[i2];
if (d1 < 0.0003) { oCol = vec4(a, 1.0); return; }
vec3 ab = b - a;
float t = clamp(dot((col - a) * W, ab) / max(dot(ab * W, ab), 1e-6), 0.0, 1.0);
oCol = vec4(t > bayer4(ip) ? b : a, 1.0);
}`;
// ===== constants & small math ===============================================================
const TILE = 5120, TN = 1024, TEXEL = TILE / TN, CLOUD_N = 512;
const CB = 190, CT = 88, GRID_B = 16;
const SUN = norm3([-0.919, 0.208, 0.333]);
const JET_SUN = norm3([SUN[0], 0.62, SUN[2]]); // steeper light for the jet's own shadow so it stays in view
const D2R = Math.PI / 180;
function v3(x, y, z) { return [x, y, z]; }
function add3(a, b) { return [a[0] + b[0], a[1] + b[1], a[2] + b[2]]; }
function sub3(a, b) { return [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; }
function mul3(a, s) { return [a[0] * s, a[1] * s, a[2] * s]; }
function madd3(a, b, s) { return [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s]; }
function dot3(a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; }
function cross3(a, b) { return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]; }
function len3(a) { return Math.hypot(a[0], a[1], a[2]); }
function norm3(a) { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; }
function lerp3(a, b, t) { return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t]; }
const clamp = (x, a, b) => x < a ? a : x > b ? b : x;
const lerp = (a, b, t) => a + (b - a) * t;
const smooth = (t) => t * t * (3 - 2 * t);
const sstep = (a, b, x) => smooth(clamp((x - a) / (b - a), 0, 1));
const wrapPi = (a) => { while (a > Math.PI) a -= 2 * Math.PI; while (a < -Math.PI) a += 2 * Math.PI; return a; };
const rnd = (a, b) => a + Math.random() * (b - a);
const damp = (cur, target, rate, dt) => cur + (target - cur) * (1 - Math.exp(-rate * dt));
// quaternions [x,y,z,w]
function quatFromBasis(R, U, F) { // columns: right, up, back(-F) form a rotation matrix
const m00 = R[0], m01 = U[0], m02 = -F[0], m10 = R[1], m11 = U[1], m12 = -F[1], m20 = R[2], m21 = U[2], m22 = -F[2];
const tr = m00 + m11 + m22; let x, y, z, w;
if (tr > 0) { const s = Math.sqrt(tr + 1) * 2; w = 0.25 * s; x = (m21 - m12) / s; y = (m02 - m20) / s; z = (m10 - m01) / s; }
else if (m00 > m11 && m00 > m22) { const s = Math.sqrt(1 + m00 - m11 - m22) * 2; w = (m21 - m12) / s; x = 0.25 * s; y = (m01 + m10) / s; z = (m02 + m20) / s; }
else if (m11 > m22) { const s = Math.sqrt(1 + m11 - m00 - m22) * 2; w = (m02 - m20) / s; x = (m01 + m10) / s; y = 0.25 * s; z = (m12 + m21) / s; }
else { const s = Math.sqrt(1 + m22 - m00 - m11) * 2; w = (m10 - m01) / s; x = (m02 + m20) / s; y = (m12 + m21) / s; z = 0.25 * s; }
return [x, y, z, w];
}
function quatSlerp(a, b, t) {
let d = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
let bb = b; if (d < 0) { d = -d; bb = [-b[0], -b[1], -b[2], -b[3]]; }
if (d > 0.9995) { const r = [a[0] + (bb[0] - a[0]) * t, a[1] + (bb[1] - a[1]) * t, a[2] + (bb[2] - a[2]) * t, a[3] + (bb[3] - a[3]) * t]; const l = Math.hypot(...r); return r.map(v => v / l); }
const th = Math.acos(d), s = Math.sin(th), wa = Math.sin((1 - t) * th) / s, wb = Math.sin(t * th) / s;
return [a[0] * wa + bb[0] * wb, a[1] * wa + bb[1] * wb, a[2] * wa + bb[2] * wb, a[3] * wa + bb[3] * wb];
}
function quatBasis(q) {
const [x, y, z, w] = q;
const R = [1 - 2 * (y * y + z * z), 2 * (x * y + z * w), 2 * (x * z - y * w)];
const U = [2 * (x * y - z * w), 1 - 2 * (x * x + z * z), 2 * (y * z + x * w)];
const B = [2 * (x * z + y * w), 2 * (y * z - x * w), 1 - 2 * (x * x + y * y)];
return { R, U, F: [-B[0], -B[1], -B[2]] };
}
// rotate vector v around unit axis k by angle a
function rotAxis(v, k, a) {
const c = Math.cos(a), s = Math.sin(a), kv = cross3(k, v), kd = dot3(k, v) * (1 - c);
return [v[0] * c + kv[0] * s + k[0] * kd, v[1] * c + kv[1] * s + k[1] * kd, v[2] * c + kv[2] * s + k[2] * kd];
}
function viewMat(pos, R, U, F) {
return new Float32Array([
R[0], U[0], -F[0], 0, R[1], U[1], -F[1], 0, R[2], U[2], -F[2], 0,
-dot3(R, pos), -dot3(U, pos), dot3(F, pos), 1]);
}
function projMat(tanX, tanY, n, f) {
return new Float32Array([1 / tanX, 0, 0, 0, 0, 1 / tanY, 0, 0, 0, 0, -(f + n) / (f - n), -1, 0, 0, -2 * f * n / (f - n), 0]);
}
function mulMat(a, b) { // a*b column-major
const o = new Float32Array(16);
for (let c = 0; c < 4; c++) for (let r = 0; r < 4; r++) {
let s = 0; for (let k = 0; k < 4; k++) s += a[k * 4 + r] * b[c * 4 + k]; o[c * 4 + r] = s;
}
return o;
}
function modelMat(pos, R, U, F) { // object x→R, y→U, z(nose)→F
return new Float32Array([R[0], R[1], R[2], 0, U[0], U[1], U[2], 0, F[0], F[1], F[2], 0, pos[0], pos[1], pos[2], 1]);
}
// ===== terrain on the CPU (flight, missiles, radar) ==========================================
const World = {
H: null, // Float32Array TN*TN heights
ready: false,
maxH: 0,
h(x, z) { // bilinear, wrapped; raw height (negative = lake bed)
const H = this.H; if (!H) return 0;
const gx = x / TEXEL, gz = z / TEXEL;
const xf = Math.floor(gx), zf = Math.floor(gz), fx = gx - xf, fz = gz - zf, m = TN - 1;
const x0 = xf & m, x1 = (xf + 1) & m, z0 = zf & m, z1 = (zf + 1) & m;
const a = H[z0 * TN + x0], b = H[z0 * TN + x1], c = H[z1 * TN + x0], d = H[z1 * TN + x1];
return (a + (b - a) * fx) * (1 - fz) + (c + (d - c) * fx) * fz;
},
surf(x, z) { const h = this.h(x, z); return h > 0 ? h : 0; },
// march a ray against the surface; returns distance or -1
ray(o, d, maxT, step) {
step = step || 8; let tp = 0;
for (let t = step; t <= maxT; t += step) {
const y = o[1] + d[1] * t, s = this.surf(o[0] + d[0] * t, o[2] + d[2] * t);
if (y < s) { let a = tp, b = t; for (let k = 0; k < 8; k++) { const m = (a + b) / 2; if (o[1] + d[1] * m < this.surf(o[0] + d[0] * m, o[2] + d[2] * m)) b = m; else a = m; } return b; }
tp = t;
}
return -1;
}
};
// ===== world generation with a worker pool ===================================================
function generateWorld(onProgress) {
return new Promise((resolve) => {
const total = TN * 1.0 + CLOUD_N * 0.35 + TN * 1.6;
let done = 0;
const bump = (job, n) => { done += n * (job === 'height' ? 1 : job === 'cloud' ? 0.35 : 1.6); onProgress(Math.min(1, done / total), job); };
let nW = Math.max(2, Math.min(8, (navigator.hardwareConcurrency || 4) - 1));
let workers = [];
let useWorkers = true;
try {
const src = '(' + GEN_WORKER.toString() + ')(self);';
const url = URL.createObjectURL(new Blob([src], { type: 'application/javascript' }));
for (let i = 0; i < nW; i++) workers.push(new Worker(url));
} catch (e) { useWorkers = false; workers = []; }
// main-thread fallback: same code, chunked so the progress bar still moves
let fake = null, fakeHandler = null;
if (!useWorkers) { fake = { postMessage: (m) => fakeHandler && fakeHandler(m) }; GEN_WORKER(fake); }
function runJobs(jobs, onDone) {
let pending = jobs.length;
if (useWorkers) {
let qi = 0;
const feed = (w) => {
if (qi >= jobs.length) return;
const job = jobs[qi++];
w.onmessage = (e) => {
const m = e.data;
if (m.type === 'progress') bump(m.job, m.n);
else if (m.type === 'done') { onDone(m); feed(w); if (--pending === 0) finish(); }
};
w.onerror = (err) => { console.error('worker error', err.message); };
w.postMessage(job);
};
let finish = () => {};
const p = new Promise((r) => { finish = r; });
workers.forEach(feed);
return p;
}
return new Promise((res) => {
let i = 0;
const step = () => {
if (i >= jobs.length) { res(); return; }
const job = jobs[i++];
fakeHandler = (m) => { if (m.type === 'progress') bump(m.job, m.n); else if (m.type === 'done') onDone(m); };
fake.onmessage({ data: job });
setTimeout(step, 0);
};
step();
});
}
const H = new Float32Array(TN * TN);
const C = new Uint8Array(CLOUD_N * CLOUD_N);
const chunk = useWorkers ? 64 : 16;
const jobsA = [];
for (let y = 0; y < TN; y += chunk) jobsA.push({ job: 'height', y0: y, y1: Math.min(TN, y + chunk), N: TN });
for (let y = 0; y < CLOUD_N; y += 128) jobsA.push({ job: 'cloud', y0: y, y1: Math.min(CLOUD_N, y + 128), N: CLOUD_N });
runJobs(jobsA, (m) => {
if (m.job === 'height') H.set(m.data, m.y0 * TN);
else C.set(m.data, m.y0 * CLOUD_N);
}).then(() => {
// block-max grid (dilated by one block) for fast skipping in the ray marcher and the shadow baker
const GN = TN / GRID_B, M0 = new Float32Array(GN * GN).fill(-1e9);
let maxH = 0;
for (let y = 0; y < TN; y++) for (let x = 0; x < TN; x++) {
const h = Math.max(H[y * TN + x], 0), i = ((y / GRID_B) | 0) * GN + ((x / GRID_B) | 0);
if (h > M0[i]) M0[i] = h; if (h > maxH) maxH = h;
}
const MG = new Float32Array(GN * GN);
for (let y = 0; y < GN; y++) for (let x = 0; x < GN; x++) {
let m = -1e9;
for (let j = -1; j <= 1; j++) for (let i = -1; i <= 1; i++) m = Math.max(m, M0[((y + j + GN) % GN) * GN + (x + i + GN) % GN]);
MG[y * GN + x] = m + 1.0;
}
const B = new Float32Array(TN * TN * 4);
const jobsB = [];
const bchunk = useWorkers ? 32 : 8;
const setH = { job: 'setH', N: TN, height: H, maxGrid: MG, gridBlock: GRID_B, texel: TEXEL, sun: SUN, maxH: maxH + 1 };
if (useWorkers) workers.forEach(w => w.postMessage(setH)); else fake.onmessage({ data: setH });
for (let y = 0; y < TN; y += bchunk) jobsB.push({ job: 'bake', y0: y, y1: Math.min(TN, y + bchunk) });
runJobs(jobsB, (m) => B.set(m.data, m.y0 * TN * 4)).then(() => {
workers.forEach(w => w.terminate());
World.H = H; World.maxH = maxH; World.ready = true;
// coarse level: 4x4 blocks, dilated again
const CN = GN / 4, C0 = new Float32Array(CN * CN).fill(-1e9), MG2 = new Float32Array(CN * CN);
for (let y = 0; y < GN; y++) for (let x = 0; x < GN; x++) { const i = ((y / 4) | 0) * CN + ((x / 4) | 0); C0[i] = Math.max(C0[i], M0[y * GN + x]); }
for (let y = 0; y < CN; y++) for (let x = 0; x < CN; x++) {
let m = -1e9;
for (let j = -1; j <= 1; j++) for (let i = -1; i <= 1; i++) m = Math.max(m, C0[((y + j + CN) % CN) * CN + (x + i + CN) % CN]);
MG2[y * CN + x] = m + 1.0;
}
resolve({ bake: B, grid: MG, gridN: GN, grid2: MG2, grid2N: CN, cloud: C });
});
});
});
}
// ===== low-poly meshes built in code ===========================================================
// Materials: 0 camo, 1 radome, 2 canopy, 3 red cap, 4 nozzle metal, 5 nozzle glow, 6 missile white, 7 dark
function MeshBuilder() {
const P = [], N = [], A = [];
function tri(a, b, c, mat, slot, hint) {
let n = norm3(cross3(sub3(b, a), sub3(c, a)));
if (hint) {
const cen = [(a[0] + b[0] + c[0]) / 3, (a[1] + b[1] + c[1]) / 3, (a[2] + b[2] + c[2]) / 3];
if (dot3(n, sub3(cen, hint)) < 0) n = mul3(n, -1);
}
for (const v of [a, b, c]) { P.push(v[0], v[1], v[2]); N.push(n[0], n[1], n[2]); A.push(mat, slot || 0); }
}
function quad(a, b, c, d, mat, slot, hint) { tri(a, b, c, mat, slot, hint); tri(a, c, d, mat, slot, hint); }
return { tri, quad, P, N, A, count: () => P.length / 3 };
}
function buildJet() {
const mb = MeshBuilder();
const { tri, quad } = mb;
// ---- fuselage from cross-sections
const S = [
// z, w, top, bottom, y
[8.5, 0.30, 0.27, 0.25, 0.05],
[7.0, 0.56, 0.50, 0.44, 0.05],
[5.6, 0.72, 0.62, 0.54, 0.05],
[3.6, 0.86, 0.70, 0.60, 0.05],
[1.6, 1.36, 0.70, 0.62, 0.0],
[-1.0, 1.74, 0.66, 0.66, 0.0],
[-4.6, 1.84, 0.62, 0.62, 0.0],
[-7.1, 1.70, 0.55, 0.58, 0.0],
[-8.1, 1.52, 0.48, 0.50, 0.0],
];
const K = 8;
const ring = (s) => {
const pts = [];
for (let i = 0; i < K; i++) {
const a = (i + 0.5) / K * Math.PI * 2, c = Math.cos(a), sn = Math.sin(a);
const x = s[1] * Math.sign(c) * Math.pow(Math.abs(c), 0.75);
const y = (sn > 0 ? s[2] : s[3]) * Math.sign(sn) * Math.pow(Math.abs(sn), 0.75) + s[4];
pts.push([x, y, s[0]]);
}
return pts;
};
const tip = [0, 0.05, 9.7];
let prev = ring(S[0]);
for (let i = 0; i < K; i++) tri(tip, prev[i], prev[(i + 1) % K], 1, 0, [0, 0.05, 9.0]);
for (let s = 1; s < S.length; s++) {
const cur = ring(S[s]);
const mat = s === 1 ? 1 : 0;
const hint = [0, (S[s][4] + S[s - 1][4]) / 2, (S[s][0] + S[s - 1][0]) / 2];
for (let i = 0; i < K; i++) quad(prev[i], prev[(i + 1) % K], cur[(i + 1) % K], cur[i], mat, 0, hint);
prev = cur;
}
// tail cap
const tc = [0, 0, -8.1];
for (let i = 0; i < K; i++) tri(tc, prev[i], prev[(i + 1) % K], 4, 0, [0, 0, -7.0]);
// ---- twin nozzles
for (const sx of [-1, 1]) {
const cx = sx * 0.66, R = 0.5, z0 = -7.9, z1 = -8.95, M = 8;
const ringA = [], ringB = [], ringC = [];
for (let i = 0; i < M; i++) {
const a = (i + 0.5) / M * Math.PI * 2;
ringA.push([cx + Math.cos(a) * R, Math.sin(a) * R, z0]);
ringB.push([cx + Math.cos(a) * R * 0.92, Math.sin(a) * R * 0.92, z1]);
ringC.push([cx + Math.cos(a) * R * 0.66, Math.sin(a) * R * 0.66, z1 - 0.02]);
}
for (let i = 0; i < M; i++) {
const j = (i + 1) % M;
quad(ringA[i], ringA[j], ringB[j], ringB[i], 4, 0, [cx, 0, (z0 + z1) / 2]);
quad(ringB[i], ringB[j], ringC[j], ringC[i], 4, 0, [cx, 0, z1 + 0.5]);
tri([cx, 0, z1 - 0.03], ringC[i], ringC[j], 5, 0, [cx, 0, z1 + 1]);
}
}
// ---- canopy bubble
{
const zs = [6.1, 5.4, 4.6, 3.8, 3.0, 2.3];
const base = 0.66, Wc = 0.52, Hc = 0.62;
const rings = zs.map((z) => {
const u = (z - 2.3) / (6.1 - 2.3);
const prof = Math.pow(Math.sin(Math.PI * Math.min(1, Math.max(0, u * 0.85 + 0.08))), 0.55);
const pts = [];
for (let i = 0; i <= 4; i++) {
const a = Math.PI * i / 4;
pts.push([Math.cos(a) * Wc * Math.max(0.25, prof), base + Math.sin(a) * Hc * prof, z]);
}
return pts;
});
for (let r = 0; r < rings.length - 1; r++)
for (let i = 0; i < 4; i++) quad(rings[r][i], rings[r][i + 1], rings[r + 1][i + 1], rings[r + 1][i], 2, 0, [0, base, (zs[r] + zs[r + 1]) / 2]);
const f = rings[0], b = rings[rings.length - 1];
for (let i = 0; i < 4; i++) { tri([0, base, zs[0] + 0.15], f[i], f[i + 1], 2, 0, [0, base + 0.1, 5.0]); tri([0, base, zs[5] - 0.2], b[i], b[i + 1], 2, 0, [0, base + 0.1, 3.0]); }
}
// ---- intakes (dark mouths)
for (const sx of [-1, 1]) {
const z = 1.75;
quad([sx * 0.86, -0.52, z], [sx * 1.4, -0.52, z], [sx * 1.4, 0.16, z], [sx * 0.86, 0.16, z], 7, 0, [sx * 1.1, 0, z - 1]);
}
// ---- cropped delta wings
for (const sx of [-1, 1]) {
const wy = -0.05;
const rLE = [sx * 1.0, wy, 2.4], tLE = [sx * 6.9, wy - 0.16, -3.7], tTE = [sx * 6.9, wy - 0.16, -6.1], rTE = [sx * 1.0, wy, -6.1];
const up = (p, t) => [p[0], p[1] + t, p[2]];
const tr = 0.13, tt = 0.04;
const a1 = up(rLE, tr), a2 = up(tLE, tt), a3 = up(tTE, tt), a4 = up(rTE, tr);
const b1 = up(rLE, -tr), b2 = up(tLE, -tt), b3 = up(tTE, -tt), b4 = up(rTE, -tr);
const mid = [sx * 3.5, wy - 0.07, -3.3];
quad(a1, a2, a3, a4, 0, 0, [mid[0], mid[1] - 1, mid[2]]);
quad(b1, b2, b3, b4, 0, 0, [mid[0], mid[1] + 1, mid[2]]);
quad(a1, a2, b2, b1, 0, 0, mid);
quad(a3, a4, b4, b3, 0, 0, mid);
quad(a2, a3, b3, b2, 0, 0, mid);
// pylons
for (const px of [3.0, 4.8]) {
const zc = px < 4 ? -1.6 : -2.9;
quad([sx * px, wy - 0.1, zc + 0.8], [sx * px, wy - 0.1, zc - 0.9], [sx * px, wy - 0.33, zc - 0.7], [sx * px, wy - 0.33, zc + 0.6], 7, 0, [sx * (px + 1), wy - 0.2, zc]);
}
}
// ---- horizontal stabilisers
for (const sx of [-1, 1]) {
const y = 0.02;
const r1 = [sx * 1.6, y, -6.0], r2 = [sx * 1.6, y, -8.7], t2 = [sx * 4.2, y - 0.05, -8.7], t1 = [sx * 4.2, y - 0.05, -7.5];
const a = [r1, t1, t2, r2].map(p => [p[0], p[1] + 0.05, p[2]]), b = [r1, t1, t2, r2].map(p => [p[0], p[1] - 0.05, p[2]]);
const m = [sx * 2.9, y, -7.8];
quad(a[0], a[1], a[2], a[3], 0, 0, [m[0], m[1] - 1, m[2]]);
quad(b[0], b[1], b[2], b[3], 0, 0, [m[0], m[1] + 1, m[2]]);
quad(a[0], a[1], b[1], b[0], 0, 0, m); quad(a[1], a[2], b[2], b[1], 0, 0, m);
}
// ---- canted twin fins with red caps
for (const sx of [-1, 1]) {
const cant = 22 * D2R;
const dirU = [sx * Math.sin(cant), Math.cos(cant), 0], dirN = [sx * Math.cos(cant), -Math.sin(cant), 0];
const base = (z) => [sx * 1.25, 0.5, z];
const Hf = 3.1, capH = 2.6;
const zLE = (h) => lerp(-3.4, -6.3, h / Hf), zTE = (h) => lerp(-7.9, -8.2, h / Hf);
const pt = (h, z, side) => { const b = base(z); return [b[0] + dirU[0] * h + dirN[0] * side, b[1] + dirU[1] * h + dirN[1] * side, z]; };
const th = 0.07;
const part = (h0, h1, mat) => {
const A0 = pt(h0, zLE(h0), th), A1 = pt(h1, zLE(h1), th), A2 = pt(h1, zTE(h1), th), A3 = pt(h0, zTE(h0), th);
const B0 = pt(h0, zLE(h0), -th), B1 = pt(h1, zLE(h1), -th), B2 = pt(h1, zTE(h1), -th), B3 = pt(h0, zTE(h0), -th);
const c = pt((h0 + h1) / 2, (zLE((h0 + h1) / 2) + zTE((h0 + h1) / 2)) / 2, 0);
quad(A0, A1, A2, A3, mat, 0, sub3(c, dirN));
quad(B0, B1, B2, B3, mat, 0, add3(c, dirN));
quad(A0, A1, B1, B0, mat, 0, c); quad(A2, A3, B3, B2, mat, 0, c);
if (h1 >= Hf) quad(A1, A2, B2, B1, mat, 0, c);
};
part(0, capH, 0); part(capH, Hf, 3);
}
// ---- four missiles on rails (slots 1..4)
const rails = jetRails();
rails.forEach((r, i) => addMissile(mb, r, i + 1));
return mb;
}
function jetRails() { // missile centres in jet space: right inner, left inner, right outer, left outer
return [[3.0, -0.52, -1.5], [-3.0, -0.52, -1.5], [4.8, -0.52, -2.8], [-4.8, -0.52, -2.8]];
}
function addMissile(mb, c, slot) {
const L = 3.1, R = 0.15, z0 = c[2] - L / 2, z1 = c[2] + L / 2 - 0.5;
const ring = (z, r) => [0, 1, 2, 3].map(i => { const a = i * Math.PI / 2 + Math.PI / 4; return [c[0] + Math.cos(a) * r, c[1] + Math.sin(a) * r, z]; });
const A = ring(z0, R), B = ring(z1, R);
const hint = (z) => [c[0], c[1], z];
for (let i = 0; i < 4; i++) {
const j = (i + 1) % 4;
mb.quad(A[i], A[j], B[j], B[i], 6, slot, hint((z0 + z1) / 2));
mb.tri([c[0], c[1], z1 + 0.5], B[i], B[j], i % 2 ? 7 : 6, slot, hint(z1));
mb.tri([c[0], c[1], z0], A[i], A[j], 4, slot, hint(z0 + 1));
}
for (let i = 0; i < 4; i++) {
const a = i * Math.PI / 2, d = [Math.cos(a), Math.sin(a), 0];
const p0 = [c[0] + d[0] * R, c[1] + d[1] * R, z0 + 0.05], p1 = [c[0] + d[0] * 0.42, c[1] + d[1] * 0.42, z0 + 0.05], p2 = [c[0] + d[0] * R, c[1] + d[1] * R, z0 + 0.55];
mb.tri(p0, p1, p2, 6, slot, null);
}
}
function buildMissile() { const mb = MeshBuilder(); addMissile(mb, [0, 0, 0], 0); return mb; }
// ===== the aircraft: arcade flight ===========================================================
const G0 = 9.81;
const CRUISE_ALT = 335;
const Jet = {
pos: [2600, CRUISE_ALT, 2600], psi: 1.2, gamma: 0, psiDot: 0, gammaDot: 0, bank: 0, V: 92,
vel: [0, 0, 0], F: [0, 0, -1], R: [1, 0, 0], U: [0, 1, 0], rpos: [0, 0, 0],
ab: false, abAmt: 0, throttle: 0.72, rollT: -1, rollDir: 1, rollAng: 0,
G: 1, ball: 0, Gmax: 1, agl: 300, pullUp: false, pullUpT: 0, autopilot: true,
fuel: 0.86, rpm: [84, 84], egt: [610, 610], hidden: false, time: 0, dist: 0,
lastAccel: [0, 0, 0],
};
function jetDir(psi, gamma) { const cg = Math.cos(gamma); return [Math.sin(psi) * cg, Math.sin(gamma), -Math.cos(psi) * cg]; }
function jetBarrelRoll() {
if (Jet.rollT >= 0) return;
Jet.rollT = 0;
Jet.rollDir = Jet.psiDot > 0.02 ? 1 : Jet.psiDot < -0.02 ? -1 : (Math.random() < 0.5 ? -1 : 1);
Audio_.whoosh();
}
function updateJet(dt, ctl) {
const J = Jet;
J.time += dt;
let turnCmd = ctl.turn, gammaCmd = ctl.gamma;
// ---- terrain look-ahead: never let the jet hit anything
const look = [0.35, 0.7, 1.1, 1.6, 2.2, 2.9, 3.7];
let gFloor = -1.4, urgent = 0;
const hd = Math.sin(J.psi), hz = -Math.cos(J.psi);
for (const s of look) {
const d = J.V * s * Math.cos(J.gamma);
const a = J.psi + J.psiDot * s * 0.5;
const x = J.pos[0] + Math.sin(a) * d, z = J.pos[2] - Math.cos(a) * d;
const h = World.h(x, z), sh = h > 0 ? h : 0;
const clear = h < 0 ? 6 : 24;
const need = sh + clear - J.pos[1];
// the further away the obstacle, the more time there is to pull up later
const g = Math.atan2(need, Math.max(d, 1)) - 0.55 * Math.max(0, s - 0.45);
if (g > gFloor) gFloor = g;
if (need > 0 && s < 1.2) urgent = Math.max(urgent, need / 30);
}
J.pullUp = gammaCmd < gFloor - 0.02 && gFloor > J.gamma - 0.05;
if (J.pullUp) J.pullUpT = 0.8; else J.pullUpT = Math.max(0, J.pullUpT - dt);
if (gammaCmd < gFloor) gammaCmd = gFloor;
if (J.pos[1] > 1500) gammaCmd = Math.min(gammaCmd, (1500 - J.pos[1]) / 250);
gammaCmd = clamp(gammaCmd, -1.15, 1.15);
// ---- dynamics
J.psiDot = damp(J.psiDot, turnCmd, 2.6, dt);
const gdT = clamp((gammaCmd - J.gamma) * 2.2, -0.75, 0.75 + urgent);
J.gammaDot = damp(J.gammaDot, gdT, 5 + urgent * 6, dt);
J.gamma = clamp(J.gamma + J.gammaDot * dt, -1.2, 1.25);
J.psi = wrapPi(J.psi + J.psiDot * dt);
// speed
J.abAmt = damp(J.abAmt, J.ab ? 1 : 0, J.ab ? 5 : 3, dt);
J.throttle = damp(J.throttle, J.ab ? 1 : 0.72, 7, dt);
const Vt = lerp(92, 178, J.abAmt) - Math.sin(J.gamma) * 46;
const V0 = J.V;
J.V = damp(J.V, Vt, J.ab ? 0.55 : 0.32, dt);
const dV = (J.V - V0) / Math.max(dt, 1e-4);
J.vel = mul3(jetDir(J.psi, J.gamma), J.V);
J.pos = madd3(J.pos, J.vel, dt);
J.dist += J.V * dt;
// hard floor: physical guarantee
const hh = World.h(J.pos[0], J.pos[2]);
const floor = (hh > 0 ? hh : 0) + (hh < 0 ? 3.0 : 7.0);
if (J.pos[1] < floor) { J.pos[1] = floor; if (J.gamma < 0) { J.gamma = 0.02; J.gammaDot = Math.max(J.gammaDot, 0); } }
J.agl = J.pos[1] - (hh > 0 ? hh : 0);
// ---- attitude: coordinated bank from the turn
const cg = Math.cos(J.gamma);
const bankT = Math.atan2(J.V * J.psiDot * cg, Math.max(3, G0 * cg + J.V * J.gammaDot));
J.bank = damp(J.bank, clamp(bankT, -1.45, 1.45), 6, dt);
const F = jetDir(J.psi, J.gamma);
let R0 = norm3(cross3(F, [0, 1, 0]));
let U0 = cross3(R0, F);
let U = rotAxis(U0, F, J.bank), R = cross3(F, U);
// barrel roll
let off = [0, 0, 0];
if (J.rollT >= 0) {
J.rollT += dt;
const D = 1.15, u = Math.min(1, J.rollT / D);
const e = u * u * (3 - 2 * u);
J.rollAng = J.rollDir * Math.PI * 2 * e;
const rr = 3.6 * Math.sin(Math.PI * u);
off = add3(mul3(U0, rr * (1 - Math.cos(J.rollAng)) * 0.5), mul3(R0, -J.rollDir * rr * Math.sin(J.rollAng) * 0.5));
U = rotAxis(U, F, J.rollAng); R = cross3(F, U);
if (u >= 1) { J.rollT = -1; J.rollAng = 0; }
}
// tiny angle of attack under load
const alpha = clamp(0.012 * J.G, -0.03, 0.09);
const Fb = rotAxis(F, R, alpha), Ub = rotAxis(U, R, alpha);
J.F = Fb; J.U = Ub; J.R = cross3(Fb, Ub);
J.rpos = add3(J.pos, off);
// ---- instruments: specific force in body axes
const right = R0, upv = U0;
const aTurn = mul3(right, J.V * J.psiDot * cg);
const aPitch = mul3(upv, J.V * J.gammaDot);
const acc = add3(add3(aTurn, aPitch), mul3(F, dV));
const f = add3(acc, [0, G0, 0]);
J.G = damp(J.G, dot3(f, J.U) / G0, 10, dt);
J.ball = damp(J.ball, dot3(f, J.R) / G0, 8, dt);
J.Gmax = Math.max(J.Gmax, J.G);
// engines & fuel
const rpmT = lerp(84, 102, J.abAmt) + Math.sin(J.time * 0.7) * 0.3;
J.rpm[0] = damp(J.rpm[0], rpmT, 1.6, dt); J.rpm[1] = damp(J.rpm[1], rpmT + 0.6, 1.4, dt);
const egtT = lerp(615, 905, J.abAmt);
J.egt[0] = damp(J.egt[0], egtT, 0.9, dt); J.egt[1] = damp(J.egt[1], egtT - 8, 0.8, dt);
J.fuel = Math.max(0.05, J.fuel - dt * (0.00011 + 0.0007 * J.abAmt));
}
// ===== input ===================================================================================
const Input = {
mx: 0, my: 0, has: false, moved: false, left: false, leftT: 0, right: false,
touchSteer: null, touchAB: false, touchMode: false, started: false,
};
// ===== cameras ===================================================================================
const VIEWS = ['TOP', 'CHASE', 'COCKPIT'];
const FOVS = [38, 56, 64];
const Cam = {
view: 0, from: null, blend: 1, chaseDir: null, shake: 0,
pos: [0, 0, 0], R: [1, 0, 0], U: [0, 1, 0], F: [0, 0, -1], fovY: 38, tanX: 1, tanY: 1, aspect: 1.7,
cockpitW: 0, topW: 1,
};
function camForView(v) {
const J = Jet;
if (v === 0) {
const lead = [J.vel[0] * 0.2, 0, J.vel[2] * 0.2];
const pos = add3(J.pos, [lead[0], 255, lead[2]]);
return { pos, q: quatFromBasis([1, 0, 0], [0, 0, -1], [0, -1, 0]), fov: FOVS[0], look: [pos[0], J.pos[1], pos[2]] };
}
if (v === 1) {
const cd = Cam.chaseDir || J.F;
const pos = add3(madd3(J.pos, cd, -37), [0, 13, 0]);
const F = norm3(sub3(madd3(J.pos, cd, 110), pos));
let U0 = norm3(sub3([0, 1, 0], mul3(F, F[1])));
const roll = J.bank * 0.32;
const U = rotAxis(U0, F, roll), R = cross3(F, U);
return { pos, q: quatFromBasis(R, U, F), fov: FOVS[1], look: madd3(J.pos, cd, 110) };
}
const pos = add3(add3(J.rpos, mul3(J.U, 1.32)), mul3(J.F, 4.3));
return { pos, q: quatFromBasis(J.R, J.U, J.F), fov: FOVS[2], look: madd3(pos, J.F, 60) };
}
function setView(v, instant) {
if (instant) { Cam.view = v; Cam.blend = 1; Cam.from = null; return; }
// freeze current blended camera relative to the jet as the start of the new blend
Cam.from = { rel: sub3(Cam.pos, Jet.pos), relLook: sub3(madd3(Cam.pos, Cam.F, 120), Jet.pos), q: quatFromBasis(Cam.R, Cam.U, Cam.F), fov: Cam.fovY, view: Cam.view, wasBlend: Cam.blend };
Cam.view = v; Cam.blend = 0;
}
function updateCamera(dt) {
const J = Jet;
if (!Cam.chaseDir) Cam.chaseDir = J.F.slice();
Cam.chaseDir = norm3(lerp3(Cam.chaseDir, norm3([J.vel[0], J.vel[1] * 0.6, J.vel[2]]), 1 - Math.exp(-3.2 * dt)));
Cam.blend = Math.min(1, Cam.blend + dt / 1.05);
const to = camForView(Cam.view);
let pos = to.pos, q = to.q, fov = to.fov;
let blendLook = null;
if (Cam.blend < 1 && Cam.from) {
const e = smooth(smooth(Cam.blend));
let fp = add3(Jet.pos, Cam.from.rel), fl = add3(Jet.pos, Cam.from.relLook), fq = Cam.from.q, ff = Cam.from.fov;
if (Cam.from.wasBlend >= 1) { const c = camForView(Cam.from.view); fp = c.pos; fq = c.q; ff = c.fov; fl = c.look; }
pos = lerp3(fp, pos, e);
// lift the path a little when swooping between views so it never clips the jet
const arc = Math.sin(Math.PI * e) * (Cam.view === 2 || Cam.from.view === 2 ? 4 : 18);
pos = add3(pos, [0, arc, 0]);
q = quatSlerp(fq, q, e);
fov = lerp(ff, fov, e);
// keep the jet in frame: interpolate where it sits on screen between the two views
if (Cam.view !== 2 && Cam.from.view !== 2) {
const ang = (cp, cb) => { const d = sub3(Jet.rpos, cp), z = Math.max(dot3(d, cb.F), 1e-3); return [Math.atan2(dot3(d, cb.R), z), Math.atan2(dot3(d, cb.U), z)]; };
const a0 = ang(fp, quatBasis(fq)), a1 = ang(to.pos, quatBasis(to.q));
blendLook = [lerp(a0[0], a1[0], e), lerp(a0[1], a1[1], e)];
}
}
const b = quatBasis(q);
if (blendLook) {
const F = norm3(sub3(Jet.rpos, pos));
let U = sub3(b.U, mul3(F, dot3(b.U, F)));
if (len3(U) < 1e-3) U = sub3(b.F, mul3(F, dot3(b.F, F)));
U = norm3(U);
let R = cross3(F, U);
const F2 = rotAxis(F, R, -blendLook[1]), U2 = rotAxis(U, R, -blendLook[1]);
const F3 = rotAxis(F2, U2, blendLook[0]);
b.F = F3; b.U = U2; b.R = cross3(F3, U2);
}
Cam.pos = pos; Cam.R = b.R; Cam.U = b.U; Cam.F = b.F; Cam.fovY = fov;
if (Cam.shake > 0) {
Cam.shake = Math.max(0, Cam.shake - dt * 2.2);
const s = Cam.shake * Cam.shake * 1.6;
Cam.pos = add3(Cam.pos, [rnd(-s, s), rnd(-s, s), rnd(-s, s)]);
}
Cam.tanY = Math.tan(Cam.fovY * D2R / 2);
Cam.tanX = Cam.tanY * Cam.aspect;
// weights for UI
const w = (v) => Cam.blend >= 1 || !Cam.from ? (Cam.view === v ? 1 : 0) : (Cam.view === v ? smooth(Cam.blend) : 0) + (Cam.from.view === v ? 1 - smooth(Cam.blend) : 0);
Cam.cockpitW = w(2); Cam.topW = w(0);
// hide the jet when the camera is inside it
Jet.hidden = len3(sub3(Cam.pos, Jet.rpos)) < 11 && (Cam.view === 2 || (Cam.from && Cam.from.view === 2));
}
// world → internal pixel coordinates (y down), or null if behind
function project(p, W, H) {
const d = sub3(p, Cam.pos);
const z = dot3(d, Cam.F);
if (z < 0.1) return null;
const x = dot3(d, Cam.R) / (z * Cam.tanX), y = dot3(d, Cam.U) / (z * Cam.tanY);
return [(x * 0.5 + 0.5) * W, (0.5 - y * 0.5) * H, z];
}
// ===== controls → flight commands ===============================================================
function computeControls(W, H) {
const J = Jet;
// lazy S-turns until the player takes over
if (J.autopilot) {
const t = J.time;
const turn = Math.sin(t * 0.33) * 0.2 + Math.sin(t * 0.11) * 0.05;
const hdgBias = 0;
return { turn: turn + hdgBias, gamma: clamp((CRUISE_ALT - J.pos[1]) / 160, -0.25, 0.25) };
}
let aim = Input.touchSteer;
if (!aim && Input.has) {
const r = Input.rect || { left: 0, top: 0, width: W, height: H };
aim = { x: (Input.mx - r.left) / r.width * W, y: (Input.my - r.top) / r.height * H, abs: true };
}
const topMode = Cam.view === 0;
if (!aim) return { turn: 0, gamma: topMode ? clamp((CRUISE_ALT - J.pos[1]) / 160, -0.3, 0.3) : 0 };
if (topMode) {
let tx, ty;
if (aim.abs) { tx = aim.x; ty = aim.y; } else { tx = W / 2 + aim.ox * H * 0.5; ty = H / 2 + aim.oy * H * 0.5; }
const js = project(J.pos, W, H) || [W / 2, H / 2];
const dx = tx - js[0], dy = ty - js[1];
let turn = 0;
if (Math.hypot(dx, dy) > H * 0.03) {
const want = Math.atan2(dx, -dy);
const d = wrapPi(want - J.psi);
turn = clamp(d * 1.5, -0.62, 0.62);
}
return { turn, gamma: clamp((CRUISE_ALT - J.pos[1]) / 160, -0.3, 0.3) };
}
let ox, oy;
if (aim.abs) { ox = (aim.x - W / 2) / (H / 2); oy = (aim.y - H / 2) / (H / 2); } else { ox = aim.ox; oy = aim.oy; }
const dz = (v) => { const a = Math.abs(v); return a < 0.04 ? 0 : Math.sign(v) * Math.pow(Math.min(1, (a - 0.04) / 0.96), 1.25); };
return { turn: dz(ox) * 0.7, gamma: -dz(oy) * 1.0 };
}
// ===== particles =================================================================================
const MAXP = 2600;
const FX = {
parts: [], impacts: [], missiles: [], wake: [], trails: [[], []], flash: 0, time: 0,
slots: [0, 1, 2, 3].map(() => ({ state: 'armed', t: 0, s: 1 })), lastFire: -9, fireIdx: 0, wakeD: 0,
};
// p: position, v: velocity; opts: life, s0, s1, type, c0, c1, core [r,g,b,frac], a0, a1, drag, grav, fog
function spawn(p, v, o) {
if (FX.parts.length >= MAXP) FX.parts.shift();
FX.parts.push({
p: p.slice(), v: v.slice(), age: 0, life: o.life || 1, s0: o.s0 || 1, s1: o.s1 === undefined ? o.s0 || 1 : o.s1,
type: o.type || 0, c0: o.c0 || [1, 1, 1], c1: o.c1 || o.c0 || [1, 1, 1], core: o.core || [1, 1, 1, 0],
a0: o.a0 === undefined ? 1 : o.a0, a1: o.a1 === undefined ? 0 : o.a1, drag: o.drag || 0, grav: o.grav || 0,
fog: o.fog === undefined ? 1 : o.fog, rot: o.rot || 0, wind: o.wind || 0, fin: o.fin || 0, ap: o.ap || 1,
});
}
const COL = {
white: [1.0, 0.98, 0.88], yellow: [1.0, 0.88, 0.55], orange: [0.97, 0.63, 0.25], red: [0.85, 0.39, 0.16], dark: [0.38, 0.11, 0.20],
smokeD: [0.30, 0.26, 0.40], smokeL: [0.48, 0.43, 0.56], smokeW: [0.88, 0.85, 0.93], smokeP: [0.68, 0.62, 0.70],
foam: [0.92, 0.98, 0.95], spray: [0.66, 0.90, 0.82],
};
function updateParticles(dt) {
const P = FX.parts;
let w = 0;
for (let i = 0; i < P.length; i++) {
const q = P[i];
q.age += dt;
if (q.age >= q.life) continue;
const k = Math.exp(-q.drag * dt);
q.v[0] *= k; q.v[1] = q.v[1] * k - q.grav * dt; q.v[2] *= k;
q.p[0] += (q.v[0] + q.wind * 3) * dt; q.p[1] += q.v[1] * dt; q.p[2] += (q.v[2] + q.wind * 1.2) * dt;
if (q.grav > 0 && q.p[1] < 0 && World.h(q.p[0], q.p[2]) < 0) { q.age = q.life; continue; }
P[w++] = q;
}
P.length = w;
}
// ===== missiles =================================================================================
function missileTarget() {
const J = Jet;
const o = madd3(J.rpos, J.F, 10);
const t = World.ray(o, J.F, 4200, 10);
if (t > 0) return madd3(o, J.F, t);
// flying level or upward: aim at the ground ahead
const h = norm3([J.F[0], 0, J.F[2]]);
const d = clamp(650 + J.agl * 0.8, 650, 1500);
const x = J.pos[0] + h[0] * d, z = J.pos[2] + h[2] * d;
return [x, World.surf(x, z), z];
}
function fireMissile() {
if (!World.ready) return false;
const order = [0, 1, 2, 3];
let slot = -1;
for (let k = 0; k < 4; k++) { const i = order[(FX.fireIdx + k) % 4]; if (FX.slots[i].state === 'armed') { slot = i; break; } }
if (slot < 0) { Audio_.click(); return false; }
FX.fireIdx = (slot + 1) % 4;
const s = FX.slots[slot]; s.state = 'fired'; s.t = 4.2; s.s = 0;
const J = Jet, r = jetRails()[slot];
const p = add3(add3(add3(J.rpos, mul3(J.R, r[0])), mul3(J.U, r[1])), mul3(J.F, r[2]));
FX.missiles.push({ p, v: madd3(J.vel, J.U, -7), age: 0, target: missileTarget(), smoke: 0, slot, alive: true, F: J.F.slice(), U: J.U.slice() });
Audio_.launch();
return true;
}
function updateMissiles(dt) {
for (const s of FX.slots) {
if (s.state === 'fired') { s.t -= dt; if (s.t <= 0) { s.state = 'arming'; s.s = 0; } }
else if (s.state === 'arming') { s.s = Math.min(1, s.s + dt / 0.7); if (s.s >= 1) s.state = 'armed'; }
}
for (const m of FX.missiles) {
if (!m.alive) continue;
m.age += dt;
let sp = len3(m.v);
let dir = mul3(m.v, 1 / sp);
if (m.age < 0.3) {
m.v[1] -= G0 * dt;
} else {
sp = Math.min(340, sp + 300 * dt);
const want = norm3(sub3(m.target, m.p));
const ang = Math.acos(clamp(dot3(dir, want), -1, 1));
const maxT = 2.6 * dt * Math.min(1, (m.age - 0.3) * 2.5);
if (ang > 1e-4) { const k = Math.min(1, maxT / ang); dir = norm3(lerp3(dir, want, k)); }
m.v = mul3(dir, sp);
// smoke trail
m.smoke += dt;
const tail = madd3(m.p, dir, -1.7);
while (m.smoke > 0.014) {
m.smoke -= 0.014;
const jit = [rnd(-0.3, 0.3), rnd(-0.3, 0.3), rnd(-0.3, 0.3)];
spawn(add3(madd3(tail, dir, -sp * m.smoke), jit), [rnd(-1, 1), rnd(0, 1.5), rnd(-1, 1)], {
life: rnd(2.2, 3.4), s0: 0.7, s1: rnd(3.2, 4.6), type: 1, c0: COL.smokeP, c1: COL.smokeL, core: [...COL.smokeW, 0], a0: 1, a1: 0, ap: 2, drag: 0.8, wind: 1,
});
}
}
m.p = madd3(m.p, m.v, dt);
m.F = dir;
const hs = World.surf(m.p[0], m.p[2]);
if (m.p[1] <= hs + 0.3 || m.age > 9 || (m.age > 0.5 && len3(sub3(m.p, m.target)) < 4)) {
m.alive = false;
const water = World.h(m.p[0], m.p[2]) < 0;
explode([m.p[0], Math.max(hs, Math.min(m.p[1], hs + 2)), m.p[2]], water);
}
}
FX.missiles = FX.missiles.filter(m => m.alive);
}
function explode(p, water) {
const now = FX.time;
FX.impacts.push({ x: p[0], y: p[1], z: p[2], t0: now, type: water ? 2 : 1, size: water ? 11 : 12 });
if (FX.impacts.length > 8) FX.impacts.shift();
// flash + fireball
spawn(add3(p, [0, 4, 0]), [0, 0, 0], { life: 0.2, s0: 14, s1: 64, c0: COL.white, core: [1, 1, 1, 0.7], a0: 1, a1: 0.5, fog: 0.25 });
spawn(add3(p, [0, 2, 0]), [0, 0, 0], { life: 0.6, s0: 8, s1: 110, type: 2, c0: water ? COL.foam : COL.white, core: [1, 1, 1, 0.07], a0: 1, a1: 0.3, fog: 0.4 });
const nS = water ? 12 : 40;
for (let i = 0; i < nS; i++) {
const k = i / nS;
spawn(add3(p, [rnd(-7, 7), rnd(2, 10) + k * 30, rnd(-7, 7)]), [rnd(-3, 3), rnd(8, 16) * (0.35 + k), rnd(-3, 3)], {
life: rnd(10, 15), s0: rnd(5, 8), s1: rnd(15, 25), type: 1, c0: water ? COL.smokeP : COL.smokeD, c1: COL.smokeL, core: [...(water ? COL.smokeW : COL.smokeP), 0], a0: 1, a1: 0, ap: 3, drag: 0.3, wind: 1.5, fin: 0.45,
});
}
const nF = water ? 12 : 24;
for (let i = 0; i < nF; i++) {
const up = i < nF / 3;
const v = up ? [rnd(-5, 5), rnd(32, 50), rnd(-5, 5)] : [rnd(-22, 22), rnd(8, 30), rnd(-22, 22)];
spawn(add3(p, [rnd(-4, 4), rnd(1, 5), rnd(-4, 4)]), v, {
life: rnd(0.9, 1.7), s0: rnd(7, 11), s1: rnd(19, 30), c0: COL.yellow, c1: COL.dark, core: [...COL.white, 0.5], a0: 1, a1: 0.55, drag: 1.6, fog: 0.4,
});
}
for (let i = 0; i < 30; i++) {
const v = [rnd(-40, 40), rnd(18, 60), rnd(-40, 40)];
spawn(add3(p, [0, 2, 0]), v, { life: rnd(0.8, 1.8), s0: 1.0, s1: 0.7, type: 3, c0: COL.orange, core: [...COL.white, 1], a0: 1, a1: 1, grav: 28, drag: 0.6 });
}
if (water) {
for (let i = 0; i < 90; i++) {
const a = rnd(0, Math.PI * 2), r = rnd(0, 5);
spawn([p[0] + Math.cos(a) * r, 0.5, p[2] + Math.sin(a) * r], [Math.cos(a) * rnd(1, 7), rnd(18, 58), Math.sin(a) * rnd(1, 7)], {
life: rnd(1.6, 3.0), s0: rnd(1.2, 2.2), s1: rnd(2.5, 4), c0: COL.white, c1: COL.spray, core: [...COL.foam, 0.5], a0: 1, a1: 0.4, grav: 24, drag: 0.4,
});
}
for (let i = 0; i < 18; i++) spawn([p[0] + rnd(-7, 7), rnd(4, 40), p[2] + rnd(-7, 7)], [rnd(-2, 2), rnd(3, 9), rnd(-2, 2)], { life: rnd(3.5, 6), s0: 7, s1: 22, type: 1, c0: COL.smokeW, c1: COL.smokeP, core: [...COL.foam, 0], a0: 0.85, a1: 0, drag: 0.6, wind: 1 });
} else {
for (let i = 0; i < 20; i++) {
spawn(add3(p, [rnd(-4, 4), 1, rnd(-4, 4)]), [rnd(-14, 14), rnd(10, 26), rnd(-14, 14)], { life: rnd(1.5, 3.2), s0: 0.6, s1: 0.4, c0: COL.orange, c1: COL.red, core: [...COL.yellow, 0.5], a0: 1, a1: 0.7, grav: 18, drag: 0.5 });
}
for (let i = 0; i < 10; i++) spawn(add3(p, [rnd(-7, 7), 1, rnd(-7, 7)]), [rnd(-6, 6), rnd(1, 4), rnd(-6, 6)], { life: rnd(2, 3.5), s0: 4, s1: 12, type: 1, c0: [0.85, 0.74, 0.54], c1: COL.smokeP, core: [0.95, 0.87, 0.64, 0], a0: 0.8, a1: 0, drag: 0.9 });
}
// camera feedback
const d = len3(sub3(p, Cam.pos));
const vis = project(p, 100, 100);
if (vis && vis[0] > -20 && vis[0] < 120 && vis[1] > -20 && vis[1] < 120) FX.flash = Math.max(FX.flash, clamp(1 - d / 1400, 0, 1) * 0.55);
Cam.shake = Math.max(Cam.shake, clamp(1 - d / 900, 0, 1));
Audio_.boom(d);
}
// ===== per-frame effects tied to the jet =========================================================
function updateJetFX(dt) {
const J = Jet, now = FX.time;
// wingtip vapour trails
const inten = 0.72 + 0.28 * clamp((J.G - 1.3) / 3.2, 0, 1) + 0.2 * J.abAmt;
for (let s = 0; s < 2; s++) {
const sx = s ? -1 : 1;
const tipP = add3(add3(add3(J.rpos, mul3(J.R, sx * 6.95)), mul3(J.F, -5.6)), mul3(J.U, -0.18));
const tr = FX.trails[s];
const last = tr[tr.length - 1];
if (!last || len3(sub3(last.p, tipP)) > 1.5 || now - last.t > 0.05) tr.push({ p: tipP, t: now, k: Math.min(1, inten) });
else { last.p = tipP; }
while (tr.length && now - tr[0].t > 2.4) tr.shift();
if (tr.length > 400) tr.splice(0, tr.length - 400);
}
// spray + wake when skimming water
const hh = World.h(J.pos[0], J.pos[2]);
const overWater = hh < 0;
const alt = J.pos[1] - Math.max(hh, 0);
if (overWater && alt < 24) {
const str = clamp((24 - alt) / 15, 0, 1);
FX.wakeD += J.V * dt;
if (FX.wakeD > 7 || !FX.wake.length) { FX.wakeD = 0; FX.wake.unshift({ x: J.pos[0], z: J.pos[2], t0: now, s: str }); if (FX.wake.length > 24) FX.wake.pop(); }
else { FX.wake[0].x = J.pos[0]; FX.wake[0].z = J.pos[2]; FX.wake[0].t0 = now; FX.wake[0].s = str; }
if (alt < 14) {
const n = Math.floor((14 - alt) * 26 * dt + Math.random());
const back = madd3(J.pos, norm3([J.F[0], 0, J.F[2]]), -9);
for (let i = 0; i < n; i++) {
const side = rnd(-1, 1);
const p = [back[0] + J.R[0] * side * 3 + rnd(-2, 2), 0.3, back[2] + J.R[2] * side * 3 + rnd(-2, 2)];
spawn(p, add3(mul3(J.vel, 0.25), [J.R[0] * side * 9 + rnd(-3, 3), rnd(5, 16) * (1 - alt / 18), J.R[2] * side * 9 + rnd(-3, 3)]), {
life: rnd(0.6, 1.2), s0: rnd(0.8, 1.6), s1: rnd(1.8, 3.2), c0: COL.white, c1: COL.spray, core: [...COL.foam, 0.4], a0: 1, a1: 0.2, grav: 22, drag: 1.2,
});
}
if (Math.random() < dt * 14) spawn([back[0], rnd(1, 4), back[2]], [J.vel[0] * 0.1, 2, J.vel[2] * 0.1], { life: 1.6, s0: 3, s1: 8, type: 1, c0: COL.smokeW, c1: COL.smokeP, core: [...COL.foam, 0], a0: 0.7, a1: 0, drag: 1 });
}
} else if (FX.wake.length && now - FX.wake[0].t0 > 12) FX.wake.length = 0;
}
// sprites that live for one frame only (plumes, missile flames)
function frameSprites(out) {
const J = Jet, t = FX.time;
if (!J.hidden) {
for (const sx of [-1, 1]) {
const noz = add3(add3(J.rpos, mul3(J.R, sx * 0.66)), mul3(J.F, -9.0));
const ab = J.abAmt;
if (ab < 0.05) {
out.push({ p: madd3(noz, J.F, -0.2), s: 0.38, c: [0.85, 0.39, 0.16], core: [1.0, 0.88, 0.55, 0.45], a: 1, type: 0, fog: 1 });
} else {
const n = 11;
for (let k = 0; k < n; k++) {
const u = k / (n - 1);
const fl = 0.85 + 0.3 * Math.sin(t * 61 + k * 1.7 + sx) * Math.sin(t * 37 + k);
const d = 0.3 + u * 6.2 * ab * fl;
const s = (0.52 - u * 0.36) * (0.6 + 0.4 * ab) * (0.9 + 0.2 * fl);
const c = u < 0.25 ? [1.0, 0.88, 0.55] : u < 0.6 ? [0.97, 0.63, 0.25] : [0.85, 0.39, 0.16];
const core = u < 0.45 ? [1, 0.98, 0.88, 0.62 - u * 0.6] : [...c, 0];
out.push({ p: madd3(noz, J.F, -d), s, c, core, a: u < 0.8 ? 1 : 0.6, type: 0, fog: 1 });
// shock diamonds
if (ab > 0.6 && (k === 3 || k === 6)) out.push({ p: madd3(noz, J.F, -d), s: s * 0.45, c: [1, 0.98, 0.88], core: [1, 1, 1, 1], a: 1, type: 0, fog: 1 });
}
}
}
}
for (const m of FX.missiles) {
if (m.age < 0.3) continue;
const fl = 0.8 + 0.4 * Math.random();
const tail = madd3(m.p, m.F, -1.8);
out.push({ p: tail, s: 0.75 * fl, c: [0.97, 0.63, 0.25], core: [1, 0.98, 0.88, 0.55], a: 1, type: 0, fog: 0.8 });
out.push({ p: madd3(tail, m.F, -1.3), s: 0.5 * fl, c: [0.85, 0.39, 0.16], core: [1.0, 0.88, 0.55, 0.4], a: 0.9, type: 0, fog: 0.8 });
}
}
// ===== pixel overlay: cockpit panel, HUD, cursor ===================================================
const UI = { cv: null, ctx: null, img: null, buf: null, W: 0, H: 0, stripPx: 10 };
const rgb = (h, a) => { const n = parseInt(h.slice(1), 16); return (((a === undefined ? 255 : a) << 24) | ((n & 255) << 16) | (((n >> 8) & 255) << 8) | (n >> 16)) >>> 0; };
const C = {
panel: rgb('#2a2736'), panelL: rgb('#363244'), panelD: rgb('#1b1923'), panelDD: rgb('#121017'), rivetL: rgb('#5a556c'),
bezel: rgb('#46415a'), bezelL: rgb('#6f6984'), bezelD: rgb('#0e0d13'), face: rgb('#100f16'), faceL: rgb('#1a1824'),
mark: rgb('#ece4d2'), markD: rgb('#8a8498'), needle: rgb('#f2c443'), needleT: rgb('#f0a13a'), red: rgb('#e04a3a'), white: rgb('#fff8ec'),
sky: rgb('#4a64a6'), skyL: rgb('#5d7bbd'), ground: rgb('#8a5634'), groundD: rgb('#6c4128'), orange: rgb('#f0a13a'), green: rgb('#6cc690'),
am0: rgb('#140c07'), am1: rgb('#3a2208'), am2: rgb('#6c3e0e'), am3: rgb('#a8641c'), am4: rgb('#e09c34'), am5: rgb('#ffd27a'),
lampOff: rgb('#2c2834'), lampTxt: rgb('#5f5970'), lampRed: rgb('#e04a3a'), lampAmb: rgb('#f0a13a'), lampGrn: rgb('#5cc08a'),
hud: rgb('#8df5a2'), hudS: rgb('#123222', 200), hood: rgb('#18161f'), hoodL: rgb('#4d4760'), frame: rgb('#141219'), frameL: rgb('#2e2a3a'),
cursor: rgb('#fff4d6'), cursorD: rgb('#1d1a3a'), black: rgb('#000000'),
};
const FONT = {
'0': '111101101101111', '1': '010110010010111', '2': '111001111100111', '3': '111001111001111', '4': '101101111001001', '5': '111100111001111',
'6': '111100111101111', '7': '111001001001001', '8': '111101111101111', '9': '111101111001111',
A: '010101111101101', B: '110101110101110', C: '011100100100011', D: '110101101101110', E: '111100110100111', F: '111100110100100',
G: '011100101101011', H: '101101111101101', I: '111010010010111', J: '001001001101010', K: '101101110101101', L: '100100100100111',
M: '101111111101101', N: '110101101101101', O: '010101101101010', P: '110101110100100', Q: '010101101110011', R: '110101110101101',
S: '011100010001110', T: '111010010010010', U: '101101101101111', V: '101101101101010', W: '101101111111101', X: '101101010101101',
Y: '101101010010010', Z: '111001010100111', ' ': '000000000000000', '-': '000000111000000', '.': '000000000000010', ':': '000010000010000',
'/': '001001010100100', '+': '000010111010000', '%': '101001010100101', '<': '001010100010001', '>': '100010001010100', '!': '010010010000010',
"'": '010010000000000', '^': '010101000000000',
};
function uiInit(cv) { UI.cv = cv; UI.ctx = cv.getContext('2d'); }
function uiResize(W, H) {
UI.W = W; UI.H = H; UI.cv.width = W; UI.cv.height = H;
UI.img = UI.ctx.createImageData(W, H); UI.buf = new Uint32Array(UI.img.data.buffer);
}
let CLIP = null; // [x0,y0,x1,y1]
function px(x, y, c) {
x |= 0; y |= 0;
if (x < 0 || y < 0 || x >= UI.W || y >= UI.H) return;
if (CLIP && (x < CLIP[0] || y < CLIP[1] || x > CLIP[2] || y > CLIP[3])) return;
UI.buf[y * UI.W + x] = c;
}
function rect(x, y, w, h, c) { x |= 0; y |= 0; for (let j = 0; j < h; j++) for (let i = 0; i < w; i++) px(x + i, y + j, c); }
function frame(x, y, w, h, c) { for (let i = 0; i < w; i++) { px(x + i, y, c); px(x + i, y + h - 1, c); } for (let j = 0; j < h; j++) { px(x, y + j, c); px(x + w - 1, y + j, c); } }
function line(x0, y0, x1, y1, c, dash) {
x0 = Math.round(x0); y0 = Math.round(y0); x1 = Math.round(x1); y1 = Math.round(y1);
const dx = Math.abs(x1 - x0), dy = -Math.abs(y1 - y0), sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1;
let err = dx + dy, n = 0;
for (let k = 0; k < 2000; k++) {
if (!dash || ((n >> 1) % 2 === 0)) px(x0, y0, c);
n++;
if (x0 === x1 && y0 === y1) break;
const e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; }
if (e2 <= dx) { err += dx; y0 += sy; }
}
}
function disc(cx, cy, r, c) { const rr = (r + 0.5) * (r + 0.5); for (let y = -r; y <= r; y++) { const w = Math.floor(Math.sqrt(Math.max(0, rr - y * y))); for (let x = -w; x <= w; x++) px(cx + x, cy + y, c); } }
function ring(cx, cy, r, c, cL, cD) {
let x = r, y = 0, err = 1 - r;
const put = (a, b) => { const col = cL === undefined ? c : (a + b < 0 ? cL : (a + b > 0 ? cD : c)); px(cx + a, cy + b, col); };
while (x >= y) {
put(x, y); put(y, x); put(-y, x); put(-x, y); put(-x, -y); put(-y, -x); put(y, -x); put(x, -y);
y++;
if (err < 0) err += 2 * y + 1; else { x--; err += 2 * (y - x) + 1; }
}
}
function textW(s, sc) { return s.length * 4 * (sc || 1) - (sc || 1); }
function text(s, x, y, c, align, sc, shadow) {
sc = sc || 1; s = String(s).toUpperCase();
if (align === 'c') x -= Math.floor(textW(s, sc) / 2); else if (align === 'r') x -= textW(s, sc);
x = Math.round(x); y = Math.round(y);
for (let i = 0; i < s.length; i++) {
const g = FONT[s[i]] || FONT[' '];
for (let r = 0; r < 5; r++) for (let q = 0; q < 3; q++) if (g[r * 3 + q] === '1') {
if (shadow !== undefined) rect(x + i * 4 * sc + q * sc + 1, y + r * sc + 1, sc, sc, shadow);
rect(x + i * 4 * sc + q * sc, y + r * sc, sc, sc, c);
}
}
}
const pol = (cx, cy, a, r) => [cx + Math.sin(a) * r, cy - Math.cos(a) * r];
// ---- instrument primitives --------------------------------------------------------------------------
function bezel(cx, cy, r) {
disc(cx, cy, r + 2, C.panelD);
disc(cx, cy, r + 1, C.bezel);
ring(cx, cy, r + 1, C.bezel, C.bezelL, C.bezelD);
disc(cx, cy, r - 1, C.face);
// screws
for (const a of [0.785, 2.356, 3.927, 5.498]) { const p = pol(cx, cy, a, r + 1); px(p[0], p[1], C.panelDD); }
}
function ticks(cx, cy, r, a0, a1, n, len, c, every, lenMajor) {
for (let i = 0; i <= n; i++) {
const a = a0 + (a1 - a0) * i / n, maj = every && i % every === 0;
const L = maj ? lenMajor : len;
const p0 = pol(cx, cy, a, r - 1), p1 = pol(cx, cy, a, r - L);
line(p0[0], p0[1], p1[0], p1[1], maj ? c : C.markD);
}
}
function needle(cx, cy, a, r, c, tail) {
const p = pol(cx, cy, a, r), q = pol(cx, cy, a + Math.PI, tail || 2);
line(q[0], q[1], p[0], p[1], c);
px(cx, cy, C.mark);
}
function drawASI(cx, cy, r, kt) {
bezel(cx, cy, r);
const a0 = -150 * D2R, a1 = 150 * D2R, map = (v) => a0 + (a1 - a0) * clamp(v / 600, 0, 1);
ticks(cx, cy, r, a0, a1, 24, 2, C.mark, 4, 4);
for (let v = 100; v <= 500; v += 200) { const p = pol(cx, cy, map(v), r - 8); text(String(v / 100), p[0], p[1] - 2, C.mark, 'c'); }
rect(cx - 7, cy + 5, 15, 7, C.faceL); text(String(Math.round(kt)).padStart(3, '0'), cx, cy + 6, C.white, 'c');
text('KT', cx, cy - 9, C.markD, 'c');
needle(cx, cy, map(kt), r - 3, C.needle);
}
function drawALT(cx, cy, r, ft) {
bezel(cx, cy, r);
ticks(cx, cy, r, 0, Math.PI * 2 * 0.98, 49, 2, C.mark, 5, 4);
for (let d = 0; d < 10; d += 2) { const p = pol(cx, cy, d / 10 * Math.PI * 2, r - 8); text(String(d), p[0], p[1] - 2, C.mark, 'c'); }
const s = String(Math.max(0, Math.round(ft))).padStart(5, '0');
rect(cx - 10, cy + 4, 21, 7, C.faceL); text(s, cx, cy + 5, C.white, 'c');
text('ALT', cx, cy - 10, C.markD, 'c');
needle(cx, cy, (ft / 10000) * Math.PI * 2, r - 8, C.markD, 0);
needle(cx, cy, (ft % 1000) / 1000 * Math.PI * 2, r - 3, C.needle);
}
function drawADI(cx, cy, r, pitch, roll) {
bezel(cx, cy, r);
const R = r - 1, k = R / 40 * 57.3; // px per radian (40 deg to the rim)
const n = [Math.sin(roll), Math.cos(roll)], d = [Math.cos(roll), -Math.sin(roll)];
const c0 = [n[0] * pitch * k, n[1] * pitch * k];
const rr = (R + 0.5) * (R + 0.5);
for (let y = -R; y <= R; y++) for (let x = -R; x <= R; x++) {
if (x * x + y * y > rr) continue;
const s = (x - c0[0]) * n[0] + (y - c0[1]) * n[1];
px(cx + x, cy + y, s < 0 ? (s < -R * 0.55 ? C.sky : C.skyL) : (s > R * 0.5 ? C.groundD : C.ground));
}
CLIP = [cx - R + 1, cy - R + 1, cx + R - 1, cy + R - 1];
const inDisc = (x, y) => (x - cx) * (x - cx) + (y - cy) * (y - cy) < (R - 1) * (R - 1);
const seg = (off, half, c) => {
const ox = cx + c0[0] - n[0] * off, oy = cy + c0[1] - n[1] * off;
for (let t = -half; t <= half; t++) { const x = ox + d[0] * t, y = oy + d[1] * t; if (inDisc(x, y)) px(x, y, c); }
};
seg(0, R, C.white);
for (const deg of [-20, -10, 10, 20]) seg(deg * D2R * k, Math.abs(deg) === 10 ? 5 : 8, C.white);
CLIP = null;
// bank scale
for (const deg of [-60, -45, -30, -20, -10, 0, 10, 20, 30, 45, 60]) {
const a = deg * D2R, p0 = pol(cx, cy, a, R), p1 = pol(cx, cy, a, R - (deg % 30 === 0 ? 3 : 2));
line(p0[0], p0[1], p1[0], p1[1], deg === 0 ? C.needle : C.white);
}
const bp = pol(cx, cy, -roll, R - 4); px(bp[0], bp[1], C.needleT); px(bp[0] - 1, bp[1] + 1, C.needleT); px(bp[0] + 1, bp[1] + 1, C.needleT);
// fixed aircraft symbol
for (let i = 4; i <= 10; i++) { px(cx - i, cy, C.needleT); px(cx + i, cy, C.needleT); }
px(cx - 4, cy + 1, C.needleT); px(cx + 4, cy + 1, C.needleT); px(cx - 3, cy + 2, C.needleT); px(cx + 3, cy + 2, C.needleT);
px(cx - 2, cy + 2, C.needleT); px(cx + 2, cy + 2, C.needleT); px(cx, cy, C.needleT); px(cx - 1, cy + 1, C.needleT); px(cx + 1, cy + 1, C.needleT);
}
function drawTurn(cx, cy, r, rate, ball) { // rate in deg/s
bezel(cx, cy, r);
const sr = 20 * D2R;
for (const a of [-sr, sr]) { const p0 = pol(cx, cy, Math.PI / 2 + a, r - 1), p1 = pol(cx, cy, Math.PI / 2 + a, r - 4); line(p0[0], p0[1], p1[0], p1[1], C.white); const q0 = pol(cx, cy, -Math.PI / 2 + a, r - 1), q1 = pol(cx, cy, -Math.PI / 2 + a, r - 4); line(q0[0], q0[1], q1[0], q1[1], C.white); }
const lv0 = pol(cx, cy, Math.PI / 2, r - 1), lv1 = pol(cx, cy, Math.PI / 2, r - 4); line(lv0[0], lv0[1], lv1[0], lv1[1], C.markD);
const lv2 = pol(cx, cy, -Math.PI / 2, r - 1), lv3 = pol(cx, cy, -Math.PI / 2, r - 4); line(lv2[0], lv2[1], lv3[0], lv3[1], C.markD);
const a = clamp(rate / 3, -1.8, 1.8) * sr;
const L = r - 5, p = [Math.cos(a) * L, Math.sin(a) * L];
line(cx - p[0], cy - 2 - p[1], cx + p[0], cy - 2 + p[1], C.white);
const up = [Math.sin(a) * 3, -Math.cos(a) * 3]; line(cx, cy - 2, cx + up[0], cy - 2 + up[1], C.white);
disc(cx, cy - 2, 1, C.white);
// slip tube
const ty = cy + Math.round(r * 0.5);
rect(cx - Math.round(r * 0.62), ty - 2, Math.round(r * 1.24) + 1, 5, C.faceL);
px(cx - 3, ty - 2, C.markD); px(cx - 3, ty + 2, C.markD); px(cx + 3, ty - 2, C.markD); px(cx + 3, ty + 2, C.markD);
const bx = Math.round(clamp(ball, -1, 1) * r * 0.5);
disc(cx + bx, ty, 2, C.mark);
text('TURN', cx, cy - r + 5, C.markD, 'c');
}
function drawHSI(cx, cy, r, hdg) {
bezel(cx, cy, r);
for (let d = 0; d < 360; d += 10) {
const a = (d - hdg) * D2R, maj = d % 30 === 0;
const p0 = pol(cx, cy, a, r - 1), p1 = pol(cx, cy, a, r - (maj ? 4 : 2));
line(p0[0], p0[1], p1[0], p1[1], maj ? C.mark : C.markD);
}
const lbl = { 0: 'N', 90: 'E', 180: 'S', 270: 'W' };
for (const k in lbl) { const p = pol(cx, cy, (k - hdg) * D2R, r - 8); text(lbl[k], p[0], p[1] - 2, k === '0' ? C.needleT : C.mark, 'c'); }
// lubber + aircraft
line(cx, cy - r + 1, cx, cy - r + 4, C.needle);
line(cx, cy - 4, cx, cy + 4, C.needle); line(cx - 4, cy - 1, cx + 4, cy - 1, C.needle); line(cx - 2, cy + 4, cx + 2, cy + 4, C.needle);
const hs = String(Math.round(((hdg % 360) + 360) % 360) % 360).padStart(3, '0');
rect(cx - 7, cy + r + 3, 15, 7, C.panelD); text(hs, cx, cy + r + 4, C.white, 'c');
}
function drawVSI(cx, cy, r, fpm) {
bezel(cx, cy, r);
const map = (v) => { const a = Math.abs(v), s = Math.sign(v); const ang = a < 2000 ? a / 2000 * 80 : 80 + Math.min(1, (a - 2000) / 4000) * 85; return -Math.PI / 2 + s * ang * D2R; };
for (const v of [-6000, -4000, -2000, -1000, 0, 1000, 2000, 4000, 6000]) {
const a = map(v), p0 = pol(cx, cy, a, r - 1), p1 = pol(cx, cy, a, r - (v % 2000 === 0 ? 4 : 2));
line(p0[0], p0[1], p1[0], p1[1], v === 0 ? C.mark : C.markD);
}
for (const v of [2, 4]) { for (const s of [1, -1]) { const p = pol(cx, cy, map(s * v * 1000), r - 8); text(String(v), p[0], p[1] - 2, C.mark, 'c'); } }
text('0', cx - r + 7, cy - 2, C.mark, 'c');
text('VS', cx + 5, cy - 8, C.markD, 'c');
needle(cx, cy, map(fpm), r - 3, C.needle);
}
function drawMini(cx, cy, r, v, lo, hi, label, val) {
bezel(cx, cy, r);
const a0 = -135 * D2R, a1 = 135 * D2R;
ticks(cx, cy, r, a0, a1, 6, 2, C.mark, 2, 3);
const red = a0 + (a1 - a0) * 0.88;
const rp0 = pol(cx, cy, red, r - 1), rp1 = pol(cx, cy, red, r - 3); line(rp0[0], rp0[1], rp1[0], rp1[1], C.red);
needle(cx, cy, a0 + (a1 - a0) * clamp((v - lo) / (hi - lo), 0, 1), r - 3, C.needle, 1);
text(val, cx, cy + r + 3, C.white, 'c');
text(label, cx, cy + 3, C.markD, 'c');
}
function drawClock(cx, cy, r, d) {
bezel(cx, cy, r);
ticks(cx, cy, r, 0, Math.PI * 2 * 11 / 12, 11, 2, C.mark, 3, 3);
const h = d.getHours(), m = d.getMinutes(), s = d.getSeconds() + d.getMilliseconds() / 1000;
needle(cx, cy, ((h % 12) + m / 60) / 12 * Math.PI * 2, r - 7, C.mark, 1);
needle(cx, cy, (m + s / 60) / 60 * Math.PI * 2, r - 3, C.mark, 1);
const sp = pol(cx, cy, Math.floor(s) / 60 * Math.PI * 2, r - 2); px(sp[0], sp[1], C.red);
const hm = String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0');
rect(cx - 10, cy + r + 3, 21, 7, C.panelD); text(hm, cx, cy + r + 4, C.white, 'c');
}
function drawLamp(x, y, w, label, on, col, blink) {
const lit = on && (!blink || (Math.floor(performance.now() / 260) % 2 === 0));
rect(x - 1, y - 1, w + 2, 9, C.panelDD);
rect(x, y, w, 7, lit ? col : C.lampOff);
for (let i = 0; i < w; i++) px(x + i, y, lit ? C.white : C.panelL);
text(label, x + w / 2, y + 1, lit ? C.panelDD : C.lampTxt, 'c');
}
// ---- radar --------------------------------------------------------------------------------------------
const Radar = { AZ: 56, RB: 44, val: null, tPaint: null, sweep: -1, dir: 1, range: 4000, span: 60 * D2R };
function radarUpdate(dt, t) {
if (!Radar.val) { Radar.val = new Float32Array(Radar.AZ * Radar.RB); Radar.tPaint = new Float32Array(Radar.AZ).fill(-99); }
const prev = Radar.sweep;
Radar.sweep += Radar.dir * dt / 1.25;
if (Radar.sweep > 1) { Radar.sweep = 1; Radar.dir = -1; } else if (Radar.sweep < -1) { Radar.sweep = -1; Radar.dir = 1; }
const b0 = Math.floor((Math.min(prev, Radar.sweep) * 0.5 + 0.5) * (Radar.AZ - 1)), b1 = Math.ceil((Math.max(prev, Radar.sweep) * 0.5 + 0.5) * (Radar.AZ - 1));
if (!World.ready) return;
const J = Jet, hdg = Math.atan2(J.F[0], -J.F[2]);
for (let b = b0; b <= b1; b++) {
const az = hdg + (b / (Radar.AZ - 1) * 2 - 1) * Radar.span;
const sx = Math.sin(az), sz = -Math.cos(az);
let maxE = -9, prevH = World.surf(J.pos[0], J.pos[2]);
for (let k = 0; k < Radar.RB; k++) {
const rng = (k + 0.5) / Radar.RB * Radar.range;
const x = J.pos[0] + sx * rng, z = J.pos[2] + sz * rng;
const hr = World.h(x, z), h = hr > 0 ? hr : 0;
const e = (h - J.pos[1]) / rng;
let v = 0;
if (e >= maxE) {
if (hr < 0) v = 0.04;
else v = clamp(0.32 + (h - prevH) / (Radar.range / Radar.RB) * 2.4 + h * 0.0012, 0.12, 1);
}
maxE = Math.max(maxE, e);
prevH = h;
Radar.val[b * Radar.RB + k] = v;
}
Radar.tPaint[b] = t;
}
}
function drawRadar(x0, y0, w, h, t) {
rect(x0 - 2, y0 - 2, w + 4, h + 4, C.panelDD);
frame(x0 - 2, y0 - 2, w + 4, h + 4, C.bezel);
for (let i = 0; i < w + 4; i++) px(x0 - 2 + i, y0 - 2, C.bezelL);
rect(x0, y0, w, h, C.am0);
if (!Radar.val) return;
const ax = x0 + w / 2, ay = y0 + h - 4, R = h - 9;
const lv = [C.am0, C.am1, C.am2, C.am3, C.am4, C.am5];
for (let y = y0; y < y0 + h; y++) for (let x = x0; x < x0 + w; x++) {
const dx = x + 0.5 - ax, dy = ay - (y + 0.5);
const r = Math.hypot(dx, dy);
if (r > R || dy < -1) continue;
const a = Math.atan2(dx, dy);
if (Math.abs(a) > Radar.span) continue;
const b = Math.round((a / Radar.span * 0.5 + 0.5) * (Radar.AZ - 1));
const k = Math.min(Radar.RB - 1, Math.floor(r / R * Radar.RB));
const age = t - Radar.tPaint[b];
const v = Radar.val[b * Radar.RB + k] * Math.exp(-age / 2.6) + (((x * 7 + y * 13) & 15) === 0 ? 0.08 : 0);
const q = clamp(Math.floor(v * 5.4 + ((x + y * 2) & 3) * 0.12), 0, 5);
if (q > 0) px(x, y, lv[q]);
}
// range rings + bearing lines
for (let i = 1; i <= 4; i++) {
const rr = R * i / 4;
for (let a = -Radar.span; a <= Radar.span; a += 0.9 / rr) { const xx = ax + Math.sin(a) * rr, yy = ay - Math.cos(a) * rr; if (((xx + yy) | 0) % 2 === 0) px(xx, yy, C.am2); }
}
for (const a of [-Radar.span, -Radar.span / 2, 0, Radar.span / 2, Radar.span]) line(ax, ay, ax + Math.sin(a) * R, ay - Math.cos(a) * R, C.am1, true);
const sa = Radar.sweep * Radar.span;
line(ax, ay, ax + Math.sin(sa) * R, ay - Math.cos(sa) * R, C.am5);
// missiles in flight and fresh impacts
const J = Jet, hdg = Math.atan2(J.F[0], -J.F[2]);
const plot = (p, c, big) => {
const dx = p[0] - J.pos[0], dz = p[2] - J.pos[2];
const rng = Math.hypot(dx, dz); if (rng > Radar.range) return;
const a = wrapPi(Math.atan2(dx, -dz) - hdg); if (Math.abs(a) > Radar.span) return;
const xx = ax + Math.sin(a) * rng / Radar.range * R, yy = ay - Math.cos(a) * rng / Radar.range * R;
px(xx, yy, c); if (big) { px(xx + 1, yy, c); px(xx - 1, yy, c); px(xx, yy + 1, c); px(xx, yy - 1, c); }
};
for (const m of FX.missiles) plot(m.p, C.am5, false);
for (const im of FX.impacts) if (FX.time - im.t0 < 6 && Math.floor(t * 4) % 2 === 0) plot([im.x, 0, im.z], C.am5, true);
// own ship
px(ax, ay - 1, C.am5); px(ax - 1, ay, C.am5); px(ax + 1, ay, C.am5); px(ax, ay, C.am5);
text('MAP', x0 + 2, y0 + 2, C.am3); text('4KM', x0 + w - 2, y0 + 2, C.am3, 'r');
}
// ---- panel ------------------------------------------------------------------------------------------
function drawPanel(off) {
const W = UI.W, H = UI.H, J = Jet;
const u = Math.min(H / 340, W / 450, 1.35);
const PH = Math.round(118 * u) + UI.stripPx;
const top = H - PH + off;
const cx0 = Math.round(W / 2);
// canopy frame: side posts and bow
const fT = off;
const sx0 = Math.round(W * 0.012), sx1 = Math.round(W * 0.115), sy0 = fT + Math.round(18 * u);
for (let t = -4; t <= 4; t++) {
const c = t === -4 ? C.frameL : (t === 4 ? C.panelDD : C.frame);
line(sx0 + t, sy0, sx1 + t, top + 2, c);
line(W - 1 - sx0 - t, sy0, W - 1 - sx1 - t, top + 2, c);
}
for (let y = fT; y < sy0; y++) { rect(0, y, sx0 + 5, 1, C.frame); rect(W - sx0 - 5, y, sx0 + 5, 1, C.frame); px(sx0 + 4, y, C.frameL); px(W - sx0 - 5, y, C.frameL); }
// panel body with a curved glare shield
for (let x = 0; x < W; x++) {
const k = (x - cx0) / (W / 2);
const yt = Math.round(top + 7 * k * k * u);
for (let y = Math.max(0, yt); y < H; y++) {
let c;
if (y < yt + 2) c = y === yt ? C.hoodL : C.hood;
else if (y < yt + Math.round(8 * u)) c = C.hood;
else c = ((x * 3 + y * 5) % 17 === 0) ? C.panelL : C.panel;
UI.buf[y * W + x] = c;
}
const ye = yt + Math.round(8 * u);
if (ye < H) UI.buf[ye * W + x] = C.panelDD;
if (ye + 1 < H) UI.buf[(ye + 1) * W + x] = C.panelL;
}
const y0 = top + Math.round(12 * u);
const S = (v) => Math.round(v * u);
// sub-panel plates
const plate = (x, y, w, h) => { rect(x, y, w, h, C.panelL); frame(x, y, w, h, C.panelD); px(x + 2, y + 2, C.rivetL); px(x + w - 3, y + 2, C.rivetL); px(x + 2, y + h - 3, C.rivetL); px(x + w - 3, y + h - 3, C.rivetL); };
plate(cx0 - S(206), y0 - S(4), S(158), S(112));
plate(cx0 + S(56), y0 - S(4), S(168), S(112));
// side consoles: little switch rows
for (const side of [-1, 1]) {
const xs = side < 0 ? 6 : W - 6 - S(40);
if (side < 0 ? xs + S(40) < cx0 - S(212) : xs > cx0 + S(228)) {
plate(xs, y0, S(40), S(60));
for (let i = 0; i < 4; i++) { const sx = xs + S(6) + i * S(9); rect(sx, y0 + S(10), 3, 6, C.panelDD); rect(sx, y0 + S(10) + (i % 2 ? 0 : 3), 3, 3, C.mark); }
for (let i = 0; i < 3; i++) { const sx = xs + S(8) + i * S(12); disc(sx, y0 + S(36), S(3), C.panelDD); px(sx, y0 + S(34), C.mark); }
}
}
const pitch = Math.asin(clamp(J.F[1], -1, 1));
const roll = Math.atan2(-J.R[1], J.U[1]);
const hdg = ((Math.atan2(J.F[0], -J.F[2]) / D2R) + 360) % 360;
drawASI(cx0 - S(178), y0 + S(26), S(21), J.V * 1.944);
drawADI(cx0 - S(127), y0 + S(26), S(24), pitch, roll);
drawALT(cx0 - S(76), y0 + S(26), S(21), J.pos[1] * 3.281);
drawTurn(cx0 - S(178), y0 + S(76), S(17), J.psiDot / D2R, J.ball);
drawHSI(cx0 - S(127), y0 + S(74), S(20), hdg);
drawVSI(cx0 - S(76), y0 + S(76), S(17), J.vel[1] * 196.85);
// radar
drawRadar(cx0 - S(46), y0, S(92), S(98), FX.time);
// engines
drawMini(cx0 + S(70), y0 + S(14), S(11), J.rpm[0], 0, 110, 'L', String(Math.round(J.rpm[0])));
drawMini(cx0 + S(96), y0 + S(14), S(11), J.rpm[1], 0, 110, 'R', String(Math.round(J.rpm[1])));
drawMini(cx0 + S(70), y0 + S(52), S(11), J.egt[0], 300, 1000, 'L', String(Math.round(J.egt[0])));
drawMini(cx0 + S(96), y0 + S(52), S(11), J.egt[1], 300, 1000, 'R', String(Math.round(J.egt[1])));
text('RPM', cx0 + S(83), y0 + S(30), C.markD, 'c');
text('EGT', cx0 + S(83), y0 + S(68), C.markD, 'c');
// fuel
const fx = cx0 + S(114), fy = y0 + S(2), fh = S(66);
rect(fx - 1, fy - 1, 8, fh + 2, C.panelDD); rect(fx, fy, 6, fh, C.face);
const fl = Math.round(fh * J.fuel);
rect(fx + 1, fy + fh - fl, 4, fl, J.fuel < 0.15 ? C.lampAmb : C.green);
for (let i = 0; i <= 4; i++) px(fx + 6, fy + Math.round(fh * i / 4), C.mark);
text('F', fx + 3, fy + fh + 3, C.markD, 'c');
// clock
drawClock(cx0 + S(83), y0 + S(87), S(13), new Date());
// warning lamps
const lx = cx0 + S(126), lw = Math.max(24, S(28));
const roller = J.rollT >= 0;
const armed = FX.slots.filter(s => s.state === 'armed').length;
const lamps = [
['PULLUP', J.pullUpT > 0, C.lampRed, true], ['LO ALT', J.agl < 45, C.lampAmb, false],
['OVER G', J.G > 6, C.lampRed, false], ['FUEL', J.fuel < 0.15, C.lampAmb, false],
['BURNER', J.abAmt > 0.5, C.lampAmb, false], ['ROLL', roller, C.lampGrn, false],
['ARM ' + armed, armed > 0, C.lampGrn, false], ['AUTO', J.autopilot, C.lampGrn, false],
];
lamps.forEach((l, i) => drawLamp(lx + (i % 2) * (lw + 3), y0 + S(2) + Math.floor(i / 2) * S(11), lw, l[0], l[1], l[2], l[3]));
// missile stations
for (let i = 0; i < 4; i++) {
const s = FX.slots[[3, 1, 0, 2][i]], xx = cx0 + S(132) + i * S(10), yy = y0 + S(54);
rect(xx, yy, S(6), S(16), C.panelDD);
const fill = s.state === 'armed' ? 1 : s.state === 'arming' ? s.s : 0;
if (fill > 0) rect(xx + 1, yy + 1 + Math.round(S(14) * (1 - fill)), S(6) - 2, Math.round(S(14) * fill), s.state === 'armed' ? C.white : C.orange);
}
text('MSL', cx0 + S(149), y0 + S(74), C.markD, 'c');
// throttle quadrant (lever follows the afterburner)
const tx = cx0 + S(196), ty0 = y0 + S(10), th = S(82);
text('THR', tx + 2, ty0 - S(8), C.markD, 'c');
rect(tx - 2, ty0 - 2, 9, th + 4, C.panelDD); rect(tx, ty0, 5, th, C.face);
const mil = ty0 + Math.round(th * 0.28);
text('AB', tx + 9, ty0, C.lampAmb); text('MIL', tx + 9, mil - 2, C.markD); text('IDL', tx + 9, ty0 + th - 5, C.markD);
for (let i = 0; i < 5; i++) px(tx - 3, mil + i * 2, C.mark);
const kyy = clamp(ty0 + Math.round((1 - J.throttle) * th), ty0 + 2, ty0 + th - 3);
rect(tx - 4, kyy - 3, 13, 7, C.panelDD); rect(tx - 3, kyy - 2, 11, 5, J.abAmt > 0.5 ? C.orange : C.markD); rect(tx - 3, kyy - 2, 11, 1, C.mark);
return top;
}
// ---- head-up display ----------------------------------------------------------------------------------
function drawHUD(panelTop) {
const W = UI.W, H = UI.H, J = Jet;
const u = Math.min(H / 340, W / 450, 1.35);
const cx = W / 2, cy = H / 2;
const hw = Math.round(Math.min(W * 0.46, 210 * u)), x0 = Math.round(cx - hw / 2), x1 = Math.round(cx + hw / 2);
const yTop = Math.round(H * 0.06), yBot = panelTop - 4;
const G = C.hud, S = C.hudS;
const P = (x, y) => { px(x + 1, y + 1, S); px(x, y, G); };
const L = (a, b, c, d, dash) => { line(a + 1, b + 1, c + 1, d + 1, S, dash); line(a, b, c, d, G, dash); };
const T = (s, x, y, al) => text(s, x, y, G, al, 1, S);
// combiner glass edges
for (let y = yBot - 30; y < yBot; y += 2) { px(x0 - 3, y, C.hudS); px(x1 + 3, y, C.hudS); }
// pitch ladder anchored to the true horizon (same projection as the world)
CLIP = [x0 + 18, yTop + 14, x1 - 18, yBot - 8];
const az = Math.atan2(Cam.F[0], -Cam.F[2]);
const dir = (th, a) => [Math.sin(a) * Math.cos(th), Math.sin(th), -Math.cos(a) * Math.cos(th)];
for (let deg = -85; deg <= 85; deg += 5) {
const th = deg * D2R;
const p = project(madd3(Cam.pos, dir(th, az), 1000), W, H);
const p2 = project(madd3(Cam.pos, dir(th, az + 0.02), 1000), W, H);
const pu = project(madd3(Cam.pos, dir(th + 0.02, az), 1000), W, H);
if (!p || !p2 || !pu) continue;
if (p[1] < yTop - 40 || p[1] > yBot + 40) continue;
let t = [p2[0] - p[0], p2[1] - p[1]]; const tl = Math.hypot(t[0], t[1]) || 1; t = [t[0] / tl, t[1] / tl];
let n = [-t[1], t[0]]; if ((pu[0] - p[0]) * n[0] + (pu[1] - p[1]) * n[1] < 0) n = [-n[0], -n[1]];
const gap = deg === 0 ? 10 : 7, half = deg === 0 ? hw * 0.42 : 20;
for (const s of [-1, 1]) {
const a = [p[0] + t[0] * gap * s, p[1] + t[1] * gap * s], b = [p[0] + t[0] * half * s, p[1] + t[1] * half * s];
L(a[0], a[1], b[0], b[1], deg < 0);
if (deg !== 0) {
const tk = deg > 0 ? -3 : 3; // ticks point towards the horizon
L(b[0], b[1], b[0] + n[0] * tk, b[1] + n[1] * tk);
T(String(Math.abs(deg)), b[0] + t[0] * s * 9 - 3 + (s < 0 ? -2 : 0), b[1] + t[1] * s * 9 - 2, 'c');
}
}
}
CLIP = null;
// boresight (waterline)
L(cx - 9, cy, cx - 4, cy); L(cx + 4, cy, cx + 9, cy); L(cx - 4, cy, cx - 2, cy + 2); L(cx + 4, cy, cx + 2, cy + 2); L(cx - 2, cy + 2, cx, cy); L(cx + 2, cy + 2, cx, cy);
// flight path marker
const fpm = project(madd3(Cam.pos, norm3(J.vel), 1000), W, H);
if (fpm && fpm[1] > yTop && fpm[1] < yBot) {
const fx = Math.round(fpm[0]), fy = Math.round(fpm[1]);
ring(fx, fy, 3, S); ring(fx, fy, 3, G);
L(fx - 8, fy, fx - 4, fy); L(fx + 4, fy, fx + 8, fy); L(fx, fy - 4, fx, fy - 7);
}
// heading tape
const hdg = ((Math.atan2(J.F[0], -J.F[2]) / D2R) + 360) % 360;
const ty = yTop + 2;
CLIP = [cx - 70, ty - 2, cx + 70, ty + 16];
for (let d = Math.floor((hdg - 35) / 5) * 5; d <= hdg + 35; d += 5) {
const x = Math.round(cx + (d - hdg) * 2.4);
const maj = ((d % 10) + 10) % 10 === 0;
L(x, ty + 8, x, ty + (maj ? 5 : 7));
if (maj && Math.abs(x - cx) < 60) { const v = ((d % 360) + 360) % 360; T(String(v).padStart(3, '0'), x, ty, 'c'); }
}
CLIP = null;
L(cx, ty + 10, cx - 2, ty + 12); L(cx, ty + 10, cx + 2, ty + 12);
// speed / altitude boxes
const kt = Math.round(J.V * 1.944), ft = Math.round(J.pos[1] * 3.281);
const by = cy - 3;
const bxL = x0 + 2, bxR = x1 - 26;
frame(bxL + 1, by - 2 + 1, 19, 9, S); frame(bxL, by - 2, 19, 9, G); T(String(kt), bxL + 10, by, 'c');
frame(bxR + 1, by - 2 + 1, 25, 9, S); frame(bxR, by - 2, 25, 9, G); T(String(ft), bxR + 13, by, 'c');
T('KT', bxL + 10, by - 10, 'c');
T('FT', bxR + 13, by - 10, 'c');
const ra = Math.round(J.agl * 3.281);
T('R' + String(Math.min(ra, 9999)).padStart(4, '0'), bxR + 13, by + 11, 'c');
T('VS' + (J.vel[1] >= 0 ? '+' : '-') + Math.abs(Math.round(J.vel[1] * 196.85 / 10) * 10), bxR + 13, by + 19, 'c');
T('G' + J.G.toFixed(1), bxL + 10, by + 11, 'c');
T('M' + (J.V / 340).toFixed(2).slice(1), bxL + 10, by + 19, 'c');
const armed = FX.slots.filter(s => s.state === 'armed').length;
T('MSL ' + armed, x0 + 6, yBot - 12);
if (J.abAmt > 0.5) T('AB', x0 + 6, yBot - 20);
if (J.autopilot) T('AUTO', x1 - 6, yBot - 12, 'r');
if (J.pullUpT > 0 && Math.floor(performance.now() / 240) % 2 === 0) T('PULL UP', cx, cy + 22, 'c');
else if (J.agl < 45) T('LO ALT', cx, cy + 22, 'c');
if (J.G > 6) T('OVER G', cx, cy + 30, 'c');
}
function drawCursor() {
if (Input.touchMode || !Input.has || !Input.started) return;
const W = UI.W, H = UI.H;
const r = UI.cv.getBoundingClientRect();
const x = Math.round((Input.mx - r.left) / r.width * W), y = Math.round((Input.my - r.top) / r.height * H);
if (Cam.topW > 0.5) {
const js = project(Jet.pos, W, H);
if (js) {
const dx = x - js[0], dy = y - js[1], d = Math.hypot(dx, dy);
for (let s = 14; s < d - 6; s += 4) px(js[0] + dx * s / d, js[1] + dy * s / d, C.cursor);
}
for (let i = -4; i <= 4; i++) { const k = 4 - Math.abs(i); px(x + i, y - k, C.cursor); px(x + i, y + k, C.cursor); }
px(x, y, C.cursor);
} else {
const cx = W >> 1, cy = H >> 1;
if (Cam.cockpitW < 0.5) for (const [a, b] of [[-3, 0], [3, 0], [0, -3], [0, 3]]) px(cx + a, cy + b, C.cursor);
for (const [a, b] of [[1, 0], [0, 1], [-1, 0], [0, -1]]) for (let k = 2; k <= 4; k++) { px(x + a * k + 1, y + b * k + 1, C.cursorD); }
for (const [a, b] of [[1, 0], [0, 1], [-1, 0], [0, -1]]) for (let k = 2; k <= 4; k++) px(x + a * k, y + b * k, C.cursor);
}
}
function drawOverlay() {
if (!UI.buf) return;
UI.buf.fill(0);
const cw = Cam.cockpitW;
if (cw > 0.001) {
const H = UI.H, u = Math.min(H / 340, UI.W / 450, 1.35);
const PH = Math.round(118 * u) + UI.stripPx;
const off = Math.round((1 - cw) * (PH + 20));
const top = drawPanel(off);
if (cw > 0.97) drawHUD(top);
}
drawCursor();
UI.ctx.putImageData(UI.img, 0, 0);
}
// ===== synthesised sound (off by default) ==========================================================
const Audio_ = {
ctx: null, on: false, nodes: null,
ensure() {
if (this.ctx) return true;
const AC = window.AudioContext || window.webkitAudioContext;
if (!AC) return false;
const ctx = this.ctx = new AC();
const mk = (sec, brown) => {
const b = ctx.createBuffer(1, ctx.sampleRate * sec, ctx.sampleRate), d = b.getChannelData(0);
let last = 0;
for (let i = 0; i < d.length; i++) { const w = Math.random() * 2 - 1; if (brown) { last = (last + 0.02 * w) / 1.02; d[i] = last * 3.5; } else d[i] = w; }
return b;
};
this.white = mk(2, false); this.brown = mk(3, true);
const master = ctx.createGain(); master.gain.value = 0;
const comp = ctx.createDynamicsCompressor(); comp.threshold.value = -18; comp.ratio.value = 4;
master.connect(comp); comp.connect(ctx.destination);
const loop = (buf) => { const s = ctx.createBufferSource(); s.buffer = buf; s.loop = true; s.start(); return s; };
// engine rumble
const rum = loop(this.brown), rumF = ctx.createBiquadFilter(); rumF.type = 'lowpass'; rumF.frequency.value = 260;
const rumG = ctx.createGain(); rumG.gain.value = 0.5; rum.connect(rumF); rumF.connect(rumG); rumG.connect(master);
// turbine whine
const o1 = ctx.createOscillator(), o2 = ctx.createOscillator(); o1.type = 'sawtooth'; o2.type = 'triangle';
o1.frequency.value = 2200; o2.frequency.value = 3310;
const wF = ctx.createBiquadFilter(); wF.type = 'bandpass'; wF.frequency.value = 2600; wF.Q.value = 6;
const wG = ctx.createGain(); wG.gain.value = 0.012; o1.connect(wF); o2.connect(wF); wF.connect(wG); wG.connect(master); o1.start(); o2.start();
// afterburner roar
const ab = loop(this.white), abF = ctx.createBiquadFilter(); abF.type = 'lowpass'; abF.frequency.value = 700;
const abG = ctx.createGain(); abG.gain.value = 0; ab.connect(abF); abF.connect(abG); abG.connect(master);
// wind
const wi = loop(this.white), wiF = ctx.createBiquadFilter(); wiF.type = 'bandpass'; wiF.frequency.value = 900; wiF.Q.value = 0.7;
const wiG = ctx.createGain(); wiG.gain.value = 0.05; wi.connect(wiF); wiF.connect(wiG); wiG.connect(master);
// warning tone
const wa = ctx.createOscillator(); wa.type = 'square'; wa.frequency.value = 880;
const waG = ctx.createGain(); waG.gain.value = 0; wa.connect(waG); waG.connect(master); wa.start();
this.nodes = { master, rumG, rumF, o1, o2, wG, abG, abF, wiG, wiF, waG };
return true;
},
setOn(v) {
if (v && !this.ensure()) return;
this.on = v;
if (!this.ctx) return;
if (v && this.ctx.state === 'suspended') this.ctx.resume();
this.nodes.master.gain.setTargetAtTime(v ? 0.55 : 0, this.ctx.currentTime, 0.08);
},
update(J, t) {
if (!this.on || !this.nodes) return;
const n = this.nodes, c = this.ctx.currentTime, k = 0.06;
const rpm = (J.rpm[0] + J.rpm[1]) / 200;
n.o1.frequency.setTargetAtTime(1600 + rpm * 900, c, k); n.o2.frequency.setTargetAtTime(2400 + rpm * 1300, c, k);
n.wG.gain.setTargetAtTime(0.006 + 0.012 * rpm, c, k);
n.rumG.gain.setTargetAtTime(0.35 + 0.35 * J.abAmt, c, k);
n.rumF.frequency.setTargetAtTime(200 + 160 * J.abAmt, c, k);
n.abG.gain.setTargetAtTime(J.abAmt * 0.32, c, k);
n.abF.frequency.setTargetAtTime(500 + 900 * J.abAmt, c, k);
const sp = J.V / 180;
n.wiG.gain.setTargetAtTime(0.025 + 0.06 * sp * sp + (J.rollT >= 0 ? 0.04 : 0), c, k);
n.wiF.frequency.setTargetAtTime(500 + 900 * sp + Math.max(0, J.G - 2) * 120, c, k);
const beep = J.pullUpT > 0 && Math.floor(t * 5) % 2 === 0;
n.waG.gain.setTargetAtTime(beep ? 0.025 : 0, c, 0.01);
},
burst(buf, dur, f0, f1, gain, type, delay) {
if (!this.on || !this.ctx) return;
const ctx = this.ctx, t0 = ctx.currentTime + (delay || 0);
const s = ctx.createBufferSource(); s.buffer = buf;
const f = ctx.createBiquadFilter(); f.type = type || 'lowpass'; f.frequency.setValueAtTime(f0, t0); f.frequency.exponentialRampToValueAtTime(Math.max(30, f1), t0 + dur);
const g = ctx.createGain(); g.gain.setValueAtTime(0.0001, t0); g.gain.exponentialRampToValueAtTime(gain, t0 + 0.02); g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
s.connect(f); f.connect(g); g.connect(this.nodes.master); s.start(t0); s.stop(t0 + dur + 0.05);
},
launch() { this.burst(this.white, 1.3, 3200, 380, 0.5, 'bandpass'); this.burst(this.brown, 0.6, 600, 120, 0.6); },
boom(dist) {
if (!this.on || !this.ctx) return;
const g = clamp(1.4 - dist / 2200, 0.08, 1.2), delay = Math.min(2.2, dist / 340);
this.burst(this.brown, 2.6, 900, 40, g, 'lowpass', delay);
this.burst(this.white, 0.35, 2400, 300, g * 0.4, 'lowpass', delay);
const ctx = this.ctx, t0 = ctx.currentTime + delay;
const o = ctx.createOscillator(); o.frequency.setValueAtTime(70, t0); o.frequency.exponentialRampToValueAtTime(28, t0 + 0.8);
const og = ctx.createGain(); og.gain.setValueAtTime(g * 0.9, t0); og.gain.exponentialRampToValueAtTime(0.0001, t0 + 0.9);
o.connect(og); og.connect(this.nodes.master); o.start(t0); o.stop(t0 + 1);
},
whoosh() { this.burst(this.white, 1.0, 400, 2400, 0.25, 'bandpass'); },
click() {
if (!this.on || !this.ctx) return;
const ctx = this.ctx, t0 = ctx.currentTime;
const o = ctx.createOscillator(); o.type = 'square'; o.frequency.value = 220;
const g = ctx.createGain(); g.gain.setValueAtTime(0.06, t0); g.gain.exponentialRampToValueAtTime(0.0001, t0 + 0.08);
o.connect(g); g.connect(this.nodes.master); o.start(t0); o.stop(t0 + 0.1);
},
};
// ===== main: GL, render passes, loop, input ==========================================================
(function () {
const $ = (id) => document.getElementById(id);
const glCanvas = $('gl'), uiCanvas = $('ui');
const errors = [];
window.addEventListener('error', (e) => errors.push(String(e.message || e)));
window.addEventListener('unhandledrejection', (e) => errors.push('promise: ' + String(e.reason)));
const params = new URLSearchParams(location.search);
const gl = glCanvas.getContext('webgl2', { antialias: false, alpha: false, depth: false, premultipliedAlpha: false, powerPreference: 'high-performance' });
uiInit(uiCanvas);
if (!gl) { $('status').textContent = 'This toy needs WebGL2, which this browser does not provide.'; $('bar').style.display = 'none'; return; }
gl.getExtension('EXT_color_buffer_float');
// ---------------------------------------------------------------- programs
function compile(type, src) {
const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) { const log = gl.getShaderInfoLog(s); console.error(log); errors.push('shader: ' + log); }
return s;
}
function program(vs, fs) {
const p = gl.createProgram();
gl.attachShader(p, compile(gl.VERTEX_SHADER, vs)); gl.attachShader(p, compile(gl.FRAGMENT_SHADER, fs));
gl.linkProgram(p);
if (!gl.getProgramParameter(p, gl.LINK_STATUS)) { const log = gl.getProgramInfoLog(p); console.error(log); errors.push('link: ' + log); }
const cache = {};
p.u = (n) => (n in cache ? cache[n] : (cache[n] = gl.getUniformLocation(p, n)));
p.a = (n) => gl.getAttribLocation(p, n);
return p;
}
const PAL_FLAT = new Float32Array(PALETTE.flat());
const progWorld = program(VS_QUAD, FS_WORLD);
const progMesh = program(VS_MESH, FS_MESH);
const progSprite = program(VS_SPRITE, FS_SPRITE);
const progLine = program(VS_LINE, FS_LINE);
const progPost = program(VS_QUAD, FS_POST.replace(/PALN/g, String(PALETTE.length)));
// ---------------------------------------------------------------- geometry
const triVAO = gl.createVertexArray(); gl.bindVertexArray(triVAO);
const triBuf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, triBuf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
for (const p of [progWorld, progPost]) { const l = p.a('aP'); if (l >= 0) { gl.enableVertexAttribArray(l); gl.vertexAttribPointer(l, 2, gl.FLOAT, false, 0, 0); } }
function meshVAO(mb) {
const vao = gl.createVertexArray(); gl.bindVertexArray(vao);
const put = (arr, name, n) => {
const b = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, b); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arr), gl.STATIC_DRAW);
const l = progMesh.a(name); if (l >= 0) { gl.enableVertexAttribArray(l); gl.vertexAttribPointer(l, n, gl.FLOAT, false, 0, 0); }
};
put(mb.P, 'aPos', 3); put(mb.N, 'aNrm', 3); put(mb.A, 'aMat', 2);
return { vao, n: mb.count() };
}
const jetMesh = meshVAO(buildJet());
const mslMesh = meshVAO(buildMissile());
const MAXS = 3400;
const spriteData = new Float32Array(MAXS * 16);
const spriteVAO = gl.createVertexArray(); gl.bindVertexArray(spriteVAO);
const cornerBuf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cornerBuf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
{ const l = progSprite.a('aC'); gl.enableVertexAttribArray(l); gl.vertexAttribPointer(l, 2, gl.FLOAT, false, 0, 0); }
const instBuf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, instBuf); gl.bufferData(gl.ARRAY_BUFFER, spriteData.byteLength, gl.DYNAMIC_DRAW);
['aP', 'aCol', 'aCore', 'aK'].forEach((n, i) => { const l = progSprite.a(n); if (l < 0) return; gl.enableVertexAttribArray(l); gl.vertexAttribPointer(l, 4, gl.FLOAT, false, 64, i * 16); gl.vertexAttribDivisor(l, 1); });
const MAXL = 2400;
const lineData = new Float32Array(MAXL * 4);
const lineVAO = gl.createVertexArray(); gl.bindVertexArray(lineVAO);
const lineBuf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, lineBuf); gl.bufferData(gl.ARRAY_BUFFER, lineData.byteLength, gl.DYNAMIC_DRAW);
{ const l = progLine.a('aP'); gl.enableVertexAttribArray(l); gl.vertexAttribPointer(l, 4, gl.FLOAT, false, 0, 0); }
gl.bindVertexArray(null);
// ---------------------------------------------------------------- textures
function tex(w, h, ifmt, fmt, type, data, filter, wrap) {
const t = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, t);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.texImage2D(gl.TEXTURE_2D, 0, ifmt, w, h, 0, fmt, type, data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
return t;
}
const noise = new Uint8Array(256 * 256 * 4);
{ let s = 12345; for (let i = 0; i < noise.length; i++) { s = (s * 1103515245 + 12345) >>> 0; noise[i] = (s >>> 16) & 255; } }
// soften the noise a touch so it reads as value noise
const noise2 = new Uint8Array(noise.length);
for (let y = 0; y < 256; y++) for (let x = 0; x < 256; x++) for (let c = 0; c < 4; c++) {
let s = 0; for (let j = -1; j <= 1; j++) for (let i = -1; i <= 1; i++) s += noise[(((y + j) & 255) * 256 + ((x + i) & 255)) * 4 + c] * (i === 0 && j === 0 ? 4 : (i === 0 || j === 0 ? 2 : 1));
noise2[(y * 256 + x) * 4 + c] = Math.round(s / 16);
}
// stretch the contrast back out after the blur
for (let i = 0; i < noise2.length; i++) noise2[i] = clamp(Math.round((noise2[i] - 128) * 2.1 + 128), 0, 255);
const texNoise = tex(256, 256, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, noise2, gl.LINEAR, gl.REPEAT);
let texTer = null, texHgt = null, texMax = null, texMax2 = null, texCld = null;
// ---------------------------------------------------------------- framebuffer at internal resolution
let W = 0, H = 0, fbo = null, fboTex = null, fboDepth = null;
const Q = { levels: [340, 304, 270, 238, 208], idx: 0, locked: false, hist: [], cool: 0, recoverAt: 0, upgrades: 0 };
if (params.has('q')) { Q.idx = clamp(parseInt(params.get('q'), 10) || 0, 0, Q.levels.length - 1); Q.locked = true; }
function resize() {
const dpr = window.devicePixelRatio || 1;
const cssW = window.innerWidth, cssH = window.innerHeight;
const pw = Math.max(1, Math.round(cssW * dpr)), ph = Math.max(1, Math.round(cssH * dpr));
const aspect = pw / ph;
let target = Q.levels[Q.idx];
const minW = Math.round(target * 0.95);
if (target * aspect < minW) target = Math.round(minW / aspect);
let s = ph / target;
const si = Math.round(s);
if (si >= 1 && Math.abs(ph / si - target) / target < 0.1) s = si;
s = Math.max(s, 1);
const nH = Math.ceil(ph / s), nW = Math.ceil(pw / s);
const cw = (nW * s / dpr) + 'px', ch = (nH * s / dpr) + 'px';
for (const c of [glCanvas, uiCanvas]) { c.style.width = cw; c.style.height = ch; }
UI.stripPx = Math.ceil(30 * dpr / s);
if (nW === W && nH === H) return;
W = nW; H = nH;
glCanvas.width = W; glCanvas.height = H;
uiResize(W, H);
if (fbo) { gl.deleteFramebuffer(fbo); gl.deleteTexture(fboTex); gl.deleteRenderbuffer(fboDepth); }
fboTex = tex(W, H, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, null, gl.NEAREST, gl.CLAMP_TO_EDGE);
fboDepth = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, fboDepth); gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT24, W, H);
fbo = gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fboTex, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, fboDepth);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
Cam.aspect = W / H;
Q.hist.length = 0; Q.cool = 1.5;
}
window.addEventListener('resize', resize);
resize();
// ---------------------------------------------------------------- generation
const statusMsg = { height: 'Raising hills, filling lakes', cloud: 'Gathering clouds', bake: 'Baking long shadows' };
generateWorld((f, job) => {
$('barFill').style.width = (f * 100).toFixed(1) + '%';
$('status').textContent = statusMsg[job] || '';
}).then((res) => {
texTer = tex(TN, TN, gl.RGBA16F, gl.RGBA, gl.FLOAT, res.bake, gl.LINEAR, gl.REPEAT);
texHgt = tex(TN, TN, gl.R16F, gl.RED, gl.FLOAT, World.H, gl.LINEAR, gl.REPEAT);
texMax = tex(res.gridN, res.gridN, gl.R16F, gl.RED, gl.FLOAT, res.grid, gl.NEAREST, gl.REPEAT);
texMax2 = tex(res.grid2N, res.grid2N, gl.R16F, gl.RED, gl.FLOAT, res.grid2, gl.NEAREST, gl.REPEAT);
texCld = tex(CLOUD_N, CLOUD_N, gl.R8, gl.RED, gl.UNSIGNED_BYTE, res.cloud, gl.LINEAR, gl.REPEAT);
World.cloud = res.cloud;
// start over open country near a lake, heading roughly toward the low sun
let best = null;
for (let k = 0; k < 400; k++) {
const x = (k * 733.13) % TILE, z = (k * 1171.7 + 911) % TILE;
const h = World.h(x, z); let water = 0;
for (let a = 0; a < 8; a++) if (World.h(x + Math.cos(a) * 500, z + Math.sin(a) * 500) < 0) water++;
const sc = (h < 0 ? 2 : 0) + Math.min(water, 4) - Math.max(0, h - 60) / 40;
if (!best || sc > best.sc) best = { x, z, sc };
}
Jet.pos = [best.x, CRUISE_ALT, best.z];
Jet.psi = Math.atan2(SUN[0], -SUN[2]) + 0.9;
$('bar').style.display = 'none'; $('status').style.display = 'none';
$('go').style.display = 'block';
document.body.classList.add('ready');
window.__patrolReady = true;
});
// ---------------------------------------------------------------- input
const hint = $('hints');
function start() {
if (Input.started || !World.ready) return;
Input.started = true;
$('title').classList.add('gone');
document.body.classList.add('playing');
}
let swallowUp = false;
window.addEventListener('mousemove', (e) => {
const nx = e.clientX, ny = e.clientY;
if (Input.started && Input.has && (Math.abs(nx - Input.mx) + Math.abs(ny - Input.my) > 0)) Jet.autopilot = false;
Input.mx = nx; Input.my = ny; Input.has = true; Input.touchMode = false;
if (!(e.buttons & 1) && Input.left) releaseLeft(false);
});
function releaseLeft(allowRoll) {
if (!Input.left) return;
const held = performance.now() - Input.leftT;
Input.left = false;
if (allowRoll && held < 220 && !Jet.ab) jetBarrelRoll();
}
window.addEventListener('mousedown', (e) => {
if (e.target.closest && e.target.closest('#hints button')) return;
if (!Input.started) { if (World.ready) { start(); swallowUp = true; } return; }
Input.mx = e.clientX; Input.my = e.clientY; Input.has = true;
if (e.button === 0) { Input.left = true; Input.leftT = performance.now(); }
else if (e.button === 2) fireMissile();
e.preventDefault();
});
window.addEventListener('mouseup', (e) => {
if (e.button === 0) { if (swallowUp) { swallowUp = false; return; } releaseLeft(true); }
});
window.addEventListener('contextmenu', (e) => e.preventDefault());
window.addEventListener('blur', () => { Input.left = false; Input.keyAB = false; Input.touchAB = false; });
window.addEventListener('keydown', (e) => {
if (e.code === 'Space') { e.preventDefault(); if (!Input.started) { start(); return; } if (!e.repeat) setView((Cam.view + 1) % 3); }
else if (e.code === 'KeyF') { if (!e.repeat && Input.started) fireMissile(); }
else if (e.code === 'KeyM') { toggleSound(); }
else if (e.key === 'Shift') Input.keyAB = true;
else if (e.code === 'Enter' && !Input.started) start();
});
window.addEventListener('keyup', (e) => { if (e.key === 'Shift') Input.keyAB = false; });
function toggleSound() {
Audio_.setOn(!Audio_.on);
$('snd').textContent = Audio_.on ? 'SOUND ON' : 'SOUND OFF';
$('snd').classList.toggle('on', Audio_.on);
}
$('snd').addEventListener('click', (e) => { e.stopPropagation(); toggleSound(); });
$('snd').addEventListener('mousedown', (e) => e.stopPropagation());
// touch: steer by touching the screen, buttons for the rest
const touches = {};
function cssToInternal(x, y) { const r = glCanvas.getBoundingClientRect(); return [(x - r.left) / r.width * W, (y - r.top) / r.height * H]; }
window.addEventListener('touchstart', (e) => {
document.body.classList.add('touch');
Input.touchMode = true;
if (!Input.started) { if (World.ready) start(); e.preventDefault(); return; }
for (const t of e.changedTouches) {
const btn = t.target.closest && t.target.closest('[data-t]');
if (btn) {
const k = btn.dataset.t; touches[t.identifier] = { btn: k }; btn.classList.add('down');
if (k === 'fire') fireMissile(); else if (k === 'roll') jetBarrelRoll(); else if (k === 'view') setView((Cam.view + 1) % 3); else if (k === 'ab') Input.touchAB = true; else if (k === 'snd') toggleSound();
} else if (!t.target.closest('#hints')) {
touches[t.identifier] = { steer: true, sx: t.clientX, sy: t.clientY };
Jet.autopilot = false;
updateSteer(t);
}
}
e.preventDefault();
}, { passive: false });
function updateSteer(t) {
const s = touches[t.identifier];
if (Cam.view === 0) { const p = cssToInternal(t.clientX, t.clientY); Input.touchSteer = { x: p[0], y: p[1], abs: true }; }
else {
const k = Math.min(window.innerWidth, window.innerHeight) * 0.22;
Input.touchSteer = { ox: clamp((t.clientX - s.sx) / k, -1, 1), oy: clamp((t.clientY - s.sy) / k, -1, 1), abs: false };
}
}
window.addEventListener('touchmove', (e) => { for (const t of e.changedTouches) if (touches[t.identifier] && touches[t.identifier].steer) updateSteer(t); e.preventDefault(); }, { passive: false });
const tend = (e) => {
for (const t of e.changedTouches) {
const s = touches[t.identifier]; if (!s) continue;
if (s.steer) Input.touchSteer = null;
if (s.btn) { document.querySelectorAll('[data-t="' + s.btn + '"]').forEach(b => b.classList.remove('down')); if (s.btn === 'ab') Input.touchAB = false; }
delete touches[t.identifier];
}
};
window.addEventListener('touchend', tend); window.addEventListener('touchcancel', tend);
// ---------------------------------------------------------------- per-frame simulation
let lastHint = '';
function step(dt) {
FX.time += dt;
Jet.ab = (Input.left && performance.now() - Input.leftT > 220) || Input.touchAB || !!Input.keyAB || !!Input.forceAB;
Input.rect = glCanvas.getBoundingClientRect();
const ctl = computeControls(W, H);
updateJet(dt, ctl);
updateMissiles(dt);
updateJetFX(dt);
updateParticles(dt);
updateCamera(dt);
radarUpdate(dt, FX.time);
FX.flash = Math.max(0, FX.flash - dt * 3.2);
Audio_.update(Jet, FX.time);
rebase();
// keep touch buttons clear of the instrument panel
const cw = Math.round(Cam.cockpitW * 20) / 20;
if (cw !== step.lastCw) {
step.lastCw = cw;
const u = Math.min(H / 340, W / 450, 1.35), ph = Math.round(118 * u) + UI.stripPx;
const css = ph * glCanvas.getBoundingClientRect().height / H;
$('touch').style.bottom = Math.max(30, Math.round(css * cw)) + 'px';
}
// hint strip: missile counter + current view
const pips = FX.slots.map(s => s.state === 'armed' ? 'a' : s.state === 'arming' ? 'r' : 'e').join('') + VIEWS[Cam.view];
if (pips !== lastHint) {
lastHint = pips;
$('msl').innerHTML = 'MSL ' + FX.slots.map(s => '<i class="' + (s.state === 'armed' ? 'a' : s.state === 'arming' ? 'r' : 'e') + '"></i>').join('');
$('viewName').textContent = VIEWS[Cam.view];
}
}
function rebase() {
const sx = Math.abs(Jet.pos[0]) > TILE * 1.5 ? -Math.round(Jet.pos[0] / TILE) * TILE : 0;
const sz = Math.abs(Jet.pos[2]) > TILE * 1.5 ? -Math.round(Jet.pos[2] / TILE) * TILE : 0;
if (!sx && !sz) return;
const sh = (p) => { p[0] += sx; p[2] += sz; };
sh(Jet.pos); sh(Jet.rpos); sh(Cam.pos);
for (const q of FX.parts) sh(q.p);
for (const m of FX.missiles) { sh(m.p); sh(m.target); }
for (const im of FX.impacts) { im.x += sx; im.z += sz; }
for (const w of FX.wake) { w.x += sx; w.z += sz; }
for (const tr of FX.trails) for (const t of tr) sh(t.p);
}
// ---------------------------------------------------------------- rendering
const impArr = new Float32Array(32), impBArr = new Float32Array(32), wakeArr = new Float32Array(96), mslArr = new Float32Array(16);
const frameSpr = [];
function render() {
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.viewport(0, 0, W, H);
gl.disable(gl.BLEND); gl.disable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST); gl.depthMask(true); gl.colorMask(true, true, true, true);
// --- world
gl.depthFunc(gl.ALWAYS);
gl.useProgram(progWorld);
const p = progWorld;
gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, texTer); gl.uniform1i(p.u('uTer'), 0);
gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, texMax); gl.uniform1i(p.u('uMaxH'), 1);
gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, texCld); gl.uniform1i(p.u('uCld'), 2);
gl.activeTexture(gl.TEXTURE3); gl.bindTexture(gl.TEXTURE_2D, texNoise); gl.uniform1i(p.u('uNoise'), 3);
gl.activeTexture(gl.TEXTURE4); gl.bindTexture(gl.TEXTURE_2D, texMax2); gl.uniform1i(p.u('uMaxH2'), 4);
gl.activeTexture(gl.TEXTURE5); gl.bindTexture(gl.TEXTURE_2D, texHgt); gl.uniform1i(p.u('uHgt'), 5);
setCommon(p);
gl.uniform2f(p.u('uTan'), Cam.tanX, Cam.tanY);
gl.uniform2f(p.u('uRes'), W, H);
gl.uniform1f(p.u('uNear'), 0.5); gl.uniform1f(p.u('uFar'), 30000);
gl.uniform1f(p.u('uPixAng'), 2 * Cam.tanY / H);
gl.uniform2f(p.u('uCloudOff'), (FX.time * 3.2) % TILE, (FX.time * 1.3) % TILE);
gl.uniform3fv(p.u('uJetPos'), Jet.rpos); gl.uniform3fv(p.u('uJetR'), Jet.R); gl.uniform3fv(p.u('uJetU'), Jet.U); gl.uniform3fv(p.u('uJetF'), Jet.F);
gl.uniform3fv(p.u('uJetSun'), JET_SUN); gl.uniform1f(p.u('uJetVis'), 1);
impArr.fill(0); impBArr.fill(0);
FX.impacts.forEach((im, i) => { impArr.set([im.x, im.z, im.t0, im.type], i * 4); impBArr.set([im.y, im.size, 0, 0], i * 4); });
gl.uniform4fv(p.u('uImp'), impArr); gl.uniform4fv(p.u('uImpB'), impBArr);
wakeArr.fill(0);
FX.wake.forEach((w, i) => { if (i < 24) wakeArr.set([w.x, w.z, w.t0, w.s], i * 4); });
gl.uniform4fv(p.u('uWake'), wakeArr); gl.uniform1i(p.u('uWakeN'), Math.min(24, FX.wake.length));
mslArr.fill(0);
FX.missiles.slice(0, 4).forEach((m, i) => { if (m.age > 0.3) mslArr.set([m.p[0], m.p[1], m.p[2], 1], i * 4); });
gl.uniform4fv(p.u('uMsl'), mslArr);
gl.bindVertexArray(triVAO);
gl.drawArrays(gl.TRIANGLES, 0, 3);
// --- meshes
const V = viewMat(Cam.pos, Cam.R, Cam.U, Cam.F), P = projMat(Cam.tanX, Cam.tanY, 0.5, 30000), VP = mulMat(P, V);
gl.depthFunc(gl.LESS);
gl.useProgram(progMesh);
setCommon(progMesh);
gl.uniformMatrix4fv(progMesh.u('uVP'), false, VP);
gl.uniform1f(progMesh.u('uMask'), 1);
if (!Jet.hidden) {
gl.uniformMatrix4fv(progMesh.u('uM'), false, modelMat(Jet.rpos, Jet.R, Jet.U, Jet.F));
gl.uniform4fv(progMesh.u('uSlot'), FX.slots.map(s => s.state === 'armed' ? 1 : s.state === 'arming' ? Math.max(0.01, s.s) : 0));
gl.uniform1f(progMesh.u('uGlow'), Jet.abAmt);
gl.bindVertexArray(jetMesh.vao); gl.drawArrays(gl.TRIANGLES, 0, jetMesh.n);
}
gl.uniform4fv(progMesh.u('uSlot'), [1, 1, 1, 1]);
gl.bindVertexArray(mslMesh.vao);
for (const m of FX.missiles) {
const F = m.F, up = Math.abs(F[1]) > 0.95 ? [1, 0, 0] : [0, 1, 0];
const R = norm3(cross3(F, up)), U = cross3(R, F);
gl.uniformMatrix4fv(progMesh.u('uM'), false, modelMat(m.p, R, U, F));
gl.drawArrays(gl.TRIANGLES, 0, mslMesh.n);
}
// --- sprites (dithered transparency, no depth writes, alpha channel kept for the outline mask)
gl.depthMask(false); gl.colorMask(true, true, true, false); gl.depthFunc(gl.LEQUAL);
frameSpr.length = 0; frameSprites(frameSpr);
let n = 0;
const put = (x, y, z, s, r, g, b, a, cr, cg, cb, cf, type, rot, fog) => {
if (n >= MAXS) return; const o = n * 16;
spriteData[o] = x; spriteData[o + 1] = y; spriteData[o + 2] = z; spriteData[o + 3] = s;
spriteData[o + 4] = r; spriteData[o + 5] = g; spriteData[o + 6] = b; spriteData[o + 7] = a;
spriteData[o + 8] = cr; spriteData[o + 9] = cg; spriteData[o + 10] = cb; spriteData[o + 11] = cf;
spriteData[o + 12] = type; spriteData[o + 13] = rot; spriteData[o + 14] = fog; spriteData[o + 15] = 0; n++;
};
for (const q of FX.parts) {
const t = q.age / q.life, e = 1 - (1 - t) * (1 - t);
const s = q.s0 + (q.s1 - q.s0) * e;
const c0 = q.c0, c1 = q.c1;
const fin = q.fin > 0 ? Math.min(1, q.age / q.fin) : 1;
const at = q.ap === 1 ? t : Math.pow(t, q.ap);
put(q.p[0], q.p[1], q.p[2], s, c0[0] + (c1[0] - c0[0]) * t, c0[1] + (c1[1] - c0[1]) * t, c0[2] + (c1[2] - c0[2]) * t, (q.a0 + (q.a1 - q.a0) * at) * fin,
q.core[0], q.core[1], q.core[2], q.core[3] * (1 - t * 0.6), q.type, q.rot, q.fog);
}
for (const f of frameSpr) put(f.p[0], f.p[1], f.p[2], f.s, f.c[0], f.c[1], f.c[2], f.a, f.core[0], f.core[1], f.core[2], f.core[3], f.type, 0, f.fog);
if (n > 0) {
gl.useProgram(progSprite); setCommon(progSprite);
gl.uniformMatrix4fv(progSprite.u('uV'), false, V); gl.uniformMatrix4fv(progSprite.u('uP'), false, P);
gl.bindVertexArray(spriteVAO);
gl.bindBuffer(gl.ARRAY_BUFFER, instBuf); gl.bufferSubData(gl.ARRAY_BUFFER, 0, spriteData, 0, n * 16);
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, n);
}
// --- vapour trails
let ln = 0;
for (const tr of FX.trails) {
for (let i = 1; i < tr.length && ln < MAXL - 2; i++) {
const a = tr[i - 1], b = tr[i];
const fa = a.k * Math.min(1, (1 - (FX.time - a.t) / 2.4) * 2.2), fb = b.k * Math.min(1, (1 - (FX.time - b.t) / 2.4) * 2.2);
lineData.set([a.p[0], a.p[1], a.p[2], fa, b.p[0], b.p[1], b.p[2], fb], ln * 4); ln += 2;
}
}
if (ln > 0) {
gl.useProgram(progLine); setCommon(progLine);
gl.uniformMatrix4fv(progLine.u('uVP'), false, VP);
gl.bindVertexArray(lineVAO);
gl.bindBuffer(gl.ARRAY_BUFFER, lineBuf); gl.bufferSubData(gl.ARRAY_BUFFER, 0, lineData, 0, ln * 4);
gl.drawArrays(gl.LINES, 0, ln);
}
gl.depthMask(true); gl.colorMask(true, true, true, true);
// --- post: outline + palette dither to the screen
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, W, H);
gl.disable(gl.DEPTH_TEST);
gl.useProgram(progPost);
gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, fboTex); gl.uniform1i(progPost.u('uScene'), 0);
gl.uniform3fv(progPost.u('uPal'), PAL_FLAT);
gl.uniform1f(progPost.u('uFlash'), FX.flash);
gl.uniform2f(progPost.u('uRes'), W, H);
gl.bindVertexArray(triVAO);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.bindVertexArray(null);
}
function setCommon(p) {
gl.uniform3fv(p.u('uSun'), SUN);
gl.uniform3fv(p.u('uCamPos'), Cam.pos); gl.uniform3fv(p.u('uCamR'), Cam.R); gl.uniform3fv(p.u('uCamU'), Cam.U); gl.uniform3fv(p.u('uCamF'), Cam.F);
gl.uniform1f(p.u('uTime'), FX.time);
}
// ---------------------------------------------------------------- quality: drop resolution when frames get slow
function quality(dtMs) {
if (Q.locked) return;
if (Q.cool > 0) { Q.cool -= dtMs / 1000; return; }
Q.hist.push(dtMs);
if (Q.hist.length < 50) return;
if (Q.hist.length > 50) Q.hist.shift();
const sorted = Q.hist.slice().sort((a, b) => a - b), med = sorted[25];
const now = performance.now();
if (med > 24 && Q.idx < Q.levels.length - 1) { Q.idx++; Q.recoverAt = now + 12000; resize(); }
else if (med < 10 && Q.idx > 0 && now > Q.recoverAt && Q.upgrades < 3) { Q.idx--; Q.upgrades++; Q.recoverAt = now + 15000; resize(); }
}
// ---------------------------------------------------------------- loop
let last = performance.now();
let frames = 0;
function frame(now) {
requestAnimationFrame(frame);
const rawMs = now - last; last = now;
const dt = clamp(rawMs / 1000, 0, 0.05);
if (!World.ready || !texTer) return;
step(dt);
render();
drawOverlay();
frames++;
if (frames > 30 && !document.hidden) quality(rawMs);
}
requestAnimationFrame(frame);
// ---------------------------------------------------------------- hooks for automated testing
window.PATROL = {
errors, Jet, Cam, FX, Input, World, Q,
ready: () => World.ready && !!texTer,
start,
setView: (v, instant) => setView(v, instant),
fire: () => fireMissile(),
roll: () => jetBarrelRoll(),
res: () => [W, H],
// advance the simulation with a fixed step; render every `every` steps (and always the last)
simulate(sec, opts) {
opts = opts || {};
const dt = opts.dt || 1 / 60, steps = Math.round(sec / dt), every = opts.every || 0;
let t0 = performance.now();
for (let i = 0; i < steps; i++) {
if (opts.random && i % 20 === 0) {
Input.has = true; Jet.autopilot = false;
const r = glCanvas.getBoundingClientRect();
Input.mx = r.left + Math.random() * r.width; Input.my = r.top + Math.random() * r.height;
const k = Math.random();
if (k < 0.04) fireMissile(); else if (k < 0.06) jetBarrelRoll(); else if (k < 0.075) setView((Cam.view + 1) % 3);
Input.forceAB = Math.random() < 0.25;
}
step(dt);
if ((every && i % every === 0) || i === steps - 1) { render(); drawOverlay(); }
}
Input.forceAB = false;
return { ms: performance.now() - t0, errors: errors.slice() };
},
renderNow() { render(); drawOverlay(); },
setJet(o) { Object.assign(Jet, o); Jet.autopilot = false; Input.has = false; },
hudHorizon() { // screen y of the HUD horizon at the centre column, plus the camera pitch
const az = Math.atan2(Cam.F[0], -Cam.F[2]);
const p = project(madd3(Cam.pos, [Math.sin(az), 0, -Math.cos(az)], 1000), W, H);
return { y: p && p[1], x: p && p[0], H, W, pitch: Math.asin(Cam.F[1]) / D2R };
},
};
})();
</script>
</body>
</html>