Back to the app
The code
Swim the Kelp Garden by Claude Haiku 5.5
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
partly works · auto-checked (Checked by an automated pass. Not hand-played yet.)<!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>Kelp Garden</title>
<style>
html,body{margin:0;height:100%;background:#1277a0;overflow:hidden;font-family:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;touch-action:none;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none;}
canvas{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none;}
#top{position:fixed;top:0;left:0;right:0;padding:16px 16px 0;color:#fff3d0;pointer-events:none;text-shadow:0 1px 6px rgba(0,30,50,.55);}
#top h1{margin:0;font-size:22px;font-weight:700;letter-spacing:.4px;color:#ffd98a;}
#top p{margin:4px 0 0;font-size:14px;opacity:.92;}
#hint{position:fixed;left:16px;right:136px;bottom:22px;color:#fff3d0;font-size:15px;line-height:1.35;pointer-events:none;text-shadow:0 1px 6px rgba(0,30,50,.6);transition:opacity 1.2s;}
#reset{position:fixed;right:16px;bottom:16px;min-width:104px;min-height:64px;padding:0 18px;border:0;border-radius:32px;background:#f0a045;color:#0a2f3f;font:700 17px system-ui,-apple-system,sans-serif;cursor:pointer;box-shadow:0 6px 18px rgba(0,0,0,.35);touch-action:manipulation;}
#reset:active{transform:scale(.96);}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="top"><h1>Kelp Garden</h1><p id="sub">Mouse: hold the button and move the pointer.</p></div>
<div id="hint">Press and hold to glide ahead. Drag to turn.</div>
<button id="reset" type="button">Start over</button>
<script type="module">
import * as THREE from "https://unpkg.com/three@0.160.0/build/three.module.js";
const canvas = document.getElementById("c");
const hintEl = document.getElementById("hint");
const resetBtn = document.getElementById("reset");
/* ---------- renderer / scene ---------- */
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
const WATER = 0x1277a0;
const scene = new THREE.Scene();
scene.background = new THREE.Color(WATER);
scene.fog = new THREE.FogExp2(WATER, 0.013);
const camera = new THREE.PerspectiveCamera(68, window.innerWidth / window.innerHeight, 0.1, 400);
scene.add(new THREE.HemisphereLight(0x8ff5e0, 0x0b3f57, 1.1));
const sun = new THREE.DirectionalLight(0xffd98a, 0.9);
sun.position.set(-25, 60, 15);
scene.add(sun);
/* ---------- deterministic random helpers ---------- */
let seed = 20241;
function rnd() {
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
const rr = (a, b) => a + (b - a) * rnd();
const pick = (arr) => arr[Math.floor(rnd() * arr.length)];
const hash3 = (x, y, z) => {
const s = Math.sin(x * 12.9898 + y * 78.233 + z * 37.719) * 43758.5453;
return s - Math.floor(s);
};
/* ---------- world constants ---------- */
const FLOOR = -18;
const SURF = 14;
function floorH(x, z) {
return FLOOR + 0.9 * Math.sin(x * 0.07) * Math.cos(z * 0.05) + 0.5 * Math.sin(z * 0.13 + x * 0.04);
}
/* ---------- sand ---------- */
const sandGeo = new THREE.PlaneGeometry(400, 400, 120, 120);
sandGeo.rotateX(-Math.PI / 2);
{
const p = sandGeo.attributes.position;
for (let i = 0; i < p.count; i++) p.setY(i, floorH(p.getX(i), p.getZ(i)));
sandGeo.computeVertexNormals();
}
scene.add(new THREE.Mesh(sandGeo, new THREE.MeshLambertMaterial({ color: 0xc9a064 })));
/* ---------- caustic light texture (procedural) ---------- */
function makeCaustic(rep) {
const s = 256;
const cv = document.createElement("canvas");
cv.width = s; cv.height = s;
const g = cv.getContext("2d");
const img = g.createImageData(s, s);
const d = img.data;
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const u = (x / s) * Math.PI * 2, v = (y / s) * Math.PI * 2;
const a = Math.sin(3 * u + 1.6 * Math.sin(2 * v));
const b = Math.sin(3 * v + 1.6 * Math.sin(2 * u));
const val = Math.pow(Math.max(0, (a * b + 1) / 2), 5);
const i = (y * s + x) * 4;
d[i] = 255; d[i + 1] = 255; d[i + 2] = 255; d[i + 3] = val * 255;
}
}
g.putImageData(img, 0, 0);
const t = new THREE.CanvasTexture(cv);
t.wrapS = t.wrapT = THREE.RepeatWrapping;
t.repeat.set(rep, rep);
return t;
}
const causticSand = makeCaustic(36);
const sandGlow = new THREE.Mesh(sandGeo, new THREE.MeshBasicMaterial({
map: causticSand, color: 0xffefb0, transparent: true, opacity: 0.5,
blending: THREE.AdditiveBlending, depthWrite: false,
polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2
}));
scene.add(sandGlow);
/* ---------- surface with rippling light ---------- */
const surfGeo = new THREE.PlaneGeometry(500, 500);
surfGeo.rotateX(-Math.PI / 2);
const surface = new THREE.Mesh(surfGeo, new THREE.MeshBasicMaterial({
color: 0x8ff5e6, transparent: true, opacity: 0.3, side: THREE.DoubleSide, depthWrite: false
}));
surface.position.y = SURF;
scene.add(surface);
const causticSurf = makeCaustic(20);
const surfGlow = new THREE.Mesh(surfGeo, new THREE.MeshBasicMaterial({
map: causticSurf, color: 0xe9fff7, transparent: true, opacity: 0.55,
blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide
}));
surfGlow.position.y = SURF;
scene.add(surfGlow);
/* ---------- sunbeams ---------- */
const beams = [];
for (let i = 0; i < 10; i++) {
const h = SURF - FLOOR + 4;
const mat = new THREE.MeshBasicMaterial({
color: 0xffd98a, transparent: true, opacity: 0.08,
blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide
});
const m = new THREE.Mesh(new THREE.CylinderGeometry(0.8, 5.5, h, 10, 1, true), mat);
m.position.set(rr(-70, 70), (SURF + FLOOR) / 2, rr(-70, 70));
m.rotation.set(rr(-0.12, 0.12), 0, rr(-0.14, 0.14));
m.userData.ph = rnd() * 10;
scene.add(m);
beams.push(m);
}
/* ---------- solids (things the swimmer cannot pass) ---------- */
const solids = []; // {parent, local, r, c}
function addSolid(parent, x, y, z, r) {
solids.push({ parent, local: new THREE.Vector3(x, y, z), r, c: new THREE.Vector3() });
}
const rockMat = new THREE.MeshStandardMaterial({ color: 0x1d4d6b, flatShading: true, roughness: 0.95, metalness: 0 });
const rockMat2 = new THREE.MeshStandardMaterial({ color: 0x2b6283, flatShading: true, roughness: 0.95, metalness: 0 });
const rockMats = [rockMat, rockMat2];
function rockBlob(parent, x, y, z, r, sx, sy, sz) {
const geo = new THREE.IcosahedronGeometry(r, 1);
const p = geo.attributes.position;
for (let i = 0; i < p.count; i++) {
const px = p.getX(i), py = p.getY(i), pz = p.getZ(i);
const j = 0.82 + 0.3 * hash3(px * 10, py * 10, pz * 10);
p.setXYZ(i, px * j, py * j, pz * j);
}
geo.computeVertexNormals();
const m = new THREE.Mesh(geo, pick(rockMats));
m.position.set(x, y, z);
m.scale.set(sx, sy, sz);
m.rotation.set(rr(0, 6), rr(0, 6), rr(0, 6));
parent.add(m);
addSolid(parent, x, y, z, r * Math.max(sx, sy, sz) * 1.1);
}
/* ---------- rock clusters ---------- */
for (let c = 0; c < 22; c++) {
const a = rnd() * Math.PI * 2, d = rr(12, 48);
const x = Math.cos(a) * d, z = Math.sin(a) * d;
const g = new THREE.Group();
g.position.set(x, floorH(x, z), z);
g.rotation.y = rr(0, 6.28);
scene.add(g);
const n = 2 + Math.floor(rnd() * 3);
for (let k = 0; k < n; k++) {
rockBlob(g, rr(-4, 4), rr(0.2, 1.2), rr(-4, 4), rr(1.5, 3.5), rr(0.9, 1.4), rr(0.6, 1.0), rr(0.9, 1.4));
}
}
/* ---------- rocky arches (the swimmer can pass under them) ---------- */
for (let c = 0; c < 4; c++) {
const a = rnd() * Math.PI * 2, d = rr(16, 40);
const x = Math.cos(a) * d, z = Math.sin(a) * d;
const g = new THREE.Group();
g.position.set(x, floorH(x, z), z);
g.rotation.y = rr(0, 6.28);
scene.add(g);
const arc = new THREE.Mesh(new THREE.TorusGeometry(6, 1.7, 8, 20, Math.PI), rockMats[1]);
arc.position.set(0, 4, 0);
g.add(arc);
rockBlob(g, -6, 2.5, 0, 2.8, 1.0, 1.1, 1.0);
rockBlob(g, 6, 2.5, 0, 2.8, 1.0, 1.1, 1.0);
for (let k = 1; k < 6; k++) {
const th = (Math.PI * k) / 6;
addSolid(g, 6 * Math.cos(th), 4 + 6 * Math.sin(th), 0, 2.2);
}
}
/* ---------- sunken wooden ship ---------- */
const ship = new THREE.Group();
ship.position.set(32, floorH(32, -18) - 1.4, -18);
ship.rotation.set(0.07, 0.7, 0.16);
scene.add(ship);
{
const hs = new THREE.Shape();
hs.moveTo(-11, 3);
hs.lineTo(11, 3);
hs.quadraticCurveTo(7, -4, 0, -4.5);
hs.quadraticCurveTo(-7, -4, -11, 3);
const hullGeo = new THREE.ExtrudeGeometry(hs, { depth: 5, bevelEnabled: false, steps: 1 });
hullGeo.translate(0, 0, -2.5);
const woodMat = new THREE.MeshLambertMaterial({ color: 0xc06a3a });
const woodDark = new THREE.MeshLambertMaterial({ color: 0x7a4426 });
ship.add(new THREE.Mesh(hullGeo, woodMat));
const deck = new THREE.Mesh(new THREE.BoxGeometry(20.5, 0.5, 4.8), woodDark);
deck.position.set(0, 3.1, 0);
ship.add(deck);
const mast = new THREE.Mesh(new THREE.CylinderGeometry(0.28, 0.36, 18, 6), woodDark);
mast.position.set(-2, 12, 0);
ship.add(mast);
const yard = new THREE.Mesh(new THREE.BoxGeometry(0.35, 0.35, 10), woodDark);
yard.position.set(-2, 17, 0);
ship.add(yard);
const sail = new THREE.Mesh(new THREE.PlaneGeometry(10, 11, 1, 1),
new THREE.MeshLambertMaterial({ color: 0xe8c27a, side: THREE.DoubleSide }));
sail.rotation.y = Math.PI / 2;
sail.position.set(-2, 11.5, 0);
ship.add(sail);
[-9, -4.5, 0, 4.5, 9].forEach((x) => addSolid(ship, x, -0.5, 0, 4.2));
addSolid(ship, -2, 9, 0, 1.4);
}
/* ---------- coral (warm colors, decorative) ---------- */
const coralMats = [0xff8a4c, 0xffc24a, 0xf27b3c, 0xffa35c].map(
(c) => new THREE.MeshLambertMaterial({ color: c, flatShading: true })
);
for (let c = 0; c < 34; c++) {
const a = rnd() * Math.PI * 2, d = rr(6, 50);
const x = Math.cos(a) * d, z = Math.sin(a) * d;
const g = new THREE.Group();
g.position.set(x, floorH(x, z), z);
scene.add(g);
const n = 4 + Math.floor(rnd() * 4);
for (let k = 0; k < n; k++) {
const h = rr(1, 2.6);
const m = new THREE.Mesh(new THREE.ConeGeometry(rr(0.22, 0.4), h, 5), pick(coralMats));
m.position.set(rr(-1.2, 1.2), h / 2, rr(-1.2, 1.2));
m.rotation.set(rr(-0.4, 0.4), 0, rr(-0.5, 0.5));
g.add(m);
}
}
/* ---------- kelp (sways, no collision) ---------- */
const kelpMat = new THREE.MeshLambertMaterial({ color: 0x4d8f3c });
const leafMat = new THREE.MeshLambertMaterial({ color: 0x2e7d4f, side: THREE.DoubleSide });
const leafGeo = new THREE.SphereGeometry(1, 6, 4);
const kelps = [];
function makeKelp(x, z, segs) {
const root = new THREE.Group();
root.position.set(x, floorH(x, z) - 0.3, z);
scene.add(root);
const chain = [];
const seg = 2.1;
let parent = root;
for (let i = 0; i < segs; i++) {
const g = new THREE.Group();
g.position.y = i === 0 ? 0 : seg;
parent.add(g);
const k = i / segs;
const r = 0.26 * (1 - k * 0.45);
const stalk = new THREE.Mesh(new THREE.CylinderGeometry(r * 0.8, r, seg * 1.04, 6), kelpMat);
stalk.position.y = seg / 2;
g.add(stalk);
if (i > 1 && i % 2 === 0) {
const side = i % 4 === 0 ? 1 : -1;
const leaf = new THREE.Mesh(leafGeo, leafMat);
leaf.scale.set(1.5 + k, 0.05, 0.42);
leaf.position.set(side * (1.1 + k * 0.4), seg * 0.6, 0);
leaf.rotation.z = side * -0.5;
g.add(leaf);
}
chain.push(g);
parent = g;
}
kelps.push({ chain, ph: rnd() * 10, ph2: rnd() * 6 });
}
for (let c = 0; c < 7; c++) {
const cx = rr(-70, 70), cz = rr(-70, 70);
const n = 5 + Math.floor(rnd() * 3);
for (let j = 0; j < n; j++) makeKelp(cx + rr(-7, 7), cz + rr(-7, 7), 7 + Math.floor(rnd() * 5));
}
/* ---------- fish schools (boids) ---------- */
const FISH = 3, PER = 36, NF = FISH * PER;
const fishGeo = new THREE.ConeGeometry(0.34, 1.4, 6);
fishGeo.rotateX(Math.PI / 2); // tip points along +z
const fishMesh = new THREE.InstancedMesh(fishGeo, new THREE.MeshLambertMaterial({ color: 0xffffff }), NF);
fishMesh.frustumCulled = false;
const schoolCols = [0xf2b33d, 0xf0823a, 0x3fc1b4];
const fp = [], fv = [], fs = [];
const schoolC = [], schoolT = [], schoolTimer = [];
const colTmp = new THREE.Color();
for (let s = 0; s < FISH; s++) {
const c = new THREE.Vector3(rr(-30, 30), rr(-10, 2), rr(-30, 30));
schoolC.push(c.clone());
schoolT.push(c.clone().add(new THREE.Vector3(rr(-20, 20), 0, rr(-20, 20))));
schoolTimer.push(rr(4, 8));
colTmp.setHex(schoolCols[s]);
for (let k = 0; k < PER; k++) {
fp.push(c.clone().add(new THREE.Vector3(rr(-4, 4), rr(-2, 2), rr(-4, 4))));
fv.push(new THREE.Vector3(rr(-1, 1), rr(-0.2, 0.2), rr(-1, 1)).normalize().multiplyScalar(3));
fs.push(s);
fishMesh.setColorAt(fp.length - 1, colTmp);
}
}
fishMesh.instanceColor.needsUpdate = true;
scene.add(fishMesh);
const dummy = new THREE.Object3D();
const sepV = new THREE.Vector3(), aliV = new THREE.Vector3(), cohV = new THREE.Vector3();
const accV = new THREE.Vector3(), tmpF = new THREE.Vector3();
const cnt = new Array(FISH).fill(0);
function updateFish(dt) {
for (let s = 0; s < FISH; s++) {
schoolTimer[s] -= dt;
if (schoolTimer[s] <= 0) {
schoolT[s].set(rr(-45, 45), rr(-10, 3), rr(-45, 45));
schoolTimer[s] = rr(6, 10);
}
}
for (let s = 0; s < FISH; s++) { schoolC[s].set(0, 0, 0); cnt[s] = 0; }
for (let i = 0; i < NF; i++) { schoolC[fs[i]].add(fp[i]); cnt[fs[i]]++; }
for (let s = 0; s < FISH; s++) schoolC[s].divideScalar(cnt[s] || 1);
for (let i = 0; i < NF; i++) {
const p = fp[i], v = fv[i], s = fs[i];
sepV.set(0, 0, 0); aliV.set(0, 0, 0); cohV.set(0, 0, 0);
let n = 0;
for (let j = 0; j < NF; j++) {
if (j === i || fs[j] !== s) continue;
const q = fp[j];
tmpF.subVectors(p, q);
const d2 = tmpF.lengthSq();
if (d2 < 36) {
n++;
cohV.add(q);
aliV.add(fv[j]);
if (d2 < 4) sepV.addScaledVector(tmpF, 1 / Math.max(d2, 0.2));
}
}
accV.set(0, 0, 0);
accV.addScaledVector(sepV, 3);
if (n > 0) {
aliV.divideScalar(n).sub(v).multiplyScalar(1.2);
accV.add(aliV);
cohV.divideScalar(n).sub(p).multiplyScalar(0.5);
accV.add(cohV);
}
// drift toward the school's wandering target
tmpF.subVectors(schoolT[s], p);
const td = tmpF.length();
if (td > 0.01) accV.addScaledVector(tmpF, 0.8 / td);
// scatter away from the swimmer
tmpF.subVectors(p, player.pos);
const pd = tmpF.length();
if (pd < 12 && pd > 0.01) accV.addScaledVector(tmpF, ((12 - pd) * 0.6) / pd);
// steer around solids
for (const sol of solids) {
tmpF.subVectors(p, sol.c);
const d = tmpF.length();
const lim = sol.r + 6;
if (d < lim && d > 0.01) accV.addScaledVector(tmpF, ((lim - d) * 2) / d);
}
// keep within the water column and the garden
const fy = floorH(p.x, p.z) + 3;
if (p.y < fy) accV.y += (fy - p.y) * 2;
if (p.y > 9) accV.y -= (p.y - 9) * 2;
if (Math.abs(p.x) > 65) accV.x -= Math.sign(p.x) * (Math.abs(p.x) - 65) * 2;
if (Math.abs(p.z) > 65) accV.z -= Math.sign(p.z) * (Math.abs(p.z) - 65) * 2;
if (accV.length() > 6) accV.setLength(6);
v.addScaledVector(accV, dt);
const sp = v.length();
if (sp > 0.0001) v.multiplyScalar(Math.min(4.5, Math.max(2.2, sp)) / sp);
p.addScaledVector(v, dt);
// hard push-out from solids
for (const sol of solids) {
tmpF.subVectors(p, sol.c);
const d = tmpF.length();
const m = sol.r + 0.8;
if (d < m) {
const nx = d > 1e-4 ? tmpF.multiplyScalar(1 / d) : tmpF.set(0, 1, 0);
p.copy(sol.c).addScaledVector(nx, m);
}
}
dummy.position.copy(p);
tmpF.copy(p).add(v);
dummy.lookAt(tmpF);
dummy.updateMatrix();
fishMesh.setMatrixAt(i, dummy.matrix);
}
fishMesh.instanceMatrix.needsUpdate = true;
}
/* ---------- manta rays ---------- */
const mantaMat = new THREE.MeshLambertMaterial({ color: 0x2a5fa0, emissive: 0x0c3346 });
const mantas = [];
for (let k = 0; k < 3; k++) {
const g = new THREE.Group();
scene.add(g);
const body = new THREE.Mesh(new THREE.SphereGeometry(1, 18, 10), mantaMat);
body.scale.set(4.2, 0.22, 2.4);
g.add(body);
const tail = new THREE.Mesh(new THREE.CylinderGeometry(0.07, 0.02, 5, 5), mantaMat);
tail.rotation.x = Math.PI / 2;
tail.position.z = -5.6;
g.add(tail);
mantas.push({
g, body,
cx: rr(-22, 22), cz: rr(-22, 22), R: rr(24, 36),
sp: rr(0.05, 0.09) * (rnd() < 0.5 ? 1 : -1),
ph: rnd() * 6.28, y: rr(1, 6)
});
}
function updateMantas(T) {
for (const m of mantas) {
const a = m.ph + m.sp * T;
const sg = Math.sign(m.sp);
const px = m.cx + m.R * Math.cos(a);
const pz = m.cz + m.R * Math.sin(a);
const py = m.y + Math.sin(a * 2) * 1.0;
m.g.position.set(px, py, pz);
m.g.lookAt(px - Math.sin(a) * sg, py, pz + Math.cos(a) * sg);
m.body.scale.y = 0.22 + 0.04 * Math.sin(T * 3 + m.ph);
}
}
/* ---------- bubbles ---------- */
function dotTexture() {
const c = document.createElement("canvas");
c.width = c.height = 64;
const g = c.getContext("2d");
const gr = g.createRadialGradient(32, 32, 0, 32, 32, 32);
gr.addColorStop(0, "rgba(255,255,255,1)");
gr.addColorStop(0.5, "rgba(255,255,255,0.5)");
gr.addColorStop(1, "rgba(255,255,255,0)");
g.fillStyle = gr;
g.fillRect(0, 0, 64, 64);
return new THREE.CanvasTexture(c);
}
const BN = 240;
const bX = new Float32Array(BN), bY = new Float32Array(BN), bZ = new Float32Array(BN);
const bSp = new Float32Array(BN), bPh = new Float32Array(BN);
const bPos = new Float32Array(BN * 3);
for (let i = 0; i < BN; i++) {
bX[i] = rr(-65, 65); bZ[i] = rr(-65, 65); bY[i] = rr(FLOOR, SURF);
bSp[i] = rr(0.6, 1.6); bPh[i] = rnd() * 6.28;
}
const bAttr = new THREE.BufferAttribute(bPos, 3);
const bGeo = new THREE.BufferGeometry();
bGeo.setAttribute("position", bAttr);
const bubbles = new THREE.Points(bGeo, new THREE.PointsMaterial({
color: 0xd6fff7, size: 0.7, map: dotTexture(), transparent: true, opacity: 0.6,
depthWrite: false, sizeAttenuation: true
}));
bubbles.frustumCulled = false;
scene.add(bubbles);
function updateBubbles(dt, T) {
for (let i = 0; i < BN; i++) {
bY[i] += bSp[i] * dt;
bX[i] += Math.sin(T * 1.4 + bPh[i]) * 0.4 * dt;
if (bY[i] > SURF - 0.5) bY[i] = floorH(bX[i], bZ[i]) + 0.5;
bPos[i * 3] = bX[i]; bPos[i * 3 + 1] = bY[i]; bPos[i * 3 + 2] = bZ[i];
}
bAttr.needsUpdate = true;
}
/* ---------- finalize solid positions in world space ---------- */
scene.updateMatrixWorld(true);
for (const s of solids) s.c.copy(s.parent.localToWorld(s.local.clone()));
/* ---------- player ---------- */
const START_POS = new THREE.Vector3(0, -5, 44);
const player = { pos: START_POS.clone(), yaw: 0, pitch: -0.08, speed: 0 };
const MAX_SPEED = 7.5;
const PR = 0.9; // swimmer radius
const fwd = new THREE.Vector3();
const collTmp = new THREE.Vector3();
function forward() {
const cp = Math.cos(player.pitch);
return fwd.set(-Math.sin(player.yaw) * cp, Math.sin(player.pitch), -Math.cos(player.yaw) * cp);
}
function resolveCollisions(p) {
let hit = false;
for (let pass = 0; pass < 2; pass++) {
for (const s of solids) {
collTmp.subVectors(p, s.c);
const d = collTmp.length();
const m = s.r + PR;
if (d < m) {
const nx = d > 1e-4 ? collTmp.multiplyScalar(1 / d) : collTmp.set(0, 1, 0);
p.copy(s.c).addScaledVector(nx, m);
hit = true;
}
}
}
return hit;
}
function playerUpdate(dt) {
if (holding) player.speed += (MAX_SPEED - player.speed) * Math.min(1, dt * 2.2);
else player.speed *= Math.exp(-dt * 1.6);
forward();
player.pos.addScaledVector(fwd, player.speed * dt);
if (resolveCollisions(player.pos)) player.speed *= 0.2;
const fy = floorH(player.pos.x, player.pos.z) + 1.2;
if (player.pos.y < fy) player.pos.y = fy;
if (player.pos.y > SURF - 1.5) player.pos.y = SURF - 1.5;
const r = Math.hypot(player.pos.x, player.pos.z);
if (r > 88) { const k = 88 / r; player.pos.x *= k; player.pos.z *= k; }
}
/* ---------- hands-free demo (#demo): a 20 s loop around the garden ---------- */
const demoCurve = new THREE.CatmullRomCurve3(
Array.from({ length: 8 }, (_, k) => {
const a = (k / 8) * Math.PI * 2;
const r = 62 + (k % 2 ? -4 : 3);
return new THREE.Vector3(Math.cos(a) * r, [5, 2, 6, 3, 7, 2, 6, 4][k], Math.sin(a) * r);
}),
true
);
const demoLen = demoCurve.getLength();
function demoUpdate(T) {
const u = (T / 20) % 1;
const p = demoCurve.getPointAt(u);
const t = demoCurve.getTangentAt(u);
player.pos.copy(p);
player.yaw = Math.atan2(-t.x, -t.z);
player.pitch = Math.asin(Math.max(-1, Math.min(1, t.y)));
player.speed = demoLen / 20;
}
let demo = (location.hash || "").toLowerCase() === "#demo";
/* ---------- input (mouse and touch through pointer events) ---------- */
let holding = false, lastX = 0, lastY = 0;
let hintTimer = null;
function showHint(text, sticky) {
hintEl.textContent = text;
hintEl.style.opacity = "1";
clearTimeout(hintTimer);
if (!sticky) hintTimer = setTimeout(() => { hintEl.style.opacity = "0"; }, 6000);
}
function onDown(e) {
if (e.target && e.target.closest && e.target.closest("button")) return;
if (demo) { demo = false; player.speed = 0; }
holding = true;
lastX = e.clientX; lastY = e.clientY;
showHint("Press and hold to glide ahead. Drag to turn.", false);
}
function onMove(e) {
if (!holding) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY;
player.yaw -= dx * 0.0055;
player.pitch = Math.max(-1.1, Math.min(1.1, player.pitch - dy * 0.0045));
}
function onUp() { holding = false; }
window.addEventListener("pointerdown", onDown);
window.addEventListener("pointermove", onMove);
window.addEventListener("pointerup", onUp);
window.addEventListener("pointercancel", onUp);
resetBtn.addEventListener("pointerdown", (e) => e.stopPropagation());
resetBtn.addEventListener("click", () => {
demo = false;
holding = false;
player.pos.copy(START_POS);
player.yaw = 0; player.pitch = -0.08; player.speed = 0;
camReady = false;
showHint("Press and hold to glide ahead. Drag to turn.", false);
});
/* ---------- camera ---------- */
const camTmp = new THREE.Vector3(), lookTmp = new THREE.Vector3();
let camReady = false;
function updateCamera(dt) {
forward();
camTmp.copy(player.pos).addScaledVector(fwd, -6.5);
camTmp.y += 2.2;
const cfy = floorH(camTmp.x, camTmp.z) + 0.6;
if (camTmp.y < cfy) camTmp.y = cfy;
if (!camReady) { camera.position.copy(camTmp); camReady = true; }
else camera.position.lerp(camTmp, 1 - Math.exp(-dt * 7));
lookTmp.copy(player.pos).addScaledVector(fwd, 9);
camera.lookAt(lookTmp);
}
/* ---------- animation of kelp, beams and caustics ---------- */
function updateKelp(T) {
for (const k of kelps) {
const n = k.chain.length;
for (let i = 0; i < n; i++) {
const f = (i + 1) / n;
const g = k.chain[i];
g.rotation.z = Math.sin(T * 0.9 + k.ph + i * 0.35) * 0.05 * (0.3 + f);
g.rotation.x = Math.cos(T * 0.7 + k.ph2 + i * 0.3) * 0.04 * (0.3 + f);
}
}
}
function updateBeams(T) {
for (const m of beams) {
m.material.opacity = 0.05 + 0.035 * (0.5 + 0.5 * Math.sin(T * 0.5 + m.userData.ph));
}
}
/* ---------- main loop ---------- */
window.addEventListener("resize", () => {
const w = window.innerWidth, h = window.innerHeight;
renderer.setSize(w, h, false);
camera.aspect = w / h;
camera.updateProjectionMatrix();
});
if (demo) showHint("Hands-free demo. Touch or click to take over.", true);
else showHint("Press and hold to glide ahead. Drag to turn.", false);
let last = performance.now();
let T = 0;
function frame(now) {
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
T += dt;
if (demo) demoUpdate(T);
else playerUpdate(dt);
updateCamera(dt);
updateFish(dt);
updateMantas(T);
updateKelp(T);
updateBubbles(dt, T);
updateBeams(T);
causticSand.offset.set(T * 0.01, T * 0.006);
causticSurf.offset.set(-T * 0.008, T * 0.012);
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
