WebGL map: fill overlapping coastlines as land, as the 2D map does

The card filled the land by an odd/even count over all the coastlines at once, so two coasts that overlap cancelled
out into sea: Timor, drawn by two coastlines, lost most of its inside and its shadow when zoomed in. The 2D map fills
each coast on its own. The fan now turns every coast the same way and the stencil counts windings (+1 and -1 by the
triangles' facing, filled where not zero), which is the 2D canvas's nonzero rule: an overlap counts 2 and stays land.
Same number of draws.
This commit is contained in:
Antoine 2026-11-04 23:04:48 +01:00
parent b03bec267c
commit fe109cff84
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
2 changed files with 56 additions and 4 deletions

View file

@ -41,11 +41,16 @@ function coastSimplify(P,tol){const n=P.length;if(n<4||!(tol>0))return P;const k
// around a power of 2 does not switch back and forth.
function coastLevel(z){const want=Math.floor(Math.log2(Math.max(z,1/64))),cur=GLX.coastLv;
if(cur===undefined||want>cur||z<Math.pow(2,cur)/1.1)GLX.coastLv=want;return GLX.coastLv;}
/* A coastline as a fan of triangles from its first point, all turned the same way (anticlockwise on the map): summed
with their signs, they count how many times the coast winds round each point. Every coast counts +1 inside, so where
two coasts overlap (Timor is drawn twice) the land is 2, still land, as each coast filled on its own in 2D. */
function coastFan(P,out){let a=0;const n=P.length;for(let i=0;i<n;i++){const p=P[i],q=P[(i+1)%n];a+=p[0]*q[1]-q[0]*p[1];}
const s=a<0?1:0;for(let i=1;i+1<n;i++){const b=P[i+s],c=P[i+1-s];out.push(P[0][0],P[0][1],b[0],b[1],c[0],c[1]);}return out;}
function glLandGeo(gl,far,lv){const key=(far?'far':'all')+(lv===undefined?'':'@'+lv);let G=GLX.land[key];if(G)return G;
const keep=far?new Set(LANDP_FAR):null,tol=lv===undefined?0:COAST_TOL/Math.pow(2,lv),fan=[],sand=[],edge=[],SAND=glColor('#F8E6A8'),EDGE=glColor('#1E2640');
COAST.forEach((ring,ri)=>{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+1<P.length;i++)fan.push(P[0][0],P[0][1],P[i][0],P[i][1],P[i+1][0],P[i+1][1]);
coastFan(P,fan);
// Round ends make the joins round, as lineJoin='round' does; both lines are opaque, so the overlaps do not show.
for(let i=0;i+1<P.length;i++){glSeg(sand,P[i],P[i+1],0,10,0,LINE_ROUND,SAND);glSeg(edge,P[i],P[i+1],0,2.5,0,LINE_ROUND,EDGE);}});
const hills=[],shine=[];for(const [x,y,r] of HILLS){hills.push(x,y,r*1.4,r*.8);shine.push(x-r*.3,y-r*.25,r*.6,r*.3);}
@ -54,12 +59,15 @@ function glLandGeo(gl,far,lv){const key=(far?'far':'all')+(lv===undefined?'':'@'
hills:glVao(gl,[4],1,F(hills)).v,shine:glVao(gl,[4],1,F(shine)).v,nHill:HILLS.length};
glMem(gl,'the land');return GLX.land[key]=G;}
// Fills the land's shape (shifted by ox, oy CSS px) with one colour, for every copy of the map.
// The stencil counts the coasts' windings (+1 for a triangle facing one way, -1 the other: coastFan()), as the 2D
// canvas's nonzero rule does; counting only odd or even made two overlapping coasts cancel out into sea.
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);
gl.colorMask(false,false,false,false);gl.stencilFunc(gl.ALWAYS,0,255);gl.stencilMask(255);
gl.stencilOpSeparate(gl.FRONT,gl.KEEP,gl.KEEP,gl.INCR_WRAP);gl.stencilOpSeparate(gl.BACK,gl.KEEP,gl.KEEP,gl.DECR_WRAP);
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);
// The cover paints where the count is not zero and sets the stencil back to zero for the next user.
gl.colorMask(true,true,true,true);gl.stencilFunc(gl.NOTEQUAL,0,255);gl.stencilOp(gl.ZERO,gl.ZERO,gl.ZERO);
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);}}

View file

@ -74,3 +74,47 @@ test('each frame on the card fills the land from its zoom level’s geometry; th
assert.equal(built, 0, 'the 2D map does not use it');
assert.deepEqual(errors, []);
});
/* Where two coastlines overlap, the land stays land, as the 2D map fills each coast on its own (nonzero). Timor is drawn
twice (COAST[58] and the finer COAST[154], with Dili): the card counted only odd or even coverings over all the coasts,
so the two cancelled out and most of the island was sea, its shadow too. The fan now winds every coast the same way
and the stencil counts windings. w(x,y) is the winding the card counts at a point: the fan's triangles that cover it,
+1 or -1 by the way they turn. */
const WIND = `window.__wind=(fan,x,y)=>{let w=0;const s=(ax,ay,bx,by)=>(bx-ax)*(y-ay)-(by-ay)*(x-ax);
for(let i=0;i<fan.length;i+=6){const [ax,ay,bx,by,cx,cy]=fan.slice(i,i+6),d1=s(ax,ay,bx,by),d2=s(bx,by,cx,cy),d3=s(cx,cy,ax,ay);
if(d1>0&&d2>0&&d3>0)w++;else if(d1<0&&d2<0&&d3<0)w--;}return w;};`;
test('two overlapping coastlines leave their overlap filled (Timor), at every zoom', () => {
const { ev, errors } = open();
ev(WIND);
const r = JSON.parse(ev(`(()=>{const pt=[1692,600],o={both:[58,154].map(i=>inPoly(...pt,coastPts(i)))};
for(const lv of [undefined,-1,0,1,2,3,4,5]){const tol=lv===undefined?0:COAST_TOL/Math.pow(2,lv),fan=[];
COAST.forEach((ring,ri)=>{if(ring.length<3)return;const P=coastSimplify(coastPts(ri),tol);if(P.length>=4)coastFan(P,fan);});
o['lv'+lv]=__wind(fan,...pt);}
return JSON.stringify(o);})()`));
assert.deepEqual(r.both, [true, true], 'the point is inside both of Timor’s coastlines');
for (const lv of [undefined, -1, 0, 1, 2, 3, 4, 5]) assert.equal(r['lv' + lv], 2, `level ${lv}: counted twice, so land (an odd/even count gave 0, sea)`);
assert.deepEqual(errors, []);
});
test('every coastline counts +1 inside, whichever way it was drawn', () => {
const { ev, errors } = open();
ev(WIND);
const r = JSON.parse(ev(`(()=>{const sq=(x,y,s,cw)=>{const p=[[x,y],[x+s,y],[x+s,y+s],[x,y+s]];return cw?p.reverse():p;};
const fan=[];coastFan(sq(0,0,10,false),fan);coastFan(sq(5,5,10,true),fan);
return JSON.stringify({a:__wind(fan,2,1),both:__wind(fan,8,6),b:__wind(fan,12,13),out:__wind(fan,20,20)});})()`));
assert.deepEqual(r, { a: 1, both: 2, b: 1, out: 0 }, 'one square turned each way: the overlap is 2, not 0');
assert.deepEqual(errors, []);
});
test('the card fills the land where the winding count is not zero, and leaves the stencil at zero', () => {
const { ev, errors } = open();
ev(STUB);
const c = JSON.parse(ev(`(()=>{const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;GLX.gl.calls.length=0;draw(1000);
return JSON.stringify(GLX.gl.calls.filter(c=>/^stencil/.test(c[0])).map(c=>c.join(' ')));})()`));
assert.ok(c.includes('stencilOpSeparate FRONT KEEP KEEP INCR_WRAP') && c.includes('stencilOpSeparate BACK KEEP KEEP DECR_WRAP'), 'counts windings, +1 one way and -1 the other');
assert.ok(c.includes('stencilFunc NOTEQUAL 0 255'), 'fills where the count is not zero, on all 8 bits');
assert.ok(c.includes('stencilOp ZERO ZERO ZERO'), 'and sets it back to zero');
assert.ok(!c.some(s => /INVERT/.test(s)), 'no odd/even count left');
assert.deepEqual(errors, []);
});