Back to the app
The code
Swim the Kelp Garden by GPT-6.1 Sol
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
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">
<meta name="theme-color" content="#073d49"><title>Stillwater — An underwater garden</title>
<style>
*{box-sizing:border-box}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#062d3c;color:#e7f1df;font-family:Arial,Helvetica,sans-serif}canvas{display:block;width:100%;height:100%;touch-action:none}button{font:inherit;color:inherit;cursor:pointer;touch-action:manipulation}#veil{position:fixed;inset:0;pointer-events:none;background:linear-gradient(180deg,rgba(1,27,36,.28),transparent 26%,transparent 65%,rgba(2,28,36,.65));box-shadow:inset 0 0 150px #062e3b55}.top{position:fixed;top:38px;left:42px;right:42px;display:flex;justify-content:space-between;align-items:flex-start;pointer-events:none}.brand{display:flex;gap:14px;align-items:center}.mark{width:36px;height:44px;position:relative}.mark i{position:absolute;border:1.5px solid #dcbd7d;border-radius:80% 0 80% 0;width:12px;height:28px;transform:rotate(-23deg);bottom:5px;left:8px}.mark i:nth-child(2){left:20px;height:37px;transform:rotate(14deg)}.brand-name{letter-spacing:6px;font-size:18px;font-weight:500}.eyebrow{font-size:9px;letter-spacing:3px;color:#c2d8cf;margin-top:9px}.status{display:flex;align-items:center;gap:12px;background:#072f3d66;border:1px solid #b1d7c325;border-radius:30px;padding:13px 19px;font-size:10px;letter-spacing:2px}.dot{width:5px;height:5px;border-radius:50%;background:#a8d5ac;box-shadow:0 0 10px #b2dbac}.left-note{position:fixed;left:44px;top:50%;transform:translateY(-50%);display:flex;gap:17px;align-items:center;pointer-events:none}.rule{width:1px;height:84px;background:linear-gradient(transparent,#d4ce9c88,transparent)}.left-note span{writing-mode:vertical-rl;transform:rotate(180deg);font-size:9px;letter-spacing:4px;color:#d0e0cc99}.intro{position:fixed;left:50%;bottom:151px;transform:translateX(-50%);text-align:center;pointer-events:none;width:90%;transition:opacity 1.6s}.intro small{font-size:9px;letter-spacing:4px;color:#e0c991}.intro h1{font-family:Georgia,'Times New Roman',serif;font-weight:400;font-size:clamp(32px,4.3vw,61px);letter-spacing:-1.5px;margin:12px 0}.intro p{font-size:12px;color:#c3d8d1;letter-spacing:.5px;line-height:1.8;margin:0}.bottom{position:fixed;bottom:34px;left:42px;right:42px;display:flex;align-items:flex-end;justify-content:space-between;gap:18px}.location{font-size:10px;letter-spacing:2px;line-height:2.2;color:#b2ccc5}.location b{color:#e0e8d7;font-weight:400}.controls{display:flex;align-items:center;gap:8px;padding:8px;border:1px solid #badcc130;background:#073543a6;backdrop-filter:blur(16px);border-radius:40px;box-shadow:0 10px 35px #001d2d30}.swim{display:flex;align-items:center;gap:12px;padding:10px 19px;font-size:11px;letter-spacing:1px;white-space:nowrap}.mouse{width:16px;height:23px;border:1px solid #c9d9c6;border-radius:10px;position:relative}.mouse:before{content:'';position:absolute;top:4px;left:6px;height:5px;border-left:2px solid #d5c18e}.round{height:44px;width:44px;display:grid;place-items:center;border-radius:50%;border:1px solid #c3d6bf30;background:#97bdab0c;transition:background .2s}.round:hover,.round[aria-pressed=true]{background:#aec9a328}.round svg{width:18px;height:18px;fill:none;stroke:currentColor;stroke-width:1.4}.depth{text-align:right;min-width:126px}.depth strong{font-family:Georgia,serif;font-weight:400;font-size:30px}.depth span{font-size:10px;letter-spacing:1px;color:#b6cfc6}.depth small{display:block;letter-spacing:3px;font-size:8px;margin-top:6px;color:#aec8c0}.reticle{position:fixed;left:50%;top:50%;width:4px;height:4px;border-radius:50%;border:1px solid #e5f0ce66;pointer-events:none;opacity:.5}.loading{position:fixed;inset:0;display:grid;place-content:center;text-align:center;background:#073843;z-index:10;transition:opacity 1s}.loading h2{font:normal 40px Georgia,serif}.loading p{font-size:10px;letter-spacing:3px;color:#cfccac}.hint{position:fixed;top:97px;right:43px;font-size:9px;letter-spacing:1px;color:#bad2c5;pointer-events:none}button:focus-visible{outline:2px solid #eed79f;outline-offset:4px}@media(max-width:650px){.top{top:24px;left:22px;right:22px}.brand-name{font-size:14px;letter-spacing:4px}.eyebrow{font-size:8px;letter-spacing:2px}.status{padding:11px;font-size:8px;letter-spacing:1px}.mark{transform:scale(.85);width:28px}.brand{gap:9px}.bottom{bottom:23px;left:18px;right:18px;justify-content:center;flex-wrap:wrap}.location{position:fixed;bottom:98px;left:24px;font-size:8px}.depth{position:fixed;bottom:100px;right:24px}.depth strong{font-size:24px}.depth small{font-size:7px}.controls{gap:6px;padding:6px}.swim{padding:9px 12px;font-size:10px}.intro{bottom:174px}.intro h1{margin:11px 0}.intro p{font-size:11px}.left-note{display:none}.hint{right:25px;top:83px;font-size:8px}}
</style>
</head>
<body>
<div id="loading" class="loading"><h2>Stillwater</h2><p>GROWING YOUR UNDERWATER GARDEN</p></div>
<div id="veil"></div>
<header class="top"><div class="brand"><div class="mark"><i></i><i></i></div><div><div class="brand-name">STILLWATER</div><div class="eyebrow">A LITTLE BELOW THE EVERYDAY</div></div></div><div class="status"><i class="dot"></i> FREE EXPLORATION</div></header>
<div class="hint" id="demoLabel">TAKE YOUR TIME. THERE'S NO DESTINATION.</div>
<div class="left-note"><div class="rule"></div><span>THE KELP GARDEN · 01</span></div>
<div class="reticle"></div>
<section class="intro" id="intro"><small>AN UNDERWATER SANCTUARY</small><h1>Let the current take you.</h1><p>Wander through the kelp. Follow a school of fish.<br>Stay a little longer.</p></section>
<footer class="bottom"><div class="location"><b>THE SUNLIT GARDEN</b><br>34° 02′ N / A MOMENT OF STILLNESS</div><div class="controls"><div class="swim"><span class="mouse"></span><span id="instruction">Hold & move to swim</span></div><button class="round" id="sound" title="Turn underwater sound on" aria-label="Turn underwater sound on" aria-pressed="false"><svg viewBox="0 0 24 24"><path d="M4 10h4l5-4v12l-5-4H4zM17 8q5 4 0 8"/><path id="mute" d="m3 3 18 18"/></svg></button><button class="round" id="home" title="Return to the garden entrance" aria-label="Return to the garden entrance"><svg viewBox="0 0 24 24"><path d="M5 10a7 7 0 1 1 0 5M5 5v5h5"/></svg></button></div><div class="depth"><strong id="depth">8.0</strong><span> m</span><small>BELOW THE SURFACE</small></div></footer>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js';
const scene=new THREE.Scene();scene.background=new THREE.Color('#106576');scene.fog=new THREE.FogExp2('#106576',.035);
const renderer=new THREE.WebGLRenderer({antialias:true,powerPreference:'high-performance'});renderer.setPixelRatio(Math.min(devicePixelRatio,1.65));renderer.setSize(innerWidth,innerHeight);renderer.outputColorSpace=THREE.SRGBColorSpace;renderer.toneMapping=THREE.ACESFilmicToneMapping;renderer.toneMappingExposure=1.16;document.body.prepend(renderer.domElement);
const camera=new THREE.PerspectiveCamera(64,innerWidth/innerHeight,.1,145);camera.position.set(0,5,22);camera.rotation.order='YXZ';
scene.add(new THREE.HemisphereLight(0xb8e1cf,0x164345,2));const sun=new THREE.DirectionalLight(0xffe4a3,2.8);sun.position.set(-12,25,8);scene.add(sun);
let seed=278;function rnd(a=0,b=1){seed=(seed*16807)%2147483647;return a+(b-a)*(seed/2147483647)}
const clock=new THREE.Clock(), colliders=[],kelpUniform={value:0};
const mat=(color,roughness=.88)=>new THREE.MeshStandardMaterial({color,roughness});const rockMats=[mat('#476c60'),mat('#3f615a'),mat('#657c65'),mat('#3c6260')],wood=mat('#605539'),woodLight=mat('#88724a'),darkWood=mat('#413f30'),gold=mat('#bd9148'),corals=[mat('#c68148'),mat('#e0a45c'),mat('#b8a656'),mat('#9e6a42')];
function mesh(geo,m,pos,scale,parent=scene){const o=new THREE.Mesh(geo,m);if(pos)o.position.set(...pos);if(scale)o.scale.set(...scale);parent.add(o);return o}
const sand=new THREE.ShaderMaterial({uniforms:{time:{value:0},fogColor:{value:new THREE.Color('#106576')}},vertexShader:`varying vec3 w; void main(){vec4 p=modelMatrix*vec4(position,1.);w=p.xyz;gl_Position=projectionMatrix*viewMatrix*p;}`,fragmentShader:`varying vec3 w;uniform float time;uniform vec3 fogColor;void main(){float r=sin(w.x*3.8+sin(w.z*.9)*1.1)*.024+sin(w.z*9.+w.x)*.012;float a=sin(w.x*.9+w.z*.7+sin(w.z*1.7-time*.45));float b=sin(w.z*1.1-w.x*.65+sin(w.x*1.8+time*.33));float c=pow(max(0.,1.-abs(a+b)*1.7),9.);vec3 col=vec3(.52,.57,.39)+r+c*vec3(.19,.22,.11);float d=distance(cameraPosition,w);float f=1.-exp(-.035*.035*d*d);gl_FragColor=vec4(mix(col,fogColor,f),1.);}`});mesh(new THREE.PlaneGeometry(230,230),sand,[0,-.16,0]).rotation.x=-Math.PI/2;
const sphereGeo=new THREE.IcosahedronGeometry(1,2);
function rock(x,y,z,sx,sy,sz,solid=true){const r=mesh(sphereGeo,rockMats[Math.floor(rnd(0,4))],[x,y,z],[sx,sy,sz]);r.rotation.set(rnd(-.2,.2),rnd(0,6.28),rnd(-.2,.2));if(solid)colliders.push({type:'ellipsoid',x,y,z,sx:sx+ .55,sy:sy+.55,sz:sz+.55});return r}
for(let i=0;i<65;i++){let x=rnd(-50,50),z=rnd(-65,24);if(Math.abs(x)<5&&z>0)continue;const s=rnd(.35,2.4);rock(x,s*.32,z,s,s*rnd(.5,1),s*rnd(.7,1.4));}
// A real opening under a weathered stone arch.
for(const side of [-1,1]){rock(9+side*3.3,1.8,-10,1.65,2.5,1.8);rock(9+side*2.8,4.4,-10,1.5,1.5,1.5)}rock(9,5.7,-10,2.8,1.05,1.55);rock(13,0,-12,2.5,.9,2.3);
// Long, tapered ribbons bend independently in a shared current.
const kelpMat=new THREE.ShaderMaterial({side:THREE.DoubleSide,uniforms:{time:kelpUniform},vertexShader:`uniform float time;attribute float phase;varying float h;varying vec3 w;varying float edge;void main(){vec3 p=position;h=uv.y;edge=uv.x;p.x+=sin(time*.56+phase+p.y*.48)*p.y*.115;p.z+=cos(time*.39+phase+p.y*.37)*p.y*.075;vec4 q=modelMatrix*vec4(p,1.);w=q.xyz;gl_Position=projectionMatrix*viewMatrix*q;}`,fragmentShader:`varying float h;varying vec3 w;varying float edge;void main(){vec3 c=mix(vec3(.105,.245,.085),vec3(.44,.54,.16),h);c+=pow(abs(edge-.5)*2.,3.)*.055;float vein=exp(-abs(edge-.5)*95.);c+=vein*.09;float f=1.-exp(-.035*.035*distance(cameraPosition,w)*distance(cameraPosition,w));gl_FragColor=vec4(mix(c,vec3(.063,.396,.463),f),1.);}`});
function ribbon(height,width,phase){const vs=[],uv=[],ind=[],ps=[];for(let j=0;j<=18;j++){const t=j/18,w=width*(Math.sin(Math.PI*Math.pow(t,.8))*.84+.035),bend=Math.sin(t*9+phase)*width*.6;for(let k=0;k<2;k++){vs.push((k?1:-1)*w+bend,t*height,Math.sin(t*12+phase)*.08);uv.push(k,t);ps.push(phase)}if(j<18){let a=j*2;ind.push(a,a+1,a+2,a+1,a+3,a+2)}}const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(vs,3));g.setAttribute('uv',new THREE.Float32BufferAttribute(uv,2));g.setAttribute('phase',new THREE.Float32BufferAttribute(ps,1));g.setIndex(ind);g.computeVertexNormals();return g}
for(let i=0;i<170;i++){let x=rnd(-47,47),z=rnd(-63,22);if((Math.abs(x)<4.5&&z> -10)||(x>4&&x<14&&z>-15&&z<-5)||(x<-4&&x>-17&&z<-13&&z>-27))continue;const h=rnd(3,12),p=rnd(0,6.3);for(let j=0;j<2;j++){const k=mesh(ribbon(h*rnd(.75,1.15),rnd(.22,.48),p+j),kelpMat,[x+rnd(-.3,.3),0,z+rnd(-.3,.3)]);k.rotation.y=rnd(0,6.3);}if(i%4===0)rock(x,.05,z,.5,.25,.5,false)}
// Branching warm coral, small sea fans, and low garden plants.
function branch(a,b,r,m,parent){const d=new THREE.Vector3().subVectors(b,a);const o=mesh(new THREE.CylinderGeometry(r*.63,r,d.length(),5),m,null,null,parent);o.position.copy(a).add(b).multiplyScalar(.5);o.quaternion.setFromUnitVectors(new THREE.Vector3(0,1,0),d.normalize());}
for(let i=0;i<50;i++){let x=rnd(-34,34),z=rnd(-47,18);if(Math.abs(x)<3&&z>0)continue;const g=new THREE.Group();g.position.set(x,0,z);scene.add(g);let m=corals[i%4],h=rnd(.45,1.3);branch(new THREE.Vector3(),new THREE.Vector3(0,h,0),.09,m,g);for(let j=0;j<5;j++){let a=new THREE.Vector3(0,h*(.25+j*.13),0),b=new THREE.Vector3(Math.sin(j*2.4)*h*.65,a.y+h*.42,Math.cos(j*2.4)*h*.4);branch(a,b,.055,m,g);branch(b,b.clone().add(new THREE.Vector3(.14,h*.23,.1)),.034,m,g)}}
// Sunken timber ship, its ribs and deck have actual volume and collision bounds.
const ship=new THREE.Group();ship.position.set(-10,.65,-21);ship.rotation.y=-.33;ship.rotation.z=-.11;scene.add(ship);
for(let side of [-1,1])for(let j=0;j<7;j++){const y=j*.26;const w=1.3+Math.sin(j/7*Math.PI/2)*.9;const plank=mesh(new THREE.BoxGeometry(.16,.225,10-j*.18),j%3===0?woodLight:wood,[side*w,y,0],null,ship);plank.rotation.z=side*-.13;}
mesh(new THREE.BoxGeometry(4.1,.22,9),darkWood,[0,1.1,0],null,ship);
for(let j=0;j<17;j++){const z=-4.3+j*.53;mesh(new THREE.BoxGeometry(4.2,.1,.46),j%4===0?woodLight:wood,[0,1.28,z],null,ship)}
for(let z of [-5,5]){const end=mesh(new THREE.BoxGeometry(3.8,1.7,.22),wood,[0,.9,z],null,ship);end.rotation.x=z<0?-.35:.35;}
for(let z of [-3.9,-2,0,2,3.9])for(let side of [-1,1]){mesh(new THREE.BoxGeometry(.18,2.1,.23),woodLight,[side*2.05,.9,z],null,ship)}
mesh(new THREE.BoxGeometry(2.6,1.6,2.1),wood,[0,2.05,3],null,ship);mesh(new THREE.BoxGeometry(2.9,.16,2.4),darkWood,[0,2.9,3],null,ship);mesh(new THREE.BoxGeometry(.8,.65,.05),mat('#183d3d'),[0,2.25,1.92],null,ship);
for(let z of [-1.8,2]){mesh(new THREE.CylinderGeometry(.12,.18,7,9),woodLight,[0,4.5,z],null,ship);const yard=mesh(new THREE.CylinderGeometry(.07,.07,4.5,7),wood,[0,6.2,z],null,ship);yard.rotation.z=Math.PI/2;}
const sailM=new THREE.MeshStandardMaterial({color:'#9aab80',roughness:1,side:THREE.DoubleSide,transparent:true,opacity:.78});const sailG=new THREE.BufferGeometry();sailG.setAttribute('position',new THREE.Float32BufferAttribute([-2,6.1,-1.8,2,6.1,-1.8,1.5,3.6,-1.35,.6,3.9,-1.25,.2,3.3,-1.2,-.2,4,-1.25,-1.7,3.8,-1.4],3));sailG.setIndex([0,1,3,1,2,3,0,3,5,3,4,5,0,5,6]);sailG.computeVertexNormals();mesh(sailG,sailM,null,null,ship);
for(const x of [-1.8,1.8]){branch(new THREE.Vector3(x,1.5,-4),new THREE.Vector3(0,7.9,-1.8),.018,darkWood,ship)}
ship.updateMatrixWorld(true);const hullBox=new THREE.Box3().setFromObject(ship);colliders.push({type:'box',box:new THREE.Box3(new THREE.Vector3(-13.9,-.5,-27.1),new THREE.Vector3(-6.2,3.6,-14.8))});for(let z of [-1.8,2]){const p=new THREE.Vector3(0,4.5,z).applyMatrix4(ship.matrixWorld);colliders.push({type:'ellipsoid',x:p.x,y:p.y,z:p.z,sx:.75,sy:4.1,sz:.75})}
// Cohesive schools: bodies and tails are instanced, animated along shared flow paths.
const fishBody=new THREE.SphereGeometry(1,9,6),tailG=new THREE.ConeGeometry(1,1,3);tailG.rotateZ(-Math.PI/2);const schools=[];const dummy=new THREE.Object3D();
for(let s=0;s<4;s++){const n=s===0?65:45,m=mat(s%2?'#d1a550':'#81b8ac',.45),body=new THREE.InstancedMesh(fishBody,m,n),tails=new THREE.InstancedMesh(tailG,m,n);scene.add(body,tails);let offsets=[];for(let j=0;j<n;j++)offsets.push(new THREE.Vector3(rnd(-4,4),rnd(-1.2,1.2),rnd(-2.2,2.2)));schools.push({body,tails,offsets,s,n});}
// Mantas: swept wings undulate in silence.
const rays=[];const rayM=mat('#315c60',.7);for(let j=0;j<3;j++){let vs=[],inds=[];for(let i=0;i<=20;i++){const x=(i/20*2-1)*2.4,w=Math.pow(1-Math.abs(x)/2.6,.8);vs.push(x,0,-w*.8+Math.abs(x)*.18,x,0,w*.9+Math.abs(x)*.42);if(i<20){let a=i*2;inds.push(a,a+1,a+2,a+1,a+3,a+2)}}const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(vs,3));g.setIndex(inds);g.computeVertexNormals();const group=new THREE.Group();const wings=mesh(g,new THREE.MeshStandardMaterial({color:'#446e6c',roughness:.75,side:THREE.DoubleSide}),null,null,group);mesh(new THREE.SphereGeometry(1,14,8),rayM,[0,0,.05],[.45,.18,1.1],group);const tail=mesh(new THREE.ConeGeometry(.055,3,6),rayM,[0,0,2.1],null,group);tail.rotation.x=Math.PI/2;scene.add(group);rays.push({group,wings,base:vs.slice(),j})}
// Dust motes and rising bubbles provide depth without textures.
const particles=650,pos=new Float32Array(particles*3);for(let i=0;i<particles;i++){pos[i*3]=rnd(-48,48);pos[i*3+1]=rnd(0,15);pos[i*3+2]=rnd(-65,30)}const pg=new THREE.BufferGeometry();pg.setAttribute('position',new THREE.BufferAttribute(pos,3));scene.add(new THREE.Points(pg,new THREE.PointsMaterial({color:'#c6ddba',size:.045,transparent:true,opacity:.45,depthWrite:false})));
const bubbles=[];const bubbleM=new THREE.MeshPhysicalMaterial({color:'#a7d9c5',metalness:.05,roughness:.1,transparent:true,opacity:.26});for(let i=0;i<36;i++){const b=mesh(new THREE.SphereGeometry(rnd(.035,.10),7,5),bubbleM,[rnd(-18,18),rnd(0,14),rnd(-27,14)]);bubbles.push(b)}
const beamM=new THREE.MeshBasicMaterial({color:'#c7e4ac',transparent:true,opacity:.024,depthWrite:false,blending:THREE.AdditiveBlending,side:THREE.DoubleSide});for(let i=0;i<11;i++){const beam=mesh(new THREE.CylinderGeometry(.22,rnd(1.3,3),23,24,1,true),beamM,[rnd(-28,25),8,rnd(-40,15)]);beam.rotation.z=-.27;beam.rotation.x=.14}
const surface=new THREE.ShaderMaterial({side:THREE.DoubleSide,transparent:true,depthWrite:false,uniforms:{time:{value:0}},vertexShader:`varying vec2 v;uniform float time;void main(){v=position.xy;vec3 p=position;p.z+=sin(p.x*.5+time*.6)*.12+sin(p.y*.6-time*.5)*.12;gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.);}`,fragmentShader:`varying vec2 v;uniform float time;void main(){float r=pow(max(0.,sin(v.x*.8+sin(v.y*.6+time*.4))*sin(v.y*.7-time*.25)),4.);gl_FragColor=vec4(mix(vec3(.19,.57,.56),vec3(.64,.78,.61),r),.38);}`});const surf=mesh(new THREE.PlaneGeometry(190,190,70,70),surface,[0,15,0]);surf.rotation.x=Math.PI/2;
let yaw=0,pitch=-.055,held=false,prevX=0,prevY=0,vel=0,demo=location.hash==='#demo',demoStart=0,started=false;const intro=document.getElementById('intro'),label=document.getElementById('demoLabel');if('ontouchstart'in window)document.getElementById('instruction').textContent='Hold to glide · drag to turn';
function wake(){if(!started){started=true;intro.style.opacity='0'}if(demo){demo=false;label.textContent="TAKE YOUR TIME. THERE'S NO DESTINATION."}}
const canvas=renderer.domElement;canvas.addEventListener('pointerdown',e=>{wake();held=true;prevX=e.clientX;prevY=e.clientY;canvas.setPointerCapture(e.pointerId)});canvas.addEventListener('pointermove',e=>{if(!held)return;yaw-=(e.clientX-prevX)*.0032;pitch-=(e.clientY-prevY)*.0028;pitch=THREE.MathUtils.clamp(pitch,-.85,.85);prevX=e.clientX;prevY=e.clientY});for(let ev of ['pointerup','pointercancel','lostpointercapture'])canvas.addEventListener(ev,()=>held=false);canvas.addEventListener('contextmenu',e=>e.preventDefault());document.addEventListener('pointerdown',e=>{if(e.target!==canvas&&demo)wake()});window.addEventListener('blur',()=>held=false);
function resolve(p){p.y=THREE.MathUtils.clamp(p.y,1.05,13.6);p.x=THREE.MathUtils.clamp(p.x,-57,57);p.z=THREE.MathUtils.clamp(p.z,-70,37);for(let c of colliders){if(c.type==='ellipsoid'){let dx=(p.x-c.x)/c.sx,dy=(p.y-c.y)/c.sy,dz=(p.z-c.z)/c.sz,d=Math.sqrt(dx*dx+dy*dy+dz*dz);if(d<1){if(d<.001){dx=1;d=1}p.set(c.x+dx/d*c.sx,c.y+dy/d*c.sy,c.z+dz/d*c.sz)}}else{let b=c.box,min=b.min,max=b.max,r=.55;if(p.x>min.x-r&&p.x<max.x+r&&p.y>min.y-r&&p.y<max.y+r&&p.z>min.z-r&&p.z<max.z+r){const ds=[p.x-min.x+r,max.x+r-p.x,p.y-min.y+r,max.y+r-p.y,p.z-min.z+r,max.z+r-p.z],k=ds.indexOf(Math.min(...ds));if(k===0)p.x=min.x-r;if(k===1)p.x=max.x+r;if(k===2)p.y=min.y-r;if(k===3)p.y=max.y+r;if(k===4)p.z=min.z-r;if(k===5)p.z=max.z+r}}}p.y=Math.max(1.05,p.y);return p}
document.getElementById('home').onclick=()=>{wake();camera.position.set(0,5,22);yaw=0;pitch=-.055;vel=0;held=false;intro.style.opacity='1';setTimeout(()=>{if(started)intro.style.opacity='0'},4500)};
let audioCtx,audioGain,audioOn=false;document.getElementById('sound').onclick=async()=>{wake();try{if(!audioCtx){audioCtx=new(window.AudioContext||window.webkitAudioContext)();const buffer=audioCtx.createBuffer(1,audioCtx.sampleRate*4,audioCtx.sampleRate),data=buffer.getChannelData(0);let last=0;for(let i=0;i<data.length;i++){last=(last+Math.random()*.04-.02)*.985;data[i]=last}let source=audioCtx.createBufferSource();source.buffer=buffer;source.loop=true;let filter=audioCtx.createBiquadFilter();filter.type='lowpass';filter.frequency.value=420;audioGain=audioCtx.createGain();audioGain.gain.value=0;source.connect(filter).connect(audioGain).connect(audioCtx.destination);source.start()}await audioCtx.resume();audioOn=!audioOn;audioGain.gain.setTargetAtTime(audioOn?.8:0,audioCtx.currentTime,.5);document.getElementById('sound').setAttribute('aria-pressed',audioOn);document.getElementById('sound').setAttribute('aria-label',audioOn?'Turn underwater sound off':'Turn underwater sound on');document.getElementById('mute').style.display=audioOn?'none':''}catch{document.getElementById('sound').title='Sound is unavailable in this browser'}};
const direction=new THREE.Vector3(),look=new THREE.Vector3();const demoPoints=[new THREE.Vector3(0,5,22),new THREE.Vector3(6,4.5,8),new THREE.Vector3(9,3,-4),new THREE.Vector3(9,3,-13),new THREE.Vector3(1,5,-17),new THREE.Vector3(-3,6,-16),new THREE.Vector3(-2,7,-3),new THREE.Vector3(0,5,22)];const path=new THREE.CatmullRomCurve3(demoPoints,true,'catmullrom',.35);
function animate(){requestAnimationFrame(animate);const dt=Math.min(clock.getDelta(),.04),t=clock.elapsedTime;kelpUniform.value=t;sand.uniforms.time.value=t;surface.uniforms.time.value=t;
for(let school of schools){let s=school.s,a=t*.12+s*1.7,cx=Math.sin(a)*(s===0?10:18),cz=-14+Math.cos(a)*10-s*5,cy=5+s*1.4+Math.sin(t*.2+s)*.7;for(let i=0;i<school.n;i++){const o=school.offsets[i],ang=a+Math.PI/2;let x=cx+o.x*Math.cos(a)-o.z*Math.sin(a),z=cz+o.x*Math.sin(a)+o.z*Math.cos(a),y=cy+o.y+Math.sin(t*2+i)*.055;dummy.position.set(x,y,z);dummy.rotation.set(0,-a,0);dummy.scale.set(.29,.10,.085);dummy.updateMatrix();school.body.setMatrixAt(i,dummy.matrix);dummy.position.x-=Math.cos(a)*.29;dummy.position.z-=Math.sin(a)*.29;dummy.rotation.y=-a+Math.sin(t*8+i)*.35;dummy.scale.set(.12,.13,.065);dummy.updateMatrix();school.tails.setMatrixAt(i,dummy.matrix)}school.body.instanceMatrix.needsUpdate=true;school.tails.instanceMatrix.needsUpdate=true}
for(let ray of rays){let a=t*.065+ray.j*1.8;ray.group.position.set(Math.sin(a)*20,9+Math.sin(a*2+ray.j)*1.1,-18+Math.cos(a)*17);ray.group.rotation.y=Math.PI/2-a;ray.group.rotation.z=Math.sin(t*.55+ray.j)*.06;let arr=ray.wings.geometry.attributes.position.array;for(let i=0;i<arr.length;i+=3)arr[i+1]=Math.sin(t*1.35+ray.j+Math.abs(ray.base[i])*.6)*Math.pow(Math.abs(ray.base[i])/2.4,1.5)*.65;ray.wings.geometry.attributes.position.needsUpdate=true;ray.wings.geometry.computeVertexNormals()}
for(let i=0;i<bubbles.length;i++){let b=bubbles[i];b.position.y+=dt*(.25+(i%5)*.05);b.position.x+=Math.sin(t+i)*dt*.055;if(b.position.y>14.8)b.position.y=.1}
if(demo){label.textContent='GUIDED DRIFT · TOUCH TO EXPLORE';const u=(t%22)/22;path.getPointAt(u,camera.position);resolve(camera.position);path.getPointAt((u+.045)%1,look);if(u>.55&&u<.78)look.lerp(new THREE.Vector3(-10,4,-21),.68);camera.lookAt(look);yaw=camera.rotation.y;pitch=camera.rotation.x;if(t>4)intro.style.opacity='0'}else{camera.rotation.set(pitch,yaw,0);vel=THREE.MathUtils.damp(vel,held?3.25:0,held?2:3.8,dt);if(vel>.001){camera.getWorldDirection(direction);const steps=Math.max(1,Math.ceil(vel*dt/.15));for(let i=0;i<steps;i++){camera.position.addScaledVector(direction,vel*dt/steps);resolve(camera.position)}}}
document.getElementById('depth').textContent=(15-camera.position.y).toFixed(1);renderer.render(scene,camera)}
window.addEventListener('resize',()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);renderer.setPixelRatio(Math.min(devicePixelRatio,1.65))});
const loading=document.getElementById('loading');loading.style.opacity='0';setTimeout(()=>loading.remove(),1100);animate();
</script>
<script>window.addEventListener('error',function(e){if(e.message&&/WebGL|Failed to fetch|import|THREE/i.test(e.message)){var el=document.getElementById('loading');if(el){el.style.opacity='1';el.innerHTML='<h2>A quiet moment, interrupted.</h2><p>Please reload with WebGL and an internet connection available.</p>';}}});</script>
</body>
</html>
