WebGL map: keep each picture in a clear border in the atlas

The card blends the texels around the point it reads, so a picture drawn
off whole pixels, or stretched while a zoom moves, read half a texel past
its edge: its neighbour in the atlas, or a picture left from before the
last reset. The map showed thin coloured lines along the edges of cities
and labels. Each picture now goes in with a clear one-texel border, and
painting on the pad is clipped to the picture so the border stays clear.
This commit is contained in:
Antoine 2026-11-05 00:59:38 +01:00
parent 8c2000f6b1
commit c3e1dd9348
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
2 changed files with 70 additions and 10 deletions

View file

@ -45,7 +45,11 @@ function spritesReset(){glsDrawNow();for(const e of SPRC.values())sprFree(e);SPR
function spritesDrop(){GLS.n=GLS.up=0;GLS.upq=false;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
/* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a
canvas kept per picture made the browser hold thousands of them while you zoomed, until it lost the graphics context
and the map fell back to 2D. The 2D map, and a picture too big for the atlas, keep their own canvas. */
and the map fell back to 2D. The 2D map, and a picture too big for the atlas, keep their own canvas.
Each picture goes in with a clear border of GLS_GAP px: the card blends the texels around the point it reads, so a
picture drawn off whole pixels or a little stretched read half a texel past its edge, and showed a thin line of its
neighbour in the atlas (or of a picture from before the last reset, which leaves the texture as it was). */
const GLS_GAP=1;
const GLS_PAD={cv:null,g:null};
function glsPad(pw,ph){const P=GLS_PAD;if(!P.cv){P.cv=document.createElement('canvas');P.cv.width=P.cv.height=256;}
if(P.cv.width<pw||P.cv.height<ph){P.cv.width=Math.max(P.cv.width,Math.ceil(pw/64)*64);P.cv.height=Math.max(P.cv.height,Math.ceil(ph/64)*64);P.g=null;}
@ -60,9 +64,11 @@ function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.
if(SPRS.size+SPRC.size>=SPR_MAX)spritesReset();
const pw=Math.ceil(w*DPR),ph=Math.ceil(h*DPR),saved=ctx;
e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1,f:SPR_F};
if(gpu&&pw<=GLS.size&&ph<=GLS.size){const P=glsPad(pw,ph),g=P.g;
g.save();g.setTransform(1,0,0,1,0,0);g.clearRect(0,0,pw,ph);ctx=g;
try{g.setTransform(DPR,0,0,DPR,0,0);sprPen(g);paint(g);}finally{ctx=saved;g.restore();}
const G=GLS_GAP;
if(gpu&&pw+2*G<=GLS.size&&ph+2*G<=GLS.size){const P=glsPad(pw+2*G,ph+2*G),g=P.g;
// Clipped to the picture, as its own canvas would be, so the border stays clear.
g.save();g.setTransform(1,0,0,1,0,0);g.clearRect(0,0,pw+2*G,ph+2*G);g.beginPath();g.rect(G,G,pw,ph);g.clip();ctx=g;
try{g.setTransform(DPR,0,0,DPR,G,G);sprPen(g);paint(g);}finally{ctx=saved;g.restore();}
// A full atlas starts again (glsPut), and the picture goes into the empty one at once: the pad is reused next.
try{if(!glsPut(GLX.gl,e,P.cv))glsPut(GLX.gl,e,P.cv);}catch(x){throw glErr(x);}
}else{const cv=document.createElement('canvas');cv.width=pw;cv.height=ph;const g=cv.getContext('2d');ctx=g;
@ -74,16 +80,19 @@ function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.
the cones' tips and a sleeping POP's "zzz" took those, and a new canvas has others (square ends, text on its baseline). */
function sprPen(g){g.lineCap='round';g.lineJoin='round';g.textAlign='center';g.textBaseline='middle';}
// Puts a picture with its own canvas into the atlas (once), shelf by shelf. A full atlas starts again; this picture then waits a frame.
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl;if(!gl||!GLS.prog||!e.cv)return false;
try{return glsPut(gl,e,e.cv);}catch(x){throw glErr(x);}}
function glsPut(gl,e,src){const pw=e.pw,ph=e.ph,A=GLS.size;if(pw>A||ph>A)return false;
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl,G=GLS_GAP;if(!gl||!GLS.prog||!e.cv)return false;
if(e.pw+2*G>GLS.size||e.ph+2*G>GLS.size)return false;
const P=glsPad(e.pw+2*G,e.ph+2*G),g=P.g;g.setTransform(1,0,0,1,0,0);g.clearRect(0,0,e.pw+2*G,e.ph+2*G);g.drawImage(e.cv,G,G);
try{return glsPut(gl,e,P.cv);}catch(x){throw glErr(x);}}
// src holds the picture at (GLS_GAP,GLS_GAP), in a clear border that goes into the atlas with it.
function glsPut(gl,e,src){const G=GLS_GAP,pw=e.pw+2*G,ph=e.ph+2*G,A=GLS.size;if(pw>A||ph>A)return false;
if(GLS.shx+pw>A){GLS.shx=0;GLS.shy+=GLS.shh;GLS.shh=0;}
if(GLS.shy+ph>A){spritesReset();return false;}
const x=GLS.shx,y=GLS.shy;GLS.shx+=pw;GLS.shh=Math.max(GLS.shh,ph);
gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true);
// Only the picture's corner of the source: the pad is bigger than most pictures.
// Only the picture's corner of the source (with its border): the pad is bigger than most pictures.
gl.texSubImage2D(gl.TEXTURE_2D,0,x,y,pw,ph,gl.RGBA,gl.UNSIGNED_BYTE,src);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,false);
GLS.up++;Object.assign(e,{up:true,u0:x/A,v0:y/A,u1:(x+pw)/A,v1:(y+ph)/A});return true;}
GLS.up++;Object.assign(e,{up:true,u0:(x+G)/A,v0:(y+G)/A,u1:(x+pw-G)/A,v1:(y+ph-G)/A});return true;}
// The next record in GLS.data, which grows as needed: take it before reading GLS.data, which it may replace.
function glsNext(){if((GLS.n+1)*SPR_STRIDE>GLS.data.length){const nd=new Float32Array(GLS.data.length*2);nd.set(GLS.data);GLS.data=nd;}return GLS.n++*SPR_STRIDE;}
// Draws a picture at (x,y) of w×h CSS px, on the card for a frame drawn there, else with the 2D canvas.

View file

@ -47,6 +47,57 @@ test('on the card, pictures are painted on one canvas and each goes into the atl
assert.deepEqual(errors, []);
});
// The card blends the texels around the point it reads: a picture drawn off whole pixels, or stretched while a zoom
// moves, read half a texel past its edge, and the map showed thin coloured lines around cities (its neighbour in the
// atlas, or a picture from before the last reset). So each picture goes in with a clear border, and never touches another.
test('on the card, every picture sits in the atlas in a clear border of its own', () => {
const { ev, errors } = open();
ev(STUB);
// What the pad is asked to do for each picture: cleared, clipped to the picture, painted inside the border.
ev(`(()=>{const P=glsPad(1,1),g=P.g;window.__pad=[];for(const k of ['clearRect','rect','setTransform'])g[k]=(...a)=>__pad.push([k,...a]);})()`);
ev(ZOOM);
const r = JSON.parse(ev(`(()=>{const A=GLS.size,G=GLS_GAP,es=[...SPRS.values()].filter(e=>e.up);
const R=es.map(e=>({x0:Math.round(e.u0*A),y0:Math.round(e.v0*A),x1:Math.round(e.u1*A),y1:Math.round(e.v1*A),e}));
const exact=R.every(q=>q.x1-q.x0===q.e.pw&&q.y1-q.y0===q.e.ph);
const inside=R.every(q=>q.x0>=G&&q.y0>=G&&q.x1+G<=A&&q.y1+G<=A);
let touch=0;for(let i=0;i<R.length;i++)for(let j=i+1;j<R.length;j++){const a=R[i],b=R[j];
if(a.x0<b.x1+2*G&&b.x0<a.x1+2*G&&a.y0<b.y1+2*G&&b.y0<a.y1+2*G)touch++;}
const subs=GLX.gl.calls.filter(c=>c[0]==='texSubImage2D').slice(-es.length);
const sent=subs.every(c=>R.some(q=>c[3]===q.x0-G&&c[4]===q.y0-G&&c[5]===q.e.pw+2*G&&c[6]===q.e.ph+2*G));
// Each picture: clearRect(0,0,W,H), then rect(G,G,W-2G,H-2G) for the clip, then the transform that paints inside it.
const at=__pad.flatMap((c,i)=>c[0]==='clearRect'?[i]:[]),nx=(i,k)=>__pad[i+k]||[];
return JSON.stringify({n:es.length,exact,inside,touch,sent,G,
cleared:at.length>=es.length&&at.every(i=>nx(i,0)[1]===0&&nx(i,0)[2]===0),
clipped:at.every(i=>{const c=nx(i,0),q=nx(i,1);return q[0]==='rect'&&q[1]===G&&q[2]===G&&q[3]===c[3]-2*G&&q[4]===c[4]-2*G;}),
offset:at.every(i=>{const q=nx(i,2);return q[0]==='setTransform'&&q[5]===G&&q[6]===G;})});})()`));
assert.ok(r.n > 50, `a zoom paints many pictures (${r.n})`);
assert.ok(r.G >= 1, 'a border of at least one texel');
assert.equal(r.exact, true, 'the picture’s own texels are read, no more');
assert.equal(r.inside, true, 'with room for its border inside the atlas');
assert.equal(r.touch, 0, 'no two pictures (with their borders) overlap or touch');
assert.equal(r.sent, true, 'each was sent with its border');
assert.equal(r.cleared, true, 'the pad is cleared from its corner, border included');
assert.equal(r.clipped, true, 'painting is clipped to the picture, so the border stays clear');
assert.equal(r.offset, true, 'and starts inside the border');
assert.deepEqual(errors, []);
});
test('a picture with its own canvas goes into the atlas in a clear border too', () => {
const { ev, errors } = open();
ev(STUB);
const r = JSON.parse(ev(`(()=>{const e=spriteEntry('own',20,10,10,5,g=>{g.fillRect(0,0,20,10);});
const P=glsPad(1,1),g=P.g,log=[];g.clearRect=(...a)=>log.push(['clear',...a]);g.drawImage=(cv,x,y)=>log.push(['draw',cv===e.cv,x,y]);
const ok=glsEnsure(e),A=GLS.size,G=GLS_GAP,s=GLX.gl.calls.filter(c=>c[0]==='texSubImage2D').pop();
return JSON.stringify({cv:!!e.cv,ok,log,G,u0:e.u0*A,v0:e.v0*A,u1:e.u1*A,v1:e.v1*A,pw:e.pw,ph:e.ph,sent:s.slice(3,7),pad:s[9]===P.cv});})()`));
assert.equal(r.cv, true, 'painted outside a card frame, so with its own canvas');
assert.equal(r.ok, true);
assert.deepEqual(r.log, [['clear', 0, 0, r.pw + 2 * r.G, r.ph + 2 * r.G], ['draw', true, r.G, r.G]], 'copied into a cleared pad, inside the border');
assert.deepEqual([r.u0, r.v0, r.u1, r.v1], [r.G, r.G, r.G + r.pw, r.G + r.ph]);
assert.deepEqual(r.sent, [0, 0, r.pw + 2 * r.G, r.ph + 2 * r.G]);
assert.equal(r.pad, true);
assert.deepEqual(errors, []);
});
test('a full atlas starts again and the picture that did not fit goes into the new one at once', () => {
const { ev, errors } = open();
ev(STUB);
@ -55,7 +106,7 @@ test('a full atlas starts again and the picture that did not fit goes into the n
const e=spriteEntry('big',40,40,20,20,g=>{g.fillRect(0,0,40,40);});blit(e,10,10,40,40);
glsDraw=gd;const r={up:e.up,u0:e.u0,v0:e.v0,kept:SPRS.get('big')===e,size:SPRS.size,n:GLS.n,cv:!!e.cv,shy:GLS.shy};GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify(r);})()`));
assert.equal(r.up, true, 'the picture is in the atlas');
assert.equal(r.u0, 0); assert.equal(r.v0, 0);
assert.equal(r.u0, 1 / 4096, 'at the start of the new atlas, past its clear border'); assert.equal(r.v0, 1 / 4096);
assert.equal(r.kept, true, 'and kept under its key after the restart');
assert.equal(r.size, 1, 'the old pictures are gone');
assert.equal(r.n, 1, 'drawn this frame');