Skip to content
VibeCodeBench
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.)29.9 KB · 100 lines · loads cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js
Download
<!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="#073b48">
<title>Stillwater — An underwater garden</title>
<style>
*{box-sizing:border-box}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#074451;color:#e5e7cb;font-family:Arial,Helvetica,sans-serif}body{touch-action:none;-webkit-user-select:none;user-select:none}canvas{display:block;width:100%;height:100%;touch-action:none}button{font:inherit;color:inherit;cursor:pointer;-webkit-tap-highlight-color:transparent}.veil{position:fixed;inset:0;pointer-events:none;background:radial-gradient(ellipse at 50% 35%,transparent 35%,rgba(0,22,35,.33) 100%);z-index:2}.hud{position:fixed;inset:0;z-index:3;pointer-events:none;padding:38px 44px;display:flex;flex-direction:column;justify-content:space-between}.top{display:flex;justify-content:space-between;align-items:flex-start}.brand{display:flex;gap:13px;align-items:center}.mark{width:32px;height:40px;opacity:.9}.brand-name{font-family:Georgia,'Times New Roman',serif;font-size:29px;letter-spacing:-1px;font-weight:normal;line-height:1}.eyebrow{font-size:9px;letter-spacing:2.8px;text-transform:uppercase;opacity:.65;margin-top:10px}.tools{display:flex;gap:10px;pointer-events:auto}.icon-btn{border:1px solid #d7e6cf39;background:#073d4b55;width:46px;height:46px;border-radius:50%;display:grid;place-items:center;backdrop-filter:blur(12px);transition:background .2s}.icon-btn:hover,.icon-btn[aria-pressed=true]{background:#b4cba32a}.icon-btn svg{width:19px;height:19px;fill:none;stroke:currentColor;stroke-width:1.4;stroke-linecap:round;stroke-linejoin:round}.bottom{display:grid;grid-template-columns:1fr auto 1fr;align-items:end;gap:20px}.place .kicker{display:flex;align-items:center;gap:9px;color:#d8c58a;letter-spacing:2.2px;font-size:9px;text-transform:uppercase}.tiny-dot{width:5px;height:5px;border-radius:50%;background:#d3c17b;box-shadow:0 0 10px #d6cf9d66}.place h1{font-family:Georgia,'Times New Roman',serif;font-size:34px;font-weight:normal;letter-spacing:-.8px;margin:12px 0 8px}.place p{font-size:11px;letter-spacing:.4px;opacity:.65;margin:0;line-height:1.8}.instruction{display:flex;flex-direction:column;align-items:center;gap:13px;padding-bottom:3px;transition:opacity .6s}.hold-icon{border:1px solid #e0e9cf55;border-radius:30px;width:27px;height:39px;display:flex;justify-content:center;padding-top:8px;position:relative}.hold-icon:before{content:'';width:3px;height:7px;border-radius:3px;background:#e7e8cf;animation:drift 2.8s infinite}.instruction span{font-size:10px;letter-spacing:1.4px;white-space:nowrap;opacity:.8}.depth{justify-self:end;display:flex;align-items:center;gap:17px;padding-bottom:3px}.depth-label{font-size:8px;letter-spacing:2px;opacity:.6;text-align:right;margin-bottom:7px}.depth-value{font-family:Georgia,'Times New Roman',serif;font-size:30px;line-height:1}.depth-value small{font-family:Arial,sans-serif;font-size:10px;opacity:.7;margin-left:3px}.ruler{width:18px;height:62px;border-right:1px solid #d6e2c650;background:repeating-linear-gradient(to bottom,#d6e2c680 0 1px,transparent 1px 10px);background-size:7px 100%;background-position:right;background-repeat:no;position:relative}.ruler:after{content:'';position:absolute;right:-4px;top:var(--depth,30px);width:7px;height:7px;border-radius:50%;background:#d8c88d;box-shadow:0 0 9px #d8c88d66}.demo-badge{position:absolute;top:120px;left:46px;font-size:9px;letter-spacing:1.8px;text-transform:uppercase;display:none;align-items:center;gap:9px;color:#e0d39b}.demo-badge.on{display:flex}.demo-badge i{width:5px;height:5px;border-radius:50%;background:currentColor;animation:breathe 2s infinite}.compass{position:absolute;left:50%;top:44px;transform:translateX(-50%);width:124px;text-align:center;opacity:.55}.compass .ticks{height:9px;background:repeating-linear-gradient(90deg,#d9e3cb80 0 1px,transparent 1px 15px);mask-image:linear-gradient(90deg,transparent,#000 25%,#000 75%,transparent)}.compass span{font-size:8px;letter-spacing:3px;display:block;margin-bottom:8px}.crosshair{position:absolute;left:50%;top:50%;width:5px;height:5px;background:#e2dfb46b;border-radius:50%;opacity:0;transition:opacity .4s;box-shadow:0 0 0 5px #e2dfb40a}body.swimming .crosshair{opacity:1}body.swimming .instruction{opacity:.25}.help{position:absolute;right:44px;top:99px;width:290px;padding:25px;background:#083340e8;border:1px solid #c9ddbc33;backdrop-filter:blur(20px);border-radius:3px;pointer-events:auto;display:none}.help.open{display:block}.help h2{font-family:Georgia,serif;font-size:24px;font-weight:normal;margin:0 0 15px}.help p{font-size:12px;color:#c4d4c7;line-height:1.8;margin:9px 0}.help .rule{height:1px;background:#c9ddbc22;margin:18px 0}.help button{padding:12px 16px;background:#b4c48c22;border:1px solid #d4ddbd55;border-radius:3px;min-height:44px;width:100%;font-size:11px;letter-spacing:1px}.loading{position:fixed;inset:0;z-index:5;display:grid;place-content:center;gap:14px;text-align:center;background:#083f4b;transition:opacity .9s;pointer-events:none;font-family:Georgia,serif;font-size:27px}.loading small{font-family:Arial,sans-serif;font-size:9px;letter-spacing:3px;color:#b7c8b5}.loading.done{opacity:0}.sound-status{position:absolute;top:98px;right:102px;font-size:9px;letter-spacing:1px;opacity:0;transition:opacity .3s}.sound-status.show{opacity:.8}@keyframes drift{0%,100%{transform:translateY(0);opacity:.5}50%{transform:translateY(6px);opacity:1}}@keyframes breathe{50%{opacity:.3}}@media(max-width:700px){.hud{padding:25px 23px 30px}.brand-name{font-size:25px}.eyebrow{font-size:7px;letter-spacing:2px}.brand{gap:9px}.mark{width:26px}.icon-btn{width:44px;height:44px}.tools{gap:7px}.compass{display:none}.bottom{grid-template-columns:1fr auto;gap:15px}.place h1{font-size:28px;margin-top:10px}.place p{font-size:10px;max-width:210px}.place .kicker{font-size:8px;letter-spacing:1.6px}.instruction{position:absolute;left:50%;bottom:148px;transform:translateX(-50%);gap:10px;width:100%}.instruction span{font-size:9px}.hold-icon{width:23px;height:33px}.depth{gap:10px}.depth-value{font-size:25px}.depth-label{font-size:7px}.help{right:23px;top:83px;max-width:calc(100vw - 46px)}.demo-badge{left:25px;top:102px;font-size:8px}.sound-status{right:76px;top:83px}}@media(prefers-reduced-motion:reduce){.hold-icon:before,.demo-badge i{animation:none}}
</style>
</head>
<body>
<div id="loading" class="loading">A quieter world.<small>ENTERING STILLWATER</small></div>
<div class="veil"></div>
<main class="hud">
 <header class="top">
  <div class="brand"><svg class="mark" viewBox="0 0 32 42" fill="none" stroke="currentColor" stroke-width="1.2"><path d="M16 40V4M16 31C4 30 2 23 4 18c8 0 12 6 12 13Zm0-9C27 21 30 14 28 9c-7 0-12 6-12 13Zm0-8C8 13 7 7 9 2c5 1 7 6 7 12Z"/></svg><div><div class="brand-name">stillwater</div><div class="eyebrow">An underwater garden</div></div></div>
  <div class="tools"><button class="icon-btn" id="sound" aria-label="Enable underwater sound" aria-pressed="false" title="Underwater sound"><svg viewBox="0 0 24 24"><path d="M10 5 5 9H2v6h3l5 4V5Z"/><path id="soundWaves" d="m16 9 6 6m0-6-6 6"/></svg></button><button class="icon-btn" id="helpButton" aria-label="Swimming instructions" aria-expanded="false" title="Swimming instructions"><svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/><path d="M12 11v6m0-10v.1"/></svg></button></div>
 </header>
 <div class="compass"><span id="direction">N</span><div class="ticks"></div></div>
 <div id="demoBadge" class="demo-badge"><i></i>Drifting through · touch to explore</div>
 <div class="sound-status" id="soundStatus">Ocean sound on</div>
 <aside class="help" id="help"><h2>Take the slow way.</h2><p>Press and hold anywhere in the water to swim forward. Drag gently to turn, look up, or dive.</p><p>Release to float. There is no hurry, and no destination you have to reach.</p><div class="rule"></div><button id="tour">Take a 20-second drift ↗</button></aside>
 <div class="crosshair"></div>
 <footer class="bottom"><div class="place"><div class="kicker"><i class="tiny-dot"></i>01 / Sunlit shallows</div><h1>The kelp garden</h1><p>Follow the light. See what finds you.</p></div><div class="instruction" id="instruction"><div class="hold-icon"></div><span id="controlText">HOLD TO SWIM &nbsp;·&nbsp; DRAG TO LOOK</span></div><div class="depth"><div><div class="depth-label">BELOW THE SURFACE</div><div class="depth-value"><span id="depth">12.4</span><small>m</small></div></div><div class="ruler" id="ruler"></div></div></footer>
</main>
<script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
<script>
(()=>{'use strict';
const loading=document.getElementById('loading');
if(!window.THREE){loading.innerHTML='The garden is out of reach.<small>PLEASE RECONNECT AND RELOAD</small>';return;}
try{
const T=THREE,scene=new T.Scene();scene.background=new T.Color('#126176');scene.fog=new T.FogExp2('#126176',.028);
const renderer=new T.WebGLRenderer({antialias:true,powerPreference:'high-performance'});renderer.setPixelRatio(Math.min(window.devicePixelRatio||1,1.7));renderer.setSize(innerWidth,innerHeight);renderer.outputColorSpace=T.SRGBColorSpace;renderer.toneMapping=T.ACESFilmicToneMapping;renderer.toneMappingExposure=1.18;document.body.prepend(renderer.domElement);
const camera=new T.PerspectiveCamera(61,innerWidth/innerHeight,.1,180);camera.position.set(0,5.6,19);camera.rotation.order='YXZ';let yaw=-.04,pitch=-.045;
scene.add(new T.HemisphereLight('#99d8c3','#263e36',2));const sun=new T.DirectionalLight('#ffe3a3',3.2);sun.position.set(-16,30,8);scene.add(sun);const fill=new T.DirectionalLight('#1fadc1',1.2);fill.position.set(20,10,-40);scene.add(fill);
let seed=137;function rand(){seed=(seed*1664525+1013904223)>>>0;return seed/4294967296;}const range=(a,b)=>a+(b-a)*rand();
const time={value:0},colliders=[];
function mat(color,extra={}){return new T.MeshStandardMaterial({color,roughness:.86,...extra});}
function mesh(g,m,parent=scene){const o=new T.Mesh(g,m);parent.add(o);return o;}
function canvasTexture(w,h,draw){const c=document.createElement('canvas');c.width=w;c.height=h;draw(c.getContext('2d'),w,h);const t=new T.CanvasTexture(c);t.colorSpace=T.SRGBColorSpace;return t;}
const sandTex=canvasTexture(512,512,(c,w,h)=>{c.fillStyle='#7e8157';c.fillRect(0,0,w,h);for(let i=0;i<15000;i++){const a=rand();c.fillStyle=a>.5?'#b1a36a':'#5e7255';c.globalAlpha=.12+rand()*.15;c.fillRect(rand()*w,rand()*h,1.3,1.3);}c.globalAlpha=1;for(let y=-40;y<560;y+=22){c.beginPath();for(let x=-10;x<530;x+=6){const yy=y+Math.sin(x*.026+y)*7+Math.sin(x*.06)*2;x===-10?c.moveTo(x,yy):c.lineTo(x,yy);}c.strokeStyle='#ded5a321';c.lineWidth=2;c.stroke();}});sandTex.wrapS=sandTex.wrapT=T.RepeatWrapping;sandTex.repeat.set(28,28);
const caustics=canvasTexture(512,512,(c,w,h)=>{c.fillStyle='#143733';c.fillRect(0,0,w,h);for(let j=-4;j<15;j++){for(let k=0;k<2;k++){c.beginPath();for(let x=-30;x<550;x+=4){const y=j*47+Math.sin(x*.025+j*.8)*23+Math.sin(x*.057+j)*9;k?c.lineTo(y,x):c.lineTo(x,y);}c.strokeStyle='#d3d6a2';c.shadowColor='#d6e5a5';c.shadowBlur=9;c.globalAlpha=.58;c.lineWidth=1.8;c.stroke();}}});caustics.wrapS=caustics.wrapT=T.RepeatWrapping;caustics.repeat.set(25,25);
const floorG=new T.PlaneGeometry(220,240,100,100);floorG.rotateX(-Math.PI/2);const fp=floorG.attributes.position;for(let i=0;i<fp.count;i++){const x=fp.getX(i),z=fp.getZ(i);fp.setY(i,-.18+Math.sin(x*.38+z*.13)*.12+Math.cos(z*.4)*.06);}floorG.computeVertexNormals();mesh(floorG,mat('#c9bb83',{map:sandTex,emissive:'#8fa66a',emissiveMap:caustics,emissiveIntensity:.55})).position.z=-30;
const surfaceMat=new T.ShaderMaterial({transparent:true,side:T.DoubleSide,depthWrite:false,uniforms:{uTime:time},vertexShader:'varying vec3 vP;uniform float uTime;void main(){vec3 p=position;p.z+=sin(p.x*.25+uTime*.45)*.24+cos(p.y*.27+uTime*.3)*.18;vP=p;gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.);}',fragmentShader:'varying vec3 vP;uniform float uTime;void main(){float a=sin(vP.x*.7+sin(vP.y*.3+uTime*.2)*2.+uTime*.3)*sin(vP.y*.48-uTime*.2);gl_FragColor=vec4(mix(vec3(.06,.39,.43),vec3(.58,.75,.56),pow(abs(a),9.)),.52);}'});const surface=mesh(new T.PlaneGeometry(240,240,55,55),surfaceMat);surface.rotation.x=Math.PI/2;surface.position.set(0,18,-30);
const beamMat=new T.ShaderMaterial({transparent:true,depthWrite:false,side:T.DoubleSide,blending:T.AdditiveBlending,uniforms:{uTime:time},vertexShader:'varying vec2 vUv;void main(){vUv=uv;gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.);}',fragmentShader:'varying vec2 vUv;uniform float uTime;void main(){float side=pow(max(0.,1.-abs(vUv.x*2.-1.)),2.8);float end=smoothstep(0.,.25,vUv.y)*(1.-smoothstep(.8,1.,vUv.y));float shimmer=.85+.15*sin(vUv.y*23.+uTime*.35);gl_FragColor=vec4(.65,.81,.52,side*end*.045*shimmer);}'});
for(let i=0;i<12;i++){const beam=new T.Group();beam.position.set(-25+i*5,10,-8-(i%4)*13);beam.rotation.z=-.35;for(let j=0;j<2;j++){const p=mesh(new T.PlaneGeometry(2.5+(i%3),31),beamMat,beam);p.rotation.y=j*Math.PI/2;}scene.add(beam);}
const rockMats=['#4d6962','#596c5c','#66715b','#3d5d58'].map(c=>mat(c));const rockG=new T.IcosahedronGeometry(1,2);const rp=rockG.attributes.position;for(let i=0;i<rp.count;i++){const v=new T.Vector3().fromBufferAttribute(rp,i);v.multiplyScalar(1+.07*Math.sin(v.x*9+v.z*7)*Math.cos(v.y*8));rp.setXYZ(i,v.x,v.y,v.z);}rockG.computeVertexNormals();
function rock(x,y,z,sx,sy,sz){const r=mesh(rockG,rockMats[Math.floor(rand()*4)]);r.position.set(x,y,z);r.scale.set(sx,sy,sz);colliders.push({kind:'ellipsoid',x,y,z,sx:sx*1.09,sy:sy*1.09,sz:sz*1.09});return r;}
function arch(x,z,scale=1){for(const side of [-1,1]){rock(x+side*4.2*scale,1.3*scale,z,1.65*scale,2.4*scale,1.9*scale);rock(x+side*4.05*scale,3.5*scale,z,1.45*scale,1.75*scale,1.65*scale);}for(let i=0;i<9;i++){const a=i/8*Math.PI;rock(x+Math.cos(a)*3.9*scale,(3.7+Math.sin(a)*3.2)*scale,z+Math.sin(a*3)*.13,1.2*scale,1.1*scale,1.65*scale);}}
arch(-5,-13,1);arch(15,-61,1.2);
for(let i=0;i<52;i++){const side=i%2?-1:1,x=side*range(13,41),z=range(-90,22);rock(x,range(.1,.7),z,range(1.1,3.3),range(.6,2.3),range(1,3));}rock(-13,.4,8,4,2,3);rock(17,.1,4,3.2,1.7,3);
const kelpMats=['#6e8b32','#9a9a3c','#417c40','#b3a34c'].map(c=>{const m=mat(c,{side:T.DoubleSide,roughness:.8});m.onBeforeCompile=s=>{s.uniforms.uTime=time;s.vertexShader='uniform float uTime;\n'+s.vertexShader;s.vertexShader=s.vertexShader.replace('#include <begin_vertex>','#include <begin_vertex>\nvec3 root=(modelMatrix*vec4(0.,0.,0.,1.)).xyz;float bend=position.y*.10;transformed.x+=sin(uTime*.62+root.x*.22+position.y*.38)*bend;transformed.z+=cos(uTime*.48+root.z*.17+position.y*.31)*bend*.56;');};return m;});
function kelpGeometry(h){const pos=[],idx=[];function ribbon(points,widths){const start=pos.length/3;for(let i=0;i<points.length;i++){const p=points[i],w=widths[i];pos.push(p[0]-w,p[1],p[2],p[0]+w,p[1],p[2]);if(i){const a=start+(i-1)*2;idx.push(a,a+1,a+2,a+1,a+3,a+2);}}}let ps=[],ws=[];for(let j=0;j<=20;j++){const t=j/20;ps.push([Math.sin(t*9)*.13*t,t*h,Math.cos(t*7)*.13*t]);ws.push(.045*(1-t)+.008);}ribbon(ps,ws);for(let k=1;k<Math.floor(h*2);k++){const y=k*.5,side=k%2?-1:1,len=.7+(y/h)*1.2;ps=[];ws=[];for(let j=0;j<=9;j++){const t=j/9;ps.push([side*(t*len*.64)+Math.sin(y*2)*.1,y+t*len*.65,Math.sin(t*3+y)*.22]);ws.push(Math.sin(t*Math.PI)*(.14+y/h*.16));}ribbon(ps,ws);}const g=new T.BufferGeometry();g.setAttribute('position',new T.Float32BufferAttribute(pos,3));g.setIndex(idx);g.computeVertexNormals();return g;}
const kelpGs=Array.from({length:8},(_,i)=>kelpGeometry(3+i*1.25));
for(let i=0;i<210;i++){let x,z;if(i<44){x=(i%2?-1:1)*range(7,17);z=range(2,23);}else{x=range(-46,46);z=range(-92,10);if(Math.abs(x)<7)x+=x<0?-8:8;}const k=mesh(kelpGs[Math.floor(rand()*8)],kelpMats[Math.floor(rand()*4)]);k.position.set(x,-.15,z);k.rotation.y=range(-.6,.6);const s=range(.65,1.3);k.scale.set(s,s,s);}
const coralMats=['#c08b43','#b06435','#b7a05a','#547f59'].map(c=>mat(c));
function branch(a,b,r,m,parent){const d=new T.Vector3().subVectors(b,a),o=mesh(new T.CylinderGeometry(r*.65,r,d.length(),6),m,parent);o.position.copy(a).add(b).multiplyScalar(.5);o.quaternion.setFromUnitVectors(new T.Vector3(0,1,0),d.normalize());}
for(let c=0;c<33;c++){const group=new T.Group();group.position.set((c%2?-1:1)*range(9,30),.15,range(-64,14));scene.add(group);const m=coralMats[c%4],height=range(.5,1.4);for(let i=0;i<7;i++){const a=new T.Vector3(0,0,0),b=new T.Vector3(range(-.7,.7),height*range(.6,1.4),range(-.5,.5));branch(a,b,.055,m,group);for(let j=0;j<2;j++){const d=b.clone().multiplyScalar(.6),e=b.clone().add(new T.Vector3(range(-.4,.4),range(.15,.5),range(-.3,.3)));branch(d,e,.035,m,group);}}}
const woodTex=canvasTexture(256,256,(c,w,h)=>{c.fillStyle='#77623b';c.fillRect(0,0,w,h);for(let i=0;i<280;i++){c.strokeStyle=i%2?'#392f233b':'#c0a15a3b';c.lineWidth=range(.4,1.8);const y=rand()*h;c.beginPath();c.moveTo(0,y);c.bezierCurveTo(80,y+rand()*8,160,y-rand()*8,256,y);c.stroke();}});const wood=mat('#8d7544',{map:woodTex}),darkWood=mat('#514d32',{map:woodTex}),edgeWood=mat('#ab8d4a',{map:woodTex});
const ship=new T.Group();ship.position.set(12.8,.05,-27);ship.rotation.y=-.45;scene.add(ship);
function box(w,h,d,x,y,z,m=wood,parent=ship){const b=mesh(new T.BoxGeometry(w,h,d),m,parent);b.position.set(x,y,z);return b;}
for(const side of [-1,1])for(let row=0;row<6;row++){const pos=[],ind=[],level=row/6;for(let j=0;j<=24;j++){const zz=-7.5+j*15/24,shape=Math.pow(Math.max(0,Math.sin(j/24*Math.PI)),.46);for(const yy of [0,.34]){const w=(1.1+level*1.7)*shape;pos.push(side*w,.5+level*2.35+yy+Math.pow(Math.abs(zz)/7.5,4)*.9,zz);}if(j){const a=(j-1)*2;ind.push(a,a+1,a+2,a+1,a+3,a+2);}}const g=new T.BufferGeometry();g.setAttribute('position',new T.Float32BufferAttribute(pos,3));g.setIndex(ind);g.computeVertexNormals();const m=wood.clone();m.side=T.DoubleSide;mesh(g,m,ship);}
for(let i=0;i<25;i++){const z=-6.5+i*.54,w=4.5*Math.pow(Math.max(.01,Math.sin((z+7.5)/15*Math.PI)),.5);if(i<9||i>13)box(w,.15,.47,0,2.45,z,i%3?wood:darkWood);else{box(w*.26,.15,.47,-w*.36,2.45,z);box(w*.26,.15,.47,w*.36,2.45,z);}}
for(let i=0;i<12;i++){const z=-6.5+i*1.14,w=2.6*Math.pow(Math.max(.01,Math.sin((z+7.5)/15*Math.PI)),.46);for(const side of [-1,1]){const r=box(.13,1.9,.16,side*w*.91,2,z,darkWood);r.rotation.z=-side*.28;}}
box(2.7,1.1,2.8,0,3.05,4.4,darkWood);box(3.1,.16,3.1,0,3.65,4.4);box(1.5,.65,.04,0,3.1,2.96,mat('#233e39'));
const mast=mesh(new T.CylinderGeometry(.12,.23,10.8,9),wood,ship);mast.position.set(0,6.6,-.6);mast.rotation.z=-.09;const spar=box(7,.14,.16,.3,9.1,-.6,edgeWood);spar.rotation.z=-.09;
const sailG=new T.PlaneGeometry(5.6,4,14,10);const sp=sailG.attributes.position;for(let i=0;i<sp.count;i++){const x=sp.getX(i),y=sp.getY(i);sp.setZ(i,.45*Math.sin((x/5.6+.5)*Math.PI)+.13*Math.sin(y*3+x));if(y<-1.9)sp.setY(i,y+Math.abs(Math.sin(x*4))*.65);}sailG.computeVertexNormals();const sail=mesh(sailG,mat('#9f9860',{side:T.DoubleSide,transparent:true,opacity:.83}),ship);sail.position.set(.25,6.95,-.5);sail.rotation.y=.17;sail.rotation.z=-.09;
function rope(a,b){const curve=new T.CatmullRomCurve3([a,a.clone().lerp(b,.5).add(new T.Vector3(0,-.25,0)),b]);mesh(new T.TubeGeometry(curve,16,.023,4,false),darkWood,ship);}rope(new T.Vector3(.5,11,-.6),new T.Vector3(0,3,7));rope(new T.Vector3(.5,11,-.6),new T.Vector3(0,3,-7));rope(new T.Vector3(-3.1,9.4,-.6),new T.Vector3(-2.3,2.8,1));
colliders.push({kind:'box',x:12.8,z:-27,yaw:-.45,hx:3.05,hz:8.2,low:0,high:4.05});colliders.push({kind:'ellipsoid',x:13.1,y:7.6,z:-27.6,sx:.7,sy:4.5,sz:.7});
const fishBody=new T.SphereGeometry(1,10,7),tailG=new T.BufferGeometry();tailG.setAttribute('position',new T.Float32BufferAttribute([-.25,0,0,-.69,.29,0,-.61,-.29,0],3));tailG.computeVertexNormals();const fishMats=['#d8b65a','#c68b41','#7fa791','#c1bb79'].map(c=>mat(c,{metalness:.17,roughness:.48,side:T.DoubleSide}));const schools=[];
for(let s=0;s<5;s++){const root=new T.Group();scene.add(root);const data={root,cx:[-5,5,-13,17,0][s],cz:[-7,-30,-40,-12,-58][s],cy:[6.5,8,4.8,5.2,10][s],rx:range(4,8),rz:range(3,6),speed:range(.1,.17),phase:rand()*6.28,fish:[]};for(let j=0;j<19;j++){const f=new T.Group(),body=mesh(fishBody,fishMats[s%4],f);body.scale.set(.34,.13,.085);const tail=mesh(tailG,fishMats[s%4],f);tail.position.x=-.1;const eye=mesh(new T.SphereGeometry(.022,5,4),darkWood,f);eye.position.set(.2,.035,.079);f.position.set(range(-3,3),range(-1,1),range(-1.4,1.4));const scale=range(.65,1.2);f.scale.setScalar(scale);root.add(f);data.fish.push({f,tail,base:f.position.clone(),phase:rand()*6});}schools.push(data);}
const mantas=[];for(let k=0;k<3;k++){const g=new T.BufferGeometry(),positions=[],indices=[];for(let i=0;i<=24;i++){const x=(i/24*2-1)*2.6,a=Math.abs(x)/2.6;for(let j=0;j<=6;j++){const t=j/6;positions.push(x,0,-.85+Math.pow(a,1.4)*1.3+t*(1.8*(1-a)+.12));if(i&&j){const q=(i-1)*7+j-1;indices.push(q,q+7,q+1,q+1,q+7,q+8);}}}g.setAttribute('position',new T.Float32BufferAttribute(positions,3));g.setIndex(indices);g.computeVertexNormals();const root=new T.Group();mesh(g,mat(k===1?'#507e77':'#315c60',{side:T.DoubleSide,metalness:.12,roughness:.75}),root);const body=mesh(new T.SphereGeometry(1,12,8),mat('#4b756a'),root);body.scale.set(.34,.14,.8);const tailCurve=new T.CatmullRomCurve3([new T.Vector3(0,0,.6),new T.Vector3(0,-.1,1.8),new T.Vector3(.22,-.16,3.1)]);mesh(new T.TubeGeometry(tailCurve,18,.028,5,false),mat('#3d665f'),root);scene.add(root);mantas.push({root,g,base:Float32Array.from(positions),phase:k*2.1});}
const bubbleTex=canvasTexture(64,64,(c)=>{const g=c.createRadialGradient(27,23,2,32,32,28);g.addColorStop(0,'#c6e6cc00');g.addColorStop(.68,'#b1e2cc00');g.addColorStop(.87,'#d6edbf22');g.addColorStop(.94,'#cee3c980');g.addColorStop(1,'#b6d6c600');c.fillStyle=g;c.fillRect(0,0,64,64);c.fillStyle='#e8eadb99';c.beginPath();c.ellipse(24,19,4,2,-.6,0,Math.PI*2);c.fill();});const bubbleMat=new T.SpriteMaterial({map:bubbleTex,transparent:true,opacity:.55,depthWrite:false,fog:true});const bubbles=[];for(let i=0;i<85;i++){const s=new T.Sprite(bubbleMat),x=range(-24,24),z=range(-55,20),size=range(.035,.14);s.position.set(x,range(.2,18),z);s.scale.setScalar(size*2);scene.add(s);bubbles.push({s,x,z,speed:range(.22,.6),phase:rand()*6});}
const dustG=new T.BufferGeometry(),dust=[];for(let i=0;i<1100;i++)dust.push(range(-65,65),range(0,19),range(-100,40));dustG.setAttribute('position',new T.Float32BufferAttribute(dust,3));const dustObj=new T.Points(dustG,new T.PointsMaterial({color:'#c5d2ab',size:.045,transparent:true,opacity:.38,depthWrite:false,sizeAttenuation:true}));scene.add(dustObj);
let holding=false,activePointer=null,lastX=0,lastY=0,demo=location.hash==='#demo',demoStart=0,lastTime=0,elapsed=0,velocity=0;
const demoBadge=document.getElementById('demoBadge'),help=document.getElementById('help'),helpButton=document.getElementById('helpButton');demoBadge.classList.toggle('on',demo);
function endDemo(){if(!demo)return;demo=false;demoBadge.classList.remove('on');const e=new T.Euler().setFromQuaternion(camera.quaternion,'YXZ');yaw=e.y;pitch=e.x;}
function release(){holding=false;activePointer=null;document.body.classList.remove('swimming');}
window.addEventListener('pointerdown',e=>{endDemo();if(e.target.closest('button')||e.target.closest('.help'))return;if(activePointer!==null)return;holding=true;activePointer=e.pointerId;lastX=e.clientX;lastY=e.clientY;renderer.domElement.setPointerCapture?.(e.pointerId);document.body.classList.add('swimming');help.classList.remove('open');helpButton.setAttribute('aria-expanded','false');});
window.addEventListener('pointermove',e=>{if(!holding||e.pointerId!==activePointer)return;const scale=e.pointerType==='touch'?.004:.0032;yaw-=(e.clientX-lastX)*scale;pitch-=(e.clientY-lastY)*scale;pitch=Math.max(-1.2,Math.min(1.15,pitch));lastX=e.clientX;lastY=e.clientY;});window.addEventListener('pointerup',e=>{if(e.pointerId===activePointer)release();});window.addEventListener('pointercancel',release);window.addEventListener('blur',release);renderer.domElement.addEventListener('contextmenu',e=>e.preventDefault());
helpButton.onclick=()=>{const open=help.classList.toggle('open');helpButton.setAttribute('aria-expanded',String(open));};document.getElementById('tour').onclick=()=>{release();demo=true;demoStart=elapsed;demoBadge.classList.add('on');help.classList.remove('open');helpButton.setAttribute('aria-expanded','false');};
const radius=.48;function resolve(p){p.x=Math.max(-51,Math.min(51,p.x));p.z=Math.max(-91,Math.min(31,p.z));p.y=Math.max(.85,Math.min(16.7,p.y));for(let pass=0;pass<3;pass++)for(const c of colliders){if(c.kind==='ellipsoid'){let dx=p.x-c.x,dy=p.y-c.y,dz=p.z-c.z;const sx=c.sx+radius,sy=c.sy+radius,sz=c.sz+radius,dist=Math.sqrt(dx*dx/(sx*sx)+dy*dy/(sy*sy)+dz*dz/(sz*sz));if(dist<1){if(dist<.0001){p.y=c.y+sy+.01;continue;}const k=1.002/dist;p.set(c.x+dx*k,c.y+dy*k,c.z+dz*k);}}else{const cos=Math.cos(c.yaw),sin=Math.sin(c.yaw),dx=p.x-c.x,dz=p.z-c.z,lx=cos*dx-sin*dz,lz=sin*dx+cos*dz,hx=c.hx+radius,hz=c.hz+radius;if(Math.abs(lx)<hx&&Math.abs(lz)<hz&&p.y>c.low-radius&&p.y<c.high+radius){const ox=hx-Math.abs(lx),oz=hz-Math.abs(lz),oy=c.high+radius-p.y;let xx=lx,zz=lz;if(oy<ox&&oy<oz)p.y=c.high+radius+.01;else if(ox<oz)xx=(lx<0?-1:1)*(hx+.01);else zz=(lz<0?-1:1)*(hz+.01);p.x=c.x+cos*xx+sin*zz;p.z=c.z-sin*xx+cos*zz;}}}return p;}
const step=new T.Vector3(),forward=new T.Vector3();function move(delta){const n=Math.max(1,Math.ceil(delta.length()/.18));step.copy(delta).divideScalar(n);for(let i=0;i<n;i++){camera.position.add(step);resolve(camera.position);}}
let audio=null,audioGain=null,soundOn=false;const soundButton=document.getElementById('sound');soundButton.onclick=()=>{try{if(!audio){const AC=window.AudioContext||window.webkitAudioContext;if(!AC)return;audio=new AC();audioGain=audio.createGain();audioGain.gain.value=0;audioGain.connect(audio.destination);const len=audio.sampleRate*6,buffer=audio.createBuffer(1,len,audio.sampleRate),d=buffer.getChannelData(0);let b=0;for(let i=0;i<len;i++){b=(b+(rand()*2-1)*.018)/1.018;d[i]=b*4;}const source=audio.createBufferSource();source.buffer=buffer;source.loop=true;const low=audio.createBiquadFilter();low.type='lowpass';low.frequency.value=340;source.connect(low);low.connect(audioGain);source.start();const osc=audio.createOscillator(),g=audio.createGain();osc.frequency.value=58;g.gain.value=.035;osc.connect(g);g.connect(audioGain);osc.start();}audio.resume();soundOn=!soundOn;audioGain.gain.setTargetAtTime(soundOn?.42:0,audio.currentTime,.7);soundButton.setAttribute('aria-pressed',String(soundOn));soundButton.setAttribute('aria-label',soundOn?'Mute underwater sound':'Enable underwater sound');document.getElementById('soundWaves').setAttribute('d',soundOn?'M15 8c3 2 3 6 0 8m3-11c5 4 5 10 0 14':'m16 9 6 6m0-6-6 6');const status=document.getElementById('soundStatus');status.textContent=soundOn?'Ocean sound on':'Ocean sound off';status.classList.add('show');setTimeout(()=>status.classList.remove('show'),2200);}catch(e){document.getElementById('soundStatus').textContent='Sound unavailable';document.getElementById('soundStatus').classList.add('show');}};
const depthNode=document.getElementById('depth'),ruler=document.getElementById('ruler'),direction=document.getElementById('direction');let frame=0;
function animate(ms){requestAnimationFrame(animate);const dt=Math.min((ms-lastTime)/1000||.016,.045);lastTime=ms;elapsed+=dt;time.value=elapsed;caustics.offset.set(elapsed*.005,elapsed*.0035);
for(const s of schools){const a=elapsed*s.speed+s.phase;s.root.position.set(s.cx+Math.cos(a)*s.rx,s.cy+Math.sin(a*1.7)*.65,s.cz+Math.sin(a)*s.rz);s.root.rotation.y=Math.atan2(-Math.cos(a)*s.rz,-Math.sin(a)*s.rx);s.root.rotation.z=Math.sin(a*2)*.035;for(const f of s.fish){f.f.position.y=f.base.y+Math.sin(elapsed*1.5+f.phase)*.09;f.f.position.x=f.base.x+Math.sin(elapsed*.9+f.phase)*.12;f.tail.rotation.y=Math.sin(elapsed*8+f.phase)*.35;}}
for(const m of mantas){const a=elapsed*.065+m.phase;m.root.position.set(Math.sin(a)*23,10+Math.sin(a*2)*1.5,-26+Math.cos(a)*18);m.root.rotation.y=Math.atan2(-Math.cos(a)*23,Math.sin(a)*18);m.root.rotation.z=Math.sin(a)*.09;const p=m.g.attributes.position;for(let i=0;i<p.count;i++){const x=m.base[i*3],z=m.base[i*3+2];p.setY(i,Math.sin(elapsed*1.1+m.phase+Math.abs(x)*.65)*Math.pow(Math.abs(x)/2.6,1.5)*.64+.04*Math.cos(z*2));}p.needsUpdate=true;m.g.computeVertexNormals();}
for(const b of bubbles){b.s.position.y+=dt*b.speed;if(b.s.position.y>18)b.s.position.y=.2;b.s.position.x=b.x+Math.sin(elapsed*.4+b.phase+b.s.position.y)*.24;b.s.position.z=b.z+Math.cos(elapsed*.3+b.phase)*.18;}dustObj.position.x=Math.sin(elapsed*.055)*.65;
if(demo){const a=((elapsed-demoStart)%20)/20*Math.PI*2;const target=new T.Vector3(Math.sin(a)*21,7.6+Math.sin(a*2)*1.1,Math.cos(a)*23-16);const delta=target.sub(camera.position);delta.multiplyScalar(1-Math.exp(-dt*2));move(delta);const look=new T.Vector3(1+Math.sin(a*.8)*3,4.4,-23);camera.lookAt(look);velocity=0;}else{camera.rotation.set(pitch,yaw,0,'YXZ');velocity+=( (holding?3.15:0)-velocity)*(1-Math.exp(-dt*3));forward.set(0,0,-1).applyQuaternion(camera.quaternion);move(forward.multiplyScalar(velocity*dt));if(!holding){camera.position.y+=Math.sin(elapsed*.65)*dt*.016;resolve(camera.position);}}
if(frame++%8===0){const depth=18-camera.position.y;depthNode.textContent=depth.toFixed(1);ruler.style.setProperty('--depth',Math.min(57,depth/18*57)+'px');const dir=new T.Vector3(0,0,-1).applyQuaternion(camera.quaternion),angle=(Math.atan2(dir.x,-dir.z)*180/Math.PI+360)%360;direction.textContent=['N','NE','E','SE','S','SW','W','NW'][Math.round(angle/45)%8];}renderer.render(scene,camera);}
window.addEventListener('resize',()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);renderer.setPixelRatio(Math.min(devicePixelRatio||1,1.7));});document.addEventListener('visibilitychange',()=>{release();lastTime=performance.now();if(document.hidden&&audio)audio.suspend();else if(soundOn&&audio)audio.resume();});
renderer.domElement.addEventListener('webglcontextlost',e=>{e.preventDefault();loading.classList.remove('done');loading.innerHTML='The current has paused.<small>RELOAD TO RETURN TO THE GARDEN</small>';});
camera.rotation.set(pitch,yaw,0,'YXZ');renderer.render(scene,camera);loading.classList.add('done');setTimeout(()=>loading.style.display='none',1100);requestAnimationFrame(animate);
}catch(e){loading.classList.remove('done');loading.innerHTML='A little more light is needed.<small>THIS GARDEN REQUIRES WEBGL. PLEASE TRY ANOTHER BROWSER.</small>';}
})();
</script>
</body>
</html>