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
feat(health): a shader declares which textures it reads as data
esengine7397950505b8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5f14cbbf7..b4b235ba1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,10 @@ published separately; it ships inside the editor.## [Unreleased]+### Added++- **Project Health names the textures a material reads as data while they are still decoded as colour.** In a linear project, a normal map or packed roughness marked sRGB is bent on the way in; models imported before 0.74 left every one of them that way. The report lists them in one finding and its Fix turns sRGB off on all of them. Which texture params hold numbers is the shader's to say: `#pragma param … texture … texel(data)`, which the built-in Model and Lit shaders now declare. The same report names settings in `project.esproject` that nothing reads.+## [0.74.0] - 2026-09-27### Addeddiff --git a/desktop b/desktopindex b6f86f9cf..5ec5719f0 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit b6f86f9cfb8554c00ce62b1682823fbbdacbe245+Subproject commit 5ec5719f023ee243f62ff1dff9f0cc82fc58644adiff --git a/docs/astro/src/content/docs/graphics/materials.mdx b/docs/astro/src/content/docs/graphics/materials.mdxindex f6f6de84c..57992c6f4 100644--- a/docs/astro/src/content/docs/graphics/materials.mdx+++ b/docs/astro/src/content/docs/graphics/materials.mdx@@ -121,7 +121,7 @@ The sprite's texture is `u_textures[0]` (declare `uniform sampler2D u_textures[8| `float` / `int` | `float` / `int` | `number` | Scalar knob; `range(min,max)` + `ui(slider)` for the inspector. || `vec2` / `vec3` / `vec4` | same | `[x, y, …]` | Vector knob. || `color` | `vec4` | `[r, g, b, a]` | RGBA, 0..1; color picker in the inspector. |-| `texture` | `sampler2D` | texture handle | `default(white \| black \| flatnormal)` fallback when unset. |+| `texture` | `sampler2D` | texture handle | `default(white \| black \| flatnormal)` fallback when unset. Add `texel(data)` when the shader reads the texels as numbers (a normal map, packed roughness): Project Health then flags a texture bound there that is still marked sRGB, which the linear pipeline would decode as colour. |Params live in an engine-generated std140 block, initialized from their `default(…)`values — a material that never sets a param still draws correctly.diff --git a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdxindex f1dd30932..68af75ff5 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx@@ -106,7 +106,7 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一| `float` / `int` | `float` / `int` | `number` | 标量旋钮;`range(min,max)` + `ui(slider)` 控制检视器。 || `vec2` / `vec3` / `vec4` | 同名 | `[x, y, …]` | 向量旋钮。 || `color` | `vec4` | `[r, g, b, a]` | RGBA,0..1;检视器里是取色器。 |-| `texture` | `sampler2D` | 纹理句柄 | `default(white \| black \| flatnormal)` 未设置时的回退。 |+| `texture` | `sampler2D` | 纹理句柄 | `default(white \| black \| flatnormal)` 未设置时的回退。着色器把贴图当数值读(法线贴图、打包的粗糙度)时加 `texel(data)`:项目健康检查会指出绑在这里却仍标为 sRGB 的贴图,线性管线会把它当颜色解码。 |参数存在引擎生成的 std140 块里,并从 `default(…)` 初始化——材质从未设置某参数也能正确绘制。diff --git a/pipeline/src/assets/dataTextureAudit.ts b/pipeline/src/assets/dataTextureAudit.tsnew file mode 100644index 000000000..1c3b7852a--- /dev/null+++ b/pipeline/src/assets/dataTextureAudit.ts@@ -0,0 +1,89 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file dataTextureAudit.ts — textures a material reads as numbers while their+ * settings still call them colour.+ *+ * Under the linear pipeline an sRGB texture is decoded as colour on the way in,+ * which bends a normal map and packed roughness. Which texture param holds+ * numbers is the shader's to say (`texel(data)` on its `#pragma param`), so this+ * asks every material's shader rather than guessing from names.+ */+import { reflectEsshader } from '../../../sdk/src/render/shaderReflect';+import { builtinShaderTemplate } from '../../../sdk/src/render/builtinShaders';+import { builtinRefId, resolveDocumentRef } from '../../../sdk/src/asset/documentRef';++export interface DataTextureAuditSource {+ /** Project paths of every `.esmaterial`. */+ materials: Iterable<string>;+ readText(path: string): Promise<string | null>;+ /** A ref as a project path: `@uuid:…` looked up, a path passed through; null if unknown. */+ resolveRef(ref: string): string | null;+ /** A texture's import settings (its `.meta` importer block). */+ importer(path: string): Record<string, unknown> | undefined;+}++export interface DataTextureFinding {+ texture: string;+ /** Every material that reads it as data, and through which param. */+ readBy: Array<{ material: string; param: string }>;+}++interface MaterialDoc { shader?: unknown; instanceOf?: unknown; properties?: Record<string, unknown> }++const withoutLeadingSlash = (p: string): string => (p.startsWith('/') ? p.slice(1) : p);++/** A ref a document carries, as a project path: a uuid names the asset itself, anything else sits beside the document. */+function refFrom(src: DataTextureAuditSource, documentPath: string, ref: string): string | null {+ const path = src.resolveRef(ref.startsWith('@uuid:') ? ref : resolveDocumentRef(documentPath, ref));+ return path === null ? null : withoutLeadingSlash(path);+}++/** Every texture read as data by some material and still marked sRGB, in path order. */+export async function dataTexturesMarkedColour(src: DataTextureAuditSource): Promise<DataTextureFinding[]> {+ const dataParams = new Map<string, string[]>();+ const paramsOf = async (materialPath: string, shaderRef: string): Promise<string[]> => {+ const builtin = builtinRefId(shaderRef);+ const key = builtin !== null ? shaderRef : refFrom(src, materialPath, shaderRef);+ if (key === null) return [];+ let params = dataParams.get(key);+ if (!params) {+ const source = builtin !== null+ ? builtinShaderTemplate(builtin)?.source ?? null+ : await src.readText(key);+ params = source ? reflectEsshader(source).params.filter((p) => p.texel === 'data').map((p) => p.name) : [];+ dataParams.set(key, params);+ }+ return params;+ };+ const readDoc = async (path: string): Promise<MaterialDoc | null> => {+ const text = await src.readText(path);+ if (!text) return null;+ try { return JSON.parse(text) as MaterialDoc; } catch { return null; }+ };++ const found = new Map<string, DataTextureFinding>();+ for (const materialPath of src.materials) {+ const doc = await readDoc(materialPath);+ if (!doc) continue;+ // An instance takes its parent's shader; its own properties are what it binds.+ let shader = typeof doc.shader === 'string' ? doc.shader : null;+ if (!shader && typeof doc.instanceOf === 'string') {+ const parentPath = refFrom(src, materialPath, doc.instanceOf);+ const parent = parentPath ? await readDoc(parentPath) : null;+ shader = typeof parent?.shader === 'string' ? parent.shader : null;+ }+ if (!shader) continue;+ for (const param of await paramsOf(materialPath, shader)) {+ const value = doc.properties?.[param];+ if (typeof value !== 'string') continue;+ const path = refFrom(src, materialPath, value);+ if (!path) continue;+ if (src.importer(path)?.sRGB === false) continue;+ const finding = found.get(path) ?? { texture: path, readBy: [] };+ finding.readBy.push({ material: materialPath, param });+ found.set(path, finding);+ }+ }+ return [...found.values()].sort((a, b) => a.texture.localeCompare(b.texture));+}diff --git a/pipeline/tests/data-texture-audit.test.ts b/pipeline/tests/data-texture-audit.test.tsnew file mode 100644index 000000000..607636103--- /dev/null+++ b/pipeline/tests/data-texture-audit.test.ts@@ -0,0 +1,51 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Textures a material's shader reads as numbers, still marked sRGB, are found.+ */+import { describe, it, expect } from 'vitest';+import { dataTexturesMarkedColour } from '../src/assets/dataTextureAudit';++function project(files: Record<string, unknown>, importers: Record<string, Record<string, unknown>>) {+ const text = (v: unknown) => (typeof v === 'string' ? v : JSON.stringify(v));+ return {+ materials: Object.keys(files).filter((p) => p.endsWith('.esmaterial')),+ readText: async (p: string) => (p in files ? text(files[p]) : null),+ resolveRef: (ref: string) => {+ const path = ref.startsWith('@uuid:') ? { '@uuid:n1': 'assets/m/n.png' }[ref] ?? null : ref.replace(/^\//, '');+ return path && (path in importers || path in files) ? path : null;+ },+ importer: (p: string) => importers[p],+ };+}++describe('textures read as data but marked colour', () => {+ it('finds a normal and a packed map bound to the model shader, and leaves colour and cleared ones alone', async () => {+ const found = await dataTexturesMarkedColour(project({+ 'assets/m/a.esmaterial': { shader: 'builtin:model', properties: {+ u_normalMap: 'n.png', u_metallicRoughnessMap: '/assets/m/orm.png',+ u_emissiveMap: 'glow.png', u_occlusionMap: 'ao.png' } },+ 'assets/m/b.esmaterial': { shader: 'builtin:model', properties: { u_normalMap: '@uuid:n1' } },+ }, {+ 'assets/m/n.png': { sRGB: true }, 'assets/m/orm.png': {}, 'assets/m/glow.png': {},+ 'assets/m/ao.png': { sRGB: false },+ }));+ expect(found).toEqual([+ { texture: 'assets/m/n.png', readBy: [+ { material: 'assets/m/a.esmaterial', param: 'u_normalMap' },+ { material: 'assets/m/b.esmaterial', param: 'u_normalMap' }] },+ { texture: 'assets/m/orm.png', readBy: [{ material: 'assets/m/a.esmaterial', param: 'u_metallicRoughnessMap' }] },+ ]);+ });++ it('asks a project shader which params hold data, and an instance its parent', async () => {+ const shader = '#pragma param u_bump texture default(flatnormal) texel(data)\n#pragma param u_tex texture default(white)\n';+ const found = await dataTexturesMarkedColour(project({+ 'assets/s/bump.esshader': shader,+ 'assets/s/base.esmaterial': { shader: 'bump.esshader', properties: { u_bump: 'flatnormal', u_tex: 'b.png' } },+ 'assets/s/child.esmaterial': { instanceOf: 'base.esmaterial', properties: { u_bump: 'b.png' } },+ }, { 'assets/s/b.png': {} }));+ // `flatnormal` is an engine stand-in, not a file, so the parent binds no data texture.+ expect(found).toEqual([{ texture: 'assets/s/b.png', readBy: [{ material: 'assets/s/child.esmaterial', param: 'u_bump' }] }]);+ });+});diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 441999307..def1cf5bd 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -7413,6 +7413,7 @@ defaultTexture: string | undefineddisplayName: stringname: stringrange: { min: number; max: number; } | undefined+texel: "data" | undefinedtype: ShaderParamTypeui: string | undefined```diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 914adeb1d..9f4955f38 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -7614,6 +7614,7 @@ defaultTexture: string | undefineddisplayName: stringname: stringrange: { min: number; max: number; } | undefined+texel: "data" | undefinedtype: ShaderParamTypeui: string | undefined```diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 5fd9cec29..aa870af26 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -7408,6 +7408,7 @@ defaultTexture: string | undefineddisplayName: stringname: stringrange: { min: number; max: number; } | undefined+texel: "data" | undefinedtype: ShaderParamTypeui: string | undefined```diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex bb27f6f44..1b8157ef5 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -7678,6 +7678,7 @@ defaultTexture: string | undefineddisplayName: stringname: stringrange: { min: number; max: number; } | undefined+texel: "data" | undefinedtype: ShaderParamTypeui: string | undefined```diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex fc034d6e1..d48f41682 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -7415,6 +7415,7 @@ defaultTexture: string | undefineddisplayName: stringname: stringrange: { min: number; max: number; } | undefined+texel: "data" | undefinedtype: ShaderParamTypeui: string | undefined```diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.tsindex ffab3adf5..6dd1763dc 100644--- a/sdk/src/render/builtinShaders.ts+++ b/sdk/src/render/builtinShaders.ts@@ -79,7 +79,7 @@ const SPRITE_LIT = `#pragma shader "Sprite Lit"#pragma version 300 es#pragma domain Lit#pragma param u_tint color default(1,1,1,1)-#pragma param u_normalMap texture default(flatnormal)+#pragma param u_normalMap texture default(flatnormal) texel(data)#pragma fragmentprecision mediump float;@@ -334,15 +334,15 @@ const MODEL = `#pragma shader "Model"#pragma version 300 es#pragma domain Lit#pragma param u_tint color default(1,1,1,1)-#pragma param u_normalMap texture default(flatnormal)+#pragma param u_normalMap texture default(flatnormal) texel(data)#pragma param u_emissive color default(0,0,0,1)#pragma param u_emissiveMap texture default(white)-#pragma param u_occlusionMap texture default(white)+#pragma param u_occlusionMap texture default(white) texel(data)#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)#pragma param u_alphaCutoff float default(0) range(0,1) ui(slider)#pragma param u_metallic float default(0) range(0,1) ui(slider)#pragma param u_roughness float default(1) range(0,1) ui(slider)-#pragma param u_metallicRoughnessMap texture default(white)+#pragma param u_metallicRoughnessMap texture default(white) texel(data)#pragma fragmentprecision mediump float;diff --git a/sdk/src/render/shaderReflect.ts b/sdk/src/render/shaderReflect.tsindex 1e54166ca..a82e8e1cd 100644--- a/sdk/src/render/shaderReflect.ts+++ b/sdk/src/render/shaderReflect.ts@@ -17,7 +17,7 @@* otherwise drop without a word.** Grammar (matches ShaderParser):- * #pragma param <name> <type> [default(csv)] [range(min,max)] [ui(hint)]+ * #pragma param <name> <type> [default(csv)] [range(min,max)] [ui(hint)] [texel(data)]* #pragma domain <Unlit|Lit|PostProcess|UI>*/@@ -36,6 +36,11 @@ export interface ShaderParam {range?: { min: number; max: number };/** Optional UI hint from `ui(...)`, e.g. "slider". */ui?: string;+ /**+ * `texel(data)`: a texture whose texels this shader reads as numbers (a normal,+ * packed roughness), which sRGB decode would bend. Absent means colour.+ */+ texel?: 'data';}export interface ShaderReflection {@@ -98,6 +103,8 @@ function parseParam(arg: string): ShaderParam | null {const ui = clause(arg, 'ui');if (ui !== undefined) param.ui = ui;+ if (type === 'texture' && clause(arg, 'texel') === 'data') param.texel = 'data';+return param;}diff --git a/sdk/tests/shader-reflect.test.ts b/sdk/tests/shader-reflect.test.tsindex 24e736d45..84309f02e 100644--- a/sdk/tests/shader-reflect.test.ts+++ b/sdk/tests/shader-reflect.test.ts@@ -115,3 +115,22 @@ describe('the built-in templates take their defaults from their own source', ()expect(builtinShaderTemplate('sprite-lit')!.defaults).toEqual({ u_tint: { r: 1, g: 1, b: 1, a: 1 } });});});++describe('which texture params a shader reads as numbers', () => {+ it('is what `texel(data)` says, and only on a texture', () => {+ const { params } = reflectEsshader([+ '#pragma param u_n texture default(flatnormal) texel(data)',+ '#pragma param u_c texture default(white)',+ '#pragma param u_k float default(1) texel(data)',+ ].join('\n'));+ expect(params.map((p) => [p.name, p.texel])).toEqual([['u_n', 'data'], ['u_c', undefined], ['u_k', undefined]]);+ });++ it('marks the model template\'s normal, occlusion and packed maps, and not its colour ones', () => {+ const data = reflectEsshader(builtinShaderTemplate('model')!.source).params+ .filter((p) => p.type === 'texture').map((p) => [p.name, p.texel ?? 'colour']);+ expect(Object.fromEntries(data)).toEqual({+ u_normalMap: 'data', u_emissiveMap: 'colour', u_occlusionMap: 'data', u_metallicRoughnessMap: 'data',+ });+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 683643a3e..e6413257d 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1250,6 +1250,10 @@ export const NOTED = {{ note: 'A project file\'s settings that nothing reads are named instead of dropped' },'feat(project): opening a project names the settings its file carries that nothing reads':{ note: 'A project file\'s settings that nothing reads are named instead of dropped' },+ 'feat(health): a shader declares which textures it reads as data':+ { note: 'Project Health names the textures a material reads as data while they are still decoded as colour' },+ 'feat(health): the preflight names data maps decoded as colour, and fixes them':+ { note: 'Project Health names the textures a material reads as data while they are still decoded as colour' },'fix(eht): the generator writes LF on every platform':{ internal: 'The engine header tool is a repository generator; on Windows it rewrote every generated file with CRLF, which only a contributor there ever saw.' },};