Back to the app
The code
Swim the Kelp Garden by Claude Opus 5.5
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">
<title>Kelp Garden</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#0b2e59;font-family:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;color:#e9f6f2;-webkit-user-select:none;user-select:none}
canvas{display:block;touch-action:none}
#ov{position:fixed;inset:0;display:flex;align-items:center;justify-content:center;background:rgba(6,30,56,.5);z-index:5;padding:16px;box-sizing:border-box}
#card{max-width:420px;text-align:center;background:rgba(10,50,80,.78);border:1px solid rgba(120,220,210,.35);border-radius:22px;padding:26px 22px}
h1{margin:0 0 8px;font-weight:600;color:#f3c75a;letter-spacing:.5px}
p{margin:8px 0;line-height:1.45;color:#cfeee8}
.big{font-size:20px;font-weight:600;padding:16px 36px;border-radius:999px;border:none;background:#f0a640;color:#10304a;margin-top:14px;min-height:58px;cursor:pointer}
#help{position:fixed;top:14px;right:14px;width:58px;height:58px;border-radius:50%;border:1px solid rgba(120,220,210,.5);background:rgba(10,50,80,.6);color:#f3c75a;font-size:26px;font-weight:700;z-index:4;cursor:pointer}
#hint{position:fixed;left:50%;bottom:22px;transform:translateX(-50%);background:rgba(10,50,80,.62);padding:10px 18px;border-radius:999px;font-size:15px;pointer-events:none;transition:opacity 1.2s;z-index:3;white-space:nowrap;opacity:0}
</style>
</head>
<body>
<div id="ov"><div id="card">
<h1>Kelp Garden</h1>
<p>Drift through swaying kelp, stone arches and an old sunken ship.</p>
<p><b>Touch:</b> press and hold to glide ahead, drag to turn.<br><b>Mouse:</b> hold the button and move the pointer.</p>
<button class="big" id="go">Start swimming</button>
</div></div>
<button id="help" aria-label="Help">?</button>
<div id="hint"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
(function(){
const V=THREE.Vector3, SURF=38, PR=1.0, TIME={value:0};
let seed=7331;
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 R=(a,b)=>a+(b-a)*rnd(), pick=a=>a[(rnd()*a.length)|0], clamp=(v,a,b)=>Math.max(a,Math.min(b,v));
function gH(x,z){const r=Math.hypot(x,z);return 1.6*Math.sin(x*0.05)*Math.cos(z*0.04)+0.8*Math.sin(x*0.13+z*0.09)+0.4*Math.sin(z*0.21-x*0.07)+Math.max(0,r-120)*0.25;}
const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(Math.min(window.devicePixelRatio||1,1.75));
renderer.setSize(innerWidth,innerHeight);
document.body.appendChild(renderer.domElement);
const cv=renderer.domElement;
const scene=new THREE.Scene();
const FOGC=new THREE.Color(0x0d4f73);
scene.fog=new THREE.FogExp2(FOGC,0.016);
scene.background=FOGC.clone();
const camera=new THREE.PerspectiveCamera(65,innerWidth/innerHeight,0.1,700);
const SUN=new V(0.35,1,0.2).normalize();
scene.add(new THREE.HemisphereLight(0x7fd6cf,0x0b2e59,0.9));
const dl=new THREE.DirectionalLight(0xffd690,0.9);dl.position.copy(SUN).multiplyScalar(50);scene.add(dl);
scene.add(new THREE.AmbientLight(0x1a6a80,0.35));
// background gradient
const sky=new THREE.Mesh(new THREE.SphereGeometry(500,32,16),new THREE.ShaderMaterial({side:THREE.BackSide,depthWrite:false,fog:false,uniforms:{uF:{value:FOGC}},
vertexShader:'varying vec3 vD;void main(){vD=normalize(position);gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}',
fragmentShader:'uniform vec3 uF;varying vec3 vD;void main(){float y=vD.y;vec3 c=y>0.0?mix(uF,vec3(0.16,0.55,0.62),smoothstep(0.05,0.8,y)):mix(uF,vec3(0.03,0.12,0.26),smoothstep(0.0,0.6,-y));gl_FragColor=vec4(c,1.0);}'}));
sky.renderOrder=-1;scene.add(sky);
const CAU=`float caustic(vec2 p,float t){vec2 q=p+vec2(sin(p.y*0.8+t*0.9),cos(p.x*0.8-t*0.7))*0.9;q+=vec2(sin(q.y*1.7-t*1.1),cos(q.x*1.5+t*0.8))*0.45;float a=abs(sin(q.x*1.3)+sin(q.y*1.3));float b=abs(sin((q.x+q.y)*0.9+t*0.5)+sin((q.x-q.y)*1.1-t*0.4));return pow(1.0-min(a,1.0),6.0)+pow(1.0-min(b,1.0),6.0);}`;
function SM(o){const u=THREE.UniformsUtils.merge([THREE.UniformsLib.fog,o.uniforms||{}]);u.uTime=TIME;return new THREE.ShaderMaterial(Object.assign({},o,{uniforms:u,fog:o.fog!==false}));}
function addCaustics(m,str){m.onBeforeCompile=s=>{s.uniforms.uTime=TIME;s.uniforms.uCs={value:str};
s.vertexShader='varying vec3 vWp;varying float vUp;\n'+s.vertexShader.replace('#include <worldpos_vertex>','#include <worldpos_vertex>\nvec4 wpp=vec4(transformed,1.0);\n#ifdef USE_INSTANCING\nwpp=instanceMatrix*wpp;\n#endif\nwpp=modelMatrix*wpp;vWp=wpp.xyz;vUp=normalize(mat3(modelMatrix)*objectNormal).y;');
s.fragmentShader='uniform float uTime;uniform float uCs;varying vec3 vWp;varying float vUp;\n'+CAU+'\n'+s.fragmentShader.replace('#include <fog_fragment>','gl_FragColor.rgb+=vec3(1.0,0.85,0.5)*caustic(vWp.xz*0.35,uTime*0.5)*clamp(vUp*0.8+0.2,0.0,1.0)*uCs;\n#include <fog_fragment>');};}
// sand floor
const fg=new THREE.PlaneGeometry(420,420,180,180);fg.rotateX(-Math.PI/2);
{const a=fg.attributes.position;for(let i=0;i<a.count;i++)a.setY(i,gH(a.getX(i),a.getZ(i)));fg.computeVertexNormals();}
scene.add(new THREE.Mesh(fg,SM({uniforms:{uSun:{value:SUN}},
vertexShader:`varying vec3 vW;varying vec3 vN;
#include <fog_pars_vertex>
void main(){vW=position;vN=normal;vec4 mvPosition=modelViewMatrix*vec4(position,1.0);gl_Position=projectionMatrix*mvPosition;
#include <fog_vertex>
}`,
fragmentShader:`uniform float uTime;uniform vec3 uSun;varying vec3 vW;varying vec3 vN;
${CAU}
#include <fog_pars_fragment>
void main(){vec3 n=normalize(vN);
float rip=sin(vW.x*1.3+sin(vW.z*0.35)*2.0+sin(vW.z*0.9)*0.4)*0.5+0.5;
float pt=sin(vW.x*0.07+1.0)*sin(vW.z*0.09)*0.5+0.5;
vec3 col=mix(vec3(0.40,0.34,0.20),vec3(0.66,0.55,0.31),rip*0.45+pt*0.55);
col=mix(col,vec3(0.18,0.42,0.40),smoothstep(0.65,1.0,pt)*0.4);
float diff=0.45+0.55*max(dot(n,uSun),0.0);
float c=caustic(vW.xz*0.35,uTime*0.5)+0.5*caustic(vW.xz*0.17+3.0,uTime*0.35);
col=col*diff*vec3(0.75,0.92,0.95)+vec3(1.0,0.85,0.5)*c*0.38;
gl_FragColor=vec4(col,1.0);
#include <fog_fragment>
}`})));
// rippling surface
const sg=new THREE.PlaneGeometry(700,700,140,140);sg.rotateX(-Math.PI/2);
const surf=new THREE.Mesh(sg,SM({uniforms:{uSun:{value:SUN}},transparent:true,side:THREE.DoubleSide,depthWrite:false,
vertexShader:`uniform float uTime;varying vec3 vW;
#include <fog_pars_vertex>
void main(){vec3 p=position;p.y+=sin(p.x*0.15+uTime*0.8)*0.5+sin(p.z*0.2+uTime*1.1)*0.35;vec4 w=modelMatrix*vec4(p,1.0);vW=w.xyz;vec4 mvPosition=viewMatrix*w;gl_Position=projectionMatrix*mvPosition;
#include <fog_vertex>
}`,
fragmentShader:`uniform float uTime;uniform vec3 uSun;varying vec3 vW;
${CAU}
#include <fog_pars_fragment>
void main(){float t=uTime;float r=sin(vW.x*0.15+t*0.8)*sin(vW.z*0.12-t*0.6)+0.5*sin((vW.x+vW.z)*0.3+t*1.4);
float c=caustic(vW.xz*0.12,t*0.6);
vec3 col=mix(vec3(0.14,0.52,0.60),vec3(0.52,0.84,0.80),r*0.25+0.5)+vec3(1.0,0.9,0.6)*c*0.35;
vec3 toS=normalize(vW-cameraPosition);col+=vec3(1.0,0.85,0.5)*pow(max(dot(toS,uSun),0.0),20.0)*1.3;
gl_FragColor=vec4(col,0.92);
#include <fog_fragment>
}`}));
surf.position.y=SURF;scene.add(surf);
// obstacles
const OB=[], _rv=new V(), _t=new V();
function addEll(c,ry,rx,rY,rz){OB.push({t:0,c,cos:Math.cos(ry),sin:Math.sin(ry),rx,ry:rY,rz});}
function addBox(mesh){mesh.updateMatrixWorld(true);mesh.geometry.computeBoundingBox();OB.push({t:1,m:mesh.matrixWorld.clone(),inv:mesh.matrixWorld.clone().invert(),b:mesh.geometry.boundingBox.clone()});}
function resolve(p,r){for(const o of OB){
if(o.t===0){const dx=p.x-o.c.x,dy=p.y-o.c.y,dz=p.z-o.c.z;const lx=dx*o.cos-dz*o.sin,lz=dx*o.sin+dz*o.cos;
const qx=lx/(o.rx+r),qy=dy/(o.ry+r),qz=lz/(o.rz+r);const d=Math.sqrt(qx*qx+qy*qy+qz*qz);
if(d<1&&d>1e-6){const s=1/d,nx=lx*s,ny=dy*s,nz=lz*s;p.set(o.c.x+nx*o.cos+nz*o.sin,o.c.y+ny,o.c.z-nx*o.sin+nz*o.cos);}}
else{_rv.copy(p).applyMatrix4(o.inv);const b=o.b;const cx=clamp(_rv.x,b.min.x,b.max.x),cy=clamp(_rv.y,b.min.y,b.max.y),cz=clamp(_rv.z,b.min.z,b.max.z);
const dx=_rv.x-cx,dy=_rv.y-cy,dz=_rv.z-cz;const l=Math.sqrt(dx*dx+dy*dy+dz*dz);
if(l<r){if(l>1e-5){_rv.set(cx+dx*r/l,cy+dy*r/l,cz+dz*r/l);}else{
const pen=[[_rv.x-b.min.x,0,-1],[b.max.x-_rv.x,0,1],[_rv.y-b.min.y,1,-1],[b.max.y-_rv.y,1,1],[_rv.z-b.min.z,2,-1],[b.max.z-_rv.z,2,1]];
pen.sort((a,c)=>a[0]-c[0]);const k=pen[0];const ax=['x','y','z'][k[1]];_rv[ax]=k[2]<0?b.min[ax]-r:b.max[ax]+r;}
p.copy(_rv.applyMatrix4(o.m));}}}}
function inside(p,r){_t.copy(p);resolve(_t,r);return _t.distanceToSquared(p)>1e-6;}
// rocks
const rockMats=[0x2d6670,0x1f4f66,0x3a7a6e].map(c=>{const m=new THREE.MeshStandardMaterial({color:c,roughness:0.95,flatShading:true});addCaustics(m,0.35);return m;});
function rockGeo(s){const g=new THREE.IcosahedronGeometry(1,2);const a=g.attributes.position,n=new V();
for(let i=0;i<a.count;i++){n.fromBufferAttribute(a,i).normalize();const d=1+0.22*Math.sin(n.x*3.1+s)*Math.sin(n.y*2.7+s*1.3)*Math.sin(n.z*3.3+s*0.7)+0.1*Math.sin(n.x*7.3+n.z*5.1+s)+0.06*Math.sin(n.y*11.0+s*2.0);a.setXYZ(i,n.x*d,n.y*d,n.z*d);}
g.computeVertexNormals();return g;}
const rocks=[];
function addRock(x,z,s,sy){const m=new THREE.Mesh(rockGeo(R(0,10)),pick(rockMats));const sx=s*R(0.8,1.3),syy=s*(sy||R(0.5,0.9)),sz=s*R(0.8,1.3);m.scale.set(sx,syy,sz);const ry=R(0,6.28);m.rotation.y=ry;const y=gH(x,z)-syy*0.25;m.position.set(x,y,z);scene.add(m);addEll(new V(x,y,z),ry,sx*1.1,syy*1.1,sz*1.1);rocks.push(new V(x,y+syy*0.7,z));return m;}
const arches=[];
function makeArch(x,z,ry,Rr,tube){const g=new THREE.TorusGeometry(Rr,tube,10,30,Math.PI);const a=g.attributes.position,v=new V(),w=new V();
for(let i=0;i<a.count;i++){v.fromBufferAttribute(a,i);const an=Math.atan2(v.y,v.x);w.set(Math.cos(an)*Rr,Math.sin(an)*Rr,0);v.sub(w);const n=1+0.3*Math.sin(v.x*1.3+an*5+x)*Math.sin(v.z*1.1+an*3)+0.12*Math.sin(an*17+v.y*2);v.multiplyScalar(n).add(w);a.setXYZ(i,v.x,v.y,v.z);}
g.computeVertexNormals();const m=new THREE.Mesh(g,rockMats[0]);const y=gH(x,z)-tube*0.6;m.position.set(x,y,z);m.rotation.y=ry;scene.add(m);
const c=Math.cos(ry),s=Math.sin(ry);
for(let k=0;k<=16;k++){const an=Math.PI*k/16,lx=Math.cos(an)*Rr,ly=Math.sin(an)*Rr;addEll(new V(x+lx*c,y+ly,z-lx*s),0,tube*1.3,tube*1.3,tube*1.3);}
for(const sd of[-1,1]){const lx=sd*(Rr+0.5);addRock(x+lx*c,z-lx*s,tube*1.7,0.7);}
const A={x,y,z,ry,Rr,tube};arches.push(A);return A;}
const archA=makeArch(0,-10,0,10,2.3);
makeArch(48,32,1.1,8,2);
makeArch(-62,42,-0.5,9,2.2);
makeArch(70,-55,0.4,7,1.8);
// sunken ship
const ship=new THREE.Group();const SX=-34,SZ=-52;ship.position.set(SX,gH(SX,SZ)-0.9,SZ);ship.rotation.set(0,0.6,0.16);scene.add(ship);
const wood=new THREE.MeshStandardMaterial({color:0x6b3d1d,roughness:0.9,flatShading:true});addCaustics(wood,0.3);
const wood2=new THREE.MeshStandardMaterial({color:0x8f5a2a,roughness:0.9});addCaustics(wood2,0.3);
const sh=new THREE.Shape();sh.moveTo(-8,3.4);sh.lineTo(-8.3,1.4);sh.quadraticCurveTo(-7,0,-4,0);sh.lineTo(5,0);sh.quadraticCurveTo(8.5,0.5,10.5,4.2);sh.lineTo(-8,3.4);
const hg=new THREE.ExtrudeGeometry(sh,{depth:4.6,bevelEnabled:true,bevelSize:0.4,bevelThickness:0.5,bevelSegments:2,steps:1});hg.translate(0,0,-2.3);
{const a=hg.attributes.position;for(let i=0;i<a.count;i++){const x=a.getX(i),y=a.getY(i);const f=0.3+0.7*Math.pow(clamp(y/3.6,0,1),0.6);const bw=1-0.85*Math.pow(clamp((x-4.5)/6.5,0,1),1.5);a.setZ(i,a.getZ(i)*f*bw);}hg.computeVertexNormals();}
const hull=new THREE.Mesh(hg,wood);ship.add(hull);
function part(geo,mat,x,y,z,rx,ry,rz,parent){const m=new THREE.Mesh(geo,mat);m.position.set(x,y,z);m.rotation.set(rx||0,ry||0,rz||0);(parent||ship).add(m);return m;}
part(new THREE.BoxGeometry(16.5,0.3,4.2),wood2,0.5,3.3,0);
for(let i=0;i<7;i++)part(new THREE.BoxGeometry(0.25,0.32,4.3),wood,-6+i*2.3,3.5,0);
const cabin=part(new THREE.BoxGeometry(4,2.4,3.8),wood,-5.6,4.5,0);
part(new THREE.BoxGeometry(4.6,0.3,4.3),wood2,-5.6,5.8,0);
part(new THREE.BoxGeometry(0.1,0.7,1.4),new THREE.MeshBasicMaterial({color:0xf3c75a}),-7.65,4.7,0);
const mast1=part(new THREE.CylinderGeometry(0.28,0.36,15,8),wood2,2,10.5,0,0,0,0.08);
part(new THREE.CylinderGeometry(0.14,0.14,8,6),wood2,0,5.5,0,Math.PI/2,0,0,mast1);
const sailG=new THREE.PlaneGeometry(6.5,5,8,6);{const a=sailG.attributes.position;for(let i=0;i<a.count;i++){const x=a.getX(i),y=a.getY(i);a.setZ(i,Math.sin((x/6.5+0.5)*Math.PI)*0.9*(0.6+0.4*(y/5+0.5))+0.15*Math.sin(x*2+y*3));}sailG.computeVertexNormals();}
part(sailG,new THREE.MeshStandardMaterial({color:0xc9a24a,roughness:1,side:THREE.DoubleSide,transparent:true,opacity:0.85}),0.5,2.8,0,0,Math.PI/2,0,mast1);
const mast2=part(new THREE.CylinderGeometry(0.3,0.36,5,8),wood2,-1.5,5.6,0,0,0,-0.1);
part(new THREE.CylinderGeometry(0.2,0.25,5,6),wood2,11.6,5.2,0,0,0,-1.15);
ship.updateMatrixWorld(true);
addBox(hull);addBox(cabin);addBox(mast1);addBox(mast2);
{const fx=SX+12,fz=SZ+9;const m=new THREE.Mesh(new THREE.CylinderGeometry(0.3,0.36,10,8),wood2);m.position.set(fx,gH(fx,fz)+0.2,fz);m.rotation.set(Math.PI/2,0.4,0.1);scene.add(m);addBox(m);
for(let i=0;i<3;i++){const bx=SX+R(6,11),bz=SZ+R(-12,-7);const b=new THREE.Mesh(new THREE.CylinderGeometry(0.6,0.6,1.4,10),wood);b.position.set(bx,gH(bx,bz)+0.5,bz);b.rotation.set(i===1?Math.PI/2:0,R(0,3),0);scene.add(b);addBox(b);}
const cx=SX+9,cz=SZ+14,chest=new THREE.Mesh(new THREE.BoxGeometry(1.6,1,1),wood);chest.position.set(cx,gH(cx,cz)+0.4,cz);chest.rotation.y=0.5;scene.add(chest);addBox(chest);
const lid=new THREE.Mesh(new THREE.BoxGeometry(1.6,0.12,1),new THREE.MeshBasicMaterial({color:0xf3c75a}));lid.position.y=0.56;chest.add(lid);}
// demo path (kept clear of rocks)
const gy=(x,z,o)=>new V(x,gH(x,z)+o,z);
const holeY=Math.max(archA.y+(archA.Rr-archA.tube)*0.5,gH(0,-10)+2.4);
const shipSide=ship.localToWorld(new V(0,6,11)), shipBow=ship.localToWorld(new V(17,8,6));
const curve=new THREE.CatmullRomCurve3([gy(4,34,6),gy(1,12,4.5),new V(0,holeY,-10),gy(-3,-30,5),shipSide,shipBow,gy(-24,-14,24),gy(-14,22,12)],true,'centripetal');
const pathPts=curve.getSpacedPoints(180);
function nearPath(x,z,d){for(const p of pathPts){const dx=p.x-x,dz=p.z-z;if(dx*dx+dz*dz<d*d)return true;}return false;}
function nearFeature(x,z,d){if(Math.hypot(x-SX,z-SZ)<16+d)return true;for(const a of arches)if(Math.hypot(x-a.x,z-a.z)<a.Rr+4+d)return true;return false;}
for(let i=0;i<30;i++){const an=R(0,6.28),rr=R(15,118),x=Math.cos(an)*rr,z=Math.sin(an)*rr;if(nearPath(x,z,10)||nearFeature(x,z,4))continue;
const n=2+((rnd()*4)|0);for(let k=0;k<n;k++){const px=x+R(-5,5),pz=z+R(-5,5),s=R(1.5,4.2);if(nearPath(px,pz,s+5))continue;addRock(px,pz,s,k===0&&rnd()<0.4?R(1.5,2.4):0);}}
// kelp
function buildKelp(list){const pos=[],aH=[],aP=[],aL=[],aU=[],idx=[],N=16;
for(const k of list){const by=gH(k.x,k.z)-0.3,w0=R(0.7,1.3);
for(let pl=0;pl<2;pl++){const a=k.rot+pl*Math.PI/2,ca=Math.cos(a),sa=Math.sin(a),base=pos.length/3;
for(let j=0;j<=N;j++){const f=j/N,w=w0*(0.25+0.75*Math.sin(Math.PI*Math.min(1,f*0.9+0.12)))*(0.85+0.15*Math.sin(j*1.9+k.ph)),y=by+k.h*f,cx=k.x+Math.sin(f*3+k.ph)*0.3,cz=k.z+Math.cos(f*2.5+k.ph)*0.3;
for(let s=-1;s<=1;s+=2){pos.push(cx+ca*w*0.5*s,y,cz+sa*w*0.5*s);aH.push(f);aP.push(k.ph);aL.push(k.h);aU.push(s);}
if(j<N){const b=base+j*2;idx.push(b,b+1,b+2,b+1,b+3,b+2);}}}}
const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(pos,3));
g.setAttribute('aH',new THREE.Float32BufferAttribute(aH,1));g.setAttribute('aP',new THREE.Float32BufferAttribute(aP,1));g.setAttribute('aL',new THREE.Float32BufferAttribute(aL,1));g.setAttribute('aU',new THREE.Float32BufferAttribute(aU,1));g.setIndex(idx);return g;}
const kl=[];
const groves=[[-8,30],[10,20],[14,42],[-16,8]];for(let i=0;i<14;i++)groves.push([R(-110,110),R(-110,110)]);
for(const[gx,gz]of groves){if(Math.hypot(gx,gz)>118)continue;const n=12+((rnd()*14)|0);
for(let i=0;i<n;i++){const x=gx+R(-8,8),z=gz+R(-8,8);if(inside(new V(x,gH(x,z)+1,z),1.2))continue;const gh=gH(x,z);kl.push({x,z,h:Math.min(R(12,30),SURF-3-gh),ph:R(0,6.28),rot:R(0,3.14)});}}
const kelp=new THREE.Mesh(buildKelp(kl),SM({side:THREE.DoubleSide,
vertexShader:`uniform float uTime;attribute float aH;attribute float aP;attribute float aL;attribute float aU;varying float vH;varying float vU;varying float vL;
#include <fog_pars_vertex>
void main(){vec3 p=position;float k=pow(aH,1.3);
float s=sin(uTime*0.7+aP+aH*2.6)*1.4+sin(uTime*1.6+aP*1.7+aH*6.0)*0.3;
p.x+=(s+1.0)*k*aL*0.07;p.z+=cos(uTime*0.55+aP+aH*2.2)*k*aL*0.05;
vH=aH;vU=aU;vL=0.75+0.25*sin(aP*3.0);
vec4 mvPosition=modelViewMatrix*vec4(p,1.0);gl_Position=projectionMatrix*mvPosition;
#include <fog_vertex>
}`,
fragmentShader:`uniform float uTime;varying float vH;varying float vU;varying float vL;
#include <fog_pars_fragment>
void main(){vec3 c=mix(vec3(0.07,0.26,0.11),vec3(0.25,0.52,0.18),smoothstep(0.0,0.6,vH));
c=mix(c,vec3(0.78,0.66,0.25),smoothstep(0.75,1.0,vH)*0.55);
c*=(1.0-0.35*abs(vU))*(0.7+0.5*vH)*vL;
c+=vec3(0.9,0.75,0.35)*0.08*max(0.0,sin(uTime*1.5+vH*12.0));
gl_FragColor=vec4(c,1.0);
#include <fog_fragment>
}`}));
kelp.frustumCulled=false;scene.add(kelp);
// coral (palette only)
const PAL={o:0xe8823a,g:0xf3c75a,t:0x2fa69a,k:0x4f8f34};
const CL={br:[],ba:[],tu:[],fa:[]};const dm=new THREE.Object3D();
function pushC(arr,col){dm.updateMatrix();arr.push({m:dm.matrix.clone(),c:new THREE.Color(col).offsetHSL(0,R(-0.05,0.05),R(-0.06,0.06))});}
function coralCluster(x,z){const n=3+((rnd()*5)|0);
for(let k=0;k<n;k++){const px=x+R(-3,3),pz=z+R(-3,3),py=gH(px,pz);if(inside(new V(px,py+0.3,pz),0.3)||Math.hypot(px,pz)>125)continue;const t=rnd();
if(t<0.35){const col=pick([PAL.o,PAL.g,PAL.o]),s=R(0.6,1.3),nb=5+((rnd()*6)|0);for(let b=0;b<nb;b++){dm.position.set(px+R(-0.3,0.3),py-0.1,pz+R(-0.3,0.3));dm.rotation.set(R(-0.7,0.7),R(0,6.28),R(-0.7,0.7));dm.scale.set(s,s*R(0.8,1.6),s);pushC(CL.br,col);}}
else if(t<0.6){const s=R(0.5,1.4);dm.position.set(px,py+0.1,pz);dm.rotation.set(0,R(0,6),0);dm.scale.set(s,s*0.6,s);pushC(CL.ba,pick([PAL.g,PAL.o,PAL.t]));}
else if(t<0.85){const col=pick([PAL.t,PAL.o,PAL.k]),nt=3+((rnd()*4)|0);for(let b=0;b<nt;b++){dm.position.set(px+R(-0.6,0.6),py-0.1,pz+R(-0.6,0.6));dm.rotation.set(R(-0.2,0.2),0,R(-0.2,0.2));const s=R(0.7,1.3);dm.scale.set(s,s*R(0.8,2.2),s);pushC(CL.tu,col);}}
else{dm.position.set(px,py-0.1,pz);dm.rotation.set(R(-0.15,0.15),R(0,6.28),0);dm.scale.setScalar(R(1,2.1));pushC(CL.fa,pick([PAL.o,PAL.g]));}}}
for(const r of rocks)if(rnd()<0.7)coralCluster(r.x+R(-4,4),r.z+R(-4,4));
for(let i=0;i<40;i++)coralCluster(R(-115,115),R(-115,115));
for(let i=0;i<6;i++)coralCluster(R(-12,12),R(14,40));
function inst(geo,arr,side){const m=new THREE.MeshStandardMaterial({color:0xffffff,roughness:0.8,side:side||THREE.FrontSide});addCaustics(m,0.25);
const im=new THREE.InstancedMesh(geo,m,Math.max(1,arr.length));arr.forEach((a,i)=>{im.setMatrixAt(i,a.m);im.setColorAt(i,a.c);});im.count=arr.length;im.frustumCulled=false;scene.add(im);}
inst(new THREE.CylinderGeometry(0.06,0.14,1.4,5).translate(0,0.7,0),CL.br);
inst(new THREE.SphereGeometry(1,12,8),CL.ba);
inst(new THREE.CylinderGeometry(0.18,0.22,1,8,1,true).translate(0,0.5,0),CL.tu,THREE.DoubleSide);
inst(new THREE.CircleGeometry(1,14,0,Math.PI),CL.fa,THREE.DoubleSide);
// sunbeams
const BEAMG=new THREE.PlaneGeometry(1,1).translate(0,-0.5,0);const beams=[];
function placeBeam(b){const f=new V();camera.getWorldDirection(f);const an=R(0,6.28),d=R(4,45);b.g.position.set(camera.position.x+f.x*20+Math.cos(an)*d,SURF+0.5,camera.position.z+f.z*20+Math.sin(an)*d);b.life=0;b.dur=R(9,17);b.int=R(0.6,1.1);}
for(let i=0;i<16;i++){const g=new THREE.Group();g.quaternion.setFromUnitVectors(new V(0,1,0),SUN);
const mat=SM({uniforms:{uOp:{value:0},uPh:{value:R(0,10)}},transparent:true,depthWrite:false,blending:THREE.AdditiveBlending,side:THREE.DoubleSide,fog:false,
vertexShader:'varying vec2 vUv;varying float vD;void main(){vUv=uv;vec4 mv=modelViewMatrix*vec4(position,1.0);vD=-mv.z;gl_Position=projectionMatrix*mv;}',
fragmentShader:'uniform float uTime;uniform float uOp;uniform float uPh;varying vec2 vUv;varying float vD;void main(){float e=pow(sin(3.14159*vUv.x),2.0);float st=0.7+0.3*sin(vUv.x*17.0+uTime*0.7+uPh);float a=e*st*pow(vUv.y,1.4)*uOp*exp(-vD*0.014)*0.3*smoothstep(0.0,3.0,vD);gl_FragColor=vec4(vec3(1.0,0.88,0.6)*a,a);}'});
const m=new THREE.Mesh(BEAMG,mat);m.scale.set(R(3,8),R(42,56),1);g.add(m);scene.add(g);const b={g,m,mat};beams.push(b);}
// fish schools
const fishGeo=(()=>{const P=[[0,0,1],[0,.28,0],[.13,0,0],[0,-.24,0],[-.13,0,0],[0,0,-.6],[0,.32,-1],[0,-.32,-1]];const F=[[0,2,1],[0,3,2],[0,4,3],[0,1,4],[5,1,2],[5,2,3],[5,3,4],[5,4,1],[5,6,7]];const p=[];for(const f of F)for(const i of f)p.push(...P[i]);const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(p,3));g.computeVertexNormals();return g;})();
const CFG=[{n:45,c:[0xf0a640,0xf3c75a],s:0.7},{n:40,c:[0x2fb7a8],s:0.6},{n:50,c:[0xf3c75a],s:0.5},{n:30,c:[0xe8823a,0xf3c75a],s:0.9},{n:40,c:[0x48c9b0,0x1d7fa0],s:0.55},{n:35,c:[0xe8823a],s:0.75}];
const NF=CFG.reduce((a,c)=>a+c.n,0);
const fishMat=new THREE.MeshStandardMaterial({color:0xffffff,roughness:0.45,metalness:0.25,flatShading:true,side:THREE.DoubleSide});
fishMat.onBeforeCompile=s=>{s.uniforms.uTime=TIME;s.vertexShader='attribute float aPhase;uniform float uTime;\n'+s.vertexShader.replace('#include <begin_vertex>','#include <begin_vertex>\ntransformed.x+=sin(uTime*9.0+aPhase)*0.2*clamp(-position.z+0.2,0.0,1.2);');};
fishGeo.setAttribute('aPhase',new THREE.InstancedBufferAttribute(new Float32Array(NF).map(()=>R(0,6.28)),1));
const fishMesh=new THREE.InstancedMesh(fishGeo,fishMat,NF);fishMesh.frustumCulled=false;scene.add(fishMesh);
function randOpen(anchor){const p=new V();for(let i=0;i<30;i++){if(anchor){p.set(anchor.x+R(-14,14),0,anchor.z+R(-14,14));}else{const an=R(0,6.28),r=R(0,105);p.set(Math.cos(an)*r,0,Math.sin(an)*r);}
const g=gH(p.x,p.z);p.y=anchor?clamp(anchor.y+R(-3,4),g+3,SURF-8):R(g+4,SURF-9);if(!inside(p,4))return p;}return p;}
const schools=[];let fi=0;
CFG.forEach((c,si)=>{const anchor=si===0?gy(-6,-30,5):si===1?shipSide.clone().add(new V(0,2,0)):null;
const s={anchor,pos:randOpen(anchor),dir:new V(R(-1,1),0,R(-1,1)).normalize(),timer:R(3,7),speed:R(3.5,5),fish:[]};s.tgt=randOpen(anchor);
const sp=Math.cbrt(c.n)*0.9;
for(let i=0;i<c.n;i++){const o=new V(R(-1,1),R(-1,1)*0.6,R(-1,1)*1.4).multiplyScalar(R(0.4,1)*sp);s.fish.push({i:fi,o,p:s.pos.clone().add(o),v:s.dir.clone(),k:R(1.2,2.6),sc:c.s*R(0.8,1.2)});
fishMesh.setColorAt(fi,new THREE.Color(pick(c.c)).offsetHSL(0,0,R(-0.06,0.06)));fi++;}
schools.push(s);});
// manta rays
function mantaGeo(){const U=14,VV=10,p=[],ix=[];for(let j=0;j<=VV;j++){const v=j/VV;const w=v<0.45?0.7+2.7*Math.sin(v/0.45*Math.PI/2):0.25+3.15*Math.cos((v-0.45)/0.55*Math.PI/2);
for(let i=0;i<=U;i++){const u=i/U*2-1;p.push(u*w,0.35*Math.pow(1-u*u,2)*Math.sin(Math.PI*v),2.2-v*4.2);}}
for(let j=0;j<VV;j++)for(let i=0;i<U;i++){const a=j*(U+1)+i,b=a+1,c=a+U+1,d=c+1;ix.push(a,b,c,b,d,c);}
const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(p,3));g.setIndex(ix);g.computeVertexNormals();return g;}
const MG=mantaGeo(),mantas=[];
for(let i=0;i<4;i++){const m=new THREE.MeshStandardMaterial({color:0x163c64,roughness:0.7,side:THREE.DoubleSide});const ph=i*1.7;
m.onBeforeCompile=s=>{s.uniforms.uTime=TIME;s.uniforms.uPh={value:ph};s.vertexShader='uniform float uTime;uniform float uPh;\n'+s.vertexShader.replace('#include <begin_vertex>','#include <begin_vertex>\nfloat ax=abs(position.x);transformed.y+=sin(uTime*1.3+uPh-ax*0.45)*pow(ax/3.4,1.6)*1.3;');
s.fragmentShader=s.fragmentShader.replace('#include <color_fragment>','#include <color_fragment>\nif(!gl_FrontFacing)diffuseColor.rgb=vec3(0.42,0.72,0.66);');};
const o=new THREE.Mesh(MG,m);const tail=new THREE.Mesh(new THREE.CylinderGeometry(0.03,0.07,3.2,5).rotateX(Math.PI/2).translate(0,0,-3.6),new THREE.MeshStandardMaterial({color:0x163c64}));o.add(tail);
o.scale.setScalar(R(0.9,1.4));scene.add(o);const Rr=R(28,45),dir=i%2?1:-1;
mantas.push({o,cx:R(-50,50),cz:R(-50,50),R:Rr,y:R(19,29),w:dir*R(3,4)/Rr,th:R(0,6.28)});}
mantas[0].cx=-15;mantas[0].cz=-25;mantas[0].y=24;
// bubbles
function bubTex(){const c=document.createElement('canvas');c.width=c.height=64;const x=c.getContext('2d');x.strokeStyle='rgba(255,255,255,0.9)';x.lineWidth=4;x.beginPath();x.arc(32,32,26,0,7);x.stroke();
const g=x.createRadialGradient(24,22,1,24,22,12);g.addColorStop(0,'rgba(255,255,255,0.9)');g.addColorStop(1,'rgba(255,255,255,0)');x.fillStyle=g;x.fillRect(0,0,64,64);const t=new THREE.CanvasTexture(c);return t;}
const vents=[];for(let i=0;i<10&&i<rocks.length;i++)vents.push(rocks[(rnd()*rocks.length)|0]);
vents.push(ship.localToWorld(new V(-3,3.6,0)),ship.localToWorld(new V(4,3.6,1)),gy(0,25,0.2),gy(6,-4,0.2));
const NB=520,bp=new Float32Array(NB*3),bd=[];
function spawnB(b,init){if(hold&&!demo&&started&&rnd()<0.1){b.x=camera.position.x+R(-0.8,0.8);b.y=camera.position.y-1.3;b.z=camera.position.z+R(-0.8,0.8);}
else{const v=pick(vents);b.x=v.x+R(-0.4,0.4);b.z=v.z+R(-0.4,0.4);b.y=init?R(v.y,SURF):v.y;}b.s=R(1.2,2.8);b.f=R(1,3);b.ph=R(0,6.28);}
const bg=new THREE.BufferGeometry();bg.setAttribute('position',new THREE.BufferAttribute(bp,3));
const bubbles=new THREE.Points(bg,new THREE.PointsMaterial({size:0.32,map:bubTex(),transparent:true,depthWrite:false,color:0xcff6ee}));bubbles.frustumCulled=false;scene.add(bubbles);
// drifting specks
const NP=800,pp=new Float32Array(NP*3),pb=new Float32Array(NP*3).map(()=>R(0,60));
const pg=new THREE.BufferGeometry();pg.setAttribute('position',new THREE.BufferAttribute(pp,3));
const specks=new THREE.Points(pg,new THREE.PointsMaterial({size:0.09,color:0xa8e6d6,transparent:true,opacity:0.55,depthWrite:false}));specks.frustumCulled=false;scene.add(specks);
// controls & state
let yaw=0,pitch=-0.05,hold=false,lx=0,ly=0,pid=null,speed=0,started=false,demo=false,demoT=0;
for(let i=0;i<NB;i++){const b={};bd.push(b);spawnB(b,true);}
const ov=document.getElementById('ov'),hint=document.getElementById('hint');let hintT=0;
function showHint(s,d){hint.textContent=s;hint.style.opacity=1;hintT=d;}
document.getElementById('go').addEventListener('click',()=>{ov.style.display='none';started=true;showHint('Hold to glide · drag to turn',6);});
document.getElementById('help').addEventListener('click',()=>{if(demo)endDemo();ov.style.display='flex';hold=false;pid=null;});
function endDemo(){demo=false;started=true;ov.style.display='none';const d=new V();camera.getWorldDirection(d);yaw=Math.atan2(-d.x,-d.z);pitch=Math.asin(clamp(d.y,-1,1));showHint('Your turn · hold to glide, drag to turn',5);}
window.addEventListener('pointerdown',e=>{if(demo&&e.isTrusted)endDemo();},true);
window.addEventListener('touchstart',e=>{if(demo&&e.isTrusted)endDemo();},{capture:true,passive:true});
cv.addEventListener('pointerdown',e=>{if(pid!==null||!started)return;pid=e.pointerId;hold=true;lx=e.clientX;ly=e.clientY;try{cv.setPointerCapture(pid);}catch(_){}e.preventDefault();});
cv.addEventListener('pointermove',e=>{if(e.pointerId!==pid)return;const dx=e.clientX-lx,dy=e.clientY-ly;lx=e.clientX;ly=e.clientY;const k=3.4/Math.max(360,Math.min(innerWidth,innerHeight*1.4));yaw-=dx*k;pitch=clamp(pitch-dy*k*0.8,-1.2,1.2);});
const up=e=>{if(e.pointerId!==pid)return;pid=null;hold=false;};
cv.addEventListener('pointerup',up);cv.addEventListener('pointercancel',up);cv.addEventListener('lostpointercapture',up);
cv.addEventListener('contextmenu',e=>e.preventDefault());
addEventListener('resize',()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);});
camera.position.copy(gy(4,34,6));camera.rotation.set(pitch,yaw,0,'YXZ');
if(location.hash==='#demo'){demo=true;started=true;ov.style.display='none';showHint('Demo · touch to swim',1e9);}
beams.forEach(b=>{placeBeam(b);b.life=R(0,1);});
const UP=new V(0,1,0),_m4=new THREE.Matrix4(),_q=new THREE.Quaternion(),_a=new V(),_b=new V(),fwd=new V(),rgt=new V(),up2=new V(),des=new V();
let last=performance.now();
function frame(now){requestAnimationFrame(frame);const dt=Math.min(0.05,Math.max(0.001,(now-last)/1000));last=now;TIME.value+=dt;const t=TIME.value;const cp=camera.position;
if(demo){demoT+=dt;const u=(demoT%20)/20;curve.getPointAt(u,cp);curve.getPointAt((u+0.03)%1,_a);cp.y=Math.max(cp.y,gH(cp.x,cp.z)+1.5);
_m4.lookAt(cp,_a,UP);_q.setFromRotationMatrix(_m4);if(demoT<dt*1.5)camera.quaternion.copy(_q);else camera.quaternion.slerp(_q,1-Math.exp(-dt*3));}
else{if(!started)yaw+=dt*0.03;
speed+=((hold&&started?6.5:0)-speed)*(1-Math.exp(-dt*(hold?1.8:1.1)));
camera.rotation.set(pitch,yaw,Math.sin(t*0.5)*0.015,'YXZ');
_a.set(0,0,-1).applyQuaternion(camera.quaternion);cp.addScaledVector(_a,speed*dt);cp.y+=Math.sin(t*0.8)*0.06*dt;
for(let k=0;k<3;k++)resolve(cp,PR);
cp.y=clamp(cp.y,gH(cp.x,cp.z)+1.3,SURF-1.6);const r=Math.hypot(cp.x,cp.z);if(r>140){cp.x*=140/r;cp.z*=140/r;}}
sky.position.copy(cp);
if(hintT<1e8){hintT-=dt;if(hintT<0)hint.style.opacity=0;}
dl.intensity=0.85+0.1*Math.sin(t*1.3);
// beams
for(const b of beams){b.life+=dt/b.dur;if(b.life>1)placeBeam(b);b.mat.uniforms.uOp.value=Math.pow(Math.sin(Math.PI*b.life),1.5)*b.int;_a.copy(cp);b.g.worldToLocal(_a);b.m.rotation.y=Math.atan2(_a.x,_a.z);}
// fish
for(const s of schools){s.timer-=dt;
if(s.timer<0||s.pos.distanceTo(s.tgt)<6){s.tgt=randOpen(s.anchor);s.timer=R(4,9);}
if(!demo&&s.pos.distanceTo(cp)<9&&s.timer<8.5){_a.subVectors(s.pos,cp).normalize();s.tgt.copy(s.pos).addScaledVector(_a,22);s.tgt.y=clamp(s.tgt.y,gH(s.tgt.x,s.tgt.z)+3,SURF-6);s.timer=9;}
_a.subVectors(s.tgt,s.pos).normalize();s.dir.lerp(_a,Math.min(1,dt*0.9)).normalize();
_b.copy(s.pos).addScaledVector(s.dir,6);if(inside(_b,3)){s.tgt=randOpen(s.anchor);s.timer=R(4,9);}
s.pos.addScaledVector(s.dir,s.speed*dt);s.pos.y=clamp(s.pos.y,gH(s.pos.x,s.pos.z)+3,SURF-4);
fwd.copy(s.dir);rgt.crossVectors(fwd,UP).normalize();up2.crossVectors(rgt,fwd);
for(const f of s.fish){des.copy(s.pos).addScaledVector(rgt,f.o.x+Math.sin(t*0.7+f.i)*0.3).addScaledVector(up2,f.o.y+Math.cos(t*0.9+f.i)*0.2).addScaledVector(fwd,f.o.z);
des.y=Math.max(des.y,gH(des.x,des.z)+1);_b.copy(f.p);f.p.lerp(des,1-Math.exp(-dt*f.k));_b.subVectors(f.p,_b).divideScalar(dt);f.v.lerp(_b,Math.min(1,dt*4));
if(f.v.lengthSq()<1e-4)f.v.copy(s.dir);dm.position.copy(f.p);dm.scale.setScalar(f.sc);_b.copy(f.p).add(f.v);dm.lookAt(_b);dm.updateMatrix();fishMesh.setMatrixAt(f.i,dm.matrix);}}
fishMesh.instanceMatrix.needsUpdate=true;
// mantas
for(const m of mantas){m.th+=dt*m.w;const c=Math.cos(m.th),s=Math.sin(m.th);m.o.position.set(m.cx+c*m.R,m.y+Math.sin(m.th*2)*1.5,m.cz+s*m.R);
const sg=Math.sign(m.w);_a.set(-s*sg,0,c*sg).add(m.o.position);m.o.lookAt(_a);m.o.rotateZ(-0.22*sg);}
// bubbles
for(let i=0;i<NB;i++){const b=bd[i];b.y+=b.s*dt;if(b.y>SURF-0.4)spawnB(b,false);const j=i*3;bp[j]=b.x+Math.sin(t*b.f+b.ph)*0.25;bp[j+1]=b.y;bp[j+2]=b.z+Math.cos(t*b.f*0.8+b.ph)*0.25;}
bg.attributes.position.needsUpdate=true;
for(let i=0;i<NP;i++){const j=i*3;for(let a=0;a<3;a++){const c=a===0?cp.x:a===1?cp.y:cp.z;const base=pb[j+a]+(a===0?t*0.3:a===1?Math.sin(t*0.2+i)*0.5:0);pp[j+a]=c+(((base-c)%60)+60)%60-30;}}
pg.attributes.position.needsUpdate=true;
renderer.render(scene,camera);}
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
