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(textures): atlas pages fill their clear texels too, and solid art is not taken for premultiplied
yhh96bbc945d7ab
diff --git a/pipeline/src/assets/cookAssets.ts b/pipeline/src/assets/cookAssets.tsindex edd0a6780..9d38131a5 100644--- a/pipeline/src/assets/cookAssets.ts+++ b/pipeline/src/assets/cookAssets.ts@@ -49,7 +49,7 @@ import {readKtx2Layout, wantsMipChain, halveRgba, spliceMipChain,} from './ktx2Mips';import { cookCached } from './cookCache';-import { cookTexture, loadTextureEncoder, pngDimensions, COMPRESSED_TARGETS, type BasisEncoderModule } from './textureCook';+import { cookTexture, loadTextureEncoder, pngDimensions, fillClearTexels, fillsClearTexels, COMPRESSED_TARGETS, type BasisEncoderModule } from './textureCook';import { meshTextureRoles } from './textureUsage';import { makeStreamTail, type StreamTail } from './streamTail';import { matchScriptAssetRefs, scanScriptAssetRefs } from './scriptAssetRefs';@@ -712,6 +712,14 @@ export async function cookAssets(if (images.length === 0) continue;const pages = packAtlas(images);for (let n = 0; n < pages.length; n++) {+ // A page is one image: its frames' clear texels and the clear gaps between+ // them are filled as a lone texture's are, or the frame edge samples the+ // gap's black. Only where every frame on it allows (see fillsClearTexels).+ const onPage = pages[n].placements.map((p) => byPath.get(p.key)).filter((e): e is AssetEntry => !!e);+ if (onPage.length > 0 && onPage.every((e) => fillsClearTexels(e.importer, platform))) {+ const filled = fillClearTexels(pages[n]);+ if (filled) pages[n] = { ...pages[n], rgba: filled };+ }const pagePng: Uint8Array = encodePagePng(pages[n]);let pageBytes: Uint8Array = pagePng;let pageExt = '.png';diff --git a/pipeline/src/assets/textureCook.ts b/pipeline/src/assets/textureCook.tsindex 9131739bf..9fe8df01e 100644--- a/pipeline/src/assets/textureCook.ts+++ b/pipeline/src/assets/textureCook.ts@@ -86,6 +86,22 @@ export function pngDimensions(png: Uint8Array): { width: number; height: numberreturn { width: dv.getUint32(16), height: dv.getUint32(20) };}+/**+ * A colour image's clear texels filled from their solid neighbours — the rule+ * every cooked image follows (a texture on its own, and an atlas page whose gaps+ * between frames are clear too). `null` when there is nothing to do: no clear+ * texel, or premultiplied art (see {@link looksPremultiplied}).+ */+export function fillClearTexels(image: { rgba: Uint8Array; width: number; height: number }): Uint8Array | null {+ if (!hasClearTexel(image.rgba) || looksPremultiplied(image.rgba)) return null;+ return bleedCutoutColor({ width: image.width, height: image.height, rgba: image.rgba }, { clearOnly: true });+}++/** Whether an importer lets its clear texels be filled: a colour image that has not said no. */+export function fillsClearTexels(importer: Record<string, unknown> | undefined, platform?: string): boolean {+ return readTextureCookSettings(importer, platform).srgb && importer?.alphaIsTransparency !== false;+}+/*** Whether a PNG can hold a transparent texel, from its header: grey+alpha or RGBA,* or a palette with a tRNS chunk. Answered without decoding, so an opaque image@@ -115,16 +131,21 @@ function hasClearTexel(rgba: Uint8Array): boolean {/*** Whether the colour was multiplied by alpha already (a Spine PMA page): no channel- * above its texel's alpha anywhere. Such an image's RGB under alpha 0 is 0 BY+ * above its texel's alpha anywhere, partly clear texels included. Such an image's RGB under alpha 0 is 0 BY* DEFINITION, and premultiplied blending adds it — filling it in would light up* every clear texel.*/function looksPremultiplied(rgba: Uint8Array): boolean {+ // Only partly clear texels can tell the two apart: an opaque texel and a clear+ // one read the same either way. Art with none of them (solid frames on a clear+ // page, pixel art) is straight; a PMA page has an anti-aliased edge somewhere.+ let partial = false;for (let i = 0; i < rgba.length; i += 4) {const a = rgba[i + 3]!;if (rgba[i]! > a || rgba[i + 1]! > a || rgba[i + 2]! > a) return false;+ if (a > 0 && a < 255) partial = true;}- return true;+ return partial;}function safePngDimensions(png: Uint8Array): { width: number; height: number } | null {@@ -215,11 +236,12 @@ export async function cookTexture(input: TextureCookInput): Promise<TextureCookO// colour — on a straight-alpha image it is the colour that is seen.// The importer can say no (`alphaIsTransparency: false`): an image whose RGB under// alpha 0 a shader reads as data.- const clearBleed = raster && !cutout && tex.srgb && input.importer?.alphaIsTransparency !== false && pngMayHaveAlpha(data);- const bled = (image: { rgba: Uint8Array; width: number; height: number }) =>- cutout || (clearBleed && hasClearTexel(image.rgba) && !looksPremultiplied(image.rgba))- ? { ...image, rgba: bleedCutoutColor({ width: image.width, height: image.height, rgba: image.rgba }, { clearOnly: !cutout }) }- : image;+ const clearBleed = raster && !cutout && fillsClearTexels(input.importer, platform) && pngMayHaveAlpha(data);+ const bled = (image: { rgba: Uint8Array; width: number; height: number }) => {+ if (cutout) return { ...image, rgba: bleedCutoutColor({ width: image.width, height: image.height, rgba: image.rgba }) };+ const filled = clearBleed ? fillClearTexels(image) : null;+ return filled ? { ...image, rgba: filled } : image;+ };const bleedKey = cutout ? { bleed: 1 } : clearBleed ? { bleed: 'clear' } : {};// A texture that ships as an image ships the shrunk one, and the bled one —// through the cook cache, so the editor shows the same pixels a build ships.diff --git a/pipeline/tests/cook-assets.test.ts b/pipeline/tests/cook-assets.test.tsindex a3e8ca299..a60c81804 100644--- a/pipeline/tests/cook-assets.test.ts+++ b/pipeline/tests/cook-assets.test.ts@@ -519,6 +519,11 @@ describe('cookAssets — auto-atlas (<name>.atlas folder convention)', () => {const at = (f: { x: number; y: number }) => [...page.data.subarray((f.y * page.width + f.x) * 4, (f.y * page.width + f.x) * 4 + 4)];expect(at(a.atlas!.frame)).toEqual([255, 0, 0, 255]);expect(at(b.atlas!.frame)).toEqual([0, 255, 0, 255]);+ // The clear gap beside a frame took a frame's colour, not black: a frame's+ // edge samples it, and black there is a dark fringe.+ const gap = { x: a.atlas!.frame.x + a.atlas!.frame.width, y: a.atlas!.frame.y };+ const texel = at(gap);+ if (texel[3] === 0) expect(texel.slice(0, 3)).not.toEqual([0, 0, 0]);} finally {rmSync(r, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });}