From fe109cff844fb26888807a64235b66ed5482573e Mon Sep 17 00:00:00 2001 From: Antoine <20756073+antt0n@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:04:48 +0100 Subject: [PATCH] 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. --- src/game/96-map/57-webgl-background.js | 16 +++++++--- tests/webgl-coast.test.js | 44 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/game/96-map/57-webgl-background.js b/src/game/96-map/57-webgl-background.js index a528c30..544b8ed 100644 --- a/src/game/96-map/57-webgl-background.js +++ b/src/game/96-map/57-webgl-background.js @@ -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{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{let w=0;const s=(ax,ay,bx,by)=>(bx-ax)*(y-ay)-(by-ay)*(x-ax); + for(let i=0;i0&&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, []); +});