Back to the app
The code
Swim the Kelp Garden by GPT-6.2 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, viewport-fit=cover">
<title>The Kelp Garden</title>
<style>
*{box-sizing:border-box}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#073c4b;color:#f8f4df;font-family:system-ui,sans-serif}canvas{display:block;position:absolute;inset:0;touch-action:none;cursor:grab}canvas:active{cursor:grabbing}.ui{position:absolute;inset:0;pointer-events:none;background:linear-gradient(180deg,#03282c47,transparent 26%,transparent 70%,#041c2a73)}header{position:absolute;top:max(26px,env(safe-area-inset-top));left:clamp(22px,5vw,74px)}.eyebrow,.caption,.tag{font-size:10px;letter-spacing:.23em;font-weight:750;text-transform:uppercase}.eyebrow{color:#e5d89a;display:flex;align-items:center;gap:10px}.eyebrow:before{content:'';display:block;width:23px;height:1px;background:#e5d89a}h1{font:400 clamp(40px,7vw,102px)/.92 Georgia,serif;letter-spacing:-.065em;margin:11px 0 10px;text-shadow:0 3px 30px #052b3b73}header p{margin:0;color:#dbf1e5;font-size:clamp(12px,1.3vw,15px)}.meter{position:absolute;right:clamp(18px,4vw,57px);top:max(27px,env(safe-area-inset-top));text-align:right}.meter b{font:400 clamp(20px,3vw,33px) Georgia,serif;display:block;font-variant-numeric:tabular-nums}.caption{color:#c2e8dd}.bottom{position:absolute;left:clamp(20px,5vw,74px);right:clamp(20px,5vw,74px);bottom:max(24px,env(safe-area-inset-bottom));display:flex;align-items:end;justify-content:space-between;gap:15px}.tag{color:#e5d89a;margin-bottom:6px}.hint{font-size:13px;color:#ecf8ed}button{pointer-events:auto;touch-action:manipulation;color:inherit;border:1px solid #d6f4dd69;background:#092e3bd9;padding:0 17px;min-height:48px;border-radius:30px;font:600 13px system-ui;cursor:pointer;white-space:nowrap}button:focus-visible{outline:2px solid #f1d68c;outline-offset:4px}.reticle{position:absolute;top:50%;left:50%;width:12px;height:12px;transform:translate(-50%,-50%);border:1px solid #ebffe38c;border-radius:50%;box-shadow:0 0 10px #bdf2da65}.error{position:absolute;inset:0;display:grid;place-items:center;text-align:center;padding:25px;background:#073c4b}.error[hidden]{display:none}@media(max-width:480px){header p{max-width:180px}.meter .caption{font-size:8px}.hint{font-size:11px}.bottom{gap:8px}button{padding:0 12px}}
</style>
</head>
<body>
<div class="ui" id="ui">
<header><div class="eyebrow">A quiet dive</div><h1>Kelp Garden</h1><p>Drift beneath the sunlit canopy.</p></header>
<div class="meter"><b id="depth">8.0 m</b><span class="caption">Depth</span></div>
<div class="reticle" aria-hidden="true"></div>
<div class="bottom"><div><div class="tag" id="place">Garden entrance</div><div class="hint" id="hint">Hold to swim · Drag to look</div></div><button id="tour" type="button" aria-pressed="false">▷ Guided swim</button></div>
</div>
<div class="error" id="error" hidden>Unable to start the underwater scene. A WebGL-capable browser and access to the Three.js CDN are required.</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script>
(()=>{'use strict';
const $=id=>document.getElementById(id), T=window.THREE;
if(!T){$('error').hidden=false;return}
let renderer;
try{renderer=new T.WebGLRenderer({antialias:true,powerPreference:'high-performance'})}catch(e){$('error').hidden=false;return}
document.body.insertBefore(renderer.domElement,$('ui'));
const canvas=renderer.domElement, scene=new T.Scene(), V=(x,y,z)=>new T.Vector3(x,y,z);
const sea=0x187989, surface=17.8;
scene.background=new T.Color(sea);scene.fog=new T.FogExp2(sea,.036);
const camera=new T.PerspectiveCamera(62,1,.08,120);
const cameraRig=V(0,9.5,25);
let yaw=0,pitch=-.105, tour=false, tourTime=0, hold=null, px=0,py=0, speed=0;
renderer.setClearColor(sea);renderer.setPixelRatio(Math.min(devicePixelRatio,1.65));
renderer.outputColorSpace=T.SRGBColorSpace;
function resize(){camera.aspect=innerWidth/innerHeight;camera.fov=camera.aspect<.8?72:62;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight)}
addEventListener('resize',resize);resize();
scene.add(new T.HemisphereLight(0xc6fff0,0x6d7954,2.05));
const sun=new T.DirectionalLight(0xffe5a0,2.05);sun.position.set(-18,27,6);scene.add(sun);
let seed=39159;
function rand(){seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/4294967296}
const mat=(color,more)=>new T.MeshStandardMaterial({color,roughness:.87,...more});
const time={value:0};
function caustics(material,strength){
material.onBeforeCompile=s=>{
s.uniforms.waterTime=time;
s.vertexShader='varying vec3 vReef;\n'+s.vertexShader.replace('#include <worldpos_vertex>','#include <worldpos_vertex>\nvReef=(modelMatrix * vec4(transformed,1.0)).xyz;');
s.fragmentShader='uniform float waterTime; varying vec3 vReef;\n'+s.fragmentShader.replace('#include <opaque_fragment>',`
vec2 q=vReef.xz*1.63+vReef.y*vec2(.29,-.21);
float a=sin(q.x+sin(q.y*.87+waterTime*.68)*1.32+waterTime*.46);
float b=sin(q.y*1.13+sin(q.x*.78-waterTime*.55)*1.45-waterTime*.38);
float c=pow(1.-abs(a+b)*.5,12.);
outgoingLight+=vec3(.47,.79,.64)*c*${strength.toFixed(2)};
#include <opaque_fragment>`);
};
material.customProgramCacheKey=()=> 'caustics'+strength;
return material;
}
function mesh(geo,material,parent=scene){const o=new T.Mesh(geo,material);parent.add(o);return o}
const ground=(x,z)=>.34*Math.sin(x*.16+z*.06)+.31*Math.cos(z*.21)+.14*Math.sin(x*.37-z*.27);
const sand=caustics(mat(0xb6a877,{vertexColors:true}),.62);
const floorG=new T.PlaneGeometry(166,166,120,120);floorG.rotateX(-Math.PI/2);
const fp=floorG.attributes.position, fc=[];
for(let i=0;i<fp.count;i++){
const x=fp.getX(i),z=fp.getZ(i);fp.setY(i,ground(x,z)+.08*Math.sin(z*2.15+x*.6+Math.sin(x*.27)*2));
const shade=.84+.12*Math.sin(z*2.15+x*.6+Math.sin(x*.27)*2)+.04*rand();fc.push(shade,shade,shade*.99);
}
floorG.setAttribute('color',new T.Float32BufferAttribute(fc,3));floorG.computeVertexNormals();mesh(floorG,sand);
const waterG=new T.PlaneGeometry(170,170,88,88);waterG.rotateX(-Math.PI/2);
const water=mesh(waterG,new T.ShaderMaterial({side:T.DoubleSide,uniforms:{t:time,sea:{value:new T.Color(sea)}},vertexShader:`
uniform float t; varying vec3 w;
void main(){vec3 p=position;p.y+=.19*sin(p.x*.76+p.z*.28+t*1.1)+.13*sin(p.z*1.3-p.x*.33-t*.91); w=(modelMatrix*vec4(p,1.)).xyz;gl_Position=projectionMatrix*viewMatrix*vec4(w,1.);}
`,fragmentShader:`
uniform float t;uniform vec3 sea;varying vec3 w;
void main(){vec2 p=w.xz;float a=sin(p.x*.92+p.y*.52+t+sin(p.y*.87-t*.8));float b=sin(p.y*1.6-p.x*.39-t*.9+sin(p.x*.63+t*.58));float r=pow(1.-abs(a+b)*.5,14.);float s=exp(-length((p-vec2(-13.,-2.))*vec2(.085,.054)));float d=length(cameraPosition-w);vec3 col=mix(vec3(.075,.50,.49),vec3(.57,.87,.69),s*.8);col+=r*vec3(.15,.31,.25);gl_FragColor=vec4(mix(sea,col,exp(-d*.022)),1.);
#include <colorspace_fragment>
}`}));
water.position.y=surface;
const rayG=new T.CylinderGeometry(.34,2.5,1,26,1,true);
for(let i=0;i<10;i++){
const x=-25+i*4.5+rand()*3,z=-17+rand()*31;
const from=V(x,surface-.2,z),to=V(x+7+rand()*3,ground(x+8,z-3)+.2,z-4);
const shader=new T.ShaderMaterial({side:T.DoubleSide,transparent:true,depthWrite:false,blending:T.AdditiveBlending,uniforms:{t:time,p:{value:i}},vertexShader:`
varying vec2 v;varying vec3 n;varying vec3 e;
void main(){v=uv;vec4 q=modelViewMatrix*vec4(position,1.);n=normalize(normalMatrix*normal);e=-q.xyz;gl_Position=projectionMatrix*q;}
`,fragmentShader:`
uniform float t;uniform float p;varying vec2 v;varying vec3 n;varying vec3 e;
void main(){float f=pow(abs(dot(normalize(n),normalize(e))),1.7);float a=smoothstep(0.,.43,v.y)*(1.-smoothstep(.89,1.,v.y))*f*(.68+.32*sin(t*.75+p));gl_FragColor=vec4(.74,1.,.70,a*.075*exp(-length(e)*.019));}
`});
const o=mesh(rayG,shader);o.position.copy(from).lerp(to,.5);o.scale.set(1,from.distanceTo(to),1);o.quaternion.setFromUnitVectors(V(0,1,0),from.clone().sub(to).normalize());
}
const blockers=[], rockGeo=new T.IcosahedronGeometry(1,1);
const rockM=[0x355e5c,0x477472,0x416866,0x627f69].map(c=>caustics(mat(c,{flatShading:true}),.29));
function rock(x,y,z,sx,sy,sz,solid=true){
const m=mesh(rockGeo,rockM[Math.floor(rand()*rockM.length)]);m.position.set(x,y,z);m.scale.set(sx,sy,sz);m.rotation.y=rand()*6.28;
if(solid)blockers.push({p:m.position,a:Math.max(sx,sz),b:sy,c:Math.max(sx,sz)});
return m;
}
function arch(x,z,r,h,angle){
for(let i=0;i<=13;i++){
const a=i*Math.PI/13,u=Math.cos(a)*r, xx=x+u*Math.cos(angle),zz=z+u*Math.sin(angle);const y=ground(xx,zz)+.42+h*Math.sin(a);
const b=rock(xx,y,zz,1.25+rand()*.34,1.45+rand()*.32,1.23+rand()*.42);b.rotation.z=(a-Math.PI*.5)*.65;
if(i%3===0)rock(xx+.32,y+.3,zz-.62,.72,.55,.67);
}
}
arch(-9,3,4.3,6.1,.25);arch(13,-15,4.4,5.4,-.56);arch(-18,-23,5.3,7.2,.38);
for(let i=0;i<48;i++){let x=(rand()-.5)*85,z=(rand()-.52)*79;if(Math.abs(x)<5&&z>4||x>3&&x<19&&z>-12&&z<10)continue;let s=.4+rand()*2;rock(x,ground(x,z)+s*.22,z,s,s*.69,s*.81)}
const kelpColors=[0x397f40,0x659844,0x929e40,0x287156,0x527e35];
const kelpMat=kelpColors.map(color=>{const m=mat(color,{side:T.DoubleSide});m.onBeforeCompile=s=>{
s.uniforms.t=time;
s.vertexShader='uniform float t;\n'+s.vertexShader.replace('#include <begin_vertex>',`
#include <begin_vertex>
float phase=modelMatrix[3].x*.34+modelMatrix[3].z*.28;
float w=position.y*.105;
transformed.x+=w*sin(t*1.08+phase+position.y*.30);
transformed.z+=w*.65*cos(t*.83+phase+position.y*.38);
`);
};return m});
const stalk=kelpMat[3];
function kelp(x,z,h,full){
const parent=new T.Group();scene.add(parent);parent.position.set(x,ground(x,z)-.12,z);parent.rotation.y=rand()*6.28;
const bend=(y)=>.32*Math.sin(y*.48)*y/h;
const pts=Array.from({length:9},(_,i)=>V(bend(h*i/8),h*i/8,0));
mesh(new T.TubeGeometry(new T.CatmullRomCurve3(pts),13,.046,4,false),stalk,parent);
const colors=kelpColors.map(()=>({p:[],idx:[]}));
const count=Math.floor(h*(full?1.65:1.18));
for(let j=0;j<count;j++){
const y=.48+(h-.56)*j/count,l=(.65+rand()*.75)*(1-.45*j/count),w=(.17+rand()*.18)*(1-.36*j/count),a=j*2.4+rand()*.6;
const d=colors[Math.floor(rand()*colors.length)],start=d.p.length/3;
for(let k=0;k<=8;k++){
const u=k/8,side=w*Math.pow(Math.sin(Math.PI*u),.75),sweep=l*u;
for(let b=-1;b<=1;b++){
d.p.push(bend(y)+Math.cos(a)*sweep-Math.sin(a)*side*b,y+l*(.74*u-.53*u*u)+Math.sin(u*5+j)*.11*u+b*.09*Math.sin(u*4),Math.sin(a)*sweep+Math.cos(a)*side*b);
}
if(k)for(let b=0;b<2;b++){const f=start+(k-1)*3+b;d.idx.push(f,f+1,f+3,f+1,f+4,f+3)}
}
}
colors.forEach((d,i)=>{if(!d.p.length)return;const g=new T.BufferGeometry();g.setAttribute('position',new T.Float32BufferAttribute(d.p,3));g.setIndex(d.idx);g.computeVertexNormals();mesh(g,kelpMat[i],parent)});
}
for(const [cx,cz,n,r] of [[-15,12,16,7],[15,14,15,7],[-22,-10,24,12],[23,-9,24,10],[-4,-26,18,10],[8,-37,17,15],[-27,25,16,10],[26,28,14,11]]){
for(let j=0;j<n;j++){const a=rand()*6.28,d=Math.sqrt(rand())*r;kelp(cx+Math.cos(a)*d,cz+Math.sin(a)*d,5+rand()*10.3,j%3!==0)}
}
const coralM=[0xf08b47,0xc66233,0xe5ae55,0x41857c].map(c=>mat(c,{roughness:.74}));
const branchG=new T.CylinderGeometry(.63,1,1,6), tipG=new T.IcosahedronGeometry(1,0);
function coral(x,z,size,m){
const group=new T.Group();group.position.set(x,ground(x,z),z);group.scale.setScalar(size);scene.add(group);
function branch(p,d,l,r,n){
const q=p.clone().addScaledVector(d,l),o=mesh(branchG,m,group);o.position.copy(p).lerp(q,.5);o.scale.set(r,l,r);o.quaternion.setFromUnitVectors(V(0,1,0),d);
if(n){for(let j=0;j<2;j++)branch(q,d.clone().add(V((rand()-.5)*1.35,.10,(rand()-.5)*1.35)).normalize(),l*(.63+rand()*.2),r*.61,n-1)}
else {const tip=mesh(tipG,m,group);tip.position.copy(q);tip.scale.setScalar(r*1.1)}
}
for(let k=0;k<4;k++)branch(V((rand()-.5)*.27,0,(rand()-.5)*.22),V((rand()-.5)*1.2,.95,(rand()-.5)*1.2).normalize(),.35+rand()*.48,.09+rand()*.05,2);
}
for(const [x,z] of [[-6,13],[-12,6],[-4,1],[4,11],[13,9],[17,-3],[9,-11],[-13,-15],[1,-12],[-19,15],[4,19]]){
for(let j=0;j<4;j++)coral(x+(rand()-.5)*3,z+(rand()-.5)*3,.55+rand()*.92,coralM[(j+Math.floor(rand()*3))%coralM.length]);
}
const wreck=new T.Group();scene.add(wreck);wreck.position.set(9,-.05,-1);wreck.rotation.y=-.37;
const wood=[0x674334,0x8b593c,0xa36c44,0x5b4837,0x93694c].map(c=>caustics(mat(c,{side:T.DoubleSide}),.29));
const box=new T.BoxGeometry(1,1,1);
function plank(x,y,z,w,h,d,m,parent=wreck){
const o=mesh(box,m,parent);o.position.set(x,y,z);o.scale.set(w,h,d);return o;
}
function beam(a,b,r,m,parent=wreck){
const o=mesh(branchG,m,parent);o.position.copy(a).lerp(b,.5);o.scale.set(r,a.distanceTo(b),r);o.quaternion.setFromUnitVectors(V(0,1,0),b.clone().sub(a).normalize());return o;
}
function hull(u,v){const width=.26+2.12*Math.pow(Math.sin(Math.PI*u),.65);return V((u-.5)*12,.32+2.5*v+(.45+v*.87)*Math.pow(Math.abs(2*u-1),3),width*Math.sign(v)*(.28+.72*Math.sqrt(Math.abs(v))))}
for(const side of [-1,1]){
for(let j=0;j<9;j++)for(let k=0;k<14;k++){
if(side===1&&j>3&&k>4&&k<8&& (j>5||k===6))continue;
const pts=[[k/14,j/9+.006],[(k+1)/14,j/9+.006],[(k+1)/14,(j+1)/9-.006],[k/14,(j+1)/9-.006]].map(([u,v])=>{const p=hull(u,v);p.z*=side;return p});
const g=new T.BufferGeometry();g.setFromPoints(pts);g.setIndex([0,1,2,0,2,3]);g.computeVertexNormals();mesh(g,wood[(k+2*j)%wood.length],wreck);
}
for(let k=0;k<=14;k++){
const p=hull(k/14,1);p.z*=side;
if(k>0&&!(side===1&&k>=5&&k<=8)){const q=hull((k-1)/14,1);q.z*=side;beam(q,p,.13,wood[1])}
if(k%2===0){const q=hull(k/14,.18);q.z*=side;beam(q,p,.14,wood[3]);}
}
}
for(let k=0;k<24;k++){const u=(k+.5)/24;if(k>8&&k<15&&k%3!==0)continue;const p=hull(u,.89);plank(p.x,p.y,0,.47,.13,p.z*1.89,wood[k%wood.length])}
plank(-4.32,3.36,0,2.27,.26,2.3,wood[1]);plank(-4.36,2.93,0,1.65,.68,1.67,wood[3]);
beam(V(-.8,1.4,0),V(-1.63,8,.55),.20,wood[1]);beam(V(2.75,1.6,0),V(3.85,5.4,-.83),.15,wood[0]);
beam(V(-1.51,6.8,-1.65),V(-1.53,6.58,2.1),.09,wood[2]);
beam(V(5.55,3.28,0),V(7.25,4.2,0),.115,wood[1]);
const sail=new T.BufferGeometry();sail.setFromPoints([V(-1.5,6.78,-1.3),V(-1.52,6.64,.91),V(-1.07,5.46,.77),V(-1.06,5.68,.08),V(-1.15,4.92,-.25)]);sail.setIndex([0,1,3,1,2,3,0,3,4]);sail.computeVertexNormals();mesh(sail,mat(0xa78c58,{side:T.DoubleSide}),wreck);
wreck.updateMatrixWorld(true);
for(let x=-5.4;x<=5.6;x+=.63){const u=(x+6)/12,w=hull(u,1).z;blockers.push({p:wreck.localToWorld(V(x,1.66,0)),a:1.08,b:1.79,c:w+.22,ship:true})}
for(const [x,y,z,r] of [[-1,4,0,.45],[-1.2,5,.13,.45],[-1.37,6,.3,.45],[-1.5,7,.45,.46],[-1.6,7.83,.54,.44],[3.1,3.7,-.4,.64],[3.6,4.85,-.7,.66],[6.5,3.8,0,1.02],[-1.51,6.7,-1,.72],[-1.51,6.65,1.25,1.05]])blockers.push({p:wreck.localToWorld(V(x,y,z)),a:r,b:r,c:r});
const batches=new Map();
scene.updateMatrixWorld(true);
scene.traverse(o=>{
if(!o.isMesh||![...rockM,...wood,...coralM].includes(o.material))return;
if(!batches.has(o.material))batches.set(o.material,[]);
batches.get(o.material).push(o);
});
for(const [material,objects] of batches){
const p=[],n=[];
for(const o of objects){
const g=o.geometry.index?o.geometry.toNonIndexed():o.geometry.clone();
g.applyMatrix4(o.matrixWorld);
for(const x of g.attributes.position.array)p.push(x);
for(const x of g.attributes.normal.array)n.push(x);
g.dispose();o.removeFromParent();
}
const g=new T.BufferGeometry();
g.setAttribute('position',new T.Float32BufferAttribute(p,3));
g.setAttribute('normal',new T.Float32BufferAttribute(n,3));
mesh(g,material);
}
const fishGeo=new T.SphereGeometry(1,9,6);fishGeo.scale(.38,.14,.085);
const finGeo=new T.BufferGeometry();finGeo.setFromPoints([V(-.27,0,0),V(-.58,.16,0),V(-.53,0,0),V(-.58,-.16,0)]);finGeo.setIndex([0,1,2,0,2,3]);finGeo.computeVertexNormals();
const eyeG=new T.SphereGeometry(.024,6,4), eyeM=mat(0x152f35);
const schools=[];
for(const [x,y,z,n,color] of [[-7,8,7,25,0xe8ad51],[10,7,-4,31,0xd7b85f],[-9,10,-16,24,0x69b9ac],[3,5,14,19,0xe68b51],[17,11,-19,18,0x81b7a4]]){
const bodyM=mat(color,{metalness:.14,roughness:.45}),finM=mat(color,{side:T.DoubleSide});
const fish=[];
for(let i=0;i<n;i++){
const g=new T.Group();scene.add(g);const s=.55+rand()*.67;g.scale.setScalar(s);mesh(fishGeo,bodyM,g);const tail=mesh(finGeo,finM,g);
for(const sign of [-1,1])mesh(eyeG,eyeM,g).position.set(.24,.035,.061*sign);
fish.push({g,tail,a:(rand()-.5)*6,b:(rand()-.5)*2.6,c:(rand()-.5)*3.1,f:rand()*6.28});
}
schools.push({x,y,z,fish,p:rand()*6.28});
}
const mantas=[];
const mantaTop=mat(0x32697a,{side:T.DoubleSide,roughness:.57});
const mantaBelly=mat(0x85b9ad,{side:T.DoubleSide});
for(let i=0;i<3;i++){
const group=new T.Group();scene.add(group);group.scale.setScalar([1.15,.82,.89][i]);
const wings=[];
for(const side of [-1,1]){
const g=new T.PlaneGeometry(1,1,14,9);
const p=g.attributes.position;
for(let j=0;j<p.count;j++){
const u=p.getX(j)+.5,v=.5-p.getY(j);
const chord=Math.pow(Math.max(.0001,1-u),.76);
p.setXYZ(j,.52-.98*u+(1-v*2)*chord*.84,
.07*Math.sin(Math.PI*v)*chord-.16*u*u,side*u*2.58);
}
g.computeVertexNormals();
const base=new Float32Array(p.array);
const wing=mesh(g,mantaTop,group);
const belly=mesh(g,mantaBelly,group);belly.position.y=-.042;belly.scale.x=.94;belly.scale.z=.95;
wings.push({wing,base});
}
const body=mesh(new T.SphereGeometry(1,15,9),mantaTop,group);body.scale.set(.99,.18,.56);
const tailCurve=new T.CatmullRomCurve3([V(-.73,0,0),V(-1.33,-.06,0),V(-1.97,-.1,.07),V(-2.64,-.12,.19)]);
const tail=mesh(new T.TubeGeometry(tailCurve,14,.036,5,false),mantaTop,group);
for(const side of [-1,1]){
const lobe=mesh(new T.SphereGeometry(1,9,6),mantaTop,group);lobe.position.set(.81,-.042,side*.3);lobe.scale.set(.34,.065,.095);lobe.rotation.y=side*.22;
mesh(eyeG,eyeM,group).position.set(.61,.04,side*.405);
}
mantas.push({group,wings,tail});
}
const bubbleG=new T.BufferGeometry(), bubbles=[], bp=[],bs=[];
for(let i=0;i<142;i++){
const [x,z]=[[-7,9],[8,1],[14,-7],[-14,-11],[2,17],[-7,-18],[6,21]][i%7];
bubbles.push({x:x+(rand()-.5)*3,z:z+(rand()-.5)*3,p:rand(),s:.40+rand()*.61});bp.push(0,0,0);bs.push(.025+rand()*.09);
}
bubbleG.setAttribute('position',new T.Float32BufferAttribute(bp,3));bubbleG.setAttribute('size',new T.Float32BufferAttribute(bs,1));
const bubblesMesh=new T.Points(bubbleG,new T.ShaderMaterial({transparent:true,depthWrite:false,uniforms:{scale:{value:renderer.getPixelRatio()*420}},vertexShader:`
attribute float size;uniform float scale;varying float d;
void main(){vec4 p=modelViewMatrix*vec4(position,1.);d=-p.z;gl_Position=projectionMatrix*p;gl_PointSize=clamp(scale*size/max(1.,d),1.,28.);}
`,fragmentShader:`
varying float d;void main(){vec2 p=gl_PointCoord-.5;float r=length(p);float ring=smoothstep(.25,.44,r)*(1.-smoothstep(.45,.5,r));float glint=exp(-125.*dot(p-vec2(-.18,-.2),p-vec2(-.18,-.2)));gl_FragColor=vec4(.76,1.,.91,(ring*.49+glint*.66)*exp(-d*.027));}
`}));
bubblesMesh.frustumCulled=false;scene.add(bubblesMesh);
const speckG=new T.BufferGeometry(),specks=[];
for(let i=0;i<650;i++)specks.push((rand()-.5)*100,.8+rand()*16,(rand()-.5)*100);
speckG.setAttribute('position',new T.Float32BufferAttribute(specks,3));
const silt=new T.Points(speckG,new T.PointsMaterial({color:0xb5d9b5,size:.046,transparent:true,opacity:.49,depthWrite:false}));scene.add(silt);
const clamp=T.MathUtils.clamp, forward=V(0,0,0);
function canSwim(x,y,z){
if(x*x+z*z>54*54||y<ground(x,z)+.65||y>surface-.85)return false;
const dx=x-wreck.position.x,dz=z-wreck.position.z,c=Math.cos(wreck.rotation.y),s=Math.sin(wreck.rotation.y);
const localX=c*dx-s*dz,localZ=s*dx+c*dz;
if(Math.abs(localX)<6.45&&y<4.45&&Math.abs(localZ)<hull(clamp((localX+6)/12,0,1),1).z+.50)return false;
for(const b of blockers){
if(b.ship)continue;
const ex=(x-b.p.x)/(b.a+.46),ey=(y-b.p.y)/(b.b+.48),ez=(z-b.p.z)/(b.c+.46);
if(ex*ex+ey*ey+ez*ez<1)return false;
}
return true;
}
const path=new T.CatmullRomCurve3([
V(0,9.5,25),V(-3,6.8,15),V(-8,4.05,8),V(-9,3.5,2.5),V(-6,5.8,-6),
V(1,7,-11),V(9,8.8,-8),V(16.8,7.8,-4),V(17,8,7),V(11,10,15),V(4,11,23)
],true,'centripetal');
const looks=new T.CatmullRomCurve3([
V(-3,4,2),V(-8,4,2),V(-9,4,0),V(-8,4,-7),V(1,4,-9),
V(9,4,-1),V(9,4.4,-1),V(9,4.2,-1),V(9,4.4,-1),V(4,13,0),V(-3,10.2,3)
],true,'centripetal');
function setTour(on){
tour=on;speed=0;
if(on)tourTime=0;
$('tour').textContent=on?'Ⅱ Exit tour':'▷ Guided swim';
$('tour').setAttribute('aria-pressed',String(on));
$('hint').textContent=on?'Touch or click the water to take over':'Hold to swim · Drag to look';
}
$('tour').addEventListener('click',()=>setTour(!tour));
canvas.addEventListener('pointerdown',e=>{
if(hold!==null||e.pointerType==='mouse'&&e.button!==0)return;
if(tour)setTour(false);
hold=e.pointerId;px=e.clientX;py=e.clientY;canvas.setPointerCapture(hold);
});
canvas.addEventListener('pointermove',e=>{
if(hold!==e.pointerId)return;
yaw-=(e.clientX-px)*.0045;pitch=clamp(pitch-(e.clientY-py)*.0045,-1.45,1.45);
px=e.clientX;py=e.clientY;
});
function release(e){if(e.pointerId===hold)hold=null}
for(const event of ['pointerup','pointercancel','lostpointercapture'])canvas.addEventListener(event,release);
addEventListener('blur',()=>{hold=null});
if(location.hash==='#demo')setTour(true);
const clock=new T.Clock(),look=V(0,0,0),candidate=V(0,0,0);
let t=0;
renderer.setAnimationLoop(()=>{
const dt=Math.min(clock.getDelta(),.05);t+=dt;time.value=t;
if(tour){
const u=(tourTime%20)/20;tourTime+=dt;
path.getPoint(u,candidate);
if(canSwim(candidate.x,candidate.y,candidate.z))cameraRig.copy(candidate);
looks.getPoint(u,look);look.sub(cameraRig).normalize();
yaw=Math.atan2(-look.x,-look.z);pitch=Math.asin(clamp(look.y,-1,1));
}else{
speed=T.MathUtils.lerp(speed,hold===null?0:4.8,1-Math.exp(-dt*(hold===null?5:3)));
forward.set(-Math.sin(yaw)*Math.cos(pitch),Math.sin(pitch),-Math.cos(yaw)*Math.cos(pitch));
const d=dt*speed;
const {x,y,z}=cameraRig;
if(canSwim(x+forward.x*d,y,z))cameraRig.x+=forward.x*d;
if(canSwim(cameraRig.x,y+forward.y*d,z))cameraRig.y+=forward.y*d;
if(canSwim(cameraRig.x,cameraRig.y,z+forward.z*d))cameraRig.z+=forward.z*d;
}
camera.position.copy(cameraRig);camera.rotation.set(pitch,yaw,0,'YXZ');
$('depth').textContent=(surface-cameraRig.y).toFixed(1)+' m';
const locationName=Math.hypot(cameraRig.x-9,cameraRig.z+1)<10?'The shipwreck':Math.hypot(cameraRig.x+9,cameraRig.z-3)<8?'Stone arches':cameraRig.z>13?'Garden entrance':'Kelp forest';
if($('place').textContent!==locationName)$('place').textContent=locationName;
for(const school of schools){
const a=t*.33+school.p,angle=Math.atan2(3.1*Math.sin(a),4.9*Math.cos(a)),c=Math.cos(angle),s=Math.sin(angle);
for(const {g,tail,a:f,b,c:k,f:phase} of school.fish){
g.position.set(school.x+4.9*Math.sin(a)+f*c+k*s,school.y+b+.58*Math.sin(a*1.5)+.15*Math.sin(t*2.8+phase),school.z+3.1*Math.cos(a)-f*s+k*c);
g.rotation.set(.08*Math.sin(t*2+phase),angle+.09*Math.sin(t*6+phase),.09*Math.cos(a));
tail.rotation.y=.44*Math.sin(t*9+phase);
}
}
for(let i=0;i<mantas.length;i++){
const {group,wings,tail}=mantas[i],a=t*(.24+i*.025)+i*2.16+.56;
const xc=[-3,5,-13][i],zc=[5,-8,-11][i],rx=11+i*2,rz=7+i;
group.position.set(xc+rx*Math.sin(a),11.75+i*.76+1.0*Math.sin(a*1.4+i),zc+rz*Math.cos(a));
group.rotation.set(.14*Math.sin(a),Math.atan2(rz*Math.sin(a),rx*Math.cos(a)),.085*Math.cos(a));
for(const {wing,base} of wings){
const g=wing.geometry,p=g.attributes.position;
for(let j=0;j<p.count;j++){
const u=Math.abs(base[j*3+2])/2.58;
p.setY(j,base[j*3+1]+Math.pow(u,1.62)*(.69*Math.sin(t*2.05-u*1.2+i)+.17));
}
p.needsUpdate=true;g.computeVertexNormals();
}
tail.rotation.y=.13*Math.sin(t*2+i);
}
const p=bubbleG.attributes.position;
for(let i=0;i<bubbles.length;i++){
const b=bubbles[i],q=(b.p+t*b.s*.054)%1;
p.setXYZ(i,b.x+Math.sin(q*12+i)*q*.43,ground(b.x,b.z)+q*17.25,b.z+Math.cos(q*9+i)*q*.27);
}
p.needsUpdate=true;silt.position.x=.37*Math.sin(t*.23);silt.position.y=.13*Math.sin(t*.35);
renderer.render(scene,camera);
});
})();
</script></body></html>
