Skip to content

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.

feat(renderer): sRGB uploads reach every texture path - linear C2b

esengine043112dc6cdc
diff --git a/desktop/src/project/assetImporter.ts b/desktop/src/project/assetImporter.ts
index 4a60313e4..009645f47 100644
--- a/desktop/src/project/assetImporter.ts
+++ b/desktop/src/project/assetImporter.ts
@@ -37,6 +37,11 @@ const TEXTURE: ImporterFieldSpec[] = [
key: 'premultiplyAlpha', label: 'Premultiply Alpha', type: 'bool', default: false,
category: 'Texture', advanced: true,
},
+ {
+ key: 'sRGB', label: 'sRGB Color', type: 'bool', default: true, category: 'Texture', advanced: true,
+ tooltip: 'The image stores sRGB-encoded color (albedo/UI). Disable for authored-linear '
+ + 'data like normal maps and masks — only meaningful when the project renders in linear color.',
+ },
{ key: 'sliceBorder.left', label: 'Border Left', type: 'number', default: 0, min: 0, category: '9-Slice', advanced: true },
{ key: 'sliceBorder.right', label: 'Border Right', type: 'number', default: 0, min: 0, category: '9-Slice', advanced: true },
{ key: 'sliceBorder.top', label: 'Border Top', type: 'number', default: 0, min: 0, category: '9-Slice', advanced: true },
@@ -138,11 +143,12 @@ export function buildImporterComponent(type: string, importer: Record<string, un
* Undefined when the `.meta` carries neither ⇒ loader defaults. */
export function readTextureImportSettings(
importer: Record<string, unknown> | undefined,
-): { filter?: 'linear' | 'nearest'; wrap?: 'repeat' | 'clamp' | 'mirror' } | undefined {
+): { filter?: 'linear' | 'nearest'; wrap?: 'repeat' | 'clamp' | 'mirror'; srgb?: boolean } | undefined {
if (!importer) return undefined;
const filter = importer.filterMode as 'linear' | 'nearest' | undefined;
const wrap = importer.wrapMode as 'repeat' | 'clamp' | 'mirror' | undefined;
- return filter || wrap ? { filter, wrap } : undefined;
+ const srgb = typeof importer.sRGB === 'boolean' ? importer.sRGB : undefined;
+ return filter || wrap || srgb !== undefined ? { filter, wrap, srgb } : undefined;
}
/** Apply one inspector edit to a copy of the `importer` block (dotted keys →
diff --git a/desktop/tests/asset-importer.test.ts b/desktop/tests/asset-importer.test.ts
index 40c0c1888..7143106a9 100644
--- a/desktop/tests/asset-importer.test.ts
+++ b/desktop/tests/asset-importer.test.ts
@@ -23,6 +23,7 @@ describe('importerDefaults (import-time .meta)', () => {
filterMode: 'linear',
wrapMode: 'repeat',
premultiplyAlpha: false,
+ sRGB: true,
sliceBorder: { left: 0, right: 0, top: 0, bottom: 0 },
});
});
diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.ts
index f3d79df2c..ec795d88c 100644
--- a/sdk/src/asset/loaders/TextureLoader.ts
+++ b/sdk/src/asset/loaders/TextureLoader.ts
@@ -33,6 +33,10 @@ export interface TextureImportSettings {
readonly filter?: 'linear' | 'nearest';
readonly wrap?: 'repeat' | 'clamp' | 'mirror';
readonly mipmaps?: boolean;
+ /** Whether the image stores sRGB-encoded color (default). Authored-linear
+ * data (normal maps, masks) sets false so the linear pipeline skips the
+ * hardware EOTF. Ignored in gamma mode. */
+ readonly srgb?: boolean;
}
export type TextureImportSettingsResolver = (ref: string) => TextureImportSettings | undefined;
@@ -166,7 +170,9 @@ export class TextureLoader implements AssetLoader<TextureResult> {
// runtimeLoader.loadTextures used — instead of a URL-based <img>.
if (this.pixelDecoder_) {
const result = await this.pixelDecoder_(path, flip);
- const params: TextureParams = { filterMode: settings?.filter, wrapMode: settings?.wrap };
+ const params: TextureParams = {
+ filterMode: settings?.filter, wrapMode: settings?.wrap, srgb: settings?.srgb,
+ };
const handle = createTextureFromPixels(this.module_, result, flip, params);
return { handle, width: result.width, height: result.height };
}
@@ -242,7 +248,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {
if (gl) {
return this.createTextureWebGL2(gl, img, width, height, uploadFlip, settings);
}
- return this.createTextureFallback(img, width, height, uploadFlip);
+ return this.createTextureFallback(img, width, height, uploadFlip, settings);
}
private getWebGL2Context(): WebGL2RenderingContext | null {
@@ -275,9 +281,10 @@ export class TextureLoader implements AssetLoader<TextureResult> {
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip ? 1 : 0);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
// Linear pipeline: color textures store sRGB-encoded — the sampler
- // linearizes in hardware. (Data textures opt out via the importer's
- // sRGB flag once wired; until then all image textures are color.)
- const internalFormat = linearColorSpace() ? gl.SRGB8_ALPHA8 : gl.RGBA;
+ // linearizes in hardware. Data textures (normal maps) opt out via
+ // the importer's sRGB flag.
+ const internalFormat = linearColorSpace() && (settings?.srgb ?? true)
+ ? gl.SRGB8_ALPHA8 : gl.RGBA;
gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, gl.RGBA, gl.UNSIGNED_BYTE, img as any);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glMinFilter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glMagFilter);
@@ -306,6 +313,7 @@ export class TextureLoader implements AssetLoader<TextureResult> {
private createTextureFallback(
img: HTMLImageElement | ImageBitmap,
width: number, height: number, flip: boolean,
+ settings?: TextureImportSettings,
): TextureResult {
const { canvas, ctx } = this.ensureCanvas_();
if (canvas.width < width || canvas.height < height) {
@@ -321,9 +329,11 @@ export class TextureLoader implements AssetLoader<TextureResult> {
unpremultiplyAlpha(pixels);
const rm = requireResourceManager();
+ // Format 2 = sRGB color under the linear pipeline (see rm_createTexture).
+ const format = linearColorSpace() && (settings?.srgb ?? true) ? 2 : 1;
const handle = withMalloc(this.module_, pixels.length, ptr => {
this.module_.HEAPU8.set(pixels, ptr);
- return rm.createTexture(width, height, ptr, pixels.length, 1, flip);
+ return rm.createTexture(width, height, ptr, pixels.length, format, flip);
});
return { handle, width, height };
diff --git a/sdk/src/runtimeAssets.ts b/sdk/src/runtimeAssets.ts
index 6c6031c2f..9a7c3189d 100644
--- a/sdk/src/runtimeAssets.ts
+++ b/sdk/src/runtimeAssets.ts
@@ -7,8 +7,10 @@
* builder runtime loader and the spine scene loader (and any editor that
* drives the same load path). Kept in its own module so neither the
* runtime loader nor the spine loader has to import the other.
+import { linearColorSpace } from './env';
*/
import type { ESEngineModule } from './wasm';
+import { linearColorSpace } from './env';
import type { Backend } from './asset/Backend';
import { requireResourceManager } from './resourceManager';
import { withMalloc } from './wasmScratch';
@@ -43,6 +45,8 @@ export interface RuntimeAssetSource {
export interface TextureParams {
filterMode?: string;
wrapMode?: string;
+ /** sRGB-encoded color image (default true); see TextureImportSettings.srgb. */
+ srgb?: boolean;
}
const FILTER_MODE_MAP: Record<string, number> = { 'nearest': 0, 'linear': 1 };
@@ -56,15 +60,17 @@ export function createTextureFromPixels(
params?: TextureParams,
): number {
const rm = requireResourceManager();
+ // Format code 2 = sRGB-encoded color (linear pipeline); 1 = plain RGBA8.
+ const format = linearColorSpace() && (params?.srgb ?? true) ? 2 : 1;
return withMalloc(module, result.pixels.length, ptr => {
module.HEAPU8.set(result.pixels, ptr);
if (params && (params.filterMode || params.wrapMode) && rm.createTextureEx) {
const filter = FILTER_MODE_MAP[params.filterMode ?? 'linear'] ?? 1;
const wrap = WRAP_MODE_MAP[params.wrapMode ?? 'clamp'] ?? 1;
- return rm.createTextureEx(result.width, result.height, ptr, result.pixels.length, 1, flipY, filter, wrap);
+ return rm.createTextureEx(result.width, result.height, ptr, result.pixels.length, format, flipY, filter, wrap);
}
- return rm.createTexture(result.width, result.height, ptr, result.pixels.length, 1, flipY);
+ return rm.createTexture(result.width, result.height, ptr, result.pixels.length, format, flipY);
});
}
diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.ts
index be366a29d..085ec992b 100644
--- a/sdk/src/runtimeLoader.ts
+++ b/sdk/src/runtimeLoader.ts
@@ -131,6 +131,7 @@ function mergeSceneTextureImportSettings(
settings[resolveRef(ref)] = {
filter: s.filterMode as TextureImportSettings['filter'],
wrap: s.wrapMode as TextureImportSettings['wrap'],
+ srgb: s.srgb,
};
}
}