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
perf(bake): a texture's average reads sRGB through a table
esenginef65a7b0dc2fa
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5d5dde4e2..e34ee3714 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,7 +17,7 @@ published separately; it ships inside the editor.### Added- **Baked light is a lit surface's indirect light.** A lit renderer — every imported model's is — reads its patch of the atlas in place of the environment's diffuse light and a probe volume's, and keeps its lamps, shadows and reflections live. The bake holds the sky as it reaches each point past whatever stands in the way, plus what the surfaces around give off, over as many bounces as asked: under ORCA Bistro's awnings and in its side streets the sky is now mostly hidden, where before every point got all of it. Before, a model's material never read the atlas at all, and a mesh drawn without one added it on top of the sky and the lamps and was lit twice. The sky a bake gathers is the environment's own irradiance, turned and tinted as the frame reads it, so an open floor bakes to exactly what the frame would light it with. An atlas holds light up to eight times a flat ambient of one, where eight plain bits stopped at one. A cutout lets the share of rays that meets its holes through, both faces of a two-sided surface give off light, and a layer lying closer above a surface than half a lumel — a decal, a wet sheen — no longer walls it off from the sky. A scene baked before reads as stale: its atlas is in the old encoding and must be baked again.-- **A bake uses every core, in C++.** The ray casting runs in a compiled kernel (`build-tools/lightmap`, built from `tools/lightmap-wasm`) on every core, with a tree built for tracing rather than for building quickly. Its rules are the TypeScript solve's, rounding included, and a gate holds the two to the same atlas bytes; any thread count writes the same bytes. ORCA Bistro — 1.2 million lumels, 2.8 million triangles — bakes in 81 s on a 20-thread machine, where a pool of worker threads took 312 s and one thread twenty-five to fifty-five minutes; the solve itself is 36 s of that. The editor and `bake-scene` both use it. Two triangles a ray meets at the same distance now go to the lower-numbered one, so the answer no longer depends on how a tree is built; physics-3d's committed bake moved by one step in two texels.+- **A bake uses every core, in C++.** The ray casting runs in a compiled kernel (`build-tools/lightmap`, built from `tools/lightmap-wasm`) on every core, with a tree built for tracing rather than for building quickly. Its rules are the TypeScript solve's, rounding included, and a gate holds the two to the same atlas bytes; any thread count writes the same bytes. ORCA Bistro — 1.2 million lumels, 2.8 million triangles — bakes in 63 s on a 20-thread machine, where a pool of worker threads took 312 s and one thread twenty-five to fifty-five minutes; the solve itself is 36 s of that, and most of the rest is decoding the textures a surface's colour is averaged from. The editor and `bake-scene` both use it. Two triangles a ray meets at the same distance now go to the lower-numbered one, so the answer no longer depends on how a tree is built; physics-3d's committed bake moved by one step in two texels.- **A model whose second UV set cannot be baked into gets a lightmap UV in its place.** One that covers under 5% of its square pays for the empty rest — ORCA Bistro's road, 3533 m² in 0.5% of it, asked for more atlas than the rest of the town together — and one that lies on itself gives every copy the same texel's light: Bistro's curbs share one strip eighty layers deep, and a building's facades a hundred. Import unwraps either instead and says why; `import-model --lightmap-uv` turns generation on from the command line. A bake names a surface whose UVs overlap, and one that does not fit names the three surfaces taking the most room and how much of their square their UVs cover.### Fixeddiff --git a/pipeline/src/assets/lightmapBake.ts b/pipeline/src/assets/lightmapBake.tsindex 8a533b518..f34c51fe6 100644--- a/pipeline/src/assets/lightmapBake.ts+++ b/pipeline/src/assets/lightmapBake.ts@@ -158,12 +158,12 @@ function builtinGeometry(ref: string, cache: Map<string, MeshData | null>): Meshreturn cache.get(ref) ?? null;}-/** sRGB to linear, one channel. A texture stores what a screen shows, and an+/** sRGB to linear per byte value. A texture stores what a screen shows, and an* average taken before this is decoded is brighter than the surface is. */-function toLinear(v: number): number {+const TO_LINEAR = Float64Array.from({ length: 256 }, (_, v) => {const c = v / 255;return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;-}+});/** A texture as a bake reads it: its mean colour in linear light, and the share* of it a cutout keeps. */@@ -182,11 +182,12 @@ function statsOf(file: string, cutoff: number): TextureStats | null {const pixels = png.width * png.height;if (pixels === 0) return null;const floor = cutoff > 0 ? cutoff * 255 : -1;+ const data = png.data;for (let i = 0; i < pixels; i++) {- if (png.data[i * 4 + 3] < floor) continue;- r += toLinear(png.data[i * 4]);- g += toLinear(png.data[i * 4 + 1]);- b += toLinear(png.data[i * 4 + 2]);+ if (data[i * 4 + 3] < floor) continue;+ r += TO_LINEAR[data[i * 4]];+ g += TO_LINEAR[data[i * 4 + 1]];+ b += TO_LINEAR[data[i * 4 + 2]];kept++;}if (kept === 0) return { mean: [0, 0, 0], coverage: 0 };diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex b342da5a4..6c3d14034 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1286,6 +1286,8 @@ export const NOTED = {{ note: 'A bake uses every core, in C++' },'perf(bake): the editor bakes through the C++ kernel':{ note: 'A bake uses every core, in C++' },+ 'perf(bake): a texture\'s average reads sRGB through a table':+ { note: 'A bake uses every core, in C++' },'fix(bake): a coloured light bakes the colour it lights with':{ note: 'A coloured light bakes the colour it lights with' },'fix(bake): the collector reads the colour space from the engine host':