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(render): a bake derives UVs for geometry that has no file

esengine696bf15c6d27
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a7f8d56b7..e46d2285d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,21 @@ published separately; it ships inside the editor.
### Added
+- **Stock geometry takes a bake, and the density suits this engine's units.**
+ Trying to bake a real example found both. `physics-3d` is twenty-two builtin
+ cubes and two lights — the most ordinary lightmap candidate there is — and
+ every one of them was skipped, because a builtin has no `.esmesh` to read a UV
+ set out of and no import setting anyone could turn on for it. Its UVs are
+ derived per bake instead, which rewrites nothing: stock geometry is rebuilt
+ from code every run.
+
+ Then the bake refused the scene. A world unit here is a **design pixel**, so a
+ builtin cube is a hundred units across and the old default of two texels per
+ unit asked for a patch larger than the whole atlas. The defaults are now a
+ 1024 atlas at a quarter texel per unit, which is a lumel every few design
+ pixels. A refused bake also says so where the user is, rather than as a
+ console line nobody reads.
+
- **A bounce carries the colour of what it came off.** Baked light reflected a
neutral grey whatever it hit, so a red wall lit a white floor white — most of
the difference between a bounce and a flat ambient term. A surface's albedo now
diff --git a/desktop b/desktop
index f6d571b4a..af88e40d7 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit f6d571b4a049daa15a78149df14ac3589430cc15
+Subproject commit af88e40d7ab7da3a45da59a252bb6f93ec6dc795
diff --git a/pipeline/src/assets/lightmapBake.ts b/pipeline/src/assets/lightmapBake.ts
index 9a64549e0..7f2a27639 100644
--- a/pipeline/src/assets/lightmapBake.ts
+++ b/pipeline/src/assets/lightmapBake.ts
@@ -10,14 +10,18 @@
*/
import { readFileSync } from 'node:fs';
import { PNG } from 'pngjs';
-import { bakeLightmap, decodeMesh, MeshChannel,
- type BakeSurface, type BakeLight, type BakeOptions } from 'esengine';
+import { bakeLightmap, decodeMesh, unwrapLightmapUV, builtinMeshTemplate, MeshChannel,
+ type MeshData, type BakeSurface, type BakeLight, type BakeOptions } from 'esengine';
import { encodeRgbaPng } from './png';
/** One placed object, as the editor's world describes it. */
export interface SceneBakeSurface {
- /** Absolute path of the `.esmesh` this object draws. */
+ /** Absolute path of the `.esmesh` this object draws, or '' when
+ * {@link builtinRef} names the geometry instead. */
meshFile: string;
+ /** `builtin:<id>` for stock geometry, which is built from code and has no
+ * file to read a UV set out of. */
+ builtinRef?: string;
/** How it is named where a warning has to be readable. */
label: string;
/** Column-major 4x4 world transform. */
@@ -50,6 +54,15 @@ export interface SceneBakeResult {
warnings: string[];
}
+/** Stock geometry with a lightmap UV set, unwrapped once per bake. */
+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);
+ }
+ return cache.get(ref) ?? null;
+}
+
/** sRGB to linear, one channel. 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 {
@@ -116,16 +129,25 @@ export function bakeSceneLightmap(input: SceneBakeInput): SceneBakeResult {
// One decode per texture however many objects share it: a bake reads these
// once and a scene reuses the same few across most of its surfaces.
const averages = new Map<string, [number, number, number] | null>();
+ // Stock geometry is rebuilt from code every run, so its lightmap UVs are
+ // DERIVED here rather than being an asset: unwrapping an imported mesh
+ // rewrites a file the scene already references, and this rewrites nothing.
+ const builtins = new Map<string, MeshData | null>();
for (let i = 0; i < input.surfaces.length; i++) {
const s = input.surfaces[i];
let mesh;
try {
- mesh = decodeMesh(new Uint8Array(readFileSync(s.meshFile)));
+ mesh = s.builtinRef ? builtinGeometry(s.builtinRef, builtins)
+ : decodeMesh(new Uint8Array(readFileSync(s.meshFile)));
} catch (err) {
warnings.push(`${s.label}: ${(err as Error).message}`);
continue;
}
+ if (!mesh) {
+ warnings.push(`${s.label}: "${s.builtinRef}" is not stock geometry this build has`);
+ continue;
+ }
if (!mesh.channels.some((c) => c.semantic === MeshChannel.TexCoord1)) {
warnings.push(`${s.label}: no second UV set, so it receives no baked light —`
+ ' turn on Generate Lightmap UVs in its model\'s import settings and reimport');
diff --git a/pipeline/tests/lightmap-bake.test.ts b/pipeline/tests/lightmap-bake.test.ts
index b3fa50b62..83f457d40 100644
--- a/pipeline/tests/lightmap-bake.test.ts
+++ b/pipeline/tests/lightmap-bake.test.ts
@@ -133,6 +133,33 @@ describe('baking a scene', () => {
expect(broken.warnings.join(' ')).toContain('could not be read');
});
+ it('lights stock geometry, which has no file at all', () => {
+ // Most scenes are built out of builtin primitives, and none of them is a
+ // .esmesh anyone could turn an import setting on for. Their UVs are
+ // derived per bake instead — nothing on disk is rewritten.
+ const result = bakeSceneLightmap({
+ surfaces: [{
+ meshFile: '', builtinRef: 'builtin:cube', label: 'Crate',
+ transform: IDENTITY, baseColor: [0.8, 0.8, 0.8],
+ }],
+ lights: LAMP,
+ options: SMALL,
+ });
+ expect(result.warnings.join(' ')).not.toContain('no second UV set');
+ expect(result.lumels).toBeGreaterThan(0);
+ expect(result.scaleOffset[0]).not.toBeNull();
+ });
+
+ it('names stock geometry this build does not have', () => {
+ const result = bakeSceneLightmap({
+ surfaces: [{ meshFile: '', builtinRef: 'builtin:dodecahedron', label: 'Odd',
+ transform: IDENTITY }],
+ lights: LAMP,
+ options: SMALL,
+ });
+ expect(result.warnings.join(' ')).toContain('not stock geometry');
+ });
+
it('says when it had to guess what a surface reflects', () => {
// Neutral grey is a guess, and a bounce off a red wall that arrives grey
// is the kind of wrong that looks like the bake simply being dull.
diff --git a/sdk/src/lightmap/bake.ts b/sdk/src/lightmap/bake.ts
index e403aecb3..e8f49437a 100644
--- a/sdk/src/lightmap/bake.ts
+++ b/sdk/src/lightmap/bake.ts
@@ -17,7 +17,10 @@ import { solveDirect, solveBounce, type BakeLight, type HitLookup } from './solv
export interface BakeOptions {
/** Side of the square atlas, in texels. */
atlasSize?: number;
- /** How finely a surface is lit, in texels per world unit. */
+ /** How finely a surface is lit, in texels per world unit. A world unit here
+ * is a DESIGN PIXEL, so a room is hundreds of units across and a lumel every
+ * few of them is already fine — a density borrowed from an engine whose unit
+ * is a metre would ask for an atlas no scene could fit. */
texelsPerUnit?: number;
/** How many times light is allowed to reflect. Zero is direct light only —
* useful, because past the sixteen a frame can carry it is still the answer. */
@@ -42,8 +45,8 @@ export interface BakeResult {
}
const DEFAULTS = {
- atlasSize: 512,
- texelsPerUnit: 2,
+ atlasSize: 1024,
+ texelsPerUnit: 0.25,
bounces: 2,
samples: 64,
ambient: [0, 0, 0] as readonly [number, number, number],
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index c56a3ac15..ad228c561 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,11 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'feat(lighting): stock geometry takes a bake, and a refused one says why':
+ { note: 'Stock geometry takes a bake, and the density suits this engine\'s units.' },
+ 'feat(render): a bake derives UVs for geometry that has no file':
+ { note: 'Stock geometry takes a bake, and the density suits this engine\'s units.' },
+
'feat(lighting): a bounce carries the colour of what it came off':
{ note: 'A bounce carries the colour of what it came off.' },
'feat(render): a surface reflects the colour it actually has':