Download brain_lab/static/arcade-views.js from Chungulus/brain-lab: direct link, hf CLI and curl.
- Browser
- Download file 7.65 kB
-
https://huggingface.co/spaces/Chungulus/brain-lab/resolve/main/brain_lab/static/arcade-views.js
- Command line
-
hf download hf://spaces/Chungulus/brain-lab/brain_lab/static/arcade-views.js
-
curl -L -o arcade-views.js https://huggingface.co/spaces/Chungulus/brain-lab/resolve/main/brain_lab/static/arcade-views.js
7.65 kB
| import * as THREE from './vendor/three.module.js'; | |
| export async function flightView(canvas){ | |
| const names=['background-day','pipe-green','base','yellowbird-upflap','yellowbird-midflap','yellowbird-downflap','bluebird-midflap','redbird-midflap']; | |
| const images=await Promise.all(names.map(name=>new Promise(resolve=>{const i=new Image();i.onload=()=>resolve(i);i.onerror=()=>resolve(null);i.src='/static/flappy-assets/'+name+'.png'}))); | |
| const ctx=canvas.getContext('2d'); | |
| return {draw(frame){ | |
| if(!canvas.isConnected)return;const worldWidth=canvas.clientWidth<600?360:720,scale=900/worldWidth,height=Math.round(420*scale);if(canvas.width!==900)canvas.width=900;if(canvas.height!==height)canvas.height=height; | |
| ctx.setTransform(scale,0,0,scale,0,0);ctx.imageSmoothingEnabled=false;ctx.fillStyle='#8bc3d1';ctx.fillRect(0,0,worldWidth,420); | |
| for(let x=0;x<worldWidth;x+=288)if(images[0])ctx.drawImage(images[0],x,0,288,390); | |
| for(const pipe of frame.pipes){if(pipe.x>worldWidth+52)continue;const upper=pipe.center-pipe.gap/2,lower=pipe.center+pipe.gap/2; | |
| if(images[1]){ctx.drawImage(images[1],pipe.x,lower,52,320);ctx.save();ctx.translate(pipe.x,upper);ctx.scale(1,-1);ctx.drawImage(images[1],0,0,52,320);ctx.restore()} | |
| else{ctx.fillStyle='#75a44b';ctx.fillRect(pipe.x,0,52,upper);ctx.fillRect(pipe.x,lower,52,390-lower)} | |
| } | |
| const groundOffset=-(frame.tick*2.7%336);for(let x=groundOffset;x<worldWidth;x+=336)if(images[2])ctx.drawImage(images[2],x,390,336,112); | |
| const order=frame.runners.filter(r=>r.id!==frame.leader).concat(frame.runners.filter(r=>r.id===frame.leader)); | |
| for(const bird of order){if(bird.y>410||bird.y< -30)continue;const leader=bird.id===frame.leader;ctx.globalAlpha=bird.done&&bird.reason!=='goal'?.12:leader?1:.3;ctx.save();ctx.translate(bird.x,bird.y);ctx.rotate(Math.max(-.35,Math.min(1.15,bird.vy*.07)));const image=leader?images[3+Math.floor(frame.tick/5)%3]:images[bird.id%2?6:7];if(image)ctx.drawImage(image,-13,-10,26,20);else{ctx.fillStyle='#f5d569';ctx.beginPath();ctx.ellipse(0,0,13,10,0,0,Math.PI*2);ctx.fill()}if(leader){ctx.strokeStyle='#f9e88b';ctx.lineWidth=1.2;ctx.strokeRect(-16,-13,32,26)}ctx.restore()} | |
| ctx.globalAlpha=1; | |
| },dispose(){}}; | |
| } | |
| export function crossingView(canvas){ | |
| const renderer=new THREE.WebGLRenderer({canvas,antialias:true,alpha:false});renderer.setPixelRatio(Math.min(devicePixelRatio,2));renderer.setClearColor('#dcebdc');renderer.outputColorSpace=THREE.SRGBColorSpace;renderer.shadowMap.enabled=true;renderer.shadowMap.type=THREE.PCFSoftShadowMap; | |
| const scene=new THREE.Scene(),camera=new THREE.OrthographicCamera(-8.5,8.5,6,-6,.1,100); | |
| scene.add(new THREE.HemisphereLight('#fff7df','#71896e',2.5));const sun=new THREE.DirectionalLight('#fff4d0',3);sun.position.set(-8,16,8);sun.castShadow=true;sun.shadow.mapSize.set(1024,1024);sun.shadow.camera.left=-15;sun.shadow.camera.right=15;sun.shadow.camera.top=18;sun.shadow.camera.bottom=-18;scene.add(sun,sun.target); | |
| const geometry=new THREE.BoxGeometry(1,1,1),wheelGeometry=new THREE.CylinderGeometry(.14,.14,.12,8),materials=new Map(); | |
| function mat(color,opacity=1){const key=color+'/'+opacity;if(!materials.has(key))materials.set(key,new THREE.MeshStandardMaterial({color,roughness:.85,transparent:opacity<1,opacity,depthWrite:opacity===1}));return materials.get(key)} | |
| function box(parent,color,pos,size,opacity=1){const mesh=new THREE.Mesh(geometry,mat(color,opacity));mesh.position.set(...pos);mesh.scale.set(...size);mesh.castShadow=mesh.receiveShadow=true;parent.add(mesh);return mesh} | |
| const terrain=new THREE.Group(),traffic=new THREE.Group();scene.add(terrain,traffic);let courseSeed=null,lastW=0,lastH=0; | |
| const cars=new Map(),carColors=['#d47561','#e7bb60','#7a9dbb','#a28dbb','#d5957e']; | |
| function course(frame){ | |
| const origin=frame.window_start||0,key=[frame.seed,frame.goal,origin,frame.window_end].join('/');if(courseSeed===key)return;courseSeed=key;terrain.clear();traffic.clear();cars.clear(); | |
| for(const row of frame.rows){const z=-row.row+origin;box(terrain,row.type==='road'?'#45575a':row.row===frame.goal?'#a6c87b':row.row%2?'#91b475':'#9bbd7c',[0,-.15,z],[15,.25,1]); | |
| if(row.type==='road')for(let x=-6;x<=6;x++)box(terrain,'#c1c8ae',[x,.002,z],[.45,.02,.035]); | |
| } | |
| for(const [x,y] of frame.trees){const g=new THREE.Group();g.position.set(x,0,-y+origin);box(g,'#97774c',[0,.35,0],[.2,.7,.2]);box(g,'#4f8259',[0,.95,0],[.8,.9,.8]);box(g,'#659565',[.1,1.45,0],[.6,.4,.65]);terrain.add(g)} | |
| if(!frame.endless&&frame.goal>=origin&&frame.goal<=frame.window_end)for(let x=-4;x<=4;x++)box(terrain,x%2?'#f6efcf':'#456e50',[x,.01,-frame.goal+origin],[.95,.04,.5]); | |
| for(const car of frame.cars){const g=new THREE.Group();g.position.set(car.x,0,-car.row+origin);box(g,carColors[car.id%carColors.length],[0,.34,0],[car.length,.4,.62]);box(g,'#e6ead9',[-car.direction*.05,.66,0],[car.length*.45,.35,.55]);box(g,'#87b0ba',[car.direction*.21,.69,0],[.03,.22,.48]); | |
| for(const x of [-car.length*.31,car.length*.31])for(const z of [-.32,.32]){const wheel=new THREE.Mesh(wheelGeometry,mat('#334248'));wheel.rotation.x=Math.PI/2;wheel.position.set(x,.17,z);wheel.castShadow=true;g.add(wheel)}traffic.add(g);cars.set(car.id,g)} | |
| } | |
| const parts=[{p:[0,.45,0],s:[.54,.48,.6],c:'#f9f3d8'},{p:[0,.84,-.08],s:[.4,.4,.4],c:'#fff9e7'},{p:[0,.78,-.34],s:[.22,.13,.18],c:'#eab057'},{p:[0,1.1,-.08],s:[.16,.18,.31],c:'#ce6e58'},{p:[0,.49,.35],s:[.3,.26,.16],c:'#e0dfc8'},{p:[0,.08,0],s:[.44,.12,.28],c:'#e3a44f'}]; | |
| const flock=parts.map(part=>{const mesh=new THREE.InstancedMesh(geometry,mat(part.c,.45),128);mesh.castShadow=false;mesh.frustumCulled=false;mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);scene.add(mesh);return mesh}); | |
| const leader=new THREE.Group();parts.forEach(p=>box(leader,p.c,p.p,p.s));scene.add(leader); | |
| const matrix=new THREE.Matrix4(),position=new THREE.Vector3(),scale=new THREE.Vector3(),rotation=new THREE.Quaternion(),color=new THREE.Color(); | |
| function draw(frame){ | |
| if(!canvas.isConnected)return;course(frame);const rect=canvas.getBoundingClientRect();if(rect.width!==lastW||rect.height!==lastH){lastW=rect.width;lastH=rect.height;renderer.setSize(rect.width,rect.height,false);camera.left=-8.5;camera.right=8.5;camera.top=8.5*rect.height/rect.width;camera.bottom=-camera.top;camera.updateProjectionMatrix()} | |
| for(const car of frame.cars){const g=cars.get(car.id);if(g)g.position.x=car.x} | |
| const best=frame.runners[frame.leader]||frame.runners[0],origin=frame.window_start||0;const follow=(frame.endless?best.y:Math.min(Math.max(0,frame.goal-7),best.y))-origin;camera.position.set(8,11,10-follow);camera.lookAt(0,0,-follow-3);sun.position.set(-8,16,8-follow);sun.target.position.set(0,0,-follow); | |
| for(let j=0;j<parts.length;j++){const part=parts[j],mesh=flock[j];mesh.count=frame.runners.length;for(let i=0;i<frame.runners.length;i++){const r=frame.runners[i],shown=r.y>=origin-2&&r.y<=frame.window_end+2,flat=r.done&&r.reason!=='goal'?.2:1;position.set(r.x+part.p[0],part.p[1]*flat+(r.hop||0),shown?-r.y+origin+part.p[2]:0);scale.set(part.s[0]*(shown?1:0),part.s[1]*flat*(shown?1:0),part.s[2]*(shown?1:0));matrix.compose(position,rotation,scale);mesh.setMatrixAt(i,matrix);mesh.setColorAt(i,color.set(r.done&&r.reason!=='goal'?'#9daba0':'#ffffff'))}mesh.instanceMatrix.needsUpdate=true;if(mesh.instanceColor)mesh.instanceColor.needsUpdate=true} | |
| leader.position.set(best.x,best.hop||0,-best.y+origin);leader.scale.y=best.done&&best.reason!=='goal'?.2:1;renderer.render(scene,camera); | |
| } | |
| return {draw,dispose(){geometry.dispose();wheelGeometry.dispose();for(const m of materials.values())m.dispose();for(const mesh of flock)mesh.dispose();renderer.dispose()}}; | |
| } | |