/* The background on the card, painted first each frame: the sea, the land, the waves, the clouds and the cables, as the 2D map paints them (drawFrame(), paintWorld()) and in the same colours. The land and the cables are geometry built once, so panning and zooming repaint nothing; only the waves and the clouds move. */ const glPm=s=>{const [r,g,b,a]=glColor(s);return [r*a,g*a,b*a,a];}; // premultiplied, as the card blends const SEA_TOP=glPm('#62CBEA'),SEA_BOT=glPm('#43AED6'),CLOUD_COL=glPm('rgba(255,255,255,.55)'); const LAND_SHADOW=glPm('rgba(20,90,130,.35)'),LAND_GRASS=glPm('#9BDA6C'),HILL_COL=glPm('#86CB5B'),HILL_SHINE=glPm('rgba(255,255,255,.18)'); // The land's fill: a fan of triangles per coastline, counted in the stencil (each pixel flips once per triangle over // it, so what is inside ends odd), then one cover where the stencil is odd. uOff shifts it in CSS px (the shadow). const FAN_VS=GL_HEAD+GL_CLIP+`layout(location=0) in vec2 aV;uniform vec2 uView,uOff;uniform vec3 uCam; void main(){gl_Position=clip((aV-uCam.xy)*uCam.z+uView*.5+uOff,uView,0.);}`; const FAN_FS=GL_HEAD+`out vec4 o;void main(){o=vec4(0.);}`; // Ellipses (the hills and their shine): centre and radii in map units, one premultiplied colour, smooth edges. const ELL_VS=GL_HEAD+GL_CLIP+`layout(location=0) in vec4 aE;uniform vec2 uView;uniform vec3 uCam;out vec2 vq;flat out vec2 vr; void main(){vec2 c=(aE.xy-uCam.xy)*uCam.z+uView*.5,r=aE.zw*uCam.z,m=r+1.;vq=vec2((gl_VertexID&1)==0?-m.x:m.x,(gl_VertexID&2)==0?-m.y:m.y);vr=r;gl_Position=clip(c+vq,uView,0.);}`; const ELL_FS=GL_HEAD+`in vec2 vq;flat in vec2 vr;uniform vec4 uCol;uniform float uDpr;out vec4 o; void main(){float d=(length(vq/vr)-1.)*min(vr.x,vr.y),a=clamp(.5-d*uDpr,0.,1.);if(a<=0.)discard;o=uCol*a;}`; // Discs for the stencil only (the clouds' puffs): centre and radius in map units. const CIRC_VS=GL_HEAD+GL_CLIP+`layout(location=0) in vec4 aC;uniform vec2 uView;uniform vec3 uCam;out vec2 vq;flat out float vr; void main(){vec2 c=(aC.xy-uCam.xy)*uCam.z+uView*.5;float r=aC.z*uCam.z,m=r+1.;vq=vec2((gl_VertexID&1)==0?-m:m,(gl_VertexID&2)==0?-m:m);vr=r;gl_Position=clip(c+vq,uView,0.);}`; const CIRC_FS=GL_HEAD+`in vec2 vq;flat in float vr;out vec4 o;void main(){if(length(vq)>vr)discard;o=vec4(1.);}`; /* The land, built once per level of detail (the far view leaves out the islets as LANDP_FAR does) and per zoom level lv (powers of 2): the fan for the fills, the coastlines as line segments (paintWorld()'s curves, four segments each) for the sand and the dark edge, and the hills. Zoomed out a coastline has far more points than pixels, and its vertices were most of the card's work: at level lv it is simplified so that no point moves more than COAST_TOL px at the level's zoom (Douglas-Peucker). Without lv, the whole coastline. COAST and onLand() are not touched. */ const COAST_TOL=0.25,COASTP=[]; // A coastline as paintWorld() draws it (smoothPath(): a quadratic curve from the middle of each side to the next, // through the corner between them), four segments per curve; the first point is repeated at the end. function coastPts(ri){let P=COASTP[ri];if(P)return P;const ring=COAST[ri],n=ring.length;P=[]; const mid=(a,b)=>[(a[0]+b[0])/2,(a[1]+b[1])/2];let m=mid(ring[n-1],ring[0]);P.push(m); for(let i=0;i0))return P;const keep=new Uint8Array(n),st=[0,n-1],t2=tol*tol;keep[0]=keep[n-1]=1; while(st.length){const b=st.pop(),a=st.pop(),ax=P[a][0],ay=P[a][1],dx=P[b][0]-ax,dy=P[b][1]-ay,L=dx*dx+dy*dy||1e-12;let m=-1,md=t2; for(let i=a+1;i1?1:t;const ex=ax+t*dx-P[i][0],ey=ay+t*dy-P[i][1],d=ex*ex+ey*ey;if(d>md){md=d;m=i;}} if(m>=0){keep[m]=1;st.push(a,m,m,b);}} const o=[];for(let i=0;icur||z{if(keep&&!keep.has(LANDP_ALL[ri]))return;if(ring.length<3)return; const P=coastSimplify(coastPts(ri),tol);if(P.length<4)return; for(let i=1;i+1new Float32Array(a); return GLX.land[key]={fan:glVao(gl,[2],0,F(fan)).v,nFan:fan.length/2,sand:glVao(gl,[4,4,4],1,F(sand)).v,edge:glVao(gl,[4,4,4],1,F(edge)).v,nSeg:sand.length/LINE_STRIDE, hills:glVao(gl,[4],1,F(hills)).v,shine:glVao(gl,[4],1,F(shine)).v,nHill:HILLS.length};} // Fills the land's shape (shifted by ox, oy CSS px) with one colour, for every copy of the map. function glLandFill(gl,G,ks,ox,oy,col){const P=GLX.fan;gl.enable(gl.STENCIL_TEST); gl.useProgram(P.p);gl.uniform2f(P.u.uView,CW,CH);gl.uniform2f(P.u.uOff,ox,oy);gl.bindVertexArray(G.fan); gl.colorMask(false,false,false,false);gl.stencilFunc(gl.ALWAYS,0,1);gl.stencilOp(gl.KEEP,gl.KEEP,gl.INVERT);gl.stencilMask(1); for(const k of ks){gl.uniform3f(P.u.uCam,cam.x-k*W,cam.y,cam.z);gl.drawArrays(gl.TRIANGLES,0,G.nFan);} // The cover paints where the count is odd and sets the stencil back to zero for the next user. gl.colorMask(true,true,true,true);gl.stencilFunc(gl.NOTEQUAL,0,1);gl.stencilOp(gl.ZERO,gl.ZERO,gl.ZERO);gl.stencilMask(255); glFill(gl,col);gl.disable(gl.STENCIL_TEST);} function glEllipses(gl,vao,n,ks,col){const P=GLX.ell;gl.useProgram(P.p);gl.bindVertexArray(vao);gl.uniform2f(P.u.uView,CW,CH);gl.uniform1f(P.u.uDpr,DPR);gl.uniform4fv(P.u.uCol,col); for(const k of ks){gl.uniform3f(P.u.uCam,cam.x-k*W,cam.y,cam.z);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,n);}} // In paintWorld()'s order: the shadow (5, 7 px down and right), the sand, the grass, the hills, the coastline. function glLand(gl,ks){const G=glLandGeo(gl,lodFar(),coastLevel(cam.z)); glLandFill(gl,G,ks,5,7,LAND_SHADOW); glLines(gl,G.sand,G.nSeg,ks); glLandFill(gl,G,ks,0,0,LAND_GRASS); glEllipses(gl,G.hills,G.nHill,ks,HILL_COL);glEllipses(gl,G.shine,G.nHill,ks,HILL_SHINE); glLines(gl,G.edge,G.nSeg,ks);} // The waves: each one two quadratic curves of four segments, swaying with the map's clock as in drawFrame(). They are // written into the same array every frame (no garbage for the collector to pick up while you play). const WAVE_COL=glColor('rgba(255,255,255,.45)'); function glWaves(gl,now,ks){const A=GLX.wa||(GLX.wa=new Float32Array(WAVES.length*8*LINE_STRIDE)),ph0=now/900;let m=0; for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3; // Two curves: up from x-9 to x, then down to x+9 (no array literals per wave: they were garbage every frame). for(let c=0;c<2;c++){const x0=x+o+(c?0:-9),cx=x0+4.5,cy=y+(c?4:-4),x1=x0+9;let px=x0,py=y; for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}} gl.bindBuffer(gl.ARRAY_BUFFER,GLX.wv.b);gl.bufferData(gl.ARRAY_BUFFER,A.subarray(0,m*LINE_STRIDE),gl.DYNAMIC_DRAW);glLines(gl,GLX.wv.v,m,ks);} // The clouds: their puffs are stencilled as one shape, then filled once, so overlapping puffs do not stack. function glClouds(gl,now,ks){const need=ks.length*CLOUDS.length*12;if(!need)return; // Written into one array kept from frame to frame, grown when more copies of the map show. if(!GLX.ca||GLX.ca.lengthv!==GLX.ckey[i])){const A=[]; for(const cp of CABLE_PAIRS){const hot=!!selC&&(cp.a===selC||cp.b===selC);if(ar&&!hot&&!(arKind(cp.a)&&arKind(cp.b)))continue; const C=CAB[cp.cid],[r,g,b]=glColor(cableCol(C)),col=[r,g,b,hot?.95:.4];let cum=0; for(let j=0;j+1