Skip to content
VibeCodeBench
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.)15.3 KB · 213 lines · loads cdnjs.cloudflare.com/ajax/libs/three.js/r128/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">
<title>Kelp Garden Swim</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#0d4f73;font-family:system-ui,sans-serif}
canvas{display:block;touch-action:none}
#hint{position:fixed;left:50%;bottom:22px;transform:translateX(-50%);color:#ffe7b0;background:rgba(6,30,50,.55);padding:12px 20px;border-radius:30px;font-size:16px;pointer-events:none;text-align:center;transition:opacity 1s}
#up{position:fixed;right:16px;top:16px;width:64px;height:64px;border-radius:50%;border:2px solid #ffc24a;background:rgba(6,30,50,.55);color:#ffc24a;font-size:26px}
</style>
</head>
<body>
<div id="hint">Press &amp; hold to glide &middot; drag to turn</div>
<button id="up" aria-label="Level out">&#8635;</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
(function(){
const FOG=0x0d4f73, SURF=32;
const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.setSize(innerWidth,innerHeight);
document.body.appendChild(renderer.domElement);
const scene=new THREE.Scene();
scene.background=new THREE.Color(FOG);
scene.fog=new THREE.FogExp2(FOG,0.022);
const camera=new THREE.PerspectiveCamera(65,innerWidth/innerHeight,0.1,400);
scene.add(new THREE.HemisphereLight(0x7fe0d8,0x0a2a44,0.9));
const sun=new THREE.DirectionalLight(0xffd88a,0.8);sun.position.set(20,60,10);scene.add(sun);
const T={value:0};

function H(x,z){return 1.2*Math.sin(x*0.08)*Math.cos(z*0.07)+0.6*Math.sin(x*0.21+z*0.17);}
const solids=[];

// floor with caustics
const fg=new THREE.PlaneGeometry(400,400,140,140);fg.rotateX(-Math.PI/2);
const fp=fg.attributes.position;
for(let i=0;i<fp.count;i++)fp.setY(i,H(fp.getX(i),fp.getZ(i)));
fg.computeVertexNormals();
const fm=new THREE.MeshLambertMaterial({color:0xc9a35a});
fm.onBeforeCompile=s=>{
 s.uniforms.uTime=T;
 s.vertexShader='varying vec3 vW;\n'+s.vertexShader.replace('#include <begin_vertex>','#include <begin_vertex>\nvW=(modelMatrix*vec4(transformed,1.0)).xyz;');
 s.fragmentShader='uniform float uTime;varying vec3 vW;\n'+s.fragmentShader.replace('#include <fog_fragment>',
 `vec2 p=vW.xz*0.35;float t=uTime*0.6;
 float c=abs(sin(p.x+sin(p.y*1.3+t)*1.2)+sin(p.y+sin(p.x*1.1-t*0.9)*1.2));
 float c2=abs(sin(p.x*1.7-t+sin(p.y*1.9)*0.8)+sin(p.y*1.6+t*1.1+sin(p.x*1.4)));
 float k=pow(1.0-min(c*0.5,1.0),5.0)+pow(1.0-min(c2*0.5,1.0),5.0)*0.7;
 gl_FragColor.rgb+=vec3(1.0,0.85,0.5)*k*0.55;
 #include <fog_fragment>`);
};
scene.add(new THREE.Mesh(fg,fm));

// surface
const sm=new THREE.ShaderMaterial({side:THREE.DoubleSide,uniforms:{uTime:T,fogC:{value:new THREE.Color(FOG)}},
 vertexShader:'varying vec3 vW;void main(){vW=(modelMatrix*vec4(position,1.0)).xyz;gl_Position=projectionMatrix*viewMatrix*vec4(vW,1.0);}',
 fragmentShader:`uniform float uTime;uniform vec3 fogC;varying vec3 vW;
 void main(){vec2 p=vW.xz*0.15;float t=uTime*0.5;
 float r=sin(p.x*2.0+sin(p.y*1.5+t)*2.0+t)*sin(p.y*2.3+sin(p.x*1.2-t)*2.0-t*0.7);
 vec3 col=mix(vec3(0.15,0.65,0.7),vec3(1.0,0.92,0.65),smoothstep(0.2,0.9,r));
 float d=length(vW-cameraPosition);
 gl_FragColor=vec4(mix(fogC,col,exp(-d*0.018)),1.0);}`});
const surf=new THREE.Mesh(new THREE.PlaneGeometry(600,600),sm);surf.rotation.x=Math.PI/2;surf.position.y=SURF;scene.add(surf);

// sunbeams
const bm=new THREE.ShaderMaterial({transparent:true,depthWrite:false,blending:THREE.AdditiveBlending,side:THREE.DoubleSide,uniforms:{uTime:T},
 vertexShader:'varying vec2 vU;varying float vO;void main(){vU=uv;vO=modelMatrix[3].x*0.3+modelMatrix[3].z*0.2;gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}',
 fragmentShader:'uniform float uTime;varying vec2 vU;varying float vO;void main(){float a=smoothstep(0.0,0.5,vU.x)*smoothstep(1.0,0.5,vU.x)*pow(vU.y,1.5)*(0.6+0.4*sin(uTime*0.7+vO));gl_FragColor=vec4(1.0,0.86,0.55,a*0.12);}'});
const beams=new THREE.Group();scene.add(beams);
for(let i=0;i<18;i++){const g=new THREE.Group();
 for(let j=0;j<2;j++){const m=new THREE.Mesh(new THREE.PlaneGeometry(4+Math.random()*4,46),bm);m.rotation.y=j*Math.PI/2;g.add(m);}
 g.position.set((Math.random()-.5)*140,SURF-22,(Math.random()-.5)*140);g.rotation.z=0.32;g.rotation.x=0.12;beams.add(g);}

// rocks
const rockMat=new THREE.MeshStandardMaterial({color:0x2f6f78,roughness:1,flatShading:true});
const rockMat2=new THREE.MeshStandardMaterial({color:0x1f4f6a,roughness:1,flatShading:true});
function rock(x,y,z,s,mat){const g=new THREE.DodecahedronGeometry(s,1);const p=g.attributes.position;
 for(let i=0;i<p.count;i++){const f=0.85+Math.random()*0.25;p.setXYZ(i,p.getX(i)*f,p.getY(i)*f,p.getZ(i)*f);}
 g.computeVertexNormals();const m=new THREE.Mesh(g,mat||(Math.random()<.5?rockMat:rockMat2));m.position.set(x,y,z);m.rotation.set(Math.random()*3,Math.random()*3,0);scene.add(m);solids.push({x,y,z,r:s*0.95});}
function arch(cx,cz,R,rot,s){const c=Math.cos(rot),sn=Math.sin(rot);const n=Math.ceil(Math.PI*R/(s*1.1));
 for(let i=0;i<=n;i++){const a=Math.PI*i/n;const lx=Math.cos(a)*R;const x=cx+lx*c,z=cz+lx*sn;rock(x,H(x,z)+Math.sin(a)*R,z,s*(0.9+Math.random()*0.25));}}
arch(0,8,7.5,0,1.9);arch(-28,-20,6,1.1,1.6);arch(35,25,8,-0.5,2);arch(-10,-60,6.5,0.3,1.7);
for(let i=0;i<40;i++){const x=(Math.random()-.5)*180,z=(Math.random()-.5)*180;if(Math.hypot(x,z-25)<8)continue;const s=0.8+Math.random()*2.5;rock(x,H(x,z)+s*0.3,z,s);}

// ship
const ship=new THREE.Group();const wood=new THREE.MeshStandardMaterial({color:0x8a4a22,roughness:.9,flatShading:true});
const wood2=new THREE.MeshStandardMaterial({color:0x5e3418,roughness:1,flatShading:true});
const hg=new THREE.BoxGeometry(16,3.4,4.6,8,2,2);const hp=hg.attributes.position;
for(let i=0;i<hp.count;i++){let x=hp.getX(i),y=hp.getY(i),z=hp.getZ(i);if(y<0)z*=0.35;const ax=Math.abs(x);if(ax>5)z*=1-(ax-5)/3*0.75;if(x>5&&y>0)y+= (x-5)*0.25;hp.setXYZ(i,x,y,z);}
hg.computeVertexNormals();ship.add(new THREE.Mesh(hg,wood));
const deck=new THREE.Mesh(new THREE.BoxGeometry(10,0.3,4.2),wood2);deck.position.y=1.6;ship.add(deck);
const cab=new THREE.Mesh(new THREE.BoxGeometry(3.5,2.2,3.4),wood);cab.position.set(-4.5,2.8,0);ship.add(cab);
const mast=new THREE.Mesh(new THREE.CylinderGeometry(0.25,0.32,10,7),wood2);mast.position.set(1,6.5,0);mast.rotation.z=-0.15;ship.add(mast);
const yard=new THREE.Mesh(new THREE.CylinderGeometry(0.15,0.15,6,6),wood2);yard.rotation.x=Math.PI/2;yard.position.set(1.7,9,0);ship.add(yard);
const mast2=new THREE.Mesh(new THREE.CylinderGeometry(0.22,0.3,5,7),wood2);mast2.position.set(-4.5,5.5,0);ship.add(mast2);
ship.position.set(22,H(22,-30)+1.2,-30);ship.rotation.set(0.05,0.6,0.28);scene.add(ship);ship.updateMatrixWorld(true);
const tv=new THREE.Vector3();
for(let x=-7;x<=7.5;x+=1.6){tv.set(x,0.2,0).applyMatrix4(ship.matrixWorld);solids.push({x:tv.x,y:tv.y,z:tv.z,r:2.7});}
for(let y=3;y<=11;y+=1.2){tv.set(1+(-(y-6.5))*0.15*0,y,0).applyMatrix4(ship.matrixWorld);solids.push({x:tv.x,y:tv.y,z:tv.z,r:0.8});}
for(let y=3;y<=8;y+=1.2){tv.set(-4.5,y,0).applyMatrix4(ship.matrixWorld);solids.push({x:tv.x,y:tv.y,z:tv.z,r:1.9});}
for(let z=-3;z<=3;z+=1.5){tv.set(1.7,9,z).applyMatrix4(ship.matrixWorld);solids.push({x:tv.x,y:tv.y,z:tv.z,r:0.6});}

// coral
const corC=[0xff8a3d,0xffc24a,0x2ec4b6,0xff6f2c];
function coral(x,z){const g=new THREE.Group();const col=corC[Math.random()*4|0];const m=new THREE.MeshStandardMaterial({color:col,roughness:.7,emissive:col,emissiveIntensity:0.15});
 if(Math.random()<.4){const b=new THREE.Mesh(new THREE.SphereGeometry(0.6+Math.random()*0.6,10,8),m);b.scale.y=0.6;g.add(b);}
 else for(let i=0;i<6;i++){const h=0.8+Math.random()*1.6;const c=new THREE.Mesh(new THREE.CylinderGeometry(0.06,0.14,h,5),m);c.position.y=h/2;c.rotation.set((Math.random()-.5)*1.2,0,(Math.random()-.5)*1.2);g.add(c);const tip=new THREE.Mesh(new THREE.SphereGeometry(0.12,6,5),m);tip.position.set(0,h,0);c.add(tip);tip.position.y=h/2;}
 g.position.set(x,H(x,z),z);scene.add(g);}
for(let i=0;i<140;i++){const a=Math.random()*6.28,r=4+Math.random()*90;coral(Math.cos(a)*r,Math.sin(a)*r);}

// kelp
const KL=14;const kg=new THREE.PlaneGeometry(0.7,KL,1,18);kg.translate(0,KL/2,0);
const kp=kg.attributes.position;for(let i=0;i<kp.count;i++){const y=kp.getY(i);kp.setX(i,kp.getX(i)*(0.6+0.5*Math.abs(Math.sin(y*1.3)))+Math.sin(y*0.7)*0.2);}
const km=new THREE.MeshLambertMaterial({side:THREE.DoubleSide});
km.onBeforeCompile=s=>{s.uniforms.uTime=T;s.vertexShader='uniform float uTime;\n'+s.vertexShader.replace('#include <begin_vertex>',
`#include <begin_vertex>
float h=position.y;vec3 ip=vec3(0.0);
#ifdef USE_INSTANCING
ip=instanceMatrix[3].xyz;
#endif
float sw=h*h*0.011;
transformed.x+=sin(uTime*0.9+ip.x*0.15+h*0.22)*sw;
transformed.z+=cos(uTime*0.7+ip.z*0.15+h*0.18)*sw*0.7;`);};
const KN=520;const kelp=new THREE.InstancedMesh(kg,km,KN);const dm=new THREE.Object3D();const kc=new THREE.Color();
let ki=0;while(ki<KN){const cx=(Math.random()-.5)*190,cz=(Math.random()-.5)*190;
 for(let j=0;j<10&&ki<KN;j++){const x=cx+(Math.random()-.5)*10,z=cz+(Math.random()-.5)*10;
  let bad=Math.hypot(x,z-30)<4;for(const s of solids)if(Math.hypot(x-s.x,z-s.z)<s.r+0.5){bad=true;break;}if(bad)continue;
  dm.position.set(x,H(x,z)-0.2,z);dm.rotation.set(0,Math.random()*6.28,0);dm.scale.set(1,0.6+Math.random()*1.1,1);dm.updateMatrix();kelp.setMatrixAt(ki,dm.matrix);
  kc.setHex(Math.random()<.8?0x3f9a3a:0xb8a13a).multiplyScalar(0.7+Math.random()*0.5);kelp.setColorAt(ki,kc);ki++;}}
scene.add(kelp);

// fish schools
const fgm=new THREE.ConeGeometry(0.16,0.7,5);fgm.rotateX(Math.PI/2);
const PER=45,SC=4;const fish=new THREE.InstancedMesh(fgm,new THREE.MeshStandardMaterial({roughness:.4,metalness:.3}),PER*SC);
const schools=[];const fcol=[0xffc24a,0xff8a3d,0x2ec4b6,0xffd77a];const fishes=[];
for(let s=0;s<SC;s++){const sc={c:new THREE.Vector3((Math.random()-.5)*60,6+Math.random()*10,(Math.random()-.5)*60),v:new THREE.Vector3(1,0,0),dir:new THREE.Vector3(1,0,0),tt:0};schools.push(sc);
 for(let i=0;i<PER;i++){const off=new THREE.Vector3((Math.random()-.5)*6,(Math.random()-.5)*3,(Math.random()-.5)*6);fishes.push({s:sc,off,p:sc.c.clone().add(off),v:new THREE.Vector3(1,0,0)});
  fish.setColorAt(s*PER+i,kc.setHex(fcol[s]).multiplyScalar(0.8+Math.random()*0.4));}}
scene.add(fish);

// mantas
const mantas=[];const mmat=new THREE.MeshStandardMaterial({color:0x163f63,roughness:.6,side:THREE.DoubleSide,flatShading:true});
for(let i=0;i<3;i++){const g=new THREE.PlaneGeometry(7,4,14,6);g.rotateX(-Math.PI/2);const p=g.attributes.position;
 for(let k=0;k<p.count;k++){const z=p.getZ(k);p.setX(k,p.getX(k)*Math.max(0.06,1-Math.abs(z+0.4)/2.4));}
 const base=Float32Array.from(p.array);const m=new THREE.Mesh(g,mmat);
 const tail=new THREE.Mesh(new THREE.CylinderGeometry(0.03,0.08,4,4),mmat);tail.rotation.x=Math.PI/2;tail.position.z=3.6;m.add(tail);
 scene.add(m);mantas.push({m,base,R:30+i*18,h:10+i*4,sp:0.05+i*0.015,ph:i*2.1});}

// bubbles
const cv=document.createElement('canvas');cv.width=cv.height=32;const cx=cv.getContext('2d');
cx.strokeStyle='rgba(255,255,255,0.9)';cx.lineWidth=3;cx.beginPath();cx.arc(16,16,12,0,7);cx.stroke();cx.fillStyle='rgba(200,255,250,0.25)';cx.fill();
const BN=350;const bg=new THREE.BufferGeometry();const bp=new Float32Array(BN*3);const bvel=[];
const vents=[[0,8],[22,-30],[-28,-20],[35,25],[-10,-60],[10,40]];
function resetB(i,anyY){const v=vents[Math.random()*vents.length|0];const x=v[0]+(Math.random()-.5)*4,z=v[1]+(Math.random()-.5)*4;bp[i*3]=x;bp[i*3+2]=z;bp[i*3+1]=anyY?H(x,z)+Math.random()*30:H(x,z);bvel[i]=1+Math.random()*1.5;}
for(let i=0;i<BN;i++)resetB(i,true);
bg.setAttribute('position',new THREE.BufferAttribute(bp,3));
const bubbles=new THREE.Points(bg,new THREE.PointsMaterial({size:0.35,map:new THREE.CanvasTexture(cv),transparent:true,depthWrite:false}));scene.add(bubbles);

// player
const pos=new THREE.Vector3(0,3.5,34);let yaw=0,pitch=0,speed=0,holding=false,lx=0,ly=0;
let demo=location.hash==='#demo',demoT=0;
const hint=document.getElementById('hint');
const el=renderer.domElement;
el.addEventListener('pointerdown',e=>{if(e.isTrusted&&demo){demo=false;}holding=true;lx=e.clientX;ly=e.clientY;try{el.setPointerCapture(e.pointerId);}catch(_){}hint.style.opacity=0;});
el.addEventListener('pointermove',e=>{if(!holding)return;yaw-=(e.clientX-lx)*0.005;pitch-=(e.clientY-ly)*0.004;pitch=Math.max(-1.2,Math.min(1.2,pitch));lx=e.clientX;ly=e.clientY;});
const up=()=>holding=false;el.addEventListener('pointerup',up);el.addEventListener('pointercancel',up);
document.getElementById('up').addEventListener('pointerdown',e=>{e.stopPropagation();if(e.isTrusted)demo=false;pitch=0;});

function collide(p){for(const s of solids){const dx=p.x-s.x,dy=p.y-s.y,dz=p.z-s.z,d=Math.hypot(dx,dy,dz),r=s.r+0.8;if(d<r&&d>1e-4){const k=(r-d)/d;p.x+=dx*k;p.y+=dy*k;p.z+=dz*k;}}
 const f=H(p.x,p.z)+1;if(p.y<f)p.y=f;if(p.y>SURF-1.5)p.y=SURF-1.5;
 const R=Math.hypot(p.x,p.z);if(R>170){p.x*=170/R;p.z*=170/R;}}

const fwd=new THREE.Vector3(),tmp=new THREE.Vector3(),des=new THREE.Vector3();
const clock=new THREE.Clock();
function frame(){requestAnimationFrame(frame);const dt=Math.min(clock.getDelta(),0.05);T.value+=dt;const t=T.value;
 let go=holding;
 if(demo){demoT+=dt;if(demoT>20){demoT=0;pos.set(0,3.5,34);}const d=demoT;go=true;
  yaw=-0.75*smooth(7,13,d)+0.1*Math.sin(d*0.5);pitch=0.25*smooth(9,14,d)-0.05;if(d<0.05){yaw=0;pitch=0;}hint.style.opacity=0;}
 speed+=((go?6:0)-speed)*Math.min(1,dt*(go?1.5:1.0));
 fwd.set(-Math.sin(yaw)*Math.cos(pitch),Math.sin(pitch),-Math.cos(yaw)*Math.cos(pitch));
 pos.addScaledVector(fwd,speed*dt);collide(pos);
 camera.position.copy(pos);camera.position.y+=Math.sin(t*1.2)*0.08;
 camera.rotation.set(pitch,yaw,Math.sin(t*0.6)*0.02,'YXZ');
 beams.position.set(Math.round(pos.x/140)*140*0,0,0);
 // schools
 for(const s of schools){s.tt-=dt;if(s.tt<=0){s.tt=4+Math.random()*5;s.dir.set(Math.random()-.5,(Math.random()-.5)*0.3,Math.random()-.5).normalize();}
  des.copy(s.dir);
  if(s.c.y<H(s.c.x,s.c.z)+4)des.y+=1;if(s.c.y>SURF-6)des.y-=1;
  const R=Math.hypot(s.c.x,s.c.z);if(R>80){des.x-=s.c.x/R*2;des.z-=s.c.z/R*2;}
  for(const o of solids){tmp.subVectors(s.c,tmp.set(o.x,o.y,o.z));const d=tmp.length();if(d<o.r+5)des.addScaledVector(tmp.normalize(),2);}
  des.normalize();s.v.lerp(des.multiplyScalar(3),dt*1.2);s.c.addScaledVector(s.v,dt);}
 for(let i=0;i<fishes.length;i++){const f=fishes[i];tmp.copy(f.s.c).add(f.off).sub(f.p).multiplyScalar(1.5).add(f.s.v);
  f.v.lerp(tmp,dt*2.5);f.p.addScaledVector(f.v,dt);
  dm.position.copy(f.p);tmp.copy(f.p).add(f.v);dm.scale.set(1,1,1);dm.lookAt(tmp);dm.rotateY(Math.sin(t*12+i)*0.15);dm.updateMatrix();fish.setMatrixAt(i,dm.matrix);}
 fish.instanceMatrix.needsUpdate=true;
 // mantas
 for(const M of mantas){const a=t*M.sp+M.ph;M.m.position.set(Math.cos(a)*M.R,M.h+Math.sin(a*2)*2,Math.sin(a)*M.R);
  M.m.rotation.set(0,-a+Math.PI,Math.sin(t*0.8+M.ph)*0.15);
  const p=M.m.geometry.attributes.position;for(let k=0;k<p.count;k++){const x=M.base[k*3],z=M.base[k*3+2];p.setY(k,Math.sin(t*1.6+M.ph-z*0.6)*x*x*0.12);}
  p.needsUpdate=true;M.m.geometry.computeVertexNormals();}
 // bubbles
 for(let i=0;i<BN;i++){bp[i*3+1]+=bvel[i]*dt;bp[i*3]+=Math.sin(t*2+i)*0.01;if(bp[i*3+1]>SURF)resetB(i,false);}
 bg.attributes.position.needsUpdate=true;
 renderer.render(scene,camera);}
function smooth(a,b,x){x=Math.max(0,Math.min(1,(x-a)/(b-a)));return x*x*(3-2*x);}
addEventListener('resize',()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);});
setTimeout(()=>{if(!holding)hint.style.opacity=0.9;},100);
frame();
})();
</script>
</body>
</html>