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(texture): WebGPU stops dividing a texture by its alpha twice
esengine6a1886ee5ab5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a146514bd..a712e4190 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -350,6 +350,15 @@ published separately; it ships inside the editor.### Fixed+- **A half-transparent texture looks the same on WebGPU as on WebGL2.** Without a+ WebGL2 context a texture was read through a 2D canvas, whose pixels already come+ back unpremultiplied, and then divided by alpha a second time. Every+ half-transparent texel on WebGPU came out brighter — (200,100,40) at half+ alpha drew as (128,100,40) where WebGL2 drew (100,50,20) — and a baked+ reflection atlas, whose alpha IS its brightness, turned a room's red and green+ walls grey. That read now goes through the one image-decode path every other+ texture already uses, and the reflection-room gate runs on both backends.+- **The editor packages playable ads again.** Since 0.65 a playable's gametravels deflated and a small loader starts it, and the exporter looked for thatloader beside the playable host. The editor exports from the host bundles itdiff --git a/fixtures/scenes/alpha-midtone.esscene b/fixtures/scenes/alpha-midtone.esscenenew file mode 100644index 000000000..28ef0aa8c--- /dev/null+++ b/fixtures/scenes/alpha-midtone.esscene@@ -0,0 +1,69 @@+{+ "version": 4,+ "name": "Half-transparent Midtone",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 0,+ "z": 10+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 300,+ "isActive": true,+ "priority": 0+ }+ }+ ],+ "visible": true+ },+ {+ "id": 1,+ "name": "HalfAlpha",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 0,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 300,+ "y": 300+ },+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "texture": "@uuid:7c3e9a41-2b5d-4e8f-a1c6-9d0f4b2e7a53"+ }+ }+ ],+ "visible": true+ }+ ]+}diff --git a/fixtures/scenes/alpha-midtone.png b/fixtures/scenes/alpha-midtone.pngnew file mode 100644index 000000000..79ca7d05aBinary files /dev/null and b/fixtures/scenes/alpha-midtone.png differdiff --git a/fixtures/scenes/alpha-midtone.textures.json b/fixtures/scenes/alpha-midtone.textures.jsonnew file mode 100644index 000000000..71c699c90--- /dev/null+++ b/fixtures/scenes/alpha-midtone.textures.json@@ -0,0 +1,3 @@+{+ "7c3e9a41-2b5d-4e8f-a1c6-9d0f4b2e7a53": "/scenes/alpha-midtone.png"+}diff --git a/sdk/src/asset/imageDecode.ts b/sdk/src/asset/imageDecode.tsindex 151a104c0..b5b35e4e5 100644--- a/sdk/src/asset/imageDecode.ts+++ b/sdk/src/asset/imageDecode.ts@@ -57,19 +57,31 @@ export interface DecodedPixels {export async function decodeImagePixels(src: ImageBitmapSource): Promise<DecodedPixels> {const bitmap = await decodeImageBitmap(src, false);try {- const canvas = platformCreateCanvas(bitmap.width, bitmap.height);- canvas.width = bitmap.width;- canvas.height = bitmap.height;- const ctx = canvas.getContext('2d', { willReadFrequently: true });- if (!ctx) throw new Error('imageDecode: 2D context unavailable for pixel decode');- ctx.drawImage(bitmap, 0, 0);- const data = ctx.getImageData(0, 0, bitmap.width, bitmap.height);- return { width: bitmap.width, height: bitmap.height, pixels: new Uint8Array(data.data.buffer) };+ return readImagePixels(bitmap);} finally {bitmap.close?.();}}+/**+ * The RGBA of an image that is already decoded, in its own row order.+ *+ * `ImageData` is unpremultiplied by definition, so these bytes are the upload as+ * they stand: dividing them by alpha again drives every half-transparent texel+ * toward white, and an RGBM texel — whose alpha IS its brightness — to grey.+ */+export function readImagePixels(image: PlatformImage | ImageBitmap): DecodedPixels {+ const { width, height } = image;+ const canvas = platformCreateCanvas(width, height);+ canvas.width = width;+ canvas.height = height;+ const ctx = canvas.getContext('2d', { willReadFrequently: true });+ if (!ctx) throw new Error('imageDecode: 2D context unavailable for pixel decode');+ ctx.drawImage(image, 0, 0);+ const data = ctx.getImageData(0, 0, width, height);+ return { width, height, pixels: new Uint8Array(data.data.buffer) };+}+/*** Fetch a URL and decode it to top-first RGBA pixels. The single fetch→blob→decode* path the runtime asset sources use for `estella://` / http origins (a CORS-modediff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.tsindex a7a13fd94..067333ab2 100644--- a/sdk/src/asset/loaders/TextureLoader.ts+++ b/sdk/src/asset/loaders/TextureLoader.ts@@ -2,9 +2,9 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { AssetLoader, LoadContext, TextureResult } from '../AssetLoader';import { linearColorSpace } from '../../ecs/env';-import { platformCreateCanvas, platformCreateImage } from '../../platform/base';-import type { PlatformCanvas, PlatformCanvas2DContext, PlatformImage } from '../../platform/types';-import { decodeImageBitmap } from '../imageDecode';+import { platformCreateImage } from '../../platform/base';+import type { PlatformImage } from '../../platform/types';+import { decodeImageBitmap, readImagePixels } from '../imageDecode';import { requireResourceManager } from '../../wasm/resourceManager';import type { ESEngineModule } from '../../wasm';import { withMalloc } from '../../wasm/wasmScratch';@@ -109,8 +109,6 @@ export class TextureLoader implements AssetLoader<TextureResult> {private lastDecision_: TextureUploadDecision = RAW_PAYLOAD_UPLOAD;private deviceFormats_: CompressedTextureFormat[] | null = null;- private canvas_: PlatformCanvas | null = null;- private ctx_: PlatformCanvas2DContext | null = null;/*** Optional hook that returns per-asset import settings. Invoked at* load-time with the ORIGINAL ref (pre-resolution), so callers can key@@ -135,15 +133,6 @@ export class TextureLoader implements AssetLoader<TextureResult> {setPixelDecoder(decoder: TexturePixelDecoder | null): void { this.pixelDecoder_ = decoder; }get pixelDecoder(): TexturePixelDecoder | null { return this.pixelDecoder_; }- private ensureCanvas_(): { canvas: PlatformCanvas; ctx: PlatformCanvas2DContext } {- if (this.canvas_ && this.ctx_) return { canvas: this.canvas_, ctx: this.ctx_ };- this.canvas_ = platformCreateCanvas(256, 256);- const ctx = this.canvas_.getContext('2d', { willReadFrequently: true });- if (!ctx) throw new Error('TextureLoader: failed to create 2D context');- this.ctx_ = ctx;- return { canvas: this.canvas_, ctx: this.ctx_ };- }-async load(path: string, ctx: LoadContext): Promise<TextureResult> {const settings = this.pendingSettings_;this.pendingSettings_ = undefined;@@ -442,18 +431,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {throw new Error('TextureLoader: 2D-canvas fallback needs a wasm module (native uses the pixel-decode path)');}const module = this.module_;- const { canvas, ctx } = this.ensureCanvas_();- if (canvas.width < width || canvas.height < height) {- canvas.width = Math.max(canvas.width, nextPowerOf2(width));- canvas.height = Math.max(canvas.height, nextPowerOf2(height));- }-- ctx.clearRect(0, 0, canvas.width, canvas.height);- ctx.drawImage(img, 0, 0);-- const imageData = ctx.getImageData(0, 0, width, height);- const pixels = new Uint8Array(imageData.data.buffer);- unpremultiplyAlpha(pixels);+ const { pixels } = readImagePixels(img);const rm = requireResourceManager();// Format 2 = sRGB color under the linear pipeline (see rm_createTexture).@@ -467,24 +445,6 @@ export class TextureLoader implements AssetLoader<TextureResult> {}}-function unpremultiplyAlpha(pixels: Uint8Array): void {- for (let i = 0; i < pixels.length; i += 4) {- const a = pixels[i + 3];- if (a > 0 && a < 255) {- const scale = 255 / a;- pixels[i] = Math.min(255, Math.round(pixels[i] * scale));- pixels[i + 1] = Math.min(255, Math.round(pixels[i + 1] * scale));- pixels[i + 2] = Math.min(255, Math.round(pixels[i + 2] * scale));- }- }-}--function nextPowerOf2(n: number): number {- let p = 1;- while (p < n) p *= 2;- return p;-}-/*** The engine's WebGL2 context, looked up through emscripten's GL bookkeeping.* Duck-typed on `texStorage2D` (a WebGL2-only method): WeChat MiniGames have nodiff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex f67bba3e6..ec3f13675 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -306,9 +306,9 @@ export const SCENES = [// which is the claim that a probe REPLACES an answer rather than adding one.{ id: "reflection-probe-off", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/reflection-probe.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SET_FIELD: "{\"entity\": 2, \"component\": \"ReflectionProbe\", \"key\": \"enabled\", \"value\": false, \"steps\": 2}", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.25, \"y\": 0.5, \"rgb\": [0, 228, 0], \"tol\": 30}, {\"x\": 0.75, \"y\": 0.5, \"rgb\": [0, 228, 0], \"tol\": 30}]" } },// A BAKED room and a mirror ball in it: red on its left, green on its right.- // webgl2 only, and NAMED: the second backend reads this atlas to a different- // texel for the same direction, column and params — the room goes pale there.- { id: "reflection-room", tier: "pr", webgpu: false, env: { ESTELLA_VERIFY_SCENE: "/scenes/reflection-room.esscene", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "384", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.2, \"y\": 0.55, \"rgb\": [255, 62, 54], \"tol\": 45}, {\"x\": 0.42, \"y\": 0.62, \"rgb\": [42, 255, 71], \"tol\": 45}]" } },+ // A baked atlas has mixed channels at low alpha, which is what texture-alpha+ // guards on WebGPU: dividing by alpha twice turns this ball grey.+ { id: "reflection-room", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/reflection-room.esscene", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "384", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.2, \"y\": 0.55, \"rgb\": [255, 62, 54], \"tol\": 45}, {\"x\": 0.42, \"y\": 0.62, \"rgb\": [42, 255, 71], \"tol\": 45}]" } },// The same ball with the probe off: both sides take the flat sky, and the room// stops being visible in it.{ id: "reflection-room-sky", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/reflection-room.esscene", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "384", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SET_FIELD: "{\"entity\": 8, \"component\": \"ReflectionProbe\", \"key\": \"enabled\", \"value\": false, \"steps\": 2}", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.2, \"y\": 0.55, \"rgb\": [37, 37, 39], \"tol\": 25}, {\"x\": 0.42, \"y\": 0.62, \"rgb\": [37, 37, 39], \"tol\": 25}]" } },@@ -390,6 +390,10 @@ export const SCENES = [// Linear-light pipeline (colorSpace: 'linear'). The lit falloff moves 52 to 125 (perceptually-uniform light); the mid-gray texture must ROUND-TRIP exactly (sRGB store → hw decode → blit encode = identity) — a missing sRGB upload variant would read ~187, so tol 6 is a hard discriminator.{ id: "linear-lit", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-point.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-point.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[0,254,0],\"tol\":25},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,125,0],\"tol\":25}]" } },{ id: "linear-midtone", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/srgb-midtone.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/srgb-midtone.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[128,128,128],\"tol\":6}]" } },+ // A half-transparent texel whose channels are neither 0 nor 255: (200,100,40) at alpha 128 over+ // black is (100,50,20) on every backend. Dividing by alpha a second time reads (128,100,40) —+ // and a texture of pure channels hides that, since 0 and 255 survive the division.+ { id: "texture-alpha", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/alpha-midtone.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/alpha-midtone.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[100,50,20],\"tol\":6}]" } },// The output transform on the scene whose whole subject is one known value:// 0.502 sRGB in, 157 out, with the capture engaged by the curve alone — no// effects, no linear mode. Hand-derived, so a wrong shoulder is a wrong number.