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): a texture made smaller to fit its budget keeps the colour under its clear texels: the resize was left to premultiply, which turned a terrain's weight map black
yhha19b51779819
diff --git a/sdk/src/asset/imageDecode.ts b/sdk/src/asset/imageDecode.tsindex aff8963f6..d1585accb 100644--- a/sdk/src/asset/imageDecode.ts+++ b/sdk/src/asset/imageDecode.ts@@ -55,28 +55,34 @@ export interface DecodedPixels {* v=1=top for general textures, v=0=top for spine / bitmap fonts.*/export async function decodeImagePixels(src: ImageBitmapSource, maxEdge = 0): Promise<DecodedPixels> {- let bitmap = await decodeImageBitmap(src, false);+ const bitmap = await shrinkBitmap(await decodeImageBitmap(src, false), maxEdge);try {- const longest = Math.max(bitmap.width, bitmap.height);- if (maxEdge > 0 && longest > maxEdge && typeof createImageBitmap === 'function') {- // Made smaller by the decoder, with the same options: a canvas would scale it, and a canvas keeps nothing under a clear texel.- const k = maxEdge / longest;- const smaller = await createImageBitmap(bitmap, {- resizeWidth: Math.max(1, Math.round(bitmap.width * k)),- resizeHeight: Math.max(1, Math.round(bitmap.height * k)),- resizeQuality: 'high',- premultiplyAlpha: 'none',- colorSpaceConversion: 'none',- });- bitmap.close?.();- bitmap = smaller;- }return readImagePixels(bitmap, maxEdge);} finally {bitmap.close?.();}}+/**+ * A bitmap made no longer than `maxEdge` on its long side (the bitmap itself when it already is, or when there is no decoder to do it). The decoder makes+ * it smaller with the options it decoded with: left to its defaults a resize premultiplies, and the colour under a clear texel is gone, which a weight map+ * the ground is mixed by has all over it. The bitmap given is closed when a smaller one is made.+ */+export async function shrinkBitmap(bitmap: ImageBitmap, maxEdge: number): Promise<ImageBitmap> {+ const longest = Math.max(bitmap.width, bitmap.height);+ if (!(maxEdge > 0 && longest > maxEdge) || typeof createImageBitmap !== 'function') return bitmap;+ const k = maxEdge / longest;+ const smaller = await createImageBitmap(bitmap, {+ resizeWidth: Math.max(1, Math.round(bitmap.width * k)),+ resizeHeight: Math.max(1, Math.round(bitmap.height * k)),+ resizeQuality: 'high',+ premultiplyAlpha: 'none',+ colorSpaceConversion: 'none',+ });+ bitmap.close?.();+ return smaller;+}+/** The GL context pixels are read through, made once; null where there is none (a host without WebGL2, a test). */let readbackGl: WebGL2RenderingContext | null | undefined;function readbackContext(): WebGL2RenderingContext | null {diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.tsindex 2d3dc77c5..979dee785 100644--- a/sdk/src/asset/loaders/TextureLoader.ts+++ b/sdk/src/asset/loaders/TextureLoader.ts@@ -4,7 +4,7 @@ import type { AssetLoader, LoadContext, TextureResult } from '../AssetLoader';import { linearColorSpace, samplesAsSrgb } from '../../ecs/env';import { platformCreateImage } from '../../platform/base';import type { PlatformImage } from '../../platform/types';-import { decodeImageBitmap, readImagePixels } from '../imageDecode';+import { decodeImageBitmap, readImagePixels, shrinkBitmap } from '../imageDecode';import { requireResourceManager } from '../../wasm/resourceManager';import { TextureContent, type ESEngineModule } from '../../wasm';import { withMalloc } from '../../wasm/wasmScratch';@@ -358,20 +358,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {img.onload = async () => {if (typeof createImageBitmap !== 'undefined') {try {- const bitmap = await decodeImageBitmap(img, flip);- const longest = Math.max(bitmap.width, bitmap.height);- if (maxEdge > 0 && longest > maxEdge) {- const k = maxEdge / longest;- const smaller = await createImageBitmap(bitmap, {- resizeWidth: Math.max(1, Math.round(bitmap.width * k)),- resizeHeight: Math.max(1, Math.round(bitmap.height * k)),- resizeQuality: 'high',- });- bitmap.close();- resolve(smaller);- return;- }- resolve(bitmap);+ resolve(await shrinkBitmap(await decodeImageBitmap(img, flip), maxEdge));return;} catch {// fallbackdiff --git a/sdk/tests/image-readback.test.ts b/sdk/tests/image-readback.test.tsindex c3b04c32f..1f70a892d 100644--- a/sdk/tests/image-readback.test.ts+++ b/sdk/tests/image-readback.test.ts@@ -61,3 +61,24 @@ describe('reading a decoded bitmap', () => {expect(calls.indexOf('texImage2D')).toBeLessThan(calls.indexOf('readPixels'));});});++describe('making a bitmap smaller', () => {+ it('asks the decoder for it unpremultiplied and without a colour conversion, so the colour under a clear texel is kept', async () => {+ const made: Array<Record<string, unknown>> = [];+ vi.stubGlobal('createImageBitmap', async (_source: unknown, options: Record<string, unknown>) => { made.push(options); return new FakeBitmap(options.resizeWidth as number, options.resizeHeight as number); });+ vi.stubGlobal('ImageBitmap', FakeBitmap);+ const { shrinkBitmap } = await import('../src/asset/imageDecode');+ const small = await shrinkBitmap(new FakeBitmap(4096, 2048) as unknown as ImageBitmap, 2048);+ expect([small.width, small.height]).toEqual([2048, 1024]);+ expect(made).toHaveLength(1);+ expect(made[0]).toMatchObject({ premultiplyAlpha: 'none', colorSpaceConversion: 'none', resizeWidth: 2048, resizeHeight: 1024 });+ vi.unstubAllGlobals();+ });++ it('leaves a bitmap that is small enough as it is', async () => {+ const { shrinkBitmap } = await import('../src/asset/imageDecode');+ const bitmap = new FakeBitmap(512, 512) as unknown as ImageBitmap;+ expect(await shrinkBitmap(bitmap, 2048)).toBe(bitmap);+ expect(await shrinkBitmap(bitmap, 0)).toBe(bitmap);+ });+});