Add parallax starfield that drifts with the moon across the sky

- Stars now shift a fraction of the moon's horizontal travel each frame for a subtle depth effect
- Stars leaving one edge re-enter the opposite edge with re-rolled height, brightness, twinkle and sparkle so the field feels alive
- Guard against the edge-to-edge jump when the moon starts a new pass so the starfield never lurches to the other side
This commit is contained in:
Brian Fertig 2026-10-26 19:36:38 -06:00
parent a8396c4024
commit 46d30a767f
1 changed files with 45 additions and 1 deletions

View File

@ -608,7 +608,14 @@ if (canvas) {
} }
// ---------- Sky: stars and moon (static, in front of the sunset backdrop) ---------- // ---------- Sky: stars and moon (in front of the sunset backdrop) ----------
// Starfield rides along with the moon: as the moon crosses the sky, the
// stars drift a fraction of the way (slow parallax). Any star that leaves
// one edge of the field re-enters the opposite edge as a fresh star with
// re-rolled height, brightness and twinkle.
const STAR_W = 85; // half-width of the starfield band
const STAR_SPAN = STAR_W * 2;
const STAR_PARALLAX = 0.3; // starfield moves this fraction of the moon
const starCount = 160; const starCount = 160;
const starPos = new Float32Array(starCount * 3); const starPos = new Float32Array(starCount * 3);
const starCol = new Float32Array(starCount * 3); const starCol = new Float32Array(starCount * 3);
@ -623,6 +630,19 @@ if (canvas) {
starPhase.push(Math.random() * Math.PI * 2); // twinkle offset starPhase.push(Math.random() * Math.PI * 2); // twinkle offset
starGlint.push(Math.random() < 0.35 ? 0.6 + Math.random() * 0.6 : 0); // occasional sharp sparkle starGlint.push(Math.random() < 0.35 ? 0.6 + Math.random() * 0.6 : 0); // occasional sharp sparkle
} }
const starX = [], starY = [];
for (let i = 0; i < starCount; i++) {
starX.push(starPos[i * 3]);
starY.push(starPos[i * 3 + 1]);
}
function rerollStar(i) {
starY[i] = rnd(14, 62);
starBase[i] = 0.4 + Math.random() * 0.6;
starSpeed[i] = 0.6 + Math.random() * 2.2;
starPhase[i] = Math.random() * Math.PI * 2;
starGlint[i] = Math.random() < 0.35 ? 0.6 + Math.random() * 0.6 : 0;
}
let starShift = 0; // pending drift for this frame, set by the moon
const starGeo = new THREE.BufferGeometry(); const starGeo = new THREE.BufferGeometry();
starGeo.setAttribute('position', new THREE.BufferAttribute(starPos, 3)); starGeo.setAttribute('position', new THREE.BufferAttribute(starPos, 3));
starGeo.setAttribute('color', new THREE.BufferAttribute(starCol, 3)); starGeo.setAttribute('color', new THREE.BufferAttribute(starCol, 3));
@ -660,6 +680,7 @@ if (canvas) {
side: 1, // +1 = rising on the left, -1 = rising on the right side: 1, // +1 = rising on the left, -1 = rising on the right
hidden: false, hidden: false,
restLeft: 0, restLeft: 0,
lastX: -MOON_EDGE, // moon x on the previous frame (drives star drift)
}; };
// ---------- Equalizer skyline (distant bars pulsing with the bass) ---------- // ---------- Equalizer skyline (distant bars pulsing with the bass) ----------
@ -970,6 +991,9 @@ if (canvas) {
moonCycle.hidden = false; moonCycle.hidden = false;
moonCycle.t = 0; moonCycle.t = 0;
moonCycle.side = -moonCycle.side; // rise from the opposite edge moonCycle.side = -moonCycle.side; // rise from the opposite edge
// New pass starts from a fresh edge; don't let the starfield lurch
// from the old side to the new one.
moonCycle.lastX = moonCycle.side > 0 ? -MOON_EDGE : MOON_EDGE;
} }
} else { } else {
moon.visible = true; moon.visible = true;
@ -978,6 +1002,11 @@ if (canvas) {
const x0 = moonCycle.side > 0 ? -MOON_EDGE : MOON_EDGE; const x0 = moonCycle.side > 0 ? -MOON_EDGE : MOON_EDGE;
const x1 = moonCycle.side > 0 ? MOON_EDGE : -MOON_EDGE; const x1 = moonCycle.side > 0 ? MOON_EDGE : -MOON_EDGE;
moon.position.x = x0 + (x1 - x0) * p; moon.position.x = x0 + (x1 - x0) * p;
// Starfield follows the moon with a little parallax. Skip any
// edge-to-edge jump so the field never lurches to the other side.
const dmx = moon.position.x - moonCycle.lastX;
if (Math.abs(dmx) < MOON_EDGE) starShift += dmx * STAR_PARALLAX;
moonCycle.lastX = moon.position.x;
// Sinks just below the horizon at both ends so it clearly rises & sets // Sinks just below the horizon at both ends so it clearly rises & sets
const y = -6 + 44 * Math.sin(Math.PI * p); const y = -6 + 44 * Math.sin(Math.PI * p);
moon.position.y = y; moon.position.y = y;
@ -994,6 +1023,21 @@ if (canvas) {
} }
} }
// Starfield: slip along with the moon this frame; any star that leaves the
// band re-enters on the far edge wearing a fresh identity
if (starShift !== 0) {
for (let i = 0; i < starCount; i++) {
let x = starX[i] + starShift;
if (x > STAR_W) { x -= STAR_SPAN; rerollStar(i); }
else if (x < -STAR_W) { x += STAR_SPAN; rerollStar(i); }
starX[i] = x;
starPos[i * 3] = x;
starPos[i * 3 + 1] = starY[i];
}
starShift = 0;
starGeo.attributes.position.needsUpdate = true;
}
// Shooting star streaks // Shooting star streaks
if (!shooting.active && now >= shooting.nextAt) spawnShootingStar(); if (!shooting.active && now >= shooting.nextAt) spawnShootingStar();
if (shooting.active) { if (shooting.active) {