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(lightmap): built-in meshes carry the lightmap UV set the bake lays its charts by, so a baked cube or sphere draws its lightmap instead of nothing
yhhd39f6842ed08
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ff07af1d3..2dd462b26 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,6 +18,10 @@ published separately; it ships inside the editor.- **A model of many parts that moves: `CompositeBatch`.** Put it on the root of a ship, a vehicle or a moving platform assembled from many meshes, and while the game runs its parts are drawn in a few draws parented to the root: a mesh repeated four times or more (a deck plank, a rail post) as one instanced set that copies no vertices, every other part merged by draw state into chunks of the root's own space. Moving, turning or scaling the root moves the whole model and merges nothing again. Adding, removing, re-meshing or re-placing a part re-merges only its chunk within half a second (`CompositeBatchingRes.refresh(root)` looks at once), and a part that keeps moving inside the model is let go to draw itself. The root reports what it drew, and so does `get_stats`. On a model of 2,004 parts, the meshes handed to the renderer went from 2,003 to 5 and the CPU frame from 3.3 ms to 1.2–1.4 ms; a model of about a hundred parts costs the same either way. A part under a composite is never also statically batched.+### Fixed++- **Baked lighting shows on built-in meshes.** A `builtin:` cube, sphere or plane baked with `bake-scene` or the editor drew as if nothing had been baked: the bake unwrapped a lightmap UV set for its own use, but the mesh the game loaded had none, so the atlas was never read. The built-in meshes now carry that UV set, the bake lays out its charts by it, and existing bakes stay valid.+## [0.98.0] - 2026-11-05### Addeddiff --git a/pipeline/src/assets/lightmapBake.ts b/pipeline/src/assets/lightmapBake.tsindex 58a5599f1..29c8a9416 100644--- a/pipeline/src/assets/lightmapBake.ts+++ b/pipeline/src/assets/lightmapBake.ts@@ -11,7 +11,7 @@import { readFileSync } from 'node:fs';import { readFile, writeFile } from 'node:fs/promises';import { PNG } from 'pngjs';-import { bakeLightmapSteps, bakeRunner, lightmapImage, decodeMesh, unwrapLightmapUV, builtinMeshTemplate, MeshChannel,+import { bakeLightmapSteps, bakeRunner, lightmapImage, decodeMesh, builtinMeshTemplate, MeshChannel,type MeshData, type BakeSurface, type BakeLight, type BakeOptions,type BakeStep, type CapturedPanorama, type ProbeGrid } from 'esengine';import { encodeRgbaPng } from './png';@@ -153,11 +153,12 @@ export function probeDocumentText(doc: ProbeVolumeDocument): string {+ ` "irradiance": ${JSON.stringify(doc.irradiance)}\n}\n`;}-/** Stock geometry with a lightmap UV set, unwrapped once per bake. */+/** Stock geometry as the runtime draws it: the template carries its lightmap UV set, so the bake+ * lays charts out by the very set the frame samples the atlas through. */function builtinGeometry(ref: string, cache: Map<string, MeshData | null>): MeshData | null {if (!cache.has(ref)) {const template = builtinMeshTemplate(ref);- cache.set(ref, template ? unwrapLightmapUV(template.build()).mesh : null);+ cache.set(ref, template ? template.build() : null);}return cache.get(ref) ?? null;}diff --git a/sdk/src/asset/builtinMeshes.ts b/sdk/src/asset/builtinMeshes.tsindex 57c14399e..bac3fa1ae 100644--- a/sdk/src/asset/builtinMeshes.ts+++ b/sdk/src/asset/builtinMeshes.ts@@ -7,6 +7,7 @@import { BUILTIN_REF_PREFIX, builtinRefId } from './documentRef';import { MeshChannel, MeshChannelType, packChannels, type MeshData } from './meshFormat';+import { unwrapLightmapUV } from '../lightmap/unwrap';/*** The size every primitive is built at, matching `ShapeRenderer.size` — the other@@ -31,6 +32,13 @@ export interface BuiltinMeshTemplate {* sees; a flat one stays two-sided, or a ground plane disappears from below.*/closed: boolean;+ /**+ * The geometry, with its lightmap UV set (`TexCoord1`) already unwrapped: a+ * bake lays its charts out by that set and the runtime samples the atlas+ * through it, so both must read the same one, and the template is where it is+ * made. Without it the renderer has no UV to read a baked atlas by, and a+ * baked built-in mesh draws as if nothing had been baked.+ */build(): MeshData;}@@ -249,10 +257,22 @@ function toMeshData(b: Builder): MeshData {};}+/** A copy a caller may change without changing what the next caller is given. */+function copyMesh(m: MeshData): MeshData {+ return { ...m, channels: m.channels.map((c) => ({ ...c })), vertices: m.vertices.slice(), indices: m.indices.slice(),+ aabbMin: [...m.aabbMin] as [number, number, number], aabbMax: [...m.aabbMax] as [number, number, number] };+}+const template = (id: string, label: string, description: string, closed: boolean, build: () => Builder,-): BuiltinMeshTemplate =>- ({ id, ref: BUILTIN_REF_PREFIX + id, label, description, closed, build: () => toMeshData(build()) });+): BuiltinMeshTemplate => {+ // Unwrapped once (a few milliseconds for the roundest shapes) and copied after.+ let built: MeshData | null = null;+ return {+ id, ref: BUILTIN_REF_PREFIX + id, label, description, closed,+ build: () => copyMesh(built ??= unwrapLightmapUV(toMeshData(build())).mesh),+ };+};export const BUILTIN_MESH_TEMPLATES: readonly BuiltinMeshTemplate[] = [template('cube', 'Cube', `A ${UNIT}-unit box, one flat normal per face.`, true,diff --git a/sdk/tests/builtin-meshes.test.ts b/sdk/tests/builtin-meshes.test.tsindex 7eceb18fb..2e779870c 100644--- a/sdk/tests/builtin-meshes.test.ts+++ b/sdk/tests/builtin-meshes.test.ts@@ -54,11 +54,11 @@ describe('built-in meshes', () => {expect(builtinMeshTemplate('builtin:teapot')).toBeUndefined();});- it('writes the channel layout the model import writes', () => {+ it('writes the channel layout the model import writes with --lightmap-uv', () => {for (const t of BUILTIN_MESH_TEMPLATES) {const mesh = t.build();expect(mesh.channels.map((c) => c.semantic)).toEqual([- MeshChannel.Position, MeshChannel.TexCoord0, MeshChannel.Color, MeshChannel.Normal,+ MeshChannel.Position, MeshChannel.TexCoord0, MeshChannel.Color, MeshChannel.Normal, MeshChannel.TexCoord1,]);expect(mesh.channels.find((c) => c.semantic === MeshChannel.Color)!.type).toBe(MeshChannelType.UNorm8);@@ -108,4 +108,27 @@ describe('built-in meshes', () => {expect(size).toBeCloseTo(100, 4);}});+ // A bake lays its charts out by TexCoord1 and the renderer turns the lightmap on only for a mesh+ // that has one: a template without it baked fine and drew as if nothing had been baked.+ it('carries a lightmap UV set inside the unit square, the same each time it is built', () => {+ for (const t of BUILTIN_MESH_TEMPLATES) {+ const a = t.build();+ const uv1 = a.channels.find((c) => c.semantic === MeshChannel.TexCoord1);+ expect(uv1, t.id).toBeDefined();+ expect(uv1!.type).toBe(MeshChannelType.Float32);+ const view = new DataView(a.vertices.buffer, a.vertices.byteOffset, a.vertices.byteLength);+ for (let v = 0; v < a.vertexCount; v++) {+ for (let k = 0; k < 2; k++) {+ const x = view.getFloat32(v * a.vertexStride + uv1!.offset + k * 4, true);+ expect(x).toBeGreaterThanOrEqual(0);+ expect(x).toBeLessThanOrEqual(1);+ }+ }+ const b = t.build();+ expect(Array.from(b.vertices)).toEqual(Array.from(a.vertices));+ // A copy, not the cached geometry: what one caller changes, the next is not given.+ a.vertices[0] ^= 0xff;+ expect(t.build().vertices[0]).toBe(b.vertices[0]);+ }+ });});diff --git a/tools/render-lab/README.md b/tools/render-lab/README.mdindex b8f3deb29..b87bc514d 100644--- a/tools/render-lab/README.md+++ b/tools/render-lab/README.md@@ -58,7 +58,7 @@ Each figure is a region's linear value in the engine as a share of Cycles' value- **No colour bleed.** On the red wall the engine's red channel is 0.10–0.32 of Cycles, but green and blue are 1–5×. The bounce arrives grey, and the wall's albedo does not tint it the way Cycles does. The green wall shows the same.- **No leak in `gi_leak`.** That is only because the lit outside is never on screen, so there is nothing to leak.- **Banding.** The WebGL2 voxel fallback and SSGI both band visibly in the emissive room.-- **The bake shows nothing on built-in meshes.** `BAKE=1` renders pixel-identical to no GI. `bake-scene` unwraps a built-in mesh for the bake (`pipeline/src/assets/lightmapBake.ts`), but the runtime's `builtin:cube` has no `TexCoord1`, so `MESH_LIGHTMAP` is never on and the atlas is never read. `examples/physics-3d` bakes `builtin:cube` too.+- **The bake showed nothing on built-in meshes (fixed the same day).** A `builtin:` mesh had no `TexCoord1` at runtime, so `MESH_LIGHTMAP` never turned on. The templates now carry the bake's own unwrap. Since the fix, the baked `gi_room` is 1.5–1.8× Cycles everywhere (ceiling 1.53, back wall 1.64, floor in shade 1.9, the block's shaded face 1.6), with dark seams at chart edges. This calibration gap is still open, and it is on top of the 1.19 the direct light shows.- **Direct light is 19% bright.** The sun patch is 1.19 of Cycles on both backends. This is separate from GI and not explained yet.GPU cost at 1280×720, WebGL2, on this desktop: