A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(assets): shrinking a texture to its Max Size keeps the colour of a footprint that is clear all through, instead of black: a weight map holds its numbers under a clear alpha
yhh6c83f756a3cb
diff --git a/pipeline/src/assets/atlasPacker.ts b/pipeline/src/assets/atlasPacker.tsindex 6d2e1fe91..ab38b73a3 100644--- a/pipeline/src/assets/atlasPacker.ts+++ b/pipeline/src/assets/atlasPacker.ts@@ -193,18 +193,23 @@ export function downscaleRgba(img: AtlasInputImage, maxDim: number): AtlasInputIconst sx0 = Math.floor((x * img.width) / nw);const sx1 = Math.max(sx0 + 1, Math.floor(((x + 1) * img.width) / nw));let r = 0, g = 0, b = 0, a = 0, n = 0;+ // the colour with no weight: what a footprint that is clear all through is averaged by+ let r0 = 0, g0 = 0, b0 = 0;for (let sy = sy0; sy < sy1; sy++) {for (let sx = sx0; sx < sx1; sx++) {const i = (sy * img.width + sx) * 4;const al = img.rgba[i + 3];r += img.rgba[i] * al; g += img.rgba[i + 1] * al; b += img.rgba[i + 2] * al;+ r0 += img.rgba[i]; g0 += img.rgba[i + 1]; b0 += img.rgba[i + 2];a += al; n++;}}const o = (y * nw + x) * 4;- out[o] = a > 0 ? Math.round(r / a) : 0;- out[o + 1] = a > 0 ? Math.round(g / a) : 0;- out[o + 2] = a > 0 ? Math.round(b / a) : 0;+ // Weighted by coverage, so a clear texel does not dull the ones it is averaged with; and where all of them are clear, the plain mean rather than black,+ // since a texture that holds numbers in its colour under a clear alpha (a weight map) has them there.+ out[o] = Math.round(a > 0 ? r / a : r0 / n);+ out[o + 1] = Math.round(a > 0 ? g / a : g0 / n);+ out[o + 2] = Math.round(a > 0 ? b / a : b0 / n);out[o + 3] = Math.round(a / n);}}diff --git a/pipeline/tests/atlas-packer.test.ts b/pipeline/tests/atlas-packer.test.tsindex 4a5014ac3..d2ce1d1ee 100644--- a/pipeline/tests/atlas-packer.test.ts+++ b/pipeline/tests/atlas-packer.test.ts@@ -125,6 +125,13 @@ describe('downscaleRgba (Max Size cook cap)', () => {expect([...out.rgba]).toEqual([255, 0, 0, 128]);});+ it('keeps the colour of a footprint that is clear all through (a weight map holds its numbers there)', () => {+ // 2×1: a red texel and a blue one, both with alpha 0: the shrunk pixel is their mean, not black+ const img: AtlasInputImage = { key: 'k', width: 2, height: 1, rgba: new Uint8Array([255, 0, 0, 0, 0, 0, 255, 0]) };+ const out = downscaleRgba(img, 1);+ expect([...out.rgba]).toEqual([128, 0, 128, 0]);+ });+it('encodeRgbaPng round-trips raw pixels', () => {const rgba = new Uint8Array([9, 8, 7, 255, 6, 5, 4, 255]);const back = decodePngImage('p', encodeRgbaPng(2, 1, rgba));