Source code
Play itNeon Skyway
Exactly what your browser runs — written by Claude. 2,396 lines across 1 file, plus the shared arcade kit (pause menu, audio, scores) every game loads.
games/play/neon-flap.html · 2,396 lines
<!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"/>
<title>Neon Skyway — PromptArcade</title>
<style>
html,body{margin:0;height:100%;background:#07040f;color:#eef6ff;overflow:hidden;font-family:Manrope,system-ui,sans-serif;touch-action:none;-webkit-user-select:none;user-select:none}
#game{position:fixed;inset:0;z-index:1}
#game canvas{display:block;width:100%;height:100%}
#fx{position:fixed;inset:0;z-index:2;pointer-events:none}
#sk-hud{opacity:0;transition:opacity .45s ease}
#sk-hud.on{opacity:1}
#sk-zone b{color:#8ff6ff;font-size:.86em;white-space:nowrap}
#sk-cells .bar{position:relative;width:74px;height:6px;margin-top:5px;border-radius:4px;background:rgba(255,255,255,.1);overflow:hidden}
#sk-cells .bar i{position:absolute;inset:0;width:0;border-radius:4px;background:linear-gradient(90deg,#29f0ff,#c9fff6);transition:width .25s ease}
#sk-cells.shield{box-shadow:0 0 18px rgba(80,240,255,.55)}
#sk-cells.shield .bar i{background:linear-gradient(90deg,#7dffb0,#eafff4)}
#sk-banner{position:fixed;left:50%;top:21%;z-index:20;transform:translate(-50%,-8px);text-align:center;pointer-events:none;opacity:0;transition:opacity .5s ease,transform .5s ease}
#sk-banner.on{opacity:1;transform:translate(-50%,0)}
#sk-banner small{display:block;font:800 .72rem Manrope,system-ui,sans-serif;letter-spacing:.32em;color:#ffd27a;text-shadow:0 0 12px rgba(255,190,90,.7)}
#sk-banner b{display:block;margin-top:.3rem;font:900 clamp(1.6rem,4.6vw,2.8rem) Manrope,system-ui,sans-serif;letter-spacing:.06em;color:#f2fbff;text-shadow:0 0 18px rgba(80,230,255,.85),0 0 42px rgba(255,60,170,.45)}
#sk-banner span{display:block;margin-top:.45rem;font:600 .9rem Manrope,system-ui,sans-serif;color:#cfe6ff;opacity:.9}
#sk-hint{position:fixed;left:50%;bottom:16%;z-index:20;transform:translateX(-50%);padding:.55rem 1rem;border-radius:999px;background:rgba(8,10,28,.55);border:1px solid rgba(142,233,255,.3);font:700 .85rem Manrope,system-ui,sans-serif;color:#e6f7ff;white-space:nowrap;pointer-events:none;opacity:0;transition:opacity .4s ease;backdrop-filter:blur(6px);-webkit-backdrop-filter:blur(6px)}
#sk-hint.on{opacity:1}
.sk-btn{position:fixed;bottom:max(18px,env(safe-area-inset-bottom));z-index:42;width:86px;height:86px;border-radius:50%;display:none;align-items:center;justify-content:center;flex-direction:column;font:900 .74rem Manrope,system-ui,sans-serif;letter-spacing:.12em;touch-action:none;-webkit-tap-highlight-color:transparent;backdrop-filter:blur(6px);-webkit-backdrop-filter:blur(6px);transition:transform .08s ease,box-shadow .12s ease}
.sk-btn svg{width:30px;height:30px;margin-bottom:2px}
.sk-btn.on{transform:scale(.93)}
#sk-boost{right:max(16px,env(safe-area-inset-right));width:104px;height:104px;color:#dffcff;background:radial-gradient(circle at 50% 40%,rgba(41,240,255,.32),rgba(10,24,48,.55));border:2px solid rgba(110,240,255,.75);box-shadow:0 0 22px rgba(41,240,255,.35)}
#sk-boost.on{box-shadow:0 0 34px rgba(41,240,255,.8)}
#sk-cut{left:max(16px,env(safe-area-inset-left));color:#ffe0ee;background:radial-gradient(circle at 50% 40%,rgba(255,47,168,.28),rgba(30,8,28,.55));border:2px solid rgba(255,90,180,.7);box-shadow:0 0 18px rgba(255,47,168,.3)}
#sk-cut.on{box-shadow:0 0 30px rgba(255,47,168,.8)}
body.sk-touch .sk-btn{display:flex}
@media (max-width:560px){#sk-hud .pa-stat{padding:.32rem .55rem}#sk-cells .bar{width:52px}}
</style></head><body>
<div id="game" aria-label="Neon Skyway: a hover car flying through a stacked neon megacity"></div>
<canvas id="fx" aria-hidden="true"></canvas>
<div class="pa-hud" id="sk-hud">
<div class="pa-stat" id="sk-score"><small>Score</small><b>0</b></div>
<div class="pa-stat" id="sk-zone"><small>Zone</small><b>1</b></div>
<div class="pa-stat" id="sk-cells"><small>Cells</small><b>0/8</b><div class="bar"><i></i></div></div>
</div>
<div id="sk-banner" aria-live="polite"><small>ZONE 1</small><b>Skyway Approach</b><span></span></div>
<div id="sk-hint">Hold boost to climb · cut the engine to drop</div>
<button type="button" class="sk-btn" id="sk-cut" aria-label="Cut engine"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M12 4v11M6 11l6 6 6-6M6 20h12"/></svg>CUT</button>
<button type="button" class="sk-btn" id="sk-boost" aria-label="Boost"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20V7M6 11l6-6 6 6M6 15l6-6 6 6"/></svg>BOOST</button>
<script>
window.paConfig = {
slug: "neon-flap", title: "Neon Skyway", native: true, music: "synthwave", unit: "points",
controls: [["Space / W / ↑ (hold)", "Boost: the thrusters fire and you climb fast"], ["S / ↓ / Shift (hold)", "Cut the engine: the lights go out and you drop like a stone"], ["A D / ← →", "Ease off or open up the throttle to time a gap"], ["Mouse", "Hold left button to boost, right button to cut"], ["Touch", "Hold BOOST (right) to climb, CUT (left) to drop"], ["Energy cells", "Grab 8 to charge a shield that saves you once"], ["Close calls", "Skim past things for a combo bonus"]]
};
</script>
<script src="/games/play/pa-score.js"></script>
<script src="/games/play/pa-gate.js"></script>
<script src="/games/play/pa-kit.js"></script>
<script type="module">
import * as THREE from "/vendor/three/three.module.min.js";
(() => {
"use strict";
// A one-button-and-a-half flyer in real 3D: you pilot a hover car along a skyway stacked between the lanes
// of a neon megacity. Hold boost to climb, cut the engine to drop, and thread towers, oncoming traffic,
// hardlight billboards, freight lifts, security lasers, crane drones, exhaust stacks and patrol drones.
// The game runs in the x/y plane (x forward, y up); the city is built in chunks around it and thrown away
// behind, so it never ends.
const TAU = Math.PI * 2;
const $ = (id) => document.getElementById(id);
const { clamp, lerp, rand, randInt } = PA;
const smooth = (t) => t * t * (3 - 2 * t);
/* ---------------- renderer + post ---------------- */
const mount = document.getElementById("game");
const renderer = new THREE.WebGLRenderer({ antialias: false, powerPreference: "high-performance" });
const dprCap = () => Math.min(PA.isTouch ? 1.5 : 2, devicePixelRatio || 1);
renderer.setPixelRatio(dprCap());
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
mount.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x05040d);
scene.fog = new THREE.FogExp2(0x070516, 0.012);
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.3, 500);
const hdr = (c, k) => new THREE.Color(c).multiplyScalar(k);
function makePost() {
const ext = renderer.extensions;
const fullFloat = ext.has("EXT_color_buffer_float"), hdrOk = fullFloat || ext.has("EXT_color_buffer_half_float");
const type = hdrOk ? THREE.HalfFloatType : THREE.UnsignedByteType;
const main = new THREE.WebGLRenderTarget(1, 1, { type, samples: hdrOk && !fullFloat ? 0 : PA.isTouch ? 2 : 4 });
const LV = 5, lv = [], tmp = [];
for (let i = 0; i < LV; i++) {
lv.push(new THREE.WebGLRenderTarget(1, 1, { type, depthBuffer: false }));
tmp.push(new THREE.WebGLRenderTarget(1, 1, { type, depthBuffer: false }));
}
const cam = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
const quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2));
quad.frustumCulled = false;
const pscene = new THREE.Scene();
pscene.add(quad);
const vs = "varying vec2 vUv; void main(){ vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }";
const mk = (fs, uniforms) => new THREE.ShaderMaterial({ vertexShader: vs, fragmentShader: fs, uniforms, depthTest: false, depthWrite: false });
const box4 = "vec3 c = 0.25 * (texture2D(tex, vUv + px * vec2(-0.5, -0.5)).rgb + texture2D(tex, vUv + px * vec2(0.5, -0.5)).rgb + texture2D(tex, vUv + px * vec2(-0.5, 0.5)).rgb + texture2D(tex, vUv + px * vec2(0.5, 0.5)).rgb);";
const bright = mk([
"uniform sampler2D tex; uniform vec2 px; uniform float thr; varying vec2 vUv;",
"void main(){", box4,
" float l = max(c.r, max(c.g, c.b));",
" float knee = thr * 0.5, soft = clamp(l - thr + knee, 0.0, 2.0 * knee);",
" soft = soft * soft / (4.0 * knee + 1e-4);",
" gl_FragColor = vec4(min(c * max(soft, l - thr) / max(l, 1e-4), vec3(30.0)), 1.0);",
"}"].join("\n"), { tex: { value: null }, px: { value: new THREE.Vector2() }, thr: { value: hdrOk ? 0.95 : 0.7 } });
const down = mk("uniform sampler2D tex; uniform vec2 px; varying vec2 vUv; void main(){ " + box4 + " gl_FragColor = vec4(c, 1.0); }",
{ tex: { value: null }, px: { value: new THREE.Vector2() } });
const blur = mk([
"uniform sampler2D tex; uniform vec2 dir; varying vec2 vUv;",
"void main(){",
" vec3 c = texture2D(tex, vUv).rgb * 0.2270270270;",
" c += (texture2D(tex, vUv + dir * 1.3846153846).rgb + texture2D(tex, vUv - dir * 1.3846153846).rgb) * 0.3162162162;",
" c += (texture2D(tex, vUv + dir * 3.2307692308).rgb + texture2D(tex, vUv - dir * 3.2307692308).rgb) * 0.0702702703;",
" gl_FragColor = vec4(c, 1.0);",
"}"].join("\n"), { tex: { value: null }, dir: { value: new THREE.Vector2() } });
const comp = mk([
"uniform sampler2D tScene, b0, b1, b2, b3, b4; uniform float strength, grain, time, hurt, chroma; varying vec2 vUv;",
"void main(){",
" vec2 q = vUv - 0.5;",
" vec3 c;",
" c.r = texture2D(tScene, vUv + q * chroma * 0.012).r;",
" c.g = texture2D(tScene, vUv).g;",
" c.b = texture2D(tScene, vUv - q * chroma * 0.012).b;",
" vec3 b = texture2D(b0, vUv).rgb * 0.5 + texture2D(b1, vUv).rgb * 0.6 + texture2D(b2, vUv).rgb * 0.75 + texture2D(b3, vUv).rgb * 0.85 + texture2D(b4, vUv).rgb * 0.95;",
" c += b * strength;",
" float vig = dot(q, q);",
" c *= 1.0 - vig * 0.85;",
" c += vec3(1.0, 0.2, 0.45) * hurt * smoothstep(0.08, 0.4, vig);",
" gl_FragColor = vec4(c, 1.0);",
" #include <tonemapping_fragment>",
" #include <colorspace_fragment>",
" float n = fract(sin(dot(gl_FragCoord.xy + fract(time) * 97.0, vec2(12.9898, 78.233))) * 43758.5453);",
" gl_FragColor.rgb += (n - 0.5) * grain;",
"}"].join("\n"), {
tScene: { value: main.texture }, b0: { value: lv[0].texture }, b1: { value: lv[1].texture }, b2: { value: lv[2].texture },
b3: { value: lv[3].texture }, b4: { value: lv[4].texture }, strength: { value: 0.5 }, grain: { value: 0.02 }, time: { value: 0 }, hurt: { value: 0 }, chroma: { value: 0.1 }
});
const pass = (m, target) => { quad.material = m; renderer.setRenderTarget(target); renderer.render(pscene, cam); };
return {
comp,
setSize(w, h) {
main.setSize(w, h);
for (let i = 0; i < LV; i++) {
const lw = Math.max(1, w >> (i + 1)), lh = Math.max(1, h >> (i + 1));
lv[i].setSize(lw, lh); tmp[i].setSize(lw, lh);
}
},
render(sc, cm, t) {
renderer.setRenderTarget(main);
renderer.render(sc, cm);
bright.uniforms.tex.value = main.texture;
bright.uniforms.px.value.set(1 / main.width, 1 / main.height);
pass(bright, lv[0]);
for (let i = 0; i < LV; i++) {
if (i) {
down.uniforms.tex.value = lv[i - 1].texture;
down.uniforms.px.value.set(1 / lv[i - 1].width, 1 / lv[i - 1].height);
pass(down, lv[i]);
}
blur.uniforms.tex.value = lv[i].texture; blur.uniforms.dir.value.set(1 / lv[i].width, 0);
pass(blur, tmp[i]);
blur.uniforms.tex.value = tmp[i].texture; blur.uniforms.dir.value.set(0, 1 / lv[i].height);
pass(blur, lv[i]);
}
comp.uniforms.time.value = t;
pass(comp, null);
}
};
}
const post = makePost();
function canvasTex(w, h, paint, o) {
const cv = document.createElement("canvas");
cv.width = w; cv.height = h;
paint(cv.getContext("2d"), w, h);
const t = new THREE.CanvasTexture(cv);
t.colorSpace = o && o.linear ? THREE.NoColorSpace : THREE.SRGBColorSpace;
if (o && o.repeat) { t.wrapS = t.wrapT = THREE.RepeatWrapping; }
t.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return t;
}
const glowTex = canvasTex(128, 128, (g, w) => {
const gr = g.createRadialGradient(w / 2, w / 2, 0, w / 2, w / 2, w / 2);
gr.addColorStop(0, "rgba(255,255,255,1)"); gr.addColorStop(.2, "rgba(255,255,255,.5)"); gr.addColorStop(.55, "rgba(255,255,255,.1)"); gr.addColorStop(1, "rgba(255,255,255,0)");
g.fillStyle = gr; g.fillRect(0, 0, w, w);
});
function glowSprite(color, scale) {
const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: glowTex, color, blending: THREE.AdditiveBlending, depthWrite: false, transparent: true }));
s.scale.setScalar(scale);
return s;
}
/* ---------------- particles + shards ---------------- */
const PMAX = 3000;
const pPos = new Float32Array(PMAX * 3), pCol = new Float32Array(PMAX * 3), pSize = new Float32Array(PMAX);
const pGeo = new THREE.BufferGeometry();
pGeo.setAttribute("position", new THREE.BufferAttribute(pPos, 3).setUsage(THREE.DynamicDrawUsage));
pGeo.setAttribute("color", new THREE.BufferAttribute(pCol, 3).setUsage(THREE.DynamicDrawUsage));
pGeo.setAttribute("size", new THREE.BufferAttribute(pSize, 1).setUsage(THREE.DynamicDrawUsage));
const pointsMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending,
uniforms: { scale: { value: 600 } },
vertexShader: "uniform float scale; attribute float size; attribute vec3 color; varying vec3 vC; void main(){ vC = color; vec4 mv = modelViewMatrix * vec4(position, 1.0); gl_PointSize = max(1.0, size * scale / -mv.z); gl_Position = projectionMatrix * mv; }",
fragmentShader: "varying vec3 vC; void main(){ float d = length(gl_PointCoord - 0.5); float a = smoothstep(0.5, 0.05, d); gl_FragColor = vec4(vC * a * a, 1.0); }"
});
const ptsMesh = new THREE.Points(pGeo, pointsMat);
ptsMesh.frustumCulled = false;
scene.add(ptsMesh);
const parts = [];
const C3 = (hex, k) => { const c = new THREE.Color(hex); return [c.r * k, c.g * k, c.b * k]; };
function burst(x, y, z, o) {
o = o || {};
const n = o.n || 16, cols = o.colors || [0xffffff];
for (let i = 0; i < n && parts.length < PMAX; i++) {
const a = o.angle != null ? o.angle + rand(-1, 1) * (o.spread || 0.6) : Math.random() * TAU;
const sp = (o.speed || 6) * rand(0.3, 1), life = (o.life || 0.6) * rand(0.5, 1.1);
const el = o.flat ? 0 : rand(-1, 1) * 0.9;
parts.push({ x, y, z, vx: Math.cos(a) * sp * Math.cos(el) + (o.vx || 0), vy: Math.sin(a) * sp * Math.cos(el) + rand(0, 1) * (o.up || 0), vz: Math.sin(el) * sp + rand(-1, 1) * (o.zs || 0), drag: o.drag || 0, g: o.g == null ? 6 : o.g,
life, max: life, size: (o.size || 0.22) * rand(0.6, 1.3), c: C3(cols[(Math.random() * cols.length) | 0], o.bright || 2.4) });
}
}
function updateParts(dt) {
let j = 0;
for (let i = 0; i < parts.length; i++) {
const p = parts[i];
p.life -= dt;
if (p.life <= 0) continue;
p.vy -= p.g * dt;
if (p.drag) { const k = Math.exp(-p.drag * dt); p.vx *= k; p.vy *= k; p.vz *= k; }
p.x += p.vx * dt; p.y += p.vy * dt; p.z += p.vz * dt;
parts[j++] = p;
}
parts.length = j;
for (let i = 0; i < j; i++) {
const p = parts[i], k = p.life / p.max;
pPos[i * 3] = p.x; pPos[i * 3 + 1] = p.y; pPos[i * 3 + 2] = p.z;
pCol[i * 3] = p.c[0] * k; pCol[i * 3 + 1] = p.c[1] * k; pCol[i * 3 + 2] = p.c[2] * k;
pSize[i] = p.size;
}
pGeo.setDrawRange(0, j);
pGeo.attributes.position.needsUpdate = true; pGeo.attributes.color.needsUpdate = true; pGeo.attributes.size.needsUpdate = true;
}
// Debris: little panels of bodywork that tumble and fade.
const SMAX = 420;
const shardMesh = new THREE.InstancedMesh(new THREE.BoxGeometry(0.34, 0.08, 0.22), new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.3, metalness: 0.6, emissive: 0xffffff, emissiveIntensity: 0.25 }), SMAX);
shardMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
shardMesh.setColorAt(0, new THREE.Color(1, 1, 1));
shardMesh.count = 0;
shardMesh.frustumCulled = false;
scene.add(shardMesh);
const shards = [];
function shatter(x, y, z, color, n, vx) {
const c = new THREE.Color(color);
for (let i = 0; i < n; i++) {
if (shards.length >= SMAX) shards.shift();
const a = Math.random() * TAU, sp = rand(1.5, 6.5);
shards.push({ x: x + rand(-0.8, 0.8), y: y + rand(-0.4, 0.4), z: z + rand(-0.5, 0.5), vx: Math.cos(a) * sp + (vx || 0), vy: rand(2, 8), vz: Math.sin(a) * sp * 0.7 + 1.2,
rx: rand(0, TAU), ry: rand(0, TAU), wx: rand(-14, 14), wy: rand(-14, 14), s: rand(0.5, 1.25), life: rand(1.4, 2.4), c });
}
}
const m4 = new THREE.Matrix4(), q4 = new THREE.Quaternion(), e4 = new THREE.Euler(), v4 = new THREE.Vector3(), s4 = new THREE.Vector3();
function updateShards(dt) {
let j = 0;
for (let i = 0; i < shards.length; i++) {
const s = shards[i];
s.life -= dt;
if (s.life <= 0) continue;
s.vy -= 20 * dt;
s.x += s.vx * dt; s.y += s.vy * dt; s.z += s.vz * dt;
s.rx += s.wx * dt; s.ry += s.wy * dt;
shards[j++] = s;
}
shards.length = j;
for (let i = 0; i < j; i++) {
const s = shards[i], k = Math.min(1, s.life / 0.5) * s.s;
e4.set(s.rx, s.ry, 0); q4.setFromEuler(e4);
m4.compose(v4.set(s.x, s.y, s.z), q4, s4.set(k, k, k));
shardMesh.setMatrixAt(i, m4);
shardMesh.setColorAt(i, s.c);
}
shardMesh.count = j;
shardMesh.instanceMatrix.needsUpdate = true;
if (shardMesh.instanceColor) shardMesh.instanceColor.needsUpdate = true;
}
// ribbon trails (thruster wakes)
const TV = 9000;
const tPos = new Float32Array(TV * 3), tCol = new Float32Array(TV * 3);
const tGeo = new THREE.BufferGeometry();
tGeo.setAttribute("position", new THREE.BufferAttribute(tPos, 3).setUsage(THREE.DynamicDrawUsage));
tGeo.setAttribute("color", new THREE.BufferAttribute(tCol, 3).setUsage(THREE.DynamicDrawUsage));
const trailMesh = new THREE.Mesh(tGeo, new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide }));
trailMesh.frustumCulled = false;
scene.add(trailMesh);
let tv = 0;
const camDir = new THREE.Vector3();
function ribbon(tr, col, width, taper) {
const n = tr.length / 3;
if (n < 2) return;
for (let i = 1; i < n && tv + 6 <= TV; i++) {
const ax = tr[i * 3 - 3], ay = tr[i * 3 - 2], az = tr[i * 3 - 1], bx = tr[i * 3], by = tr[i * 3 + 1], bz = tr[i * 3 + 2];
const dx = bx - ax, dy = by - ay, dz = bz - az;
camDir.set(camera.position.x - bx, camera.position.y - by, camera.position.z - bz).normalize();
const px = dy * camDir.z - dz * camDir.y, py = dz * camDir.x - dx * camDir.z, pz = dx * camDir.y - dy * camDir.x;
const pl = Math.hypot(px, py, pz) || 1;
const k0 = taper === false ? 1 : (i - 1) / (n - 1), k1 = taper === false ? 1 : i / (n - 1);
const w0 = width * k0 / pl, w1 = width * k1 / pl;
const v = [ax - px * w0, ay - py * w0, az - pz * w0, ax + px * w0, ay + py * w0, az + pz * w0, bx - px * w1, by - py * w1, bz - pz * w1, bx + px * w1, by + py * w1, bz + pz * w1];
for (const q of [0, 1, 2, 1, 3, 2]) {
tPos[tv * 3] = v[q * 3]; tPos[tv * 3 + 1] = v[q * 3 + 1]; tPos[tv * 3 + 2] = v[q * 3 + 2];
const k = q < 2 ? k0 : k1, kk = taper === false ? 1 : k * k;
tCol[tv * 3] = col[0] * kk; tCol[tv * 3 + 1] = col[1] * kk; tCol[tv * 3 + 2] = col[2] * kk;
tv++;
}
}
}
/* ---------------- tuning ---------------- */
const CEIL = 18; // the skyway runs from y 0 to CEIL, between two lanes of traffic
const ZD = 3.2; // obstacles fill z -ZD..ZD around the flight line
const CAR_K = 0.8; // player model scale
const HALF_L = 1.45, CAR_R = 0.4; // collision capsule along the car
const BASE_V = 13.5, TOP_V = 25; // cruise speed, units per second
const ZONE_LEN = 780;
const BOOST_A = 46, BOOST_MAX = 12.5; // thrusters
const CUT_A = 58, CUT_MAX = -19; // engine off: a stone
const IDLE_G = 15, IDLE_MAX = -7.5; // idling: the lifters only half hold you up
const CELLS_FOR_SHIELD = 8;
const BOTTOM = -100; // towers run down into the smog
camera.fov = 42; camera.far = 3400; camera.updateProjectionMatrix();
renderer.toneMappingExposure = 1.12;
/* ---------------- zones ---------------- */
// Every ZONE_LEN units the skyway enters a new district with its own sky, light and obstacle mix.
const ZONES = [
{ name: "Skyway Approach", tip: "Hold boost to climb, cut the engine to drop", fog: [0.085, 0.03, 0.11], hor: [0.5, 0.1, 0.34], top: [0.008, 0.006, 0.024], b1: [0.34, 0.03, 0.18], b2: [0.02, 0.13, 0.2], sun: 0, rimA: 0xff3ca8, rimB: 0x2ad8ff,
kinds: { tower: 1, traffic: 0.2 } },
{ name: "Billboard Canyon", tip: "Hardlight billboards turn solid: fly through the dim half", fog: [0.15, 0.06, 0.07], hor: [0.95, 0.4, 0.16], top: [0.03, 0.012, 0.03], b1: [0.55, 0.17, 0.04], b2: [0.32, 0.05, 0.2], sun: 1, rimA: 0xff8a3a, rimB: 0xff3ca8,
kinds: { tower: 0.55, billboard: 0.5, traffic: 0.3 } },
{ name: "Freight Docks", tip: "Lifts slide and cranes swing: time your run", fog: [0.03, 0.065, 0.085], hor: [0.08, 0.32, 0.36], top: [0.004, 0.012, 0.02], b1: [0.36, 0.18, 0.03], b2: [0.02, 0.22, 0.22], sun: 0, rimA: 0xffb21e, rimB: 0x29f0ff,
kinds: { tower: 0.35, lift: 0.45, crane: 0.45, traffic: 0.2 } },
{ name: "Security Ring", tip: "Patrol drones lock on to your height: bait them, then dodge", fog: [0.075, 0.02, 0.05], hor: [0.42, 0.03, 0.09], top: [0.01, 0.004, 0.016], b1: [0.45, 0.02, 0.06], b2: [0.03, 0.07, 0.34], sun: 0, rimA: 0xff2a4a, rimB: 0x3a6bff,
kinds: { tower: 0.3, laser: 0.5, patrol: 0.4, billboard: 0.15 } },
{ name: "Exhaust Stacks", tip: "Updrafts throw you upward: cut the engine to stay low", fog: [0.07, 0.03, 0.1], hor: [0.4, 0.1, 0.44], top: [0.01, 0.006, 0.024], b1: [0.42, 0.13, 0.03], b2: [0.17, 0.04, 0.32], sun: 0.55, rimA: 0xff7a2a, rimB: 0x9a6bff,
kinds: { vent: 0.5, tower: 0.3, lift: 0.2, traffic: 0.25 } }
];
const ALL_KINDS = { tower: 0.4, traffic: 0.3, billboard: 0.3, lift: 0.25, crane: 0.25, laser: 0.3, patrol: 0.25, vent: 0.3 };
function zoneDef(i) {
if (i < ZONES.length) return ZONES[i];
const b = ZONES[(i - ZONES.length) % ZONES.length];
return Object.assign({}, b, { name: "Overdrive " + (i - ZONES.length + 1), tip: "Everything at once, and faster", kinds: ALL_KINDS });
}
// The live palette eases toward the zone's.
const V3 = (a) => new THREE.Vector3(a[0], a[1], a[2]);
const pal = { fog: V3(ZONES[0].fog), hor: V3(ZONES[0].hor), top: V3(ZONES[0].top), b1: V3(ZONES[0].b1), b2: V3(ZONES[0].b2), sun: 0, rimA: new THREE.Color(ZONES[0].rimA), rimB: new THREE.Color(ZONES[0].rimB) };
const palT = { fog: V3(ZONES[0].fog), hor: V3(ZONES[0].hor), top: V3(ZONES[0].top), b1: V3(ZONES[0].b1), b2: V3(ZONES[0].b2), sun: 0, rimA: new THREE.Color(ZONES[0].rimA), rimB: new THREE.Color(ZONES[0].rimB) };
function setPalette(z, snap) {
for (const k of ["fog", "hor", "top", "b1", "b2"]) palT[k].set(z[k][0], z[k][1], z[k][2]);
palT.sun = z.sun; palT.rimA.set(z.rimA); palT.rimB.set(z.rimB);
if (snap) { for (const k of ["fog", "hor", "top", "b1", "b2"]) pal[k].copy(palT[k]); pal.sun = palT.sun; pal.rimA.copy(palT.rimA); pal.rimB.copy(palT.rimB); }
}
function easePalette(dt) {
const k = 1 - Math.exp(-dt * 0.9);
for (const c of ["fog", "hor", "top", "b1", "b2"]) pal[c].lerp(palT[c], k);
pal.sun = lerp(pal.sun, palT.sun, k); pal.rimA.lerp(palT.rimA, k); pal.rimB.lerp(palT.rimB, k);
}
scene.background = new THREE.Color();
scene.fog = new THREE.FogExp2(0x000000, 0.0052);
/* ---------------- geometry builder ---------------- */
// Static pieces of the world are merged into one geometry per material. Boxes get world-space UVs,
// so a facade's windows keep their size whatever the building's size.
const v3 = new THREE.Vector3(), nm3 = new THREE.Matrix3();
const CAP_UV = [0.995, 0.005]; // the facades keep a dark wall-coloured corner for roofs
class Geo {
constructor() { this.p = []; this.n = []; this.u = []; }
add(geo, m, uvf) {
const g = geo.index ? geo.toNonIndexed() : geo, P = g.attributes.position, N = g.attributes.normal, U = g.attributes.uv;
nm3.getNormalMatrix(m);
for (let i = 0; i < P.count; i++) {
v3.fromBufferAttribute(P, i).applyMatrix4(m); this.p.push(v3.x, v3.y, v3.z);
v3.fromBufferAttribute(N, i).applyMatrix3(nm3).normalize(); this.n.push(v3.x, v3.y, v3.z);
const uu = U ? U.getX(i) : 0, vv = U ? U.getY(i) : 0;
if (uvf) { const r = uvf(uu, vv); this.u.push(r[0], r[1]); } else this.u.push(uu, vv);
}
return this;
}
quad(a, b, c, d, nx, ny, nz, ua, ub, uc, ud) {
const q = [a, b, c, d], uv = [ua, ub, uc, ud];
for (const k of [0, 1, 2, 0, 2, 3]) { this.p.push(q[k][0], q[k][1], q[k][2]); this.n.push(nx, ny, nz); this.u.push(uv[k][0], uv[k][1]); }
}
// U, V: units per texture repeat (0 = one repeat over the face); ou, ov shift the pattern.
box(x0, x1, y0, y1, z0, z1, U, V, ou, ov, capUV) {
U = U || 1; V = V || 1; ou = ou || 0; ov = ov || 0;
const cu = capUV || CAP_UV, C = [cu, cu, cu, cu];
const uvx = (x, y) => [x / U + ou, y / V + ov], uvz = (z, y) => [z / U + ou + 0.37, y / V + ov];
this.quad([x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1], 0, 0, 1, uvx(x0, y0), uvx(x1, y0), uvx(x1, y1), uvx(x0, y1));
this.quad([x1, y0, z0], [x0, y0, z0], [x0, y1, z0], [x1, y1, z0], 0, 0, -1, uvx(-x1, y0), uvx(-x0, y0), uvx(-x0, y1), uvx(-x1, y1));
this.quad([x1, y0, z1], [x1, y0, z0], [x1, y1, z0], [x1, y1, z1], 1, 0, 0, uvz(-z1, y0), uvz(-z0, y0), uvz(-z0, y1), uvz(-z1, y1));
this.quad([x0, y0, z0], [x0, y0, z1], [x0, y1, z1], [x0, y1, z0], -1, 0, 0, uvz(z0, y0), uvz(z1, y0), uvz(z1, y1), uvz(z0, y1));
this.quad([x0, y1, z1], [x1, y1, z1], [x1, y1, z0], [x0, y1, z0], 0, 1, 0, ...C);
this.quad([x0, y0, z0], [x1, y0, z0], [x1, y0, z1], [x0, y0, z1], 0, -1, 0, ...C);
return this;
}
// a plain box whose UVs span 0..1 on every face
cube(x0, x1, y0, y1, z0, z1) {
const A = [0, 0], B = [1, 0], Cc = [1, 1], Dd = [0, 1];
this.quad([x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1], 0, 0, 1, A, B, Cc, Dd);
this.quad([x1, y0, z0], [x0, y0, z0], [x0, y1, z0], [x1, y1, z0], 0, 0, -1, A, B, Cc, Dd);
this.quad([x1, y0, z1], [x1, y0, z0], [x1, y1, z0], [x1, y1, z1], 1, 0, 0, A, B, Cc, Dd);
this.quad([x0, y0, z0], [x0, y0, z1], [x0, y1, z1], [x0, y1, z0], -1, 0, 0, A, B, Cc, Dd);
this.quad([x0, y1, z1], [x1, y1, z1], [x1, y1, z0], [x0, y1, z0], 0, 1, 0, A, B, Cc, Dd);
this.quad([x0, y0, z0], [x1, y0, z0], [x1, y0, z1], [x0, y0, z1], 0, -1, 0, A, B, Cc, Dd);
return this;
}
get empty() { return this.p.length === 0; }
build() {
const g = new THREE.BufferGeometry();
g.setAttribute("position", new THREE.Float32BufferAttribute(this.p, 3));
g.setAttribute("normal", new THREE.Float32BufferAttribute(this.n, 3));
g.setAttribute("uv", new THREE.Float32BufferAttribute(this.u, 2));
g.computeBoundingSphere();
return g;
}
}
// Put a list of [Geo, material] into one group.
function geoGroup(list) {
const grp = new THREE.Group();
for (const [G, mat] of list) if (G && !G.empty) grp.add(new THREE.Mesh(G.build(), mat));
return grp;
}
// Geometries reused across the world are never disposed; a mesh's own geometry and marked materials are.
const SHARED = new Set();
function disposeGroup(grp) {
grp.traverse((o) => {
if (!o.isMesh && !o.isPoints && !o.isLineSegments) return;
if (!o.userData.shared && !SHARED.has(o.geometry)) o.geometry.dispose();
if (o.userData.ownMat) o.material.dispose();
});
scene.remove(grp);
}
const planeGeo = new THREE.PlaneGeometry(1, 1);
SHARED.add(planeGeo);
function speckle(g, w, h, amt) {
const img = g.getImageData(0, 0, w, h), d = img.data;
for (let i = 0; i < d.length; i += 4) {
const n = (Math.random() - 0.5) * amt;
d[i] += n; d[i + 1] += n; d[i + 2] += n;
}
g.putImageData(img, 0, 0);
}
// Facades: a colour map and an emissive map (lit windows and LED trim only). Street buildings tile
// 6 x 8 windows over 14 m x 28 m; the megatowers tile 12 x 16 windows over 30 m x 60 m.
// Kinds: 0 habitat block, 1 glass tower, 2 neon district, 3 and 4 megatower skins.
const FAC_U = 14, FAC_V = 28, MEGA_U = 30, MEGA_V = 60;
function facade(kind) {
const mega = kind >= 3, C = mega ? 12 : kind === 1 ? 8 : 10, R = mega ? 16 : 8, S = mega ? 512 : 256, SH = 512;
const litP = [0.26, 0.3, 0.32, 0.3, 0.26][kind];
const tints = [
["#ffd9a0", "#ffe7c4", "#9fd8ff", "#ffb0d8"],
["#cfe9ff", "#a9d6ff", "#e4f1ff", "#c3b4ff"],
["#ff8fd0", "#8ff6ff", "#ffd9a0", "#c9a2ff"],
["#bfe6ff", "#ffe2b0", "#8ff6ff", "#ffffff"],
["#ffd29a", "#ff9fce", "#ffe7c4", "#b8a6ff"]
][kind];
const wall = ["#2a2930", "#111622", "#221a24", "#141826", "#1c141f"][kind];
const led = ["#2fe0ff", "#c06bff", "#ff3fb0", "#2fe0ff", "#ff5a3d"][kind];
const lit = [], colors = [], alpha = [], blinds = [];
for (let i = 0; i < C * R; i++) {
lit.push(Math.random() < litP); colors.push(PA.choice(tints)); alpha.push(rand(0.18, 0.7)); blinds.push(Math.random() < 0.35 ? rand(0.2, 0.6) : 0);
}
const cell = (x, y, cw, ch) => kind === 1 ? [x + 1.5, y + 3, cw - 3, ch - 6] : [x + cw * 0.14, y + ch * 0.18, cw * 0.72, ch * 0.6];
const leds = (g, w, h, cw, ch) => {
const t = Math.max(2, S / 128);
g.fillStyle = led;
if (kind === 2) for (let r = 1; r < R; r += 3) { g.fillStyle = ["#ff3fb0", "#2fe0ff", "#ffb21e"][r % 3]; g.fillRect(0, r * ch + ch - t * 1.5, w, t); }
if (kind === 4) for (let r = 0; r < R; r += 4) g.fillRect(0, r * ch, w, t);
if (kind === 3) for (let c = 0; c < C; c += 4) g.fillRect(c * cw, 0, t, h);
if (kind === 1) { g.globalAlpha = 0.45; for (let c = 0; c < C; c += 3) g.fillRect(c * cw, 0, t * 0.75, h); g.globalAlpha = 1; }
};
const map = canvasTex(S, SH, (g, w, h) => {
g.fillStyle = wall; g.fillRect(0, 0, w, h);
speckle(g, w, h, kind === 1 ? 6 : 18);
const cw = w / C, ch = h / R;
for (let r = 0; r < R; r++) for (let c = 0; c < C; c++) {
const i = r * C + c, [x, y, ww, hh] = cell(c * cw, r * ch, cw, ch);
const gr = g.createLinearGradient(x, y, x + ww, y + hh);
gr.addColorStop(0, "#141a28"); gr.addColorStop(1, "#080b14");
g.fillStyle = gr;
g.fillRect(x, y, ww, hh);
if (lit[i]) { g.globalAlpha = 0.4; g.fillStyle = colors[i]; g.fillRect(x, y, ww, hh); g.globalAlpha = 1; }
if (kind === 0 && Math.random() < 0.25) { g.fillStyle = "#3a3a44"; g.fillRect(x + ww * 0.55, y + hh + 2, ww * 0.4, ch * 0.13); } // AC units
}
g.fillStyle = kind === 1 ? "rgba(110,140,190,.18)" : "rgba(0,0,0,.3)";
for (let r = 0; r < R; r++) g.fillRect(0, r * ch, w, 3);
leds(g, w, h, cw, ch);
g.fillStyle = wall; g.fillRect(w - 5, h - 5, 5, 5);
}, { repeat: true });
const emissive = canvasTex(S, SH, (g, w, h) => {
g.fillStyle = "#000"; g.fillRect(0, 0, w, h);
const cw = w / C, ch = h / R;
for (let r = 0; r < R; r++) for (let c = 0; c < C; c++) {
const i = r * C + c;
if (!lit[i]) continue;
const [x, y, ww, hh] = cell(c * cw, r * ch, cw, ch);
// ceiling lights: brighter at the top of the window
const wg = g.createLinearGradient(0, y, 0, y + hh);
wg.addColorStop(0, colors[i]); wg.addColorStop(1, "#000");
g.fillStyle = wg; g.globalAlpha = alpha[i];
g.fillRect(x, y, ww, hh);
g.globalAlpha = 1;
if (blinds[i]) { g.fillStyle = "rgba(0,0,0,.55)"; g.fillRect(x, y, ww, hh * blinds[i]); }
}
leds(g, w, h, cw, ch);
g.fillStyle = "#000"; g.fillRect(w - 5, h - 5, 5, 5); // roofs sample this corner
}, { repeat: true });
return new THREE.MeshLambertMaterial({ map, emissiveMap: emissive, emissive: 0xffffff, emissiveIntensity: mega ? 1.5 : 1.15 });
}
// Neon blade signs. Half spell a word; half are written in an invented script: a few strokes on
// a 3 x 3 grid read as a foreign megacity without being any real language.
function glyph(g, cx, cy, s) {
const p = (i) => -s / 2 + i * s / 3;
g.save(); g.translate(cx, cy);
g.lineWidth = Math.max(2, s * 0.11); g.lineCap = "square";
g.beginPath();
const n = randInt(3, 5);
for (let k = 0; k < n; k++) {
const t = Math.random();
if (t < 0.36) { const y = p(randInt(0, 3)); g.moveTo(p(randInt(0, 1)), y); g.lineTo(p(randInt(2, 3)), y); }
else if (t < 0.72) { const x = p(randInt(0, 3)); g.moveTo(x, p(randInt(0, 1))); g.lineTo(x, p(randInt(2, 3))); }
else if (t < 0.86) g.rect(p(randInt(0, 2)), p(randInt(0, 2)), s / 3, s / 3);
else { const x = p(randInt(0, 2)), y = p(randInt(0, 2)); g.moveTo(x, y + s / 3); g.lineTo(x + s / 3, y); }
}
g.stroke(); g.restore();
}
const SIGN_WORDS = ["HOTEL", "RAMEN", "BAR", "CLINIC", "DATA", "24H", "NOODLE", "ARCADE", "SYNTH", "MODS", "VOLT", "ZEN"];
const NEON = ["#ff2fa8", "#29f0ff", "#ffb21e", "#9a6bff", "#7dff5a", "#ff4b3d", "#e8f4ff"];
const SIGNS = 12;
const signCol = [];
const signTex = canvasTex(128 * (SIGNS + 1), 384, (g) => {
g.fillStyle = "#fff"; g.fillRect(128 * SIGNS, 0, 128, 384);
for (let i = 0; i < SIGNS; i++) {
const col = NEON[i % NEON.length], w = 128, h = 384;
signCol.push(new THREE.Color(col));
g.save(); g.translate(i * 128, 0);
g.fillStyle = "rgba(6,6,14,.94)";
g.beginPath(); g.roundRect(6, 6, w - 12, h - 12, 14); g.fill();
g.strokeStyle = col; g.lineWidth = 5; g.shadowColor = col; g.shadowBlur = 16;
g.beginPath(); g.roundRect(12, 12, w - 24, h - 24, 10); g.stroke();
if (i % 2 === 0) {
const word = SIGN_WORDS[i], n = word.length, size = Math.min(64, (h - 50) / n);
g.font = "800 " + size + "px Manrope, system-ui, sans-serif";
g.textAlign = "center"; g.textBaseline = "middle";
for (let k = 0; k < n; k++) {
const y = h / 2 + (k - (n - 1) / 2) * size;
g.shadowBlur = 18; g.fillStyle = col; g.fillText(word[k], w / 2, y);
g.shadowBlur = 0; g.fillStyle = "rgba(255,255,255,.85)"; g.fillText(word[k], w / 2, y);
}
} else {
g.strokeStyle = "#fff"; g.shadowBlur = 14;
for (let k = 0; k < 4; k++) glyph(g, w / 2, 62 + k * 86, 58);
}
g.restore();
}
});
const signUV = (i) => (u, vv) => [(i + u) / (SIGNS + 1), vv];
const WHITE_UV = [(SIGNS + 0.5) / (SIGNS + 1), 0.5];
// Giant holographic adverts for brands that do not exist. Portrait ones hang off the towers, wide
// ones off the street buildings. Each keeps its main colour for the reflection it throws on the road.
const HOLO_N = 6, WIDE_N = 4;
const holoCol = [], wideCol = [];
const font = (wgt, size) => wgt + " " + size + "px Manrope, system-ui, sans-serif";
function glowText(g, txt, x, y, size, col, wgt) {
g.font = font(wgt || 900, size); g.textAlign = "center"; g.textBaseline = "middle";
g.shadowColor = col; g.shadowBlur = size * 0.45; g.fillStyle = col; g.fillText(txt, x, y);
g.shadowBlur = 0; g.fillStyle = "rgba(255,255,255,.88)"; g.fillText(txt, x, y);
}
function vgrad(g, h, a, b) { const gr = g.createLinearGradient(0, 0, 0, h); gr.addColorStop(0, a); gr.addColorStop(1, b); return gr; }
function scanlines(g, w, h, col) {
g.shadowBlur = 0;
g.fillStyle = "rgba(0,0,0,.24)";
for (let y = 0; y < h; y += 4) g.fillRect(0, y, w, 1.5);
g.strokeStyle = col; g.globalAlpha = 0.8; g.lineWidth = 4; g.strokeRect(2, 2, w - 4, h - 4); g.globalAlpha = 1;
}
const HOLO_ADS = [
["#29f0ff", (g, w, h) => { // an eye that watches the traffic
const gr = g.createRadialGradient(w / 2, 220, 10, w / 2, 220, 260); gr.addColorStop(0, "#0d3a58"); gr.addColorStop(1, "#03060e");
g.fillStyle = gr; g.fillRect(0, 0, w, h);
g.beginPath(); g.moveTo(24, 220); g.quadraticCurveTo(w / 2, 100, w - 24, 220); g.quadraticCurveTo(w / 2, 340, 24, 220);
g.fillStyle = "rgba(41,240,255,.12)"; g.fill();
g.strokeStyle = "#29f0ff"; g.lineWidth = 6; g.shadowColor = "#29f0ff"; g.shadowBlur = 20; g.stroke();
const ir = g.createRadialGradient(w / 2, 220, 4, w / 2, 220, 54); ir.addColorStop(0, "#ffffff"); ir.addColorStop(0.3, "#ff3fb0"); ir.addColorStop(1, "#40107a");
g.fillStyle = ir; g.beginPath(); g.arc(w / 2, 220, 54, 0, TAU); g.fill();
g.shadowBlur = 0; g.fillStyle = "#05050a"; g.beginPath(); g.arc(w / 2, 220, 22, 0, TAU); g.fill();
g.fillStyle = "#fff"; g.beginPath(); g.arc(w / 2 - 14, 206, 7, 0, TAU); g.fill();
glowText(g, "SEE MORE", w / 2, 402, 36, "#29f0ff");
g.strokeStyle = "#bff8ff"; g.shadowColor = "#29f0ff"; g.shadowBlur = 10;
for (let k = 0; k < 3; k++) glyph(g, w / 2 - 60 + k * 60, 462, 34);
}],
["#ff6a3d", (g, w, h) => { // orbital holiday resorts
g.fillStyle = vgrad(g, h, "#1a0630", "#3c0a2c"); g.fillRect(0, 0, w, h);
g.fillStyle = "#fff"; for (let k = 0; k < 40; k++) g.fillRect(rand(w), rand(h * 0.6), 2, 2);
const pl = g.createRadialGradient(100, 180, 8, w / 2, 200, 86); pl.addColorStop(0, "#ffd08a"); pl.addColorStop(0.5, "#ff3f8a"); pl.addColorStop(1, "#3a0a40");
g.fillStyle = pl; g.beginPath(); g.arc(w / 2, 200, 84, 0, TAU); g.fill();
g.strokeStyle = "rgba(255,226,190,.9)"; g.lineWidth = 6; g.shadowColor = "#ff6a3d"; g.shadowBlur = 16;
g.beginPath(); g.ellipse(w / 2, 204, 126, 26, -0.28, 0, TAU); g.stroke();
glowText(g, "NOVA", w / 2, 362, 76, "#ff6a3d");
g.shadowBlur = 0; g.font = font(700, 20); g.fillStyle = "#ffd6c0"; g.fillText("ORBITAL RESORTS", w / 2, 418);
}],
["#7dff5a", (g, w, h) => { // a soda
g.fillStyle = vgrad(g, h, "#06301c", "#020c06"); g.fillRect(0, 0, w, h);
const cg = g.createLinearGradient(78, 0, 178, 0); cg.addColorStop(0, "#1c6a3a"); cg.addColorStop(0.4, "#c8ffd8"); cg.addColorStop(1, "#0c3a20");
g.fillStyle = cg; g.shadowColor = "#7dff5a"; g.shadowBlur = 24;
g.beginPath(); g.roundRect(78, 70, 100, 240, 20); g.fill();
g.shadowBlur = 0; g.fillStyle = "#0b3a1e"; g.fillRect(78, 160, 100, 60);
g.strokeStyle = "#7dff5a"; g.lineWidth = 3; glyph(g, w / 2, 190, 42);
g.strokeStyle = "rgba(190,255,210,.6)"; g.lineWidth = 2;
for (let k = 0; k < 14; k++) { g.beginPath(); g.arc(rand(30, w - 30), rand(40, 330), rand(3, 9), 0, TAU); g.stroke(); }
glowText(g, "FIZZ", w / 2, 400, 78, "#7dff5a");
}],
["#ff2f5a", (g, w, h) => { // a vertical banner in the city script
g.fillStyle = vgrad(g, h, "#c40f3c", "#4a0418"); g.fillRect(0, 0, w, h);
g.strokeStyle = "rgba(255,255,255,.8)"; g.lineWidth = 3; g.strokeRect(18, 18, w - 36, h - 36);
g.strokeStyle = "#fff"; g.shadowColor = "#ffb0c0"; g.shadowBlur = 12;
for (let k = 0; k < 5; k++) glyph(g, w / 2, 68 + k * 94, 66);
}],
["#ffd21e", (g, w, h) => { // an energy drink
g.fillStyle = vgrad(g, h, "#1c0a40", "#06031a"); g.fillRect(0, 0, w, h);
const bg = g.createLinearGradient(0, 90, 0, 400); bg.addColorStop(0, "#fff3a0"); bg.addColorStop(1, "#ff7a1a");
g.fillStyle = bg; g.shadowColor = "#ffd21e"; g.shadowBlur = 26;
g.beginPath(); g.moveTo(152, 70); g.lineTo(70, 250); g.lineTo(122, 250); g.lineTo(96, 390); g.lineTo(190, 200); g.lineTo(138, 200); g.lineTo(172, 70); g.closePath(); g.fill();
glowText(g, "VOLT", w / 2, 450, 72, "#ffd21e");
}],
["#ff8a2a", (g, w, h) => { // a koi
g.fillStyle = vgrad(g, h, "#043a40", "#021418"); g.fillRect(0, 0, w, h);
g.strokeStyle = "rgba(120,240,255,.35)"; g.lineWidth = 2;
for (let k = 0; k < 6; k++) { g.beginPath(); g.arc(w / 2, 240, 40 + k * 26, 0, TAU); g.stroke(); }
g.shadowColor = "#ff8a2a"; g.shadowBlur = 22;
g.fillStyle = "#ff7a1a"; g.beginPath(); g.moveTo(w / 2, 330); g.lineTo(w / 2 - 44, 410); g.lineTo(w / 2, 384); g.lineTo(w / 2 + 44, 410); g.closePath(); g.fill();
const kg = g.createLinearGradient(80, 0, 176, 0); kg.addColorStop(0, "#ff5a10"); kg.addColorStop(0.5, "#ffb070"); kg.addColorStop(1, "#ff5a10");
g.fillStyle = kg; g.beginPath(); g.ellipse(w / 2, 230, 46, 112, 0, 0, TAU); g.fill();
g.shadowBlur = 0; g.fillStyle = "#fff"; g.beginPath(); g.ellipse(w / 2 + 8, 210, 18, 34, 0.3, 0, TAU); g.fill();
g.fillStyle = "#111"; for (const x of [-18, 18]) { g.beginPath(); g.arc(w / 2 + x, 142, 5, 0, TAU); g.fill(); }
glowText(g, "KOI", w / 2, 462, 62, "#ff8a2a");
g.strokeStyle = "#ffe0c0"; g.shadowColor = "#ff8a2a"; g.shadowBlur = 10; glyph(g, 44, 52, 34); glyph(g, w - 44, 52, 34);
}]
];
const WIDE_ADS = [
["#ffc21e", (g, w, h) => { // a sky taxi company
g.fillStyle = vgrad(g, h, "#160a26", "#08040f"); g.fillRect(0, 0, w, h);
g.strokeStyle = "rgba(255,194,30,.5)"; g.lineWidth = 3;
for (let k = 0; k < 9; k++) { const y = 120 + k * 10; g.beginPath(); g.moveTo(10, y); g.lineTo(rand(60, 140), y); g.stroke(); }
g.fillStyle = "#ffc21e"; g.shadowColor = "#ffc21e"; g.shadowBlur = 20;
g.beginPath(); g.moveTo(40, 190); g.lineTo(70, 140); g.lineTo(150, 118); g.lineTo(230, 132); g.lineTo(256, 178); g.lineTo(240, 196); g.closePath(); g.fill();
g.shadowBlur = 0; g.fillStyle = "#1a1a24"; g.beginPath(); g.moveTo(100, 140); g.lineTo(150, 126); g.lineTo(205, 136); g.lineTo(196, 150); g.lineTo(108, 152); g.closePath(); g.fill();
g.fillStyle = "#7ff0ff"; g.shadowColor = "#29f0ff"; g.shadowBlur = 16; g.fillRect(60, 200, 170, 6);
glowText(g, "SKY TAXI", 380, 104, 54, "#ffc21e");
g.shadowBlur = 0; g.font = font(700, 22); g.fillStyle = "#ffe9b0"; g.fillText("24H · ANY FLOOR", 380, 160);
}],
["#29f0ff", (g, w, h) => { // a network
g.fillStyle = vgrad(g, h, "#04182a", "#02060c"); g.fillRect(0, 0, w, h);
g.strokeStyle = "rgba(41,240,255,.18)"; g.lineWidth = 1;
for (let x = 0; x < w; x += 32) { g.beginPath(); g.moveTo(x, 0); g.lineTo(x, h); g.stroke(); }
for (let y = 0; y < h; y += 32) { g.beginPath(); g.moveTo(0, y); g.lineTo(w, y); g.stroke(); }
g.strokeStyle = "#29f0ff"; g.lineWidth = 5; g.shadowColor = "#29f0ff"; g.shadowBlur = 18;
g.beginPath(); g.moveTo(0, 90);
for (let x = 0; x <= w; x += 8) g.lineTo(x, 90 + (Math.abs(x - w / 2) < 60 ? Math.sin(x * 0.4) * 50 : Math.sin(x * 0.08) * 6));
g.stroke();
glowText(g, "PULSE", w / 2, 196, 70, "#29f0ff");
}],
["#ff2fa8", (g, w, h) => { // noodles
g.fillStyle = vgrad(g, h, "#2c0420", "#0e0108"); g.fillRect(0, 0, w, h);
g.fillStyle = "#ff2fa8"; g.shadowColor = "#ff2fa8"; g.shadowBlur = 22;
g.beginPath(); g.arc(122, 128, 82, 0, Math.PI); g.closePath(); g.fill();
g.fillRect(70, 212, 104, 10);
g.strokeStyle = "rgba(255,220,240,.85)"; g.lineWidth = 4; g.shadowBlur = 10;
for (let k = 0; k < 3; k++) { g.beginPath(); g.moveTo(92 + k * 30, 112); g.bezierCurveTo(78 + k * 30, 80, 108 + k * 30, 66, 92 + k * 30, 30); g.stroke(); }
glowText(g, "RAMEN", 354, 110, 72, "#ff2fa8");
g.strokeStyle = "#ffd0ea"; g.shadowBlur = 10; for (let k = 0; k < 3; k++) glyph(g, 300 + k * 54, 190, 36);
}],
["#9a6bff", (g, w, h) => { // a bank of the future
g.fillStyle = vgrad(g, h, "#2a0f5a", "#08031a"); g.fillRect(0, 0, w, h);
const sg = g.createLinearGradient(0, 70, 0, 170); sg.addColorStop(0, "#ffd6ff"); sg.addColorStop(1, "#9a6bff");
g.fillStyle = sg; g.shadowColor = "#9a6bff"; g.shadowBlur = 26; g.beginPath(); g.arc(w / 2, 168, 70, Math.PI, 0); g.fill();
g.shadowBlur = 0; g.strokeStyle = "rgba(200,170,255,.6)"; g.lineWidth = 2;
for (let k = 0; k < 7; k++) { const y = 170 + k * k * 2.2; g.beginPath(); g.moveTo(0, y); g.lineTo(w, y); g.stroke(); }
for (let k = -8; k <= 8; k++) { g.beginPath(); g.moveTo(w / 2 + k * 12, 170); g.lineTo(w / 2 + k * 70, h); g.stroke(); }
glowText(g, "NEXUS", w / 2, 70, 66, "#c9adff");
}]
];
// One atlas: the portrait ads in a row, then the wide ones two by two.
const AD_W = 256 * HOLO_N + 1024;
const holoTex = canvasTex(AD_W, 512, (g) => {
const cell = ([col, paint], x, y, cw, ch, cols) => {
cols.push(new THREE.Color(col));
g.save(); g.translate(x, y);
g.beginPath(); g.rect(0, 0, cw, ch); g.clip();
paint(g, cw, ch);
scanlines(g, cw, ch, col);
g.restore();
};
HOLO_ADS.forEach((ad, i) => cell(ad, i * 256, 0, 256, 512, holoCol));
WIDE_ADS.forEach((ad, i) => cell(ad, 256 * HOLO_N + (i % 2) * 512, (i >> 1) * 256, 512, 256, wideCol));
});
const holoUV = (i) => (u, vv) => [(i + u) * 256 / AD_W, vv];
const wideUV = (i) => (u, vv) => [(256 * HOLO_N + ((i % 2) + u) * 512) / AD_W, (i >> 1 ? 0 : 0.5) + vv * 0.5];
/* ---------------- materials ---------------- */
const NEON_N = NEON.length;
const NEON_M = NEON.map((c) => new THREE.MeshBasicMaterial({ color: hdr(c, 3.4) }));
const NEON_DIM = NEON.map((c) => new THREE.MeshBasicMaterial({ color: hdr(c, 1.5) }));
const NEON_C = NEON.map((c) => new THREE.Color(c));
const facadeSet = [0, 1, 2, 3, 4].map((k) => facade(k));
// The city behind is dimmer than the obstacles in front of it, so the play line always reads first.
const BG_FAC = facadeSet.map((m) => { const c = m.clone(); c.color.setScalar(0.5); c.emissiveIntensity *= 0.85; return c; });
const OB_FAC = facadeSet.map((m) => { const c = m.clone(); c.color.setScalar(1.15); c.emissiveIntensity *= 1.35; return c; });
const FAC_UV = (k) => k >= 3 ? [MEGA_U, MEGA_V] : [FAC_U, FAC_V];
const M = {
metal: new THREE.MeshStandardMaterial({ color: 0x1c2030, metalness: 0.8, roughness: 0.34, envMapIntensity: 1.1 }),
steel: new THREE.MeshStandardMaterial({ color: 0x5a6274, metalness: 0.9, roughness: 0.28, envMapIntensity: 1.2 }),
dark: new THREE.MeshStandardMaterial({ color: 0x0b0d14, metalness: 0.5, roughness: 0.6 }),
holo: new THREE.MeshBasicMaterial({ map: holoTex, color: hdr(0xffffff, 1.6), side: THREE.DoubleSide }),
sign: new THREE.MeshBasicMaterial({ map: signTex, color: hdr(0xffffff, 1.8), side: THREE.DoubleSide }),
warm: new THREE.MeshBasicMaterial({ color: hdr(0xffd9a0, 1.7) }),
glass: new THREE.MeshStandardMaterial({ color: 0x0a1020, metalness: 0.9, roughness: 0.08, envMapIntensity: 1.6, emissive: 0x0c1a30, emissiveIntensity: 0.6 })
};
/* ---------------- sky, skyline, smog ---------------- */
const skyMat = new THREE.ShaderMaterial({
side: THREE.BackSide, depthWrite: false, fog: false,
uniforms: { hor: { value: pal.hor }, top: { value: pal.top }, b1: { value: pal.b1 }, b2: { value: pal.b2 }, fogC: { value: pal.fog }, sun: { value: 0 }, time: { value: 0 } },
vertexShader: "varying vec3 vD; void main(){ vD = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
fragmentShader: [
"uniform vec3 hor, top, b1, b2, fogC; uniform float sun, time; varying vec3 vD;",
"void main(){",
" vec3 d = normalize(vD); float h = d.y, az = atan(d.x, -d.z);",
" vec3 mid = mix(fogC, top, 0.45);",
" vec3 c = mix(hor, mid, smoothstep(0.0, 0.22, h));",
" c = mix(c, top, smoothstep(0.18, 0.75, h));",
" float band = 1.0 - smoothstep(-0.02, 0.34, h);",
" c += b1 * band * band * (0.55 + 0.45 * sin(az * 2.0 + 0.6));",
" c += b2 * band * band * pow(0.5 + 0.5 * sin(az * 3.0 - 1.2), 3.0);",
" float streak = 0.5 + 0.5 * sin(h * 64.0 + sin(az * 4.0 + time * 0.03) * 2.0 + sin(az * 9.0));",
" c *= 1.0 + 0.28 * streak * smoothstep(0.02, 0.1, h) * (1.0 - smoothstep(0.12, 0.45, h));",
" vec3 sd = normalize(vec3(0.3, 0.075, -1.0));",
" float s = max(dot(d, sd), 0.0);",
" c += sun * (vec3(1.0, 0.62, 0.3) * smoothstep(0.99935, 0.9996, s) * 5.0 + vec3(1.0, 0.42, 0.16) * pow(s, 40.0) * 0.6 + vec3(0.9, 0.32, 0.2) * pow(s, 5.0) * 0.16);",
" c = mix(fogC, c, smoothstep(-0.06, 0.0, h));",
" gl_FragColor = vec4(c, 1.0);",
"}"
].join("\n")
});
const sky = new THREE.Mesh(new THREE.SphereGeometry(3000, 32, 16), skyMat);
sky.renderOrder = -10; sky.frustumCulled = false;
scene.add(sky);
const starGeo = new THREE.BufferGeometry();
{
const a = [];
for (let i = 0; i < 220; i++) {
const az = rand(-1.2, 1.2), e = Math.asin(rand(0.3, 1));
a.push(Math.sin(az) * Math.cos(e) * 2900, Math.sin(e) * 2900, -Math.cos(az) * Math.cos(e) * 2900);
}
starGeo.setAttribute("position", new THREE.Float32BufferAttribute(a, 3));
}
const stars = new THREE.Points(starGeo, new THREE.PointsMaterial({ color: 0xc8c0f0, size: 1.6, sizeAttenuation: false, transparent: true, opacity: 0.4, depthWrite: false, fog: false }));
stars.renderOrder = -9; stars.frustumCulled = false;
scene.add(stars);
// Two rings of megastructures on the horizon, the far one sunk deeper in the smog.
function towerOnSkyline(g, x, bw, bh, h, body, far) {
const tiers = Math.random() < 0.45 ? randInt(2, 3) : 1;
let tx = x, tw = bw, ty = h - bh;
for (let t = 0; t < tiers; t++) {
const th = t === tiers - 1 ? h - ty : (h - ty) * rand(0.15, 0.3);
g.fillStyle = body; g.fillRect(tx, ty, tw, th);
const win = PA.choice(["rgba(255,214,160,.8)", "rgba(170,225,255,.8)", "rgba(255,150,210,.7)", "rgba(255,255,255,.7)"]);
g.fillStyle = win;
for (let wy = ty + 4; wy < h - 2; wy += far ? 5 : 6) for (let wx = tx + 2; wx < tx + tw - 2; wx += far ? 4 : 5) if (Math.random() < 0.2) g.fillRect(wx, wy, 2, 2);
if (t < tiers - 1) { const inset = tw * rand(0.1, 0.25); tx += inset; tw -= inset * 2; ty += th; }
}
// the very top of the tallest tier gets the trim
const neon = PA.choice(NEON);
g.fillStyle = neon;
if (Math.random() < 0.4) { g.fillRect(x, h - bh, 2, bh); g.fillRect(x + bw - 2, h - bh, 2, bh); }
if (Math.random() < 0.35) for (let y = h - bh + rand(10, 30); y < h; y += rand(30, 70)) g.fillRect(x, y, bw, 2);
if (Math.random() < 0.25 && bw > 26) {
const sw = bw * rand(0.5, 0.8), sh = rand(16, 50), sy = h - bh + rand(10, Math.max(12, bh * 0.5));
const gr = g.createLinearGradient(0, sy, 0, sy + sh); gr.addColorStop(0, neon); gr.addColorStop(1, PA.choice(NEON));
g.fillStyle = gr; g.fillRect(x + (bw - sw) / 2, sy, sw, sh);
}
if (bh > h * 0.45 && Math.random() < 0.6) {
const sx = x + bw / 2;
g.fillStyle = body; g.fillRect(sx - 1.5, h - bh - rand(20, 70), 3, 70);
g.fillStyle = "#ff3b4e"; g.fillRect(sx - 2.5, h - bh - 72, 5, 4);
}
}
function skylineTex(far) {
return canvasTex(2048, 1024, (g, w, h) => {
g.clearRect(0, 0, w, h);
const body = far ? "#120b22" : "#0b0714";
let x = 0;
while (x < w) {
const bw = rand(16, far ? 64 : 90);
const bh = h * (Math.random() < (far ? 0.25 : 0.16) ? rand(0.5, 0.95) : rand(0.1, 0.45));
towerOnSkyline(g, x, bw, bh, h, body, far);
if (x + bw > w) towerOnSkyline(g, x - w, bw, bh, h, body, far);
x += bw + rand(-10, 6);
}
g.globalCompositeOperation = "source-atop";
g.fillStyle = vgrad(g, h, far ? "rgba(50,16,70,.35)" : "rgba(30,10,40,.12)", far ? "rgba(150,48,130,.6)" : "rgba(120,36,110,.4)");
g.fillRect(0, 0, w, h);
g.globalCompositeOperation = "source-over";
}, { repeat: true });
}
const SKY_W = 5600;
const skylines = [];
for (const [far, z, hgt] of [[true, -1700, 980], [false, -1150, 700]]) {
const t = skylineTex(far);
t.repeat.set(3, 1);
const m = new THREE.Mesh(new THREE.PlaneGeometry(SKY_W, hgt),
new THREE.MeshBasicMaterial({ map: t, color: hdr(0xffffff, far ? 1.1 : 1.35), transparent: true, depthWrite: false, fog: false }));
m.position.set(0, hgt / 2 - 140, z);
m.renderOrder = far ? -8 : -7; m.frustumCulled = false;
m.userData.k = far ? 1.1 : 1.35;
scene.add(m);
skylines.push(m);
}
// The smog deck far below: the undercity's light bleeds up through slow-moving murk.
const deckMat = new THREE.ShaderMaterial({
fog: false,
uniforms: { time: { value: 0 }, fogC: { value: pal.fog }, b1: { value: pal.b1 }, b2: { value: pal.b2 } },
vertexShader: "varying vec3 vW; void main(){ vec4 w = modelMatrix * vec4(position, 1.0); vW = w.xyz; gl_Position = projectionMatrix * viewMatrix * w; }",
fragmentShader: [
"uniform float time; uniform vec3 fogC, b1, b2; varying vec3 vW;",
"float hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }",
"float noise(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);",
" return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y); }",
"float fbm(vec2 p){ float a = 0.5, s = 0.0; for (int i = 0; i < 4; i++){ s += a * noise(p); p *= 2.03; a *= 0.5; } return s; }",
"void main(){",
" vec2 p = vW.xz;",
" float n = fbm(p * 0.012 + vec2(time * 0.015, time * 0.005));",
" float m = fbm(p * 0.035 - vec2(time * 0.03, 0.0));",
" float hot = pow(fbm(p * 0.007 + 3.1), 3.0) * 3.2;",
" vec2 cell = floor(p * 0.35);",
" float lit = step(0.93, hash(cell)) * (0.5 + 0.5 * sin(time * 2.0 + hash(cell + 7.0) * 40.0));",
" vec3 c = fogC * (0.35 + 0.5 * n) + b1 * n * m * 0.9 + b2 * hot * 0.45 + mix(b1, vec3(1.0, 0.8, 0.5), 0.5) * lit * hot * 2.5;",
" float d = length(vW.xz - cameraPosition.xz);",
" c = mix(c, fogC, smoothstep(200.0, 1600.0, d));",
" gl_FragColor = vec4(c, 1.0);",
"}"
].join("\n")
});
const deck = new THREE.Mesh(new THREE.PlaneGeometry(7000, 3400), deckMat);
deck.rotation.x = -Math.PI / 2;
deck.position.set(0, -70, -1300);
deck.frustumCulled = false;
scene.add(deck);
/* ---------------- light ---------------- */
const hemi = new THREE.HemisphereLight(0x7a64d0, 0x180a20, 0.85);
const rimA = new THREE.DirectionalLight(0xff3ca8, 0.95);
rimA.position.set(-60, 40, 30);
const rimB = new THREE.DirectionalLight(0x2ad8ff, 0.85);
rimB.position.set(70, 26, 24);
const keyL = new THREE.DirectionalLight(0xd8d4ff, 0.42);
keyL.position.set(12, 22, 60);
scene.add(hemi, rimA, rimB, keyL, rimA.target, rimB.target, keyL.target);
{
// Environment map so paint, glass and metal pick up a city of neon around them.
const env = new THREE.Scene();
env.add(new THREE.Mesh(new THREE.SphereGeometry(50, 32, 16), new THREE.ShaderMaterial({
side: THREE.BackSide,
vertexShader: skyMat.vertexShader,
fragmentShader: "varying vec3 vD; void main(){ float h = vD.y; vec3 c = mix(vec3(0.5,0.12,0.4), vec3(0.02,0.015,0.06), smoothstep(-0.05, 0.45, h)); c = mix(vec3(0.03,0.025,0.05), c, smoothstep(-0.2, 0.0, h)); gl_FragColor = vec4(c, 1.0); }"
})));
const ENV_COLS = ["#ff2fa8", "#29f0ff", "#9a6bff", "#ffffff", "#ffd29a", "#ff4b3d"];
for (let i = 0; i < 46; i++) {
const a = rand(TAU), tall = Math.random() < 0.5;
const b = new THREE.Mesh(new THREE.BoxGeometry(tall ? rand(0.4, 1.2) : rand(3, 8), tall ? rand(6, 16) : rand(1.5, 4), 0.5),
new THREE.MeshBasicMaterial({ color: hdr(PA.choice(ENV_COLS), rand(2, 6)) }));
b.position.set(Math.cos(a) * 40, rand(-1, 12), Math.sin(a) * 40);
b.lookAt(0, 0, 0);
env.add(b);
}
for (let i = 0; i < 6; i++) { // overhead strip lights
const b = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.2, 30), new THREE.MeshBasicMaterial({ color: hdr(0xcfe4ff, 4) }));
b.position.set(rand(-20, 20), 30, rand(-20, 20)); b.rotation.y = rand(TAU);
env.add(b);
}
const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(env, 0.02).texture;
pmrem.dispose();
}
/* ---------------- the city ---------------- */
// Four rows of towers behind the skyway, from a canyon wall just behind the flight line out to
// megastructures sunk in the smog. Built in CHUNK-wide strips ahead of the camera, dropped behind it.
const CHUNK = 150;
const BANDS = [
{ z0: -44, z1: -58, w0: 8, w1: 18, d0: 8, d1: 14, g0: 8, g1: 26, h0: 10, h1: 58, fac: [0, 2, 2], sign: 0.45, neon: 0.35, bridge: 0.2 },
{ z0: -85, z1: -125, w0: 14, w1: 28, d0: 12, d1: 22, g0: 6, g1: 24, h0: 24, h1: 110, fac: [1, 2, 0], ads: 0.45, neon: 0.35, bridge: 0.4 },
{ z0: -160, z1: -230, w0: 20, w1: 44, d0: 18, d1: 30, g0: 6, g1: 24, h0: 40, h1: 175, fac: [3, 4, 1], ads: 0.45, neon: 0.35, bridge: 0.3 },
{ z0: -290, z1: -420, w0: 30, w1: 66, d0: 25, d1: 40, g0: 8, g1: 34, h0: 60, h1: 270, fac: [3, 4], ads: 0.35, neon: 0.3 }
];
const beaconMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending,
uniforms: { time: { value: 0 }, scale: pointsMat.uniforms.scale },
vertexShader: "uniform float time, scale; attribute float ph; varying float vA; void main(){ vec4 mv = modelViewMatrix * vec4(position, 1.0); vA = step(0.8, fract(time * 0.6 + ph)); gl_PointSize = max(2.0, 3.4 * scale / -mv.z); gl_Position = projectionMatrix * mv; }",
fragmentShader: "varying float vA; void main(){ float d = length(gl_PointCoord - 0.5); float a = smoothstep(0.5, 0.0, d); gl_FragColor = vec4(vec3(3.2, 0.25, 0.3) * a * a * (0.12 + vA), 1.0); }"
});
function neonEdges(G, x0, x1, y0, y1, z1, th, ci) {
G.neon[ci].box(x0 - th * 0.5, x0 + th, y0, y1, z1 - th, z1 + th * 0.5);
G.neon[ci].box(x1 - th, x1 + th * 0.5, y0, y1, z1 - th, z1 + th * 0.5);
}
function neonRing(G, x0, x1, y, z0, z1, th, ci) {
G.neon[ci].box(x0 - th, x1 + th, y - th, y + th, z1, z1 + th);
G.neon[ci].box(x0 - th, x1 + th, y - th, y + th, z0 - th, z0);
G.neon[ci].box(x0 - th, x0, y - th, y + th, z0, z1);
G.neon[ci].box(x1, x1 + th, y - th, y + th, z0, z1);
}
function bgTower(G, x0, x1, top, zc, d, B, beacons) {
const z0 = zc - d / 2, z1 = zc + d / 2, fk = PA.choice(B.fac), [U, V] = FAC_UV(fk);
const th = 0.14 + Math.abs(zc) * 0.0035, ou = Math.random(), ov = Math.random();
const tiers = top > 70 && Math.random() < 0.6 ? randInt(2, 3) : 1;
let y = BOTTOM, xa = x0, xb = x1, za = z0, zb = z1, firstTop = top;
const ci = randInt(0, NEON_N - 1);
for (let t = 0; t < tiers; t++) {
const yTop = t === tiers - 1 ? top : lerp(y, top, rand(0.5, 0.72));
if (t === 0) firstTop = yTop;
G.fac[fk].box(xa, xb, y, yTop, za, zb, U, V, ou, ov);
if (t < tiers - 1 ? Math.random() < 0.7 : Math.random() < B.neon) neonRing(G, xa, xb, yTop, za, zb, th, t ? ci : randInt(0, NEON_N - 1));
y = yTop;
const ix = (xb - xa) * rand(0.1, 0.2), iz = (zb - za) * rand(0.1, 0.2);
xa += ix; xb -= ix; za += iz; zb -= iz;
}
if (Math.random() < B.neon * 0.4) neonEdges(G, x0, x1, BOTTOM, firstTop, z1, th, ci);
// stacked light bands on some of the big ones
if (Math.random() < 0.3) for (let yb = rand(8, 20); yb < firstTop - 4; yb += rand(16, 34)) G.neon[ci].box(x0 - th, x1 + th, yb, yb + th * 1.6, z1, z1 + th);
// spire and beacon
const cx = (x0 + x1) / 2;
if (top > 45 && Math.random() < 0.55) {
const sh = rand(6, 22) * (1 + Math.abs(zc) / 200);
G.metal.box(cx - th * 1.5, cx + th * 1.5, top, top + sh, zc - th * 1.5, zc + th * 1.5);
beacons.push(cx, top + sh + 0.3, zc, Math.random());
} else if (Math.random() < 0.5) beacons.push(xa + 0.4, top + 0.4, zb, Math.random(), xb - 0.4, top + 0.4, zb, Math.random());
// giant holo adverts hung on the front
if (B.ads && Math.random() < B.ads && firstTop > 30) {
const wide = Math.random() < 0.4, w = x1 - x0;
const aw = wide ? w * rand(0.7, 0.9) : Math.min(w * 0.6, rand(9, 20)), ah = wide ? aw / 2 : aw * 2;
const ay = clamp(rand(14, firstTop - ah / 2 - 2), ah / 2 + 2, Math.max(ah / 2 + 2, firstTop - ah / 2 - 2));
const idx = wide ? randInt(0, WIDE_N - 1) : randInt(0, HOLO_N - 1);
G.holo.add(planeGeo, mat4(cx + rand(-0.15, 0.15) * w, ay, z1 + 0.5, 0, 0, 0, aw, ah, 1), wide ? wideUV(idx) : holoUV(idx));
G.metal.box(cx - aw * 0.5, cx + aw * 0.5, ay - ah / 2 - 0.5, ay - ah / 2, z1, z1 + 0.6);
}
// vertical blade signs on the near row
if (B.sign && Math.random() < B.sign) {
for (let k = randInt(1, 2); k > 0; k--) {
const sx = rand(x0 + 1, x1 - 1), sy = rand(2, 30), s = rand(1.1, 1.7), idx = randInt(0, SIGNS - 1);
G.sign.add(planeGeo, mat4(sx, sy, z1 + 1.1, 0, 0, 0, 1.4 * s, 4.2 * s, 1), signUV(idx));
G.metal.box(sx - 0.05, sx + 0.05, sy - 2.1 * s, sy + 2.1 * s, z1, z1 + 1.05);
}
}
return { x0, x1, top, zc, d, fk };
}
function skyBridge(G, a, b, B) {
const lo = Math.max(-6, BOTTOM + 30), hi = Math.min(a.top, b.top) - 6;
if (hi < lo + 4) return;
const y = rand(lo, hi), h = rand(1.8, 3.2), dz = Math.min(a.d, b.d) * rand(0.25, 0.4), zc = (a.zc + b.zc) / 2;
G.metal.box(a.x1 - 0.5, b.x0 + 0.5, y, y + h, zc - dz, zc + dz);
G.warm.box(a.x1, b.x0, y + h * 0.35, y + h * 0.7, zc + dz, zc + dz + 0.06);
G.neon[randInt(0, NEON_N - 1)].box(a.x1, b.x0, y - 0.12, y, zc + dz, zc + dz + 0.1);
}
const chunks = new Map();
function buildChunk(ci) {
const x0 = ci * CHUNK;
const G = { fac: BG_FAC.map(() => new Geo()), neon: NEON_M.map(() => new Geo()), holo: new Geo(), sign: new Geo(), metal: new Geo(), warm: new Geo() };
const beacons = [];
for (const B of BANDS) {
let x = x0 + rand(0, B.g1), prev = null;
while (x < x0 + CHUNK) {
const w = rand(B.w0, B.w1), d = rand(B.d0, B.d1), zc = rand(B.z1, B.z0), top = rand(B.h0, B.h1);
const t = bgTower(G, x, x + w, top, zc, d, B, beacons);
if (prev && B.bridge && Math.random() < B.bridge && t.x0 - prev.x1 < 18) skyBridge(G, prev, t, B);
prev = t;
x += w + rand(B.g0, B.g1);
}
}
const list = [];
G.fac.forEach((g, k) => list.push([g, BG_FAC[k]]));
G.neon.forEach((g, k) => list.push([g, NEON_DIM[k]]));
list.push([G.holo, M.holo], [G.sign, M.sign], [G.metal, M.metal], [G.warm, M.warm]);
const grp = geoGroup(list);
if (beacons.length) {
const bg = new THREE.BufferGeometry(), pos = [], ph = [];
for (let i = 0; i < beacons.length; i += 4) { pos.push(beacons[i], beacons[i + 1], beacons[i + 2]); ph.push(beacons[i + 3]); }
bg.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3));
bg.setAttribute("ph", new THREE.Float32BufferAttribute(ph, 1));
const pts = new THREE.Points(bg, beaconMat);
pts.frustumCulled = false;
grp.add(pts);
}
scene.add(grp);
return grp;
}
function updateChunks(cx, all) {
const a = Math.floor((cx - 380) / CHUNK), b = Math.floor((cx + 420) / CHUNK);
for (const [k, g] of chunks) if (k < a || k > b) { disposeGroup(g); chunks.delete(k); }
let built = 0;
for (let k = a; k <= b; k++) if (!chunks.has(k) && (all || built++ < 1)) chunks.set(k, buildChunk(k));
}
function resetChunks() {
for (const [, g] of chunks) disposeGroup(g);
chunks.clear();
}
const mat4 = (x, y, z, ry, rx, rz, sx, sy, sz) => new THREE.Matrix4().compose(
new THREE.Vector3(x, y, z),
new THREE.Quaternion().setFromEuler(new THREE.Euler(rx || 0, ry || 0, rz || 0, "YXZ")),
new THREE.Vector3(sx || 1, sy || sx || 1, sz || sx || 1)
);
/* ---------------- vehicle models ---------------- */
// Hover car bodies are side profiles extruded across the car's width, with closed skirts, a glass
// canopy, LED light bars and glowing hover pads underneath. Every part is merged into one geometry
// with a group per material. Slots: 0 paint, 1 glass, 2 trim, 3 tail lights, 4 LED trim
function merge(parts) {
const pos = [], nor = [], uv = [], groups = [];
const v = new THREE.Vector3(), nm = new THREE.Matrix3();
for (let mi = 0; mi < 8; mi++) {
const start = pos.length / 3;
for (const p of parts) {
if (p.mat !== mi) continue;
const g = p.geo.index ? p.geo.toNonIndexed() : p.geo;
const Pa = g.attributes.position, Na = g.attributes.normal, Ua = g.attributes.uv;
nm.getNormalMatrix(p.m);
for (let i = 0; i < Pa.count; i++) {
v.fromBufferAttribute(Pa, i).applyMatrix4(p.m); pos.push(v.x, v.y, v.z);
v.fromBufferAttribute(Na, i).applyMatrix3(nm).normalize(); nor.push(v.x, v.y, v.z);
uv.push(Ua ? Ua.getX(i) : 0, Ua ? Ua.getY(i) : 0);
}
}
const count = pos.length / 3 - start;
if (count) groups.push([start, count, mi]);
}
const geo = new THREE.BufferGeometry();
geo.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3));
geo.setAttribute("normal", new THREE.Float32BufferAttribute(nor, 3));
geo.setAttribute("uv", new THREE.Float32BufferAttribute(uv, 2));
for (const [s, c, m] of groups) geo.addGroup(s, c, m);
geo.computeBoundingSphere();
return geo;
}
const box = (w, h, d, x, y, z, mat, rx) => ({ geo: new THREE.BoxGeometry(w, h, d), mat, m: mat4(x, y, z, 0, rx || 0) });
// sp: L length, W width, clear ride height, tail/belt/beltF/hood/nose heights, gA..gD canopy x
// stations (from the rear), roof height, wr wheel radius, axR/axF axle stations.
const CAR_SPECS = {
hyper: { L: 4.7, W: 2.04, clear: 0.12, tail: 0.92, belt: 0.86, beltF: 0.76, hood: 0.6, nose: 0.4, gA: 1.0, gB: 2.0, gC: 2.85, gD: 3.55, roof: 1.12, wr: 0.36, axR: 1.0, axF: 3.85, wing: true, jets: true },
coupe: { L: 4.5, W: 1.96, clear: 0.14, tail: 0.88, belt: 0.86, beltF: 0.8, hood: 0.68, nose: 0.46, gA: 0.95, gB: 1.9, gC: 2.7, gD: 3.35, roof: 1.18, wr: 0.34, axR: 0.95, axF: 3.62, wing: true, jets: true },
sedan: { L: 4.9, W: 1.9, clear: 0.18, tail: 1.0, belt: 0.98, beltF: 0.92, hood: 0.84, nose: 0.62, gA: 0.7, gB: 1.45, gC: 3.0, gD: 3.55, roof: 1.42, wr: 0.34, axR: 1.0, axF: 3.9 },
pod: { L: 3.9, W: 1.8, clear: 0.2, tail: 1.05, belt: 0.95, beltF: 0.9, hood: 0.86, nose: 0.66, gA: 0.15, gB: 0.55, gC: 2.5, gD: 3.2, roof: 1.55, wr: 0.32, axR: 0.72, axF: 3.15 },
van: { L: 5.0, W: 2.0, clear: 0.28, tail: 1.3, belt: 1.25, beltF: 1.15, hood: 1.0, nose: 0.82, gA: 0.15, gB: 0.4, gC: 3.6, gD: 4.2, roof: 1.95, wr: 0.38, axR: 0.95, axF: 4.0 }
};
function carParts(sp, withWheels) {
const parts = [];
const { L, W } = sp;
// Extrude a side profile across the width; taper(y) narrows it with height (tumblehome).
const extrude = (shape, depth, bev, mat, taper) => {
const g = new THREE.ExtrudeGeometry(shape, { depth, bevelEnabled: true, bevelThickness: bev, bevelSize: bev, bevelSegments: 3, curveSegments: 12 });
if (taper) {
const pa = g.attributes.position;
for (let i = 0; i < pa.count; i++) pa.setZ(i, (pa.getZ(i) - depth / 2) * taper(pa.getY(i)) + depth / 2);
}
const m = new THREE.Matrix4().makeRotationY(Math.PI / 2).multiply(new THREE.Matrix4().makeTranslation(-L / 2, 0, -depth / 2));
parts.push({ geo: g, mat, m });
};
const ar = sp.wr + 0.07, y0 = sp.clear;
const zOf = (x) => L / 2 - x; // profile station -> model z (rear is +z)
const body = new THREE.Shape();
body.moveTo(0.1, y0);
body.lineTo(0.02, y0 + 0.12);
body.lineTo(0, sp.tail - 0.1);
body.quadraticCurveTo(0, sp.tail, 0.14, sp.tail);
body.lineTo(sp.gA, sp.belt);
body.lineTo(sp.gD, sp.beltF);
body.quadraticCurveTo(L - 0.5, sp.hood, L - 0.12, sp.nose + 0.04);
body.quadraticCurveTo(L, sp.nose, L, sp.nose - 0.1);
body.lineTo(L - 0.02, y0 + 0.12);
body.lineTo(L - 0.12, y0);
if (!sp.hover) {
body.lineTo(sp.axF + ar, y0);
body.lineTo(sp.axF + ar, sp.wr);
body.absarc(sp.axF, sp.wr, ar, 0, Math.PI, false);
body.lineTo(sp.axF - ar, y0);
body.lineTo(sp.axR + ar, y0);
body.lineTo(sp.axR + ar, sp.wr);
body.absarc(sp.axR, sp.wr, ar, 0, Math.PI, false);
body.lineTo(sp.axR - ar, y0);
}
body.closePath();
const sstep = (a, b, y) => smooth(clamp((y - a) / (b - a), 0, 1));
extrude(body, W - 0.12, 0.06, 0, (y) => 1 - 0.05 * sstep(sp.belt - 0.35, sp.belt + 0.05, y) - 0.05 * sstep(y0 + 0.2, y0 - 0.05, y));
const gw = W * 0.9;
const canopy = (y) => lerp(0.97, 0.74, clamp((y - sp.belt) / (sp.roof - sp.belt), 0, 1.1));
const glass = new THREE.Shape();
glass.moveTo(sp.gA - 0.05, sp.belt - 0.04);
glass.quadraticCurveTo(sp.gB - 0.1, sp.roof - 0.02, sp.gB + 0.15, sp.roof);
glass.lineTo(sp.gC - 0.1, sp.roof);
glass.quadraticCurveTo(sp.gC + 0.12, sp.roof - 0.02, sp.gD + 0.05, sp.beltF - 0.04);
glass.closePath();
extrude(glass, gw - 0.08, 0.04, 1, canopy);
const roof = new THREE.Shape();
roof.moveTo(sp.gB + 0.1, sp.roof - 0.03);
roof.lineTo(sp.gC - 0.05, sp.roof - 0.03);
roof.lineTo(sp.gC - 0.12, sp.roof + 0.025);
roof.lineTo(sp.gB + 0.16, sp.roof + 0.025);
roof.closePath();
extrude(roof, gw - 0.02, 0.03, 0, canopy);
const rz = L / 2 + 0.065; // rear face, past the bevel
const ty = sp.tail - 0.14;
parts.push(box(W * 0.86, 0.06, 0.03, 0, ty, rz, 3)); // full-width tail bar
parts.push(box(W * 0.7, 0.018, 0.03, 0, ty - 0.1, rz - 0.003, 4)); // and an LED line under it
parts.push(box(W * 0.92, 0.14, 0.12, 0, y0 + 0.08, rz - 0.04, 2)); // diffuser
parts.push(box(W * 0.78, 0.03, 0.03, 0, sp.nose - 0.12, -L / 2 - 0.065, 4)); // front light bar
parts.push(box(0.1, 0.06, 0.16, -W / 2 - 0.02, sp.beltF + 0.05, zOf(sp.gD) + 0.1, 2), box(0.1, 0.06, 0.16, W / 2 + 0.02, sp.beltF + 0.05, zOf(sp.gD) + 0.1, 2)); // camera pods
// LED sill strips along both flanks
const sA = sp.hover ? 0.3 : sp.axR + ar + 0.05, sB = sp.hover ? L - 0.3 : sp.axF - ar - 0.05;
for (const x of [-1, 1]) parts.push(box(0.02, 0.03, sB - sA, x * (W / 2 + 0.004), y0 + 0.1, zOf((sA + sB) / 2), 4));
if (sp.wing) {
parts.push(box(W * 0.9, 0.035, 0.34, 0, sp.tail + 0.26, L / 2 - 0.22, 0, -0.08));
parts.push(box(W * 0.86, 0.02, 0.02, 0, sp.tail + 0.27, L / 2 - 0.04, 3));
parts.push(box(0.04, 0.24, 0.14, -W * 0.3, sp.tail + 0.13, L / 2 - 0.22, 2), box(0.04, 0.24, 0.14, W * 0.3, sp.tail + 0.13, L / 2 - 0.22, 2));
}
if (sp.jets) {
const ex = new THREE.CylinderGeometry(0.075, 0.075, 0.08, 14).rotateX(Math.PI / 2);
parts.push({ geo: ex, mat: 4, m: mat4(-0.42, y0 + 0.17, rz) }, { geo: ex, mat: 4, m: mat4(0.42, y0 + 0.17, rz) });
}
if (sp.taxi) parts.push(box(0.56, 0.13, 0.24, 0, sp.roof + 0.09, zOf((sp.gB + sp.gC) / 2), 4));
if (sp.hover) {
const pad = new THREE.CylinderGeometry(0.2, 0.25, 0.06, 16);
for (const z of [zOf(sp.axR), zOf(sp.axF)]) for (const x of [-1, 1]) parts.push({ geo: pad, mat: 4, m: mat4(x * (W / 2 - 0.4), y0 - 0.02, z) });
}
return parts;
}
const MODELS = {};
for (const k of ["coupe", "sedan", "pod", "van"]) MODELS[k] = merge(carParts(Object.assign({}, CAR_SPECS[k], { hover: true }), false));
const VM = {
glass: new THREE.MeshStandardMaterial({ color: 0x05070c, metalness: 0.9, roughness: 0.06, envMapIntensity: 1.6 }),
trim: new THREE.MeshStandardMaterial({ color: 0x0b0c10, metalness: 0.4, roughness: 0.5 }),
tail: new THREE.MeshBasicMaterial({ color: hdr(0xff1030, 2.2) })
};
const PAINTS = [["#e9ecf2", 0.5, 0.25], ["#3a3f4a", 0.7, 0.3], ["#121318", 0.5, 0.3], ["#c9ced8", 1, 0.14], ["#3b1a6e", 0.6, 0.28], ["#0f5f6a", 0.6, 0.28], ["#9a0f2a", 0.55, 0.3], ["#d4581a", 0.5, 0.3], ["#7fb6d9", 0.6, 0.3]]
.map(([c, m, r]) => new THREE.MeshStandardMaterial({ color: c, metalness: m, roughness: r, envMapIntensity: 1.2 }));
const ACCENTS = [0x29f0ff, 0xff2fa8, 0x9a6bff, 0xe8f4ff, 0x7dff5a, 0xffb21e].map((c) => new THREE.MeshBasicMaterial({ color: hdr(c, 3) }));
const spriteMat = (c, k) => new THREE.SpriteMaterial({ map: glowTex, color: hdr(c, k), blending: THREE.AdditiveBlending, depthWrite: false, transparent: true });
const HEAD_M = spriteMat(0xfff2dc, 2.4), TAIL_M = spriteMat(0xff2040, 2), POD_M = spriteMat(0x40d8ff, 1.4);
// Headlight beams: a soft cone of light ahead of a car.
const beamGeo = new THREE.CylinderGeometry(0.12, 2.0, 1, 18, 1, true).translate(0, -0.5, 0).rotateZ(Math.PI / 2);
SHARED.add(beamGeo);
const beamMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide,
uniforms: { col: { value: hdr(0xfff0d8, 0.4) } },
vertexShader: "varying vec2 vU; varying vec3 vN, vV; void main(){ vU = uv; vec4 mv = modelViewMatrix * vec4(position, 1.0); vN = normalize(normalMatrix * normal); vV = normalize(-mv.xyz); gl_Position = projectionMatrix * mv; }",
fragmentShader: "uniform vec3 col; varying vec2 vU; varying vec3 vN, vV; void main(){ float f = pow(abs(dot(vN, vV)), 1.5); float a = pow(vU.y, 1.8) * f; gl_FragColor = vec4(col * a, 1.0); }"
});
function trafficCar(kind, dir, beam) {
const g = new THREE.Group(), sp = CAR_SPECS[kind], L = sp.L * CAR_K / 2;
const body = new THREE.Mesh(MODELS[kind], [PA.choice(PAINTS), VM.glass, VM.trim, VM.tail, PA.choice(ACCENTS)]);
body.userData.shared = true;
body.rotation.y = dir > 0 ? -Math.PI / 2 : Math.PI / 2;
body.scale.setScalar(CAR_K);
body.position.y = -sp.roof * 0.5 * CAR_K;
g.add(body);
const head = new THREE.Sprite(HEAD_M); head.scale.set(1.5, 0.9, 1); head.position.set(dir * (L + 0.1), (sp.nose - sp.roof * 0.5) * CAR_K, 0.6);
const tail = new THREE.Sprite(TAIL_M); tail.scale.set(1.3, 0.6, 1); tail.position.set(-dir * (L + 0.05), (sp.tail - 0.14 - sp.roof * 0.5) * CAR_K, 0.4);
const pod = new THREE.Sprite(POD_M); pod.scale.set(L * 2.2, 0.7, 1); pod.position.set(0, -sp.roof * 0.5 * CAR_K, 0.2);
g.add(head, tail, pod);
if (beam) {
const b = new THREE.Mesh(beamGeo, beamMat);
b.userData.shared = true;
b.scale.set(dir * 16, 1, 1); b.position.set(dir * L, (sp.nose - sp.roof * 0.5) * CAR_K - 0.1, 0);
g.add(b);
}
g.userData.hl = L; g.userData.hh = sp.roof * 0.5 * CAR_K;
return g;
}
/* ---------------- the player's car ---------------- */
// A low wedge in sunburst yellow with a black canopy, cyan LED trim and four glowing lifters.
const PSPEC = Object.assign({}, CAR_SPECS.hyper, { hover: true });
const PL = PSPEC.L * CAR_K, PMID = PSPEC.roof * 0.5;
const car = new THREE.Group();
const tilt = new THREE.Group();
car.add(tilt);
const pBody = new THREE.Group();
pBody.rotation.y = -Math.PI / 2;
pBody.scale.setScalar(CAR_K);
pBody.position.y = -PMID * CAR_K;
tilt.add(pBody);
const pPaint = new THREE.MeshPhysicalMaterial({ color: 0xff9a00, metalness: 0.3, roughness: 0.3, clearcoat: 1, clearcoatRoughness: 0.06, envMapIntensity: 0.75 });
const pAccent = new THREE.MeshBasicMaterial({ color: hdr(0x29f0ff, 4) });
const pTail = new THREE.MeshBasicMaterial({ color: hdr(0xff1030, 2.2) });
pBody.add(new THREE.Mesh(merge(carParts(PSPEC, false)), [pPaint, VM.glass, VM.trim, pTail, pAccent]));
// lifters under the skirt
const pods = [];
for (const z of [PSPEC.L / 2 - PSPEC.axR, PSPEC.L / 2 - PSPEC.axF]) for (const x of [-1, 1]) {
const s = new THREE.Sprite(spriteMat(0x50e8ff, 2.2));
s.position.set(x * (PSPEC.W / 2 - 0.4), PSPEC.clear - 0.1, z);
s.scale.set(1.1, 1.1, 1);
pBody.add(s);
pods.push(s);
}
// thrusters: a hot core and a long plume
const flames = [];
const plumeGeo = new THREE.ConeGeometry(0.16, 1, 14, 1, true).translate(0, 0.5, 0).rotateX(Math.PI / 2);
const plumeMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide,
uniforms: { col: { value: hdr(0x6ae8ff, 2.6) }, time: { value: 0 } },
vertexShader: "varying vec2 vU; void main(){ vU = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
fragmentShader: "uniform vec3 col; uniform float time; varying vec2 vU; void main(){ float k = 1.0 - vU.y; float a = pow(k, 1.3) * (0.75 + 0.25 * sin(vU.y * 30.0 - time * 60.0)); gl_FragColor = vec4(mix(col, vec3(3.0, 2.6, 3.2), k * k) * a, 1.0); }"
});
for (const x of [-0.42, 0.42]) {
const core = new THREE.Sprite(spriteMat(0x9af2ff, 3));
core.position.set(x, PSPEC.clear + 0.17, PSPEC.L / 2 + 0.2);
core.scale.setScalar(0.6);
const plume = new THREE.Mesh(plumeGeo, plumeMat);
plume.position.set(x, PSPEC.clear + 0.17, PSPEC.L / 2 + 0.1);
pBody.add(core, plume);
flames.push({ core, plume });
}
const pHead = new THREE.Mesh(beamGeo, beamMat);
pHead.scale.set(14, 0.8, 0.8);
pHead.position.set(PL / 2, (PSPEC.nose - PMID) * CAR_K - 0.08, 0);
tilt.add(pHead);
const pTailGlow = new THREE.Sprite(spriteMat(0xff2040, 1.8));
pTailGlow.position.set(-PL / 2 - 0.1, (PSPEC.tail - 0.14 - PMID) * CAR_K, 0.7);
pTailGlow.scale.set(1.8, 0.5, 1);
tilt.add(pTailGlow);
const underLight = new THREE.PointLight(0x48dcff, 30, 14, 2);
underLight.position.set(0, -0.8, 1.2);
car.add(underLight);
// The shield: a hex-lattice bubble that flares when it takes a hit.
const shieldMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending,
uniforms: { a: { value: 0 }, time: { value: 0 }, col: { value: hdr(0x7dffd0, 1.6) } },
vertexShader: "varying vec3 vN, vV, vP; void main(){ vP = position; vec4 mv = modelViewMatrix * vec4(position, 1.0); vN = normalize(normalMatrix * normal); vV = normalize(-mv.xyz); gl_Position = projectionMatrix * mv; }",
fragmentShader: [
"uniform float a, time; uniform vec3 col; varying vec3 vN, vV, vP;",
"void main(){",
" float f = pow(1.0 - abs(dot(vN, vV)), 2.2);",
" vec2 p = vec2(atan(vP.z, vP.x) * 3.0, vP.y * 5.0);",
" vec2 h = abs(fract(p + vec2(0.0, time * 0.4)) - 0.5);",
" float line = smoothstep(0.44, 0.5, max(h.x, h.y));",
" gl_FragColor = vec4(col * a * (f * 1.2 + line * 0.35 * (0.3 + f)), 1.0);",
"}"].join("\n")
});
const shieldMesh = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), shieldMat);
shieldMesh.scale.set(PL * 0.68, 1.15, 1.5);
car.add(shieldMesh);
scene.add(car);
const jetLocal = [new THREE.Vector3(-PL / 2 - 0.1, -0.1, -0.34), new THREE.Vector3(-PL / 2 - 0.1, -0.1, 0.34)];
/* ---------------- traffic around the skyway ---------------- */
// The skyway is a lane stacked between two others: one below heading your way, one above coming at you.
const laneCars = [];
for (let i = 0; i < 18; i++) {
const upper = i % 2 === 1, kind = PA.choice(["coupe", "sedan", "pod", "sedan", "van"]);
const m = trafficCar(kind, upper ? -1 : 1, false);
scene.add(m);
laneCars.push({ m, upper, x: 0, z: rand(-1.6, 1.6), bob: rand(TAU), v: upper ? rand(14, 22) : rand(0.35, 0.55) });
}
function seedLaneCars(cx) {
let xa = cx - 40, xb = cx - 40;
for (const c of laneCars) {
if (c.upper) { c.x = xa; xa += rand(11, 20); } else { c.x = xb; xb += rand(12, 22); }
}
}
// Mid-depth flyers crossing the gaps between the towers.
const flyers = [];
for (let i = 0; i < 12; i++) {
const dir = Math.random() < 0.5 ? 1 : -1, m = trafficCar(PA.choice(["coupe", "sedan", "pod"]), dir, false);
scene.add(m);
flyers.push({ m, dir, x: 0, y: 0, z: 0, v: 0, bob: rand(TAU) });
}
function launchFlyer(f, cx, any) {
f.z = rand(-20, -9);
f.y = Math.random() < 0.6 ? rand(21, 30) : rand(-9, -4.5);
f.v = rand(16, 34);
f.x = any ? cx + rand(-60, 90) : cx + (f.dir > 0 ? -rand(55, 90) : rand(60, 110));
}
// Distant streams of traffic: just their head and tail lights, on lanes stacked up between the towers.
const AIR_N = 340;
const airPos = new Float32Array(AIR_N * 2 * 3), airCol = new Float32Array(AIR_N * 2 * 3), airSize = new Float32Array(AIR_N * 2);
const airGeo = new THREE.BufferGeometry();
airGeo.setAttribute("position", new THREE.BufferAttribute(airPos, 3).setUsage(THREE.DynamicDrawUsage));
airGeo.setAttribute("color", new THREE.BufferAttribute(airCol, 3));
airGeo.setAttribute("size", new THREE.BufferAttribute(airSize, 1));
const airPts = new THREE.Points(airGeo, pointsMat);
airPts.frustumCulled = false;
scene.add(airPts);
const AIR_LANES = [];
for (const z of [-15, -19, -42, -46, -62, -70, -96, -104, -132, -150, -190, -205, -255, -280, -320]) {
for (let k = 0; k < (Math.abs(z) > 60 ? 3 : 2); k++) {
const y = Math.abs(z) < 25 ? PA.choice([rand(-8, -4), rand(23, 34)]) : rand(-14, 40 + Math.abs(z) * 0.35);
AIR_LANES.push({ z, y, dir: Math.random() < 0.5 ? 1 : -1, v: rand(22, 55), span: 160 + Math.abs(z) * 1.1 });
}
}
const air = [];
for (let i = 0; i < AIR_N; i++) {
const ln = AIR_LANES[i % AIR_LANES.length];
air.push({ ln, x: rand(-ln.span, ln.span), y: ln.y + rand(-1.2, 1.2), z: ln.z + rand(-2, 2), v: ln.v * rand(0.85, 1.15) });
const s = 1.0 + Math.abs(ln.z) * 0.012;
airSize[i * 2] = s * 0.8; airSize[i * 2 + 1] = s * 0.9;
airCol.set([1.5, 1.35, 1.1], i * 6);
airCol.set(Math.random() < 0.8 ? [3.2, 0.2, 0.35] : [0.4, 2.2, 3.4], i * 6 + 3);
}
airGeo.attributes.size.needsUpdate = true;
function updateAir(dt, cx) {
for (let i = 0; i < AIR_N; i++) {
const a = air[i], ln = a.ln;
a.x += ln.dir * a.v * dt;
if (a.x < cx - ln.span) a.x += ln.span * 2; else if (a.x > cx + ln.span) a.x -= ln.span * 2;
airPos[i * 6] = a.x + ln.dir * 1.6; airPos[i * 6 + 1] = a.y; airPos[i * 6 + 2] = a.z;
airPos[i * 6 + 3] = a.x - ln.dir * 1.6; airPos[i * 6 + 4] = a.y + 0.15; airPos[i * 6 + 5] = a.z;
}
airGeo.attributes.position.needsUpdate = true;
}
// The skyway's own edges: dashed holo rails at the floor and the ceiling.
const railTex = canvasTex(512, 64, (g, w, h) => {
const gr = g.createLinearGradient(0, 0, 0, h);
gr.addColorStop(0, "rgba(255,255,255,.9)"); gr.addColorStop(0.06, "rgba(255,255,255,.08)"); gr.addColorStop(0.5, "rgba(255,255,255,.015)");
gr.addColorStop(0.94, "rgba(255,255,255,.08)"); gr.addColorStop(1, "rgba(255,255,255,.9)");
g.fillStyle = gr; g.fillRect(0, 0, w, h);
g.strokeStyle = "rgba(255,255,255,.16)"; g.lineWidth = 4; g.lineJoin = "round";
for (let x = 0; x < w; x += 128) { g.beginPath(); g.moveTo(x + 46, h * 0.28); g.lineTo(x + 72, h / 2); g.lineTo(x + 46, h * 0.72); g.stroke(); }
g.fillStyle = "#fff";
for (let x = 0; x < w; x += 64) { g.fillRect(x, 0, 38, 5); g.fillRect(x, h - 5, 38, 5); }
}, { repeat: true });
const rails = [];
for (const [y, c] of [[0, 0x29f0ff], [CEIL, 0xff2fa8]]) {
const t = railTex.clone(); t.needsUpdate = true; t.repeat.set(300 / 16, 1);
const m = new THREE.Mesh(new THREE.PlaneGeometry(300, ZD * 2 + 1), new THREE.MeshBasicMaterial({ map: t, color: hdr(c, 1.3), transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide }));
m.rotation.x = -Math.PI / 2; m.position.y = y;
m.frustumCulled = false;
scene.add(m);
rails.push(m);
}
// A veil of smog between the skyway and the city behind it, so the flight line always reads first.
const veil = new THREE.Mesh(new THREE.PlaneGeometry(900, 500), new THREE.MeshBasicMaterial({ color: 0x000000, transparent: true, opacity: 0.3, depthWrite: false, fog: false }));
veil.position.set(0, 60, -24);
veil.frustumCulled = false;
scene.add(veil);
// Rain, streaked by your speed.
const RAIN_N = 900;
const rainPos = new Float32Array(RAIN_N * 6), rain = [];
const rainGeo = new THREE.BufferGeometry();
rainGeo.setAttribute("position", new THREE.BufferAttribute(rainPos, 3).setUsage(THREE.DynamicDrawUsage));
const rainLines = new THREE.LineSegments(rainGeo, new THREE.LineBasicMaterial({ color: hdr(0x9fb8ff, 0.5), transparent: true, opacity: 0.45, blending: THREE.AdditiveBlending, depthWrite: false }));
rainLines.frustumCulled = false;
scene.add(rainLines);
for (let i = 0; i < RAIN_N; i++) rain.push({ x: rand(-50, 50), y: rand(-8, 30), z: rand(-28, 20), v: rand(30, 44) });
function updateRain(dt, cx, cy, spd) {
for (let i = 0; i < RAIN_N; i++) {
const r = rain[i];
r.y -= r.v * dt; r.x -= 4 * dt;
if (r.y < cy - 22) { r.y += 48; r.x = cx + rand(-50, 50); }
if (r.x < cx - 55) r.x += 110; else if (r.x > cx + 55) r.x -= 110;
const sx = (spd * 0.035 + 0.15), sy = r.v * 0.03;
rainPos[i * 6] = r.x; rainPos[i * 6 + 1] = r.y; rainPos[i * 6 + 2] = r.z;
rainPos[i * 6 + 3] = r.x + sx; rainPos[i * 6 + 4] = r.y + sy; rainPos[i * 6 + 5] = r.z;
}
rainGeo.attributes.position.needsUpdate = true;
}
/* ---------------- obstacle parts ---------------- */
const hazardTex = canvasTex(256, 64, (g, w, h) => {
g.fillStyle = "#14141a"; g.fillRect(0, 0, w, h);
for (let x = -h; x < w + h; x += 32) { g.fillStyle = "#f2b61a"; g.beginPath(); g.moveTo(x, h); g.lineTo(x + 16, h); g.lineTo(x + 16 + h, 0); g.lineTo(x + h, 0); g.fill(); }
speckle(g, w, h, 26);
g.fillStyle = "rgba(0,0,0,.35)"; g.fillRect(0, 0, w, 4); g.fillRect(0, h - 4, w, 4);
}, { repeat: true });
const crateTex = (base, mark) => canvasTex(256, 128, (g, w, h) => {
g.fillStyle = base; g.fillRect(0, 0, w, h);
speckle(g, w, h, 22);
g.fillStyle = "rgba(0,0,0,.22)"; for (let x = 6; x < w; x += 12) g.fillRect(x, 0, 3, h);
g.fillStyle = "rgba(255,255,255,.08)"; for (let x = 10; x < w; x += 12) g.fillRect(x, 0, 1, h);
g.fillStyle = "rgba(0,0,0,.45)"; g.fillRect(0, 0, w, 7); g.fillRect(0, h - 7, w, 7); g.fillRect(0, 0, 7, h); g.fillRect(w - 7, 0, 7, h);
g.font = "900 34px Manrope, system-ui, sans-serif"; g.textAlign = "center"; g.textBaseline = "middle";
g.fillStyle = "rgba(240,240,250,.78)"; g.fillText(mark, w / 2, h / 2 - 6);
g.font = "700 14px Manrope, system-ui, monospace"; g.fillText(String.fromCharCode(65 + randInt(0, 25)) + randInt(100, 999) + "-" + randInt(10, 99), w / 2, h / 2 + 24);
});
const OM = {
hazard: new THREE.MeshStandardMaterial({ map: hazardTex, metalness: 0.4, roughness: 0.5 }),
crates: [["#c8501a", "ORBIT"], ["#1a6a7a", "KRYO"], ["#e8e8ee", "MAGLEV"], ["#7a1a5a", "NOVA"], ["#2a3a6a", "PULSE"]]
.map(([c, t]) => new THREE.MeshStandardMaterial({ map: crateTex(c, t), metalness: 0.45, roughness: 0.45, envMapIntensity: 0.9 })),
cell: new THREE.MeshBasicMaterial({ color: hdr(0x5affc8, 2.6) }),
cellCore: new THREE.MeshBasicMaterial({ color: hdr(0xe8fff6, 5) }),
laser: new THREE.MeshBasicMaterial({ color: hdr(0xff3050, 7) }),
laserGlow: new THREE.MeshBasicMaterial({ color: hdr(0xff2050, 0.9), transparent: true, blending: THREE.AdditiveBlending, depthWrite: false }),
red: spriteMat(0xff2a3a, 2.6), blue: spriteMat(0x3a6bff, 2.8), amber: spriteMat(0xffa21e, 2.4), white: spriteMat(0xffffff, 2.2), green: spriteMat(0x5affc8, 2)
};
const boxGeo = new THREE.BoxGeometry(1, 1, 1);
const cylZ = new THREE.CylinderGeometry(1, 1, 1, 24).rotateX(Math.PI / 2);
const cylY = new THREE.CylinderGeometry(1, 1, 1, 28);
const torusZ = new THREE.TorusGeometry(1, 0.08, 8, 40);
const torusY = new THREE.TorusGeometry(1, 0.08, 8, 40).rotateX(Math.PI / 2);
const sprite = (m, sx, sy) => { const s = new THREE.Sprite(m); s.scale.set(sx, sy || sx, 1); return s; };
const mesh = (geo, mat) => new THREE.Mesh(geo, mat);
for (const g of [boxGeo, cylZ, cylY, torusZ, torusY]) SHARED.add(g);
function obGeo() {
return { fac: OB_FAC.map(() => new Geo()), neon: NEON_M.map(() => new Geo()), metal: new Geo(), steel: new Geo(), dark: new Geo(), hazard: new Geo(), holo: new Geo(), sign: new Geo(), warm: new Geo() };
}
function obGroup(G) {
const list = [];
G.fac.forEach((g, k) => list.push([g, OB_FAC[k]]));
G.neon.forEach((g, k) => list.push([g, NEON_M[k]]));
list.push([G.metal, M.metal], [G.steel, M.steel], [G.dark, M.dark], [G.hazard, OM.hazard], [G.holo, M.holo], [G.sign, M.sign], [G.warm, M.warm]);
return geoGroup(list);
}
// One half of a tower gap: a neon-district tower rising to the gap, or an arcology's hull hanging into it.
function towerPiece(G, grp, x0, x1, edge, hanging) {
const fk = hanging ? PA.choice([3, 4, 1]) : PA.choice([0, 2, 2, 1]), [U, V] = FAC_UV(fk), ci = randInt(0, NEON_N - 1);
const ou = Math.random(), ov = Math.random(), z0 = -ZD, z1 = ZD;
const ya = hanging ? edge + 0.55 : BOTTOM, yb = hanging ? 80 : edge - 0.55;
G.fac[fk].box(x0, x1, ya, yb, z0, z1, U, V, ou, ov);
// the cornice at the gap, ringed with light
const ca = hanging ? edge : edge - 0.55, cb = ca + 0.55;
G.metal.box(x0 - 0.3, x1 + 0.3, ca, cb, z0 - 0.3, z1 + 0.3);
neonRing(G, x0 - 0.3, x1 + 0.3, hanging ? ca + 0.06 : cb - 0.06, z0 - 0.3, z1 + 0.3, 0.07, ci);
neonEdges(G, x0, x1, hanging ? cb : edge - 14, hanging ? edge + 14 : ca, z1, 0.11, ci);
if (hanging) {
// ribbed hull, downlights shining into the gap
for (let x = x0 + 0.8; x < x1 - 0.4; x += 1.6) G.dark.box(x - 0.12, x + 0.12, cb, cb + 9, z1, z1 + 0.32);
for (let x = x0 + 0.7; x < x1; x += 1.4) G.warm.box(x - 0.25, x + 0.25, ca - 0.03, ca, z0 + 0.6, z1 - 0.6);
const n = Math.max(2, Math.round((x1 - x0) / 2));
for (let i = 0; i < n; i++) { const s = sprite(OM.white, 1.6, 0.8); s.position.set(lerp(x0 + 0.6, x1 - 0.6, n > 1 ? i / (n - 1) : 0.5), ca - 0.15, z1 + 0.35); grp.add(s); }
} else {
if (Math.random() < 0.55) {
const w = x1 - x0, aw = w * 0.82, ah = Math.min(aw / 2, 3.2), idx = randInt(0, WIDE_N - 1);
G.holo.add(planeGeo, mat4((x0 + x1) / 2, ca - 0.9 - ah / 2, z1 + 0.06, 0, 0, 0, aw, ah, 1), wideUV(idx));
} else {
const s = 0.8, idx = randInt(0, SIGNS - 1), sx = Math.random() < 0.5 ? x0 + 0.6 : x1 - 0.6;
G.sign.add(planeGeo, mat4(sx, ca - 1.2 - 2.1 * s, z1 + 0.9, Math.PI / 2, 0, 0, 1.4 * s, 4.2 * s, 1), signUV(idx));
G.metal.box(sx - 0.05, sx + 0.05, ca - 1.2 - 4.2 * s, ca - 1.2, z1, z1 + 1.6);
}
// a hot window band and rooftop warning lights
G.warm.box(x0 + 0.3, x1 - 0.3, ca - 6.2, ca - 5.9, z1, z1 + 0.04);
for (const x of [x0 - 0.2, x1 + 0.2]) { const s = sprite(OM.red, 0.9); s.position.set(x, cb + 0.1, z1 + 0.2); s.userData.blink = Math.random(); grp.add(s); }
}
}
function cellMesh() {
const g = new THREE.Group();
const hex = mesh(new THREE.CylinderGeometry(0.42, 0.42, 0.95, 6), OM.cell);
const core = mesh(new THREE.CylinderGeometry(0.16, 0.16, 1.15, 6), OM.cellCore);
const ring = mesh(torusY, OM.cell); ring.scale.setScalar(0.75);
g.add(hex, core, ring, sprite(OM.green, 2.6));
g.userData.ring = ring;
return g;
}
/* ---------------- collision shapes ---------------- */
// Shapes: r rect, c circle, s segment (capsule), o oriented rect. dist() is the signed distance from a point.
function dist(s, px, py) {
if (s.t === "r") {
const dx = Math.max(s.x0 - px, 0, px - s.x1), dy = Math.max(s.y0 - py, 0, py - s.y1);
if (dx === 0 && dy === 0) return -Math.min(px - s.x0, s.x1 - px, py - s.y0, s.y1 - py);
return Math.hypot(dx, dy);
}
if (s.t === "c") return Math.hypot(px - s.x, py - s.y) - s.r;
if (s.t === "s") {
const vx = s.bx - s.ax, vy = s.by - s.ay, l2 = vx * vx + vy * vy || 1;
const k = clamp(((px - s.ax) * vx + (py - s.ay) * vy) / l2, 0, 1);
return Math.hypot(px - s.ax - vx * k, py - s.ay - vy * k) - s.r;
}
const c = Math.cos(s.a), sn = Math.sin(s.a), lx = (px - s.x) * c + (py - s.y) * sn, ly = -(px - s.x) * sn + (py - s.y) * c;
const dx = Math.abs(lx) - s.hx, dy = Math.abs(ly) - s.hy;
return dx > 0 || dy > 0 ? Math.hypot(Math.max(dx, 0), Math.max(dy, 0)) : Math.max(dx, dy);
}
const R = (x0, x1, y0, y1) => ({ t: "r", x0, x1, y0, y1, on: true });
/* ---------------- obstacles ---------------- */
const obs = [];
let nextX = 0, gapY = 9, diff = 0, zoneS = 0, lastKind = "", spawned = 0;
const gapH = () => lerp(7.6, 5.1, diff) - (zoneS >= ZONES.length ? 0.25 : 0);
function nextGap(h) {
const lo = h / 2 + 1.4, hi = CEIL - h / 2 - 1.4, reach = lerp(4.5, 7.5, diff);
gapY = clamp(gapY + rand(-reach, reach), lo, hi);
return gapY;
}
function addCell(x, y) {
const m = cellMesh();
m.position.set(x, y, 0);
scene.add(m);
cells.push({ x, y, m, got: false, ph: rand(TAU) });
}
const cells = [];
function newOb(kind, x0, x1, grp) {
const o = { kind, x0, x1, grp, shapes: [], passed: false, clear: Infinity, t: 0, upd: null, lift: null, warn: null, act: true };
scene.add(grp);
obs.push(o);
return o;
}
function mkTower(x) {
const w = rand(4.6, 7), h = gapH(), c = nextGap(h), lo = c - h / 2, hi = c + h / 2;
const G = obGeo(), grp = new THREE.Group();
towerPiece(G, grp, x, x + w, lo, false);
towerPiece(G, grp, x, x + w, hi, true);
grp.add(obGroup(G));
const o = newOb("tower", x - 0.3, x + w + 0.3, grp);
o.shapes.push(R(x - 0.3, x + w + 0.3, BOTTOM, lo), R(x - 0.3, x + w + 0.3, hi, 90));
o.safe = () => c;
if (Math.random() < 0.5) addCell(x + w / 2, c);
return x + w + 0.3;
}
// Hardlight billboards: two stacked holo panels that take turns going solid. Fly through the dim one.
const holoPanelMat = (rect, col) => new THREE.ShaderMaterial({
transparent: true, depthWrite: false, side: THREE.DoubleSide,
uniforms: { map: { value: holoTex }, rect: { value: rect }, solid: { value: 0 }, warn: { value: 0 }, time: { value: 0 }, edge: { value: col } },
vertexShader: "varying vec2 vU; void main(){ vU = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
fragmentShader: [
"uniform sampler2D map; uniform vec4 rect; uniform float solid, warn, time; uniform vec3 edge; varying vec2 vU;",
"void main(){",
" vec3 ad = texture2D(map, rect.xy + vU * rect.zw).rgb;",
" float scan = 0.75 + 0.25 * sin(vU.y * 260.0 - time * 8.0);",
" vec2 h = abs(fract(vU * vec2(9.0, 14.0)) - 0.5);",
" float grid = smoothstep(0.43, 0.5, max(h.x, h.y));",
" vec2 e = min(vU, 1.0 - vU); float border = 1.0 - smoothstep(0.0, 0.035, min(e.x, e.y * 1.4));",
" float flick = warn * step(0.5, fract(time * 9.0));",
" float s = clamp(solid + flick * 0.7, 0.0, 1.0);",
" vec3 c = ad * mix(0.5, 1.7, s) * scan + edge * grid * mix(0.15, 1.1, s) + edge * border * mix(1.2, 4.0, s);",
" float a = mix(0.22, 0.94, s) + border * 0.5;",
" gl_FragColor = vec4(c, clamp(a, 0.0, 1.0));",
"}"].join("\n")
});
function adRect(w, h) {
const wide = w / h > 1, A = wide ? 2 : 0.5, P = w / h, i = wide ? randInt(0, WIDE_N - 1) : randInt(0, HOLO_N - 1);
let u0 = wide ? (256 * HOLO_N + (i % 2) * 512) / AD_W : i * 256 / AD_W, du = (wide ? 512 : 256) / AD_W, v0 = wide ? (i >> 1 ? 0 : 0.5) : 0, dv = wide ? 0.5 : 1;
if (P > A) { const k = A / P; v0 += dv * (1 - k) / 2; dv *= k; } else { const k = P / A; u0 += du * (1 - k) / 2; du *= k; }
return new THREE.Vector4(u0, v0, du, dv);
}
function mkBillboard(x) {
const w = rand(4.2, 5.6), split = rand(6.5, 11.5), per = lerp(3.4, 2.6, diff), ph = rand(TAU);
const G = obGeo(), grp = new THREE.Group();
for (const z of [-ZD - 0.2, ZD + 0.2]) for (const px of [x - 0.25, x + w + 0.25]) G.metal.box(px - 0.22, px + 0.22, BOTTOM, 80, z - 0.22, z + 0.22);
for (const z of [-ZD - 0.2, ZD + 0.2]) for (const y of [-0.5, split, CEIL + 0.5]) G.steel.box(x - 0.25, x + w + 0.25, y - 0.14, y + 0.14, z - 0.16, z + 0.16);
for (const z of [-ZD - 0.45, ZD + 0.45]) G.neon[1].box(x - 0.25, x + w + 0.25, split - 0.04, split + 0.04, z - 0.05, z + 0.05);
grp.add(obGroup(G));
const panels = [];
for (const [y0, y1, col] of [[-1.5, split, hdr(0x29f0ff, 1)], [split, CEIL + 1.5, hdr(0xff2fa8, 1)]]) {
const mat = holoPanelMat(adRect(w, y1 - y0), col);
const m = new THREE.Mesh(planeGeo, mat);
m.userData.ownMat = true;
m.scale.set(w, y1 - y0, 1); m.position.set(x + w / 2, (y0 + y1) / 2, 0);
grp.add(m);
panels.push({ mat, y0, y1 });
}
const o = newOb("billboard", x, x + w, grp);
const sb = R(x, x + w, BOTTOM, split), st = R(x, x + w, split, 90);
o.shapes.push(sb, st);
// which panel is solid at time t: the lower one in the first half of each period
const lowerSolid = (t) => ((t + ph) % per) / per < 0.5;
const warnAt = (t) => { const f = ((t + ph) % per) / per; return (f > 0.5 - 0.14 && f < 0.5) || f > 1 - 0.14; };
o.upd = (dt, T, px, V, py) => {
const low = lowerSolid(T), w8 = warnAt(T);
// a panel that hardens while you're already inside it lets you finish the pass
const over = px + HALF_L > x && px - HALF_L < x + w;
if (!over) sb.grace = st.grace = false;
else {
if (low && !sb.on && py < split + 0.6) sb.grace = true;
if (!low && !st.on && py > split - 0.6) st.grace = true;
}
sb.on = low && !sb.grace; st.on = !low && !st.grace;
panels[0].mat.uniforms.solid.value = lerp(panels[0].mat.uniforms.solid.value, low ? 1 : 0, 1 - Math.exp(-dt * 18));
panels[1].mat.uniforms.solid.value = lerp(panels[1].mat.uniforms.solid.value, low ? 0 : 1, 1 - Math.exp(-dt * 18));
for (let i = 0; i < 2; i++) { const u = panels[i].mat.uniforms; u.time.value = T; u.warn.value = w8 && (i === 0) !== low ? 1 : 0; }
};
o.safe = (tArrive) => lowerSolid(tArrive) ? (split + CEIL) / 2 : split / 2;
if (Math.random() < 0.4) addCell(x + w + 3, rand(4, 14));
return x + w;
}
// Freight lifts: cargo pods sliding up and down open shafts, two side by side and out of step.
function mkLift(x) {
const n = diff > 0.25 && Math.random() < 0.7 ? 2 : 1, pw = 3.4, gap = 3.2, per = lerp(3.8, 3.0, diff), ph = rand(TAU);
const G = obGeo(), grp = new THREE.Group(), pods = [];
for (let k = 0; k < n; k++) {
const x0 = x + k * (pw + gap), x1 = x0 + pw;
for (const z of [-ZD - 0.35, ZD + 0.35]) {
for (const px of [x0 - 0.3, x1 + 0.3]) G.metal.box(px - 0.2, px + 0.2, BOTTOM, 80, z - 0.2, z + 0.2);
for (let y = -12; y < 40; y += 5) {
G.steel.add(boxGeo, mat4((x0 + x1) / 2, y + 2.5, z, 0, 0, Math.atan2(5, pw + 0.6), 0.09, Math.hypot(5, pw + 0.6), 0.09));
G.steel.box(x0 - 0.3, x1 + 0.3, y - 0.07, y + 0.07, z - 0.08, z + 0.08);
}
if (z > 0) for (let y = -2; y < 22; y += 3) G.neon[2].box(x0 - 0.5, x1 + 0.5, y, y + 0.08, z + 0.2, z + 0.26);
}
const pod = new THREE.Group();
const ph2 = 4.4;
const shell = mesh(boxGeo, PA.choice(OM.crates)); shell.scale.set(pw, ph2, ZD * 2 - 0.3);
const bandT = mesh(boxGeo, OM.hazard); bandT.scale.set(pw + 0.12, 0.4, ZD * 2 - 0.1); bandT.position.y = ph2 / 2 - 0.2;
const bandB = mesh(boxGeo, OM.hazard); bandB.scale.set(pw + 0.12, 0.4, ZD * 2 - 0.1); bandB.position.y = -ph2 / 2 + 0.2;
const cable = mesh(boxGeo, M.dark); cable.scale.set(0.16, 60, 0.16); cable.position.y = ph2 / 2 + 30;
pod.add(shell, bandT, bandB, cable);
for (const sx of [-1, 1]) for (const sy of [-1, 1]) { const s = sprite(OM.amber, 1.1); s.position.set(sx * (pw / 2 + 0.05), sy * (ph2 / 2 - 0.2), ZD); s.userData.blink = (sx + sy) * 0.25 + 0.5; pod.add(s); }
pod.position.set((x0 + x1) / 2, 9, 0);
grp.add(pod);
pods.push({ pod, x0, x1, ph: ph + k * Math.PI, h: ph2, shape: R(x0, x1, 0, 0) });
}
grp.add(obGroup(G));
const o = newOb("lift", x - 0.5, x + n * pw + (n - 1) * gap + 0.5, grp);
const yAt = (p, t) => 9 + 7.4 * Math.sin(t * TAU / per + p.ph);
o.upd = (dt, T) => {
for (const p of pods) {
const y = yAt(p, T);
p.pod.position.y = y;
p.shape.y0 = y - p.h / 2; p.shape.y1 = y + p.h / 2;
}
};
for (const p of pods) o.shapes.push(p.shape);
o.safe = (t) => { const y = yAt(pods[0], t); return y > 9 ? clamp(y - 7.5, 2, 16) : clamp(y + 7.5, 2, 16); };
return o.x1;
}
// Security lasers: a turnstile of red beams sweeping round a hub hung off a mast behind the skyway.
function mkLaser(x) {
const arms = zoneS >= 3 && diff > 0.35 && Math.random() < 0.5 ? 4 : 2, yc = rand(7.5, 10.5), len = 7.3;
const w = (Math.random() < 0.5 ? -1 : 1) * rand(0.85, 1.25) * (arms === 4 ? 0.8 : 1);
const G = obGeo(), grp = new THREE.Group();
G.dark.box(x - 0.35, x + 0.35, BOTTOM, 80, -ZD - 2.6, -ZD - 1.9);
G.steel.box(x - 0.2, x + 0.2, yc - 0.2, yc + 0.2, -ZD - 2.0, -0.5);
for (let y = -4; y < 26; y += 4) G.neon[5].box(x - 0.38, x + 0.38, y, y + 0.12, -ZD - 1.9, -ZD - 1.86);
grp.add(obGroup(G));
const hub = new THREE.Group();
hub.position.set(x, yc, 0);
const body = mesh(cylZ, M.metal); body.scale.set(0.8, 0.8, 1.3);
const ring = mesh(torusZ, NEON_M[0]); ring.scale.setScalar(0.92); ring.position.z = 0.66;
const spin = new THREE.Group();
for (let i = 0; i < arms / 2; i++) {
const b = mesh(new THREE.CylinderGeometry(0.07, 0.07, len * 2, 8), OM.laser); b.rotation.z = i * Math.PI / 2;
const gl = mesh(new THREE.CylinderGeometry(0.32, 0.32, len * 2, 10, 1, true), OM.laserGlow); gl.rotation.z = i * Math.PI / 2;
spin.add(b, gl);
for (const s of [-1, 1]) {
const cap = mesh(new THREE.SphereGeometry(0.28, 12, 8), M.steel);
const a = i * Math.PI / 2 + Math.PI / 2;
cap.position.set(Math.cos(a) * len * s, Math.sin(a) * len * s, 0);
const sp = sprite(OM.red, 1.4); sp.position.copy(cap.position);
spin.add(cap, sp);
}
}
hub.add(body, ring, spin, sprite(OM.red, 3));
grp.add(hub);
const o = newOb("laser", x - len, x + len, grp);
const core = { t: "c", x, y: yc, r: 0.85, on: true };
const beams = [];
for (let i = 0; i < arms / 2; i++) beams.push({ t: "s", ax: 0, ay: 0, bx: 0, by: 0, r: 0.14, on: true, a0: i * Math.PI / 2 + Math.PI / 2 });
o.shapes.push(core, ...beams);
const ph = rand(TAU);
o.upd = (dt, T) => {
const a = ph + T * w;
spin.rotation.z = a;
for (const b of beams) {
const c = Math.cos(a + b.a0) * len, s = Math.sin(a + b.a0) * len;
b.ax = x - c; b.ay = yc - s; b.bx = x + c; b.by = yc + s;
}
};
// Crossing at x the beam is in the way when it's near vertical; pick the side of the hub away from it.
o.safe = (t) => { const a = ph + t * w + Math.PI / 2; return Math.abs(Math.cos(a)) < 0.45 ? (Math.random() < 0.5 ? 2.5 : 15.5) : yc + (yc > 9 ? -4 : 4); };
return x + len * 0.4;
}
// Crane drones: a heavy-lift quadcopter above the skyway, its container swinging down through it.
function mkCrane(x) {
const len = rand(8.5, 11.5), A = rand(0.32, 0.55) * lerp(0.8, 1.2, diff), om = rand(1.15, 1.6), ph = rand(TAU);
const dy = CEIL + 1.8, cw = 3.6, chh = 0.95;
const grp = new THREE.Group();
const drone = new THREE.Group();
drone.position.set(x, dy, 0);
const hull = mesh(boxGeo, M.metal); hull.scale.set(2.6, 0.55, 2.6);
const deck = mesh(boxGeo, OM.hazard); deck.scale.set(2.7, 0.18, 2.7); deck.position.y = -0.35;
drone.add(hull, deck);
const rotors = [];
for (const sx of [-1, 1]) for (const sz of [-1, 1]) {
const arm = mesh(boxGeo, M.steel); arm.scale.set(1.6, 0.14, 0.14); arm.position.set(sx * 1.6, 0.1, sz * 1.6); arm.rotation.y = sx * sz * Math.PI / 4;
const ring = mesh(torusY, NEON_M[1]); ring.scale.setScalar(0.85); ring.position.set(sx * 2.2, 0.15, sz * 2.2);
const blade = mesh(boxGeo, M.dark); blade.scale.set(1.6, 0.04, 0.14); blade.position.copy(ring.position);
drone.add(arm, ring, blade);
rotors.push(blade);
}
const nav = [sprite(OM.red, 1.2), sprite(OM.green, 1.2)];
nav[0].position.set(-1.4, 0, 1.4); nav[1].position.set(1.4, 0, 1.4);
drone.add(...nav);
const spot = mesh(beamGeo, beamMat); spot.scale.set(-len, 1.1, 1.1); spot.rotation.z = Math.PI / 2; spot.position.y = -0.4;
drone.add(spot);
grp.add(drone);
const pend = new THREE.Group();
pend.position.set(x, dy - 0.4, 0);
const cable = mesh(boxGeo, M.dark); cable.scale.set(0.08, len, 0.08); cable.position.y = -len / 2;
const box = mesh(boxGeo, PA.choice(OM.crates)); box.scale.set(cw, chh * 2, 2.4); box.position.y = -len - chh;
const hook = mesh(boxGeo, OM.hazard); hook.scale.set(cw + 0.1, 0.22, 2.5); hook.position.y = -len - 0.1;
pend.add(cable, box, hook);
for (const sx of [-1, 1]) { const s = sprite(OM.amber, 1); s.position.set(sx * cw / 2, -len - chh, 1.25); s.userData.blink = sx * 0.25 + 0.5; pend.add(s); }
grp.add(pend);
let stub = null;
if (Math.random() < 0.5) {
const G = obGeo(), top = rand(1.2, 3.2);
towerPiece(G, grp, x - 2.4, x + 2.4, top, false);
grp.add(obGroup(G));
stub = R(x - 2.7, x + 2.7, BOTTOM, top);
}
const reach = Math.sin(A) * (len + chh) + cw / 2 + 0.4;
const o = newOb("crane", x - reach, x + reach, grp);
const crate = { t: "o", x: 0, y: 0, hx: cw / 2, hy: chh, a: 0, on: true }, rope = { t: "s", ax: x, ay: dy - 0.4, bx: 0, by: 0, r: 0.07, on: true };
o.shapes.push(crate, rope);
if (stub) o.shapes.push(stub);
const angAt = (t) => A * Math.sin(t * om + ph);
o.upd = (dt, T) => {
const a = angAt(T);
pend.rotation.z = a;
const s = Math.sin(a), c = Math.cos(a);
crate.x = x + s * (len + chh); crate.y = dy - 0.4 - c * (len + chh); crate.a = a;
rope.bx = x + s * len; rope.by = dy - 0.4 - c * len;
for (const r of rotors) r.rotation.y += dt * 40;
drone.position.y = dy + Math.sin(T * 1.7 + ph) * 0.12;
};
const cy = dy - 0.4 - (len + chh);
o.safe = () => cy > 9 ? clamp(cy - 4.5, 2.5, 15) : clamp(cy + 4.5, 2.5, 15);
if (Math.random() < 0.45) addCell(x, cy > 9 ? cy - 4 : cy + 4);
return x + reach;
}
// Exhaust stacks: a chimney below blasts hot air up into a hull hanging above. Cut the engine to get under.
function mkVent(x) {
const r = 1.75, top = rand(1.6, 4.2), edge = rand(11.4, 13.6), xc = x + 3.5;
const G = obGeo(), grp = new THREE.Group();
towerPiece(G, grp, x, x + 7, edge, true);
grp.add(obGroup(G));
const stack = mesh(cylY, M.metal); stack.scale.set(r, top - BOTTOM, r); stack.position.set(xc, (top + BOTTOM) / 2, 0);
const lip = mesh(cylY, OM.hazard); lip.scale.set(r + 0.14, 0.5, r + 0.14); lip.position.set(xc, top - 0.25, 0);
const mouth = mesh(torusY, NEON_M[2]); mouth.scale.setScalar(r + 0.05); mouth.position.set(xc, top + 0.02, 0);
const glow = mesh(new THREE.CircleGeometry(r * 0.9, 24).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ color: hdr(0xff7a2a, 3) })); glow.position.set(xc, top - 0.1, 0);
grp.add(stack, lip, mouth, glow);
for (let y = top - 3; y > -10; y -= 4) { const b = mesh(cylY, M.steel); b.scale.set(r + 0.06, 0.18, r + 0.06); b.position.set(xc, y, 0); grp.add(b); }
const ch = edge - top;
const colMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide,
uniforms: { time: { value: 0 } },
vertexShader: "varying vec2 vU; varying vec3 vN, vV; void main(){ vU = uv; vec4 mv = modelViewMatrix * vec4(position, 1.0); vN = normalize(normalMatrix * normal); vV = normalize(-mv.xyz); gl_Position = projectionMatrix * mv; }",
fragmentShader: [
"uniform float time; varying vec2 vU; varying vec3 vN, vV;",
"float hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }",
"float noise(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f); return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y); }",
"void main(){",
" float f = pow(abs(dot(vN, vV)), 1.2);",
" float n = noise(vec2(vU.x * 10.0, vU.y * 5.0 - time * 3.2)) * 0.6 + noise(vec2(vU.x * 22.0, vU.y * 11.0 - time * 5.0)) * 0.4;",
" float k = (1.0 - vU.y * 0.75) * f * (0.35 + n * 0.9);",
" vec3 c = mix(vec3(1.4, 0.38, 0.08), vec3(0.5, 0.36, 0.42), vU.y) * k;",
" gl_FragColor = vec4(c * 0.65, 1.0);",
"}"].join("\n")
});
const col = new THREE.Mesh(new THREE.CylinderGeometry(r * 1.05, r * 0.95, ch, 24, 1, true), colMat);
col.userData.ownMat = true; glow.userData.ownMat = true;
col.position.set(xc, top + ch / 2, 0);
grp.add(col);
const o = newOb("vent", x - 0.3, x + 7.3, grp);
o.shapes.push(R(xc - r - 0.14, xc + r + 0.14, BOTTOM, top), R(x - 0.3, x + 7.3, edge, 90));
o.lift = (px, py) => Math.abs(px - xc) < r + 0.4 && py > top && py < edge ? 36 * (1 - 0.4 * (py - top) / ch) : 0;
let puff = 0;
o.upd = (dt, T) => {
colMat.uniforms.time.value = T;
puff += dt * 40;
while (puff > 1) {
puff--;
parts.length < PMAX - 10 && parts.push({ x: xc + rand(-r, r) * 0.8, y: top + 0.2, z: rand(-r, r) * 0.8, vx: rand(-0.6, 0.6), vy: rand(9, 15), vz: rand(-0.6, 0.6), g: 0, drag: 0.6,
life: rand(0.5, 0.9) * ch / 10, max: 0.9 * ch / 10, size: rand(0.5, 1.1), c: Math.random() < 0.6 ? C3(0xff7a2a, 1.6) : C3(0xffd0a0, 0.9) });
}
};
o.safe = () => clamp(top + 2.2, 2, 8);
if (Math.random() < 0.5) addCell(xc, top + 2.4);
return x + 7.3;
}
// Traffic: either a slow convoy you overtake, or a wave coming head-on with its lights blazing.
const TRAFFIC_KINDS = ["coupe", "sedan", "pod", "van"];
function trafficLanes(n) {
const h = gapH() * 1.05, c = nextGap(h), ys = [];
const free = (y) => (y < c - h / 2 - 0.6 || y > c + h / 2 + 0.6) && ys.every((q) => Math.abs(q - y) > 2.6);
for (let k = 0; k < 40 && ys.length < n; k++) { const y = rand(1.2, CEIL - 1.2); if (free(y)) ys.push(y); }
return { ys, c };
}
function mkTraffic(x) {
const head = Math.random() < (zoneS === 0 ? 0.35 : 0.55);
if (head) x += 12; // head-on cars sweep back past the start of their stretch: keep it clear
const { ys, c } = trafficLanes(diff > 0.4 ? 3 : 2);
const grp = new THREE.Group(), list = [];
ys.forEach((y, i) => {
const kind = PA.choice(TRAFFIC_KINDS), m = trafficCar(kind, head ? -1 : 1, head);
m.visible = false;
grp.add(m);
list.push({ m, y, off: i * rand(2.5, 5) + rand(0, 2), x: 0, z: 0, shape: R(0, 0, y - m.userData.hh, y + m.userData.hh), hl: m.userData.hl });
});
const span = head ? 22 : 26;
const o = newOb("traffic", x - (head ? 10 : 0), x + span, grp);
o.shapes = list.map((q) => q.shape);
o.act = false;
o.warn = head ? list : null;
o.head = head;
o.trig = x - (head ? 31 : 30);
o.upd = (dt, T, px, V) => {
if (!o.act) {
if (px < o.trig) return;
o.act = true;
for (const q of list) { q.x = head ? x + 9 + q.off : x + q.off; q.m.visible = true; }
}
let x0 = Infinity, x1 = -Infinity;
for (const q of list) {
q.x += (head ? -11 : V * 0.42) * dt;
// once past you, head-on traffic peels off behind the towers
if (head && q.x < px - 2.5) q.z = Math.max(-9, q.z - 16 * dt);
q.m.position.set(q.x, q.y + Math.sin(T * 2 + q.off) * 0.08, q.z);
q.m.rotation.y = head ? Math.min(0, q.z) * 0.06 : 0;
q.shape.x0 = q.x - q.hl; q.shape.x1 = q.x + q.hl;
q.shape.on = !o.dead && q.z > -1.2;
x0 = Math.min(x0, q.x - q.hl); x1 = Math.max(x1, q.x + q.hl);
}
o.x0 = x0; o.x1 = x1;
};
o.safe = () => c;
return x + span;
}
// Patrol drones: they match your height as they close in, then lock on with a beep. Make them commit, then move.
function mkPatrol(x) {
const n = diff > 0.45 && Math.random() < 0.6 ? 2 : 1, grp = new THREE.Group(), list = [];
for (let i = 0; i < n; i++) {
const g = new THREE.Group();
const disc = mesh(cylY, M.metal); disc.scale.set(1.0, 0.36, 1.0);
const dome = mesh(new THREE.SphereGeometry(0.55, 18, 10, 0, TAU, 0, Math.PI / 2), VM.glass); dome.position.y = 0.16;
const ring = mesh(torusY, NEON_M[1]); ring.scale.setScalar(1.08); ring.position.y = -0.12;
const r = sprite(OM.red, 1.5), b = sprite(OM.blue, 1.5);
r.position.set(-0.7, 0.28, 0.5); b.position.set(0.7, 0.28, 0.5);
const eye = sprite(OM.white, 1.6); eye.position.set(-1.0, -0.05, 0.3);
const lamp = mesh(beamGeo, beamMat); lamp.scale.set(-12, 0.7, 0.7); lamp.position.set(-0.9, -0.05, 0);
g.add(disc, dome, ring, r, b, eye, lamp);
g.visible = false;
grp.add(g);
list.push({ g, r, b, eye, x: x + i * 4.5, y: i ? 4 : 14, lock: false, shape: { t: "c", x: 0, y: 0, r: 0.95, on: true } });
}
const o = newOb("patrol", x - 1.2, x + 20, grp);
o.shapes = list.map((q) => q.shape);
o.act = false;
o.trig = x - 30;
o.upd = (dt, T, px, V, py) => {
if (!o.act) {
if (px < o.trig) return;
o.act = true;
list.forEach((q, i) => { q.g.visible = true; q.y = n === 2 ? (i ? clamp(py - 4, 1.5, 9) : clamp(py + 4, 9, 16.5)) : clamp(py + rand(-3, 3), 1.5, 16.5); });
}
let x0 = Infinity, x1 = -Infinity;
for (const q of list) {
q.x += V * 0.3 * dt;
const dx = q.x - px;
if (!q.lock && dx < 11) { q.lock = true; PA.sfx("blip", { pitch: 1.6 }); }
if (!q.lock) {
const want = n === 2 ? q.y : clamp(py, 1.5, 16.5);
q.y += clamp(want - q.y, -4.5 * dt, 4.5 * dt);
}
const s = (T * 6) % 2 < 1;
q.r.material = s ? OM.red : OM.blue; q.b.material = s ? OM.blue : OM.red;
q.eye.material = q.lock ? OM.red : OM.white;
q.g.position.set(q.x, q.y + Math.sin(T * 3 + q.x) * 0.1, 0);
q.g.rotation.z = q.lock ? 0 : clamp((py - q.y) * 0.05, -0.25, 0.25);
q.shape.x = q.x; q.shape.y = q.y; q.shape.on = !o.dead;
x0 = Math.min(x0, q.x - 1.1); x1 = Math.max(x1, q.x + 1.1);
}
o.x0 = x0; o.x1 = x1;
};
o.safe = (t, py) => { const q = list[0]; return q.lock ? (q.y > 9 ? q.y - 4 : q.y + 4) : py; };
return x + 20;
}
const MAKERS = { tower: mkTower, billboard: mkBillboard, lift: mkLift, laser: mkLaser, crane: mkCrane, vent: mkVent, traffic: mkTraffic, patrol: mkPatrol };
const KIND_TIPS = {
billboard: "Hardlight billboards: fly through the dim half",
lift: "Freight lifts: slip past while the pod is away",
laser: "Security lasers: thread the gap as the beam turns",
crane: "Crane drones: mind the swinging container",
vent: "Exhaust stacks blow you upward: cut the engine",
traffic: "Traffic in the skyway: find the open lane",
patrol: "Patrol drones lock on to your height: bait, then dodge"
};
const seenKinds = new Set();
let forceKind = null;
function pickKind() {
if (forceKind) return forceKind;
const k = zoneDef(zoneS).kinds;
let sum = 0;
for (const n in k) if (n !== lastKind || n === "tower") sum += k[n];
let r = Math.random() * sum;
for (const n in k) { if (n === lastKind && n !== "tower") continue; r -= k[n]; if (r <= 0) return n; }
return "tower";
}
function spawnUntil(limit) {
while (nextX < limit) {
zoneS = Math.floor(nextX / ZONE_LEN);
diff = clamp(nextX / 4200, 0, 1);
const kind = spawned++ < 3 && !forceKind ? "tower" : pickKind();
lastKind = kind;
const end = MAKERS[kind](nextX);
const o = obs[obs.length - 1];
if (KIND_TIPS[kind] && !seenKinds.has(kind)) { seenKinds.add(kind); o.tip = KIND_TIPS[kind]; }
o.blink = [];
o.grp.traverse((c) => { if (c.isSprite && c.userData.blink != null) o.blink.push(c); });
nextX = end + lerp(15, 10.5, diff) + rand(0, 3) + (kind === "traffic" || kind === "patrol" ? 4 : 0);
}
}
function clearObs() {
spawned = 0;
for (const o of obs) disposeGroup(o.grp);
obs.length = 0;
for (const c of cells) disposeGroup(c.m);
cells.length = 0;
}
/* ---------------- input ---------------- */
const touch = { boost: false, cut: false };
function holdButton(el, key) {
const up = () => { touch[key] = false; el.classList.remove("on"); };
el.addEventListener("pointerdown", (e) => {
e.preventDefault(); e.stopPropagation();
try { el.setPointerCapture(e.pointerId); } catch (x) { /* older browsers */ }
touch[key] = true; el.classList.add("on");
});
el.addEventListener("pointerup", up);
el.addEventListener("pointercancel", up);
el.addEventListener("lostpointercapture", up);
}
holdButton($("sk-boost"), "boost");
holdButton($("sk-cut"), "cut");
if (PA.isTouch) document.body.classList.add("sk-touch");
addEventListener("touchstart", () => document.body.classList.add("sk-touch"), { once: true, passive: true });
// Mouse: hold left to boost, right to cut. Touch anywhere else: right half boosts, left half cuts.
const ptrs = new Map();
mount.addEventListener("pointerdown", (e) => {
if (PA.state !== "play") return;
ptrs.set(e.pointerId, e.button === 2 || (e.pointerType === "touch" && e.clientX < innerWidth * 0.45) ? "cut" : "boost");
});
const ptrUp = (e) => ptrs.delete(e.pointerId);
addEventListener("pointerup", ptrUp);
addEventListener("pointercancel", ptrUp);
mount.addEventListener("contextmenu", (e) => e.preventDefault());
const held = (role) => { for (const v of ptrs.values()) if (v === role) return true; return false; };
const wantBoost = () => PA.down("Space", " ", "KeyW", "ArrowUp") || touch.boost || held("boost");
const wantCut = () => PA.down("KeyS", "ArrowDown", "ShiftLeft", "ShiftRight") || touch.cut || held("cut");
const wantThrottle = () => (PA.down("KeyD", "ArrowRight") ? 1 : 0) - (PA.down("KeyA", "ArrowLeft") ? 1 : 0);
/* ---------------- state ---------------- */
let px = 0, py = 9, vy = 0, V = BASE_V, thr = 0, pitch = 0, roll = 0, simT = 0;
let score = 0, cleared = 0, cellsN = 0, closeCalls = 0, combo = 0, comboT = 0, shield = false, shieldK = 0, invT = 0;
let alive = true, started = false, overT = 0, overSent = false, demo = true, god = false, autoTest = false;
let boosting = false, cutting = false, wasBoost = false, wasCut = false, engine = 1, boostK = 0, hurt = 0, liftK = 0;
let zone = 0, tipT = 0, bannerT = 0, spin = 0;
let camX = 0, camY = 9, camD = 32;
const auto = { boost: false, cut: false };
const trails = [[], []];
const texts = new PA.Texts();
const hud = $("sk-hud"), banner = $("sk-banner"), hint = $("sk-hint");
function showBanner(kicker, title, sub) {
banner.querySelector("small").textContent = kicker;
banner.querySelector("b").textContent = title;
banner.querySelector("span").textContent = sub || "";
banner.classList.add("on");
bannerT = 3.2;
}
function showHint(text, t) { hint.textContent = text; hint.classList.add("on"); tipT = t || 3.4; }
function viewHalfW() { return Math.tan(THREE.MathUtils.degToRad(camera.fov) / 2) * camD * camera.aspect; }
function reset(demoMode) {
clearObs();
resetChunks();
seenKinds.clear();
demo = !!demoMode;
px = demo ? rand(0, 400) : 0; py = 9; vy = 0; V = demo ? BASE_V : 0; thr = 0; pitch = 0; roll = 0;
score = 0; cleared = 0; cellsN = 0; closeCalls = 0; combo = 0; comboT = 0; shield = false; shieldK = 0; invT = 0;
alive = true; started = demo; overT = 0; overSent = false; boostK = 0; engine = 1; hurt = 0; spin = 0;
zone = Math.floor(px / ZONE_LEN);
setPalette(zoneDef(zone), true);
gapY = 9; lastKind = ""; nextX = px + 32;
camX = px + viewHalfW() * 0.42;
updateChunks(camX, true);
seedLaneCars(camX);
for (const f of flyers) launchFlyer(f, camX, true);
for (const a of air) a.x = camX + rand(-a.ln.span, a.ln.span);
spawnUntil(camX + 95);
trails[0].length = trails[1].length = 0;
parts.length = 0; shards.length = 0;
texts.clear();
car.visible = true; car.rotation.set(0, 0, 0);
hud.classList.toggle("on", !demo);
if (!demo) {
PA.stat("sk-score", 0); PA.stat("sk-zone", 1); updateCellsHud();
showBanner("ZONE 1", ZONES[0].name, "");
showHint(PA.isTouch ? "Hold BOOST to climb · hold CUT to drop" : "Hold Space to boost · hold S to cut the engine", 4.5);
} else { banner.classList.remove("on"); hint.classList.remove("on"); }
}
function updateCellsHud() {
const el = $("sk-cells");
el.classList.toggle("shield", shield);
PA.stat(el, shield ? "Shield" : (cellsN % CELLS_FOR_SHIELD) + "/" + CELLS_FOR_SHIELD);
el.querySelector(".bar i").style.width = (shield ? 100 : (cellsN % CELLS_FOR_SHIELD) / CELLS_FOR_SHIELD * 100) + "%";
}
function addText(x, y, text, color, size) {
tmpV.set(x, y, 0).project(camera);
texts.add((tmpV.x + 1) / 2 * view.w, (1 - tmpV.y) / 2 * view.h, text, { color, size: size || 18, life: 1.1, vy: -46 });
}
const tmpV = new THREE.Vector3();
const jetW = [new THREE.Vector3(), new THREE.Vector3()];
function crash(why) {
if (!alive) return;
alive = false;
overT = 1.5;
vy = 5;
spin = rand(-1, 1) < 0 ? -1 : 1;
burst(px, py, 0, { n: 90, colors: [0xffb21e, 0xff4b3d, 0xffffff, 0xff2fa8], speed: 14, life: 1.1, size: 0.5, g: 6, drag: 1.2, vx: V * 0.4, bright: 3 });
burst(px, py, 0, { n: 40, colors: [0x29f0ff, 0xffffff], speed: 22, life: 0.5, size: 0.25, g: 0, drag: 2 });
shatter(px, py, 0, 0xffb400, 26, V * 0.5);
shatter(px, py, 0, 0x22242c, 18, V * 0.5);
PA.sfx("explode"); PA.sfx("boom", { vol: 0.6 });
PA.shake(0.9, 0.55);
PA.flash("#ff5a8a", 320);
hurt = 1;
engineAudio(false);
car.userData.why = why;
}
function hitShield(o) {
shield = false; invT = 1.3; hurt = 0.6;
updateCellsHud();
burst(px, py, 0, { n: 60, colors: [0x7dffd0, 0xffffff, 0x29f0ff], speed: 16, life: 0.7, size: 0.35, g: 0, drag: 2.5 });
PA.sfx("hit"); PA.sfx("power", { pitch: 0.6 });
PA.shake(0.5, 0.3); PA.flash("#7dffd0", 220);
addText(px, py + 1.6, "SHIELD!", "#7dffd0", 22);
if (o && (o.kind === "traffic" || o.kind === "patrol")) {
// the thing you hit is knocked out of the way
o.dead = true;
for (const s of o.shapes) s.on = false;
burst(px + 2, py, 0, { n: 40, colors: [0xffb21e, 0xff4b3d, 0xffffff], speed: 12, life: 0.9, size: 0.4, g: 5, drag: 1.2 });
o.grp.visible = false;
}
}
function closeCall(o, d) {
combo = Math.min(9, combo + 1); comboT = 4;
closeCalls++;
const b = 5 * combo;
score += b;
addText(px + 1, py + 1.4, (combo > 1 ? "CLOSE ×" + combo : "CLOSE") + " +" + b, "#ffd27a", 17 + combo);
PA.sfx("combo", { pitch: 1 + combo * 0.06 });
}
function collect(c) {
c.got = true;
cellsN++;
score += 15;
burst(c.x, c.y, 0, { n: 26, colors: [0x5affc8, 0xe8fff6, 0x29f0ff], speed: 8, life: 0.6, size: 0.3, g: 0, drag: 3 });
PA.sfx("coin");
addText(c.x, c.y + 1, "+15", "#7dffd0", 16);
if (cellsN % CELLS_FOR_SHIELD === 0 && !shield) {
shield = true;
PA.sfx("power");
addText(px, py + 2, "SHIELD CHARGED", "#7dffd0", 22);
}
updateCellsHud();
}
// Autopilot for the attract mode behind the title card: aim for the safe height of the next thing ahead.
function autopilot() {
let next = null;
for (const o of obs) if (o.x1 > px - HALF_L && (!next || o.x0 < next.x0)) next = o;
let target = 9;
if (next && next.safe) target = next.safe(simT + Math.max(0, (next.x0 - px) / V), py);
const want = (target - py) * 2.4 - vy * 0.55;
auto.boost = want > 1.0;
auto.cut = want < -4.5;
}
function update(dt, T) {
if (bannerT > 0) { bannerT -= dt; if (bannerT <= 0) banner.classList.remove("on"); }
if (tipT > 0) { tipT -= dt; if (tipT <= 0) hint.classList.remove("on"); }
if (PA.state === "gate" && !demo) reset(true);
simT += dt;
hurt = Math.max(0, hurt - dt * 1.6);
texts.update(dt);
easePalette(dt);
if (!alive) {
// the wreck tumbles into the lane below, trailing fire
V *= Math.exp(-dt * 1.4);
px += V * dt;
vy -= 22 * dt; py += vy * dt;
car.rotation.z += spin * dt * 5; car.rotation.x += dt * 3;
if (py > -30 && Math.random() < 0.8) burst(px, py, 0, { n: 2, colors: [0xff7a2a, 0xffb21e, 0x553344], speed: 2, life: 0.6, size: 0.6, g: -3, drag: 1 });
if (py < -6) car.visible = false;
if (overT > 0) {
overT -= dt;
if (overT <= 0 && !overSent) {
overSent = true;
hud.classList.remove("on");
PA.sfx("gameover");
PA.gameOver(score, {
title: cleared >= 70 ? "Skyway legend!" : cleared >= 30 ? "Hot pilot!" : "Wrecked!",
stats: [["Cleared", cleared], ["Cells", cellsN], ["Close calls", closeCalls], ["Zone", zone + 1]]
});
}
}
for (const o of obs) if (o.upd) o.upd(dt, simT, px, V, py);
return;
}
const ap = demo || autoTest;
if (ap) autopilot();
boosting = ap ? auto.boost : wantBoost();
cutting = !boosting && (ap ? auto.cut : wantCut());
if (!started && (boosting || cutting || autoTest)) started = true;
if (boosting && !wasBoost) {
vy = Math.max(vy, vy * 0.3 + 3);
if (!demo) PA.sfx("whoosh", { pitch: 1.5, vol: 0.7 });
}
if (cutting && !wasCut && !demo) PA.tone({ freq: 340, to: 70, type: "sawtooth", dur: 0.22, vol: 0.05, filter: 1400 });
if (!cutting && wasCut && !demo) PA.sfx("blip", { pitch: 0.7 });
wasBoost = boosting; wasCut = cutting;
if (!started) {
// waiting for the first input: the lifters hold you level
vy = lerp(vy, (9 + Math.sin(T * 2) * 0.4 - py) * 2, 1 - Math.exp(-dt * 4));
} else if (boosting) vy = Math.min(BOOST_MAX, vy + BOOST_A * dt);
else if (cutting) vy = Math.max(CUT_MAX, vy - CUT_A * dt);
else { vy -= IDLE_G * dt; if (vy < IDLE_MAX) vy = lerp(vy, IDLE_MAX, 1 - Math.exp(-dt * 6)); }
liftK = 0;
for (const o of obs) if (o.lift) { const l = o.lift(px, py); if (l) { vy = Math.min(16, vy + l * dt); liftK = 1; } }
py += vy * dt;
zone = Math.floor(px / ZONE_LEN);
const zd = zoneDef(zone);
if (zd.name !== banner.dataset.zone) {
banner.dataset.zone = zd.name;
if (px > 10) {
setPalette(zd);
if (!demo) {
score += 50;
showBanner("ZONE " + (zone + 1), zd.name, zd.tip);
PA.sfx("levelup");
PA.stat("sk-zone", zone + 1);
}
}
}
const cruise = Math.min(TOP_V, BASE_V + px * 0.0016 + (zone >= ZONES.length ? 2 : 0));
thr = demo ? 0 : wantThrottle();
V = lerp(V, started ? cruise * (1 + 0.3 * thr) : 0, 1 - Math.exp(-dt * 2.2));
px += V * dt;
const tp = clamp(vy * 0.032, -0.3, 0.34);
pitch = lerp(pitch, tp, 1 - Math.exp(-dt * 7));
roll = lerp(roll, Math.sin(T * 1.3) * 0.04 + thr * 0.06 + (cutting ? 0.1 : 0), 1 - Math.exp(-dt * 4));
if (invT > 0) invT -= dt;
if (comboT > 0) { comboT -= dt; if (comboT <= 0) combo = 0; }
for (const o of obs) if (o.upd) o.upd(dt, simT, px, V, py);
// collisions: the car is a capsule along its pitch
const ca = Math.cos(pitch), sa = Math.sin(pitch);
for (const o of obs) {
if (!o.act || o.x1 < px - HALF_L - 1.5 || o.x0 > px + HALF_L + 1.5) continue;
let oc = Infinity;
for (const s of o.shapes) {
if (!s.on) continue;
for (let i = 0; i < 7; i++) {
const k = -HALF_L + HALF_L * i / 3, d = dist(s, px + ca * k, py + sa * k) - CAR_R;
if (d < oc) oc = d;
}
}
if (oc < o.clear) o.clear = oc;
if (oc < 0 && invT <= 0 && !god && !demo) { if (shield) hitShield(o); else { crash(o.kind); return; } }
}
if (py - CAR_R < 0 || py + CAR_R > CEIL) {
if (god || demo || invT > 0) { py = clamp(py, CAR_R, CEIL - CAR_R); vy *= -0.3; }
else if (shield) { hitShield(null); py = clamp(py, CAR_R + 0.1, CEIL - CAR_R - 0.1); vy = py < 9 ? 7 : -5; }
else { crash("lane"); return; }
}
for (const o of obs) {
if (o.passed || !o.act || px - HALF_L <= o.x1) continue;
o.passed = true;
if (demo) continue;
cleared++;
score += 10;
if (o.clear < 0.75 && o.clear > -0.01) closeCall(o, o.clear);
}
for (const c of cells) {
if (c.got) continue;
if (Math.hypot(c.x - px, c.y - py) < 1.35 && !demo) collect(c);
}
if (!demo) {
for (const o of obs) if (o.tip && o.x0 - px < 30) { showHint(o.tip, 3.6); o.tip = null; }
PA.stat("sk-score", score.toLocaleString());
}
// the world ahead, and clean-up behind
const hw = viewHalfW(), cx = px + hw * 0.42;
spawnUntil(cx + 95);
for (let i = obs.length - 1; i >= 0; i--) if (obs[i].x1 < cx - hw - 30 && obs[i].passed) { disposeGroup(obs[i].grp); obs.splice(i, 1); }
for (let i = cells.length - 1; i >= 0; i--) if (cells[i].got || cells[i].x < cx - hw - 20) { disposeGroup(cells[i].m); cells.splice(i, 1); }
engineAudio(PA.state === "play" && !demo);
}
/* ---------------- engine audio ---------------- */
// A turbine whine over a low roar; the thrusters add a deep rumble, cutting the engine kills both.
const eng = { a: null };
function engineAudio(on) {
if (!eng.a) {
if (!on) return;
const a = PA.audio();
if (!a) return;
eng.a = a;
eng.bus = a.createGain(); eng.bus.gain.value = 0; eng.bus.connect(a.destination);
const nb = a.createBuffer(1, a.sampleRate * 2, a.sampleRate), nd = nb.getChannelData(0);
let last = 0;
for (let i = 0; i < nd.length; i++) { const w = Math.random() * 2 - 1; last = (last + 0.02 * w) / 1.02; nd[i] = w * 0.6 + last * 3; }
const ns = a.createBufferSource(); ns.buffer = nb; ns.loop = true; ns.start();
eng.roarF = a.createBiquadFilter(); eng.roarF.type = "lowpass"; eng.roarF.frequency.value = 500; eng.roarF.Q.value = 0.7;
eng.roar = a.createGain(); eng.roar.gain.value = 0.3;
ns.connect(eng.roarF); eng.roarF.connect(eng.roar); eng.roar.connect(eng.bus);
eng.o = a.createOscillator(); eng.o.type = "sawtooth"; eng.o.frequency.value = 600;
eng.oF = a.createBiquadFilter(); eng.oF.type = "bandpass"; eng.oF.frequency.value = 1400; eng.oF.Q.value = 6;
eng.oG = a.createGain(); eng.oG.gain.value = 0.02;
eng.o.connect(eng.oF); eng.oF.connect(eng.oG); eng.oG.connect(eng.bus); eng.o.start();
}
const a = eng.a, now = a.currentTime;
const live = on && PA.sfxOn && PA.sfxVol > 0 && !PA.isPaused() && alive;
eng.bus.gain.setTargetAtTime(live ? 0.3 * PA.sfxVol * (0.25 + 0.75 * engine) : 0, now, 0.08);
if (!live) return;
const k = clamp(V / TOP_V, 0, 1), b = boosting ? 1 : 0;
eng.roarF.frequency.setTargetAtTime(300 + k * 300 + b * 700, now, 0.12);
eng.roar.gain.setTargetAtTime(0.18 + b * 0.6, now, 0.1);
eng.o.frequency.setTargetAtTime(480 + k * 400 + b * 380 + vy * 8, now, 0.15);
eng.oF.frequency.setTargetAtTime(1100 + k * 900 + b * 600, now, 0.15);
eng.oG.gain.setTargetAtTime(0.02 * engine, now, 0.08);
}
PA.onPause && PA.onPause(() => engineAudio(false));
/* ---------------- rendering ---------------- */
post.comp.uniforms.strength.value = 0.62;
post.comp.uniforms.grain.value = 0.024;
const bufSize = new THREE.Vector2();
function resize3d() {
renderer.setPixelRatio(dprCap());
renderer.setSize(innerWidth, innerHeight);
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.getDrawingBufferSize(bufSize);
post.setSize(bufSize.x, bufSize.y);
pointsMat.uniforms.scale.value = bufSize.y / (2 * Math.tan(THREE.MathUtils.degToRad(camera.fov) / 2));
// landscape: the whole skyway with room above and below; portrait: at least 24 units of it across
const tv = Math.tan(THREE.MathUtils.degToRad(42) / 2);
camD = Math.max(11.2 / tv, 12 / (tv * camera.aspect));
}
const view = PA.canvas($("fx"), { onResize: () => resize3d() });
const fx = view.ctx;
const tmpQ = new THREE.Vector3();
function render(T) {
const dt = Math.min(0.05, Math.max(0, T - (render.last || T)));
render.last = T;
const hw = viewHalfW();
// camera: the car sits a little left of centre with the skyway ahead of it
const portrait = camera.aspect < 1;
camX = lerp(camX, px + hw * (portrait ? 0.38 : 0.42) - thr * 1.2, 1 - Math.exp(-dt * 6));
if (Math.abs(camX - px) > hw * 2) camX = px + hw * 0.42;
const cy = portrait ? 9 : 9 + (clamp(py, 0, CEIL) - 9) * 0.22;
camY = lerp(camY, cy, 1 - Math.exp(-dt * 3));
const sh = PA.shakeOffset();
boostK = lerp(boostK, alive && boosting ? 1 : 0, 1 - Math.exp(-dt * (boosting ? 10 : 4)));
camera.fov = 42 + boostK * 2.2 + (V - BASE_V) * 0.1;
camera.updateProjectionMatrix();
camera.position.set(camX - 3.4 + sh.x * 0.06, camY + 1.7 + sh.y * 0.06, camD);
camera.lookAt(camX - 0.9, camY, 0);
// palette into the scene
const F = pal.fog;
scene.fog.color.setRGB(F.x, F.y, F.z);
scene.background.setRGB(F.x, F.y, F.z);
skyMat.uniforms.sun.value = pal.sun; skyMat.uniforms.time.value = T;
sky.position.copy(camera.position); stars.position.copy(camera.position);
stars.material.opacity = 0.4 * (1 - pal.sun);
rimA.color.copy(pal.rimA); rimB.color.copy(pal.rimB);
hemi.color.setRGB(0.35 + pal.hor.x * 0.4, 0.3 + pal.hor.y * 0.4, 0.6 + pal.hor.z * 0.3);
for (const m of skylines) {
m.position.x = camX;
m.material.map.offset.x = camX * 3 / SKY_W;
m.material.color.setRGB(0.55 + pal.hor.x * 0.7, 0.55 + pal.hor.y * 0.7, 0.55 + pal.hor.z * 0.7).multiplyScalar(m.userData.k);
}
deck.position.x = camX; deckMat.uniforms.time.value = T;
beaconMat.uniforms.time.value = T;
M.holo.color.setScalar(1.6 * (0.92 + 0.08 * Math.sin(T * 31) * Math.sin(T * 7)));
for (const r of rails) { r.position.x = camX; r.material.map.offset.x = camX / 16; }
veil.position.x = camX; veil.material.color.setRGB(F.x, F.y, F.z);
const nearRail = alive ? Math.max(1 - py / 3.5, 1 - (CEIL - py) / 3.5, 0) : 0;
rails[0].material.color.set(0x29f0ff).multiplyScalar(0.9 + (py < 9 ? nearRail : 0) * 3 * (0.6 + 0.4 * Math.sin(T * 20)));
rails[1].material.color.set(0xff2fa8).multiplyScalar(0.9 + (py > 9 ? nearRail : 0) * 3 * (0.6 + 0.4 * Math.sin(T * 20)));
if (PA.state !== "play" || demo || alive) updateChunks(camX);
updateAir(dt, camX);
updateRain(dt, camX, camY, V);
// lane traffic just below and above the skyway
let maxU = -Infinity, maxL = -Infinity;
for (const c of laneCars) if (c.upper) maxU = Math.max(maxU, c.x); else maxL = Math.max(maxL, c.x);
for (const c of laneCars) {
c.x += (c.upper ? -c.v : V * c.v) * dt;
if (c.x < camX - hw - 12) { if (c.upper) { c.x = Math.max(maxU, camX + hw + 4) + rand(10, 20); maxU = c.x; } else { c.x = Math.max(maxL, camX + hw + 4) + rand(11, 22); maxL = c.x; } }
c.m.position.set(c.x, (c.upper ? CEIL + 2.4 : -2.4) + Math.sin(T * 1.6 + c.bob) * 0.12, c.z);
}
for (const f of flyers) {
f.x += f.dir * f.v * dt;
if (f.x < camX - 100 || f.x > camX + 130) launchFlyer(f, camX, false);
f.m.position.set(f.x, f.y + Math.sin(T + f.bob) * 0.3, f.z);
}
// obstacle dressing
for (const o of obs) if (o.blink) for (const s of o.blink) s.visible = ((T * 1.1 + s.userData.blink) % 1) < 0.55;
for (const c of cells) {
c.m.rotation.y = T * 2 + c.ph; c.m.position.y = c.y + Math.sin(T * 3 + c.ph) * 0.18;
c.m.userData.ring.rotation.x = Math.sin(T * 1.7 + c.ph) * 0.6;
}
// the car
engine = lerp(engine, alive && cutting ? 0.1 : alive ? 1 : 0, 1 - Math.exp(-dt * (cutting ? 14 : 5)));
car.position.set(px, py, 0);
if (alive) { tilt.rotation.set(roll, 0, pitch); }
const flick = alive && cutting ? (Math.random() < 0.15 ? 0.6 : 0) : 0;
const en = Math.max(engine, flick);
pAccent.color.set(0x29f0ff).multiplyScalar(0.4 + 3.6 * en);
pTail.color.set(0xff1030).multiplyScalar(0.5 + 1.7 * en);
for (const s of pods) { s.material.color.set(0x50e8ff).multiplyScalar(2.2 * en * (0.85 + 0.15 * Math.sin(T * 30))); s.scale.setScalar(1.1 * (0.6 + 0.4 * en)); }
pHead.visible = en > 0.3;
pTailGlow.material.color.set(0xff2040).multiplyScalar(1.8 * (0.3 + 0.7 * en));
underLight.intensity = (22 + boostK * 30) * en;
for (const f of flames) {
const k = alive ? 0.25 * en + boostK * 1.2 : 0;
f.core.scale.setScalar(0.4 + k * 0.9);
f.core.material.color.set(0x9af2ff).multiplyScalar(1 + 2.5 * k);
f.plume.scale.set(0.6 + k, 0.6 + k, 0.3 + k * 2.6 * (0.85 + 0.15 * Math.sin(T * 50)));
f.plume.visible = k > 0.05;
}
plumeMat.uniforms.time.value = T;
shieldK = lerp(shieldK, shield ? 1 : 0, 1 - Math.exp(-dt * 6));
shieldMat.uniforms.a.value = shieldK * (0.55 + 0.15 * Math.sin(T * 5)) + (invT > 0 ? Math.max(0, invT - 0.6) * 1.5 : 0);
shieldMat.uniforms.time.value = T;
shieldMesh.visible = shieldMat.uniforms.a.value > 0.01;
if (invT > 0 && alive) pBody.visible = (T * 16) % 1 < 0.6; else pBody.visible = true;
// thruster wake, sparks and lifter shimmer
tv = 0;
car.updateMatrixWorld(true);
for (let i = 0; i < 2; i++) {
jetW[i].copy(jetLocal[i]).applyMatrix4(tilt.matrixWorld);
const tr = trails[i];
if (alive) tr.unshift(jetW[i].x, jetW[i].y, jetW[i].z); else if (tr.length) tr.splice(0, 3);
if (tr.length > 66) tr.length = 66;
const rev = [];
for (let j = tr.length - 3; j >= 0; j -= 3) rev.push(tr[j], tr[j + 1], tr[j + 2]);
const k = 0.25 + boostK * 1.6;
ribbon(rev, [0.25 * k, 1.6 * k, 2.4 * k], 0.12 + boostK * 0.16, true);
}
tGeo.setDrawRange(0, tv);
tGeo.attributes.position.needsUpdate = true; tGeo.attributes.color.needsUpdate = true;
if (alive && PA.state !== "over") {
if (boosting && Math.random() < 0.9) for (const j of jetW) burst(j.x, j.y, j.z, { n: 1, colors: [0x9af2ff, 0xffffff, 0x29f0ff], speed: 4, angle: Math.PI + pitch, spread: 0.3, life: 0.35, size: 0.22, g: 0, drag: 2, vx: -V * 0.2 });
if (cutting && Math.random() < 0.3) burst(px + rand(-1, 1), py - 0.4, rand(-0.8, 0.8), { n: 1, colors: [0xffd27a, 0xff7a2a], speed: 3, life: 0.4, size: 0.12, g: 14 });
if (liftK && Math.random() < 0.5) burst(px + rand(-1.2, 1.2), py - 0.5, rand(-1, 1), { n: 1, colors: [0xffb070, 0xffffff], speed: 2, angle: Math.PI / 2, spread: 0.4, life: 0.3, size: 0.25, g: -4 });
}
updateParts(dt);
updateShards(dt);
const pc = post.comp.uniforms;
pc.hurt.value = hurt * 0.8;
pc.chroma.value = 0.12 + boostK * 0.55 + hurt * 2;
post.render(scene, camera, T);
// HUD layer: floating text, warnings for traffic coming at you off-screen, drone lock-ons
fx.clearRect(0, 0, view.w, view.h);
if (!demo && alive && PA.state === "play") {
for (const o of obs) {
if (o.kind === "traffic" && o.head && o.act && !o.dead) for (const q of o.warn) {
tmpQ.set(q.x - q.hl, q.y, q.z).project(camera);
const sx = (tmpQ.x + 1) / 2 * view.w, sy = (1 - tmpQ.y) / 2 * view.h;
if (sx < view.w + 8 || q.z < -1) continue;
const a = 0.55 + 0.45 * Math.sin(T * 18);
fx.save();
fx.translate(view.w - 26, clamp(sy, 60, view.h - 60));
fx.globalAlpha = a; fx.fillStyle = "#ffcf6a"; fx.shadowColor = "#ff8a2a"; fx.shadowBlur = 14;
fx.beginPath(); fx.moveTo(-12, 0); fx.lineTo(8, -12); fx.lineTo(8, 12); fx.closePath(); fx.fill();
fx.restore();
}
if (o.kind === "patrol" && o.act && !o.dead) for (const s of o.shapes) {
tmpQ.set(s.x, s.y, 0).project(camera);
const sx = (tmpQ.x + 1) / 2 * view.w, sy = (1 - tmpQ.y) / 2 * view.h, r = 22;
if (sx > view.w || sx < 0) continue;
fx.save(); fx.translate(sx, sy);
fx.strokeStyle = s.x - px < 11 ? "#ff3a5a" : "rgba(140,200,255,.55)"; fx.lineWidth = 2; fx.shadowColor = fx.strokeStyle; fx.shadowBlur = 10;
fx.rotate(s.x - px < 11 ? 0 : T * 2);
for (let k = 0; k < 4; k++) { fx.rotate(Math.PI / 2); fx.beginPath(); fx.moveTo(r, -7); fx.lineTo(r, -r); fx.lineTo(7, -r); fx.stroke(); }
fx.restore();
}
}
}
texts.draw(fx);
}
/* ---------------- boot ---------------- */
PA.onStart(() => reset(false));
reset(true);
PA.loop(update, render);
// test hooks
window.__sky = {
state: () => ({ why: car.userData.why, px, py, vy, V, score, cleared, cellsN, shield, alive, zone, demo, started, obs: obs.map((o) => o.kind + "@" + Math.round(o.x0)) }),
god: (on) => { god = on !== false; },
warp: (z) => {
clearObs();
px = z * ZONE_LEN + 4; nextX = px + 30; gapY = py;
zone = z; setPalette(zoneDef(z), true); banner.dataset.zone = zoneDef(z).name;
camX = px + viewHalfW() * 0.42;
resetChunks(); updateChunks(camX, true); seedLaneCars(camX);
for (const f of flyers) launchFlyer(f, camX, true);
spawnUntil(camX + 95);
},
force: (kind) => { forceKind = kind || null; },
spawn: (kind, ahead) => {
clearObs(); nextX = px + (ahead || 22); forceKind = kind; spawnUntil(nextX + 1); forceKind = null;
},
setY: (y) => { py = y; vy = 0; },
cells: (n) => { cellsN = n; updateCellsHud(); },
shield: () => { shield = true; updateCellsHud(); },
boost: (on) => { touch.boost = !!on; },
cut: (on) => { touch.cut = !!on; },
auto: (on) => { autoTest = on !== false; },
step: (n, dt) => { for (let i = 0; i < n; i++) { update(dt || 1 / 60, simT); if (PA.state !== "play") break; } }
};
})();
</script>
</body></html>