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.

fix(water): baking a water region no longer holds the frame - its steps were a fixed count of rows and whole sweeps of the grid, 70 to 400 ms each at 2048 texels a side; a step is a count of cells now, 20 ms at the most

yhhe970e80d08a1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 432df72c3..deeb3a619 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Fixed
+- **Baking a water region no longer holds the frame.** A `WaterZone` is baked over several frames, a step at a time inside the frame's spare milliseconds, but a step was 16 or 32 rows whatever the rows' length and then whole sweeps of the grid: at a `resolution` of 2048 each step took 70 ms and the last ones 190 to 400, so the editor and a game ran at 10 frames a second for as long as the bake took, with frames of two seconds in it. A step is a count of cells now (the flood, the two distance sweeps and the blurs step too): the longest measured is 20 ms, and the same bake takes 7,600 steps where it took 300. The baked field is the same. It still runs on the main thread.
- **A ground with no layer files is painted again.** In 0.104.0 a `Terrain` drawn by the stock shader (four layer colours or textures and a splatmap, no `.esterrainlayer` files) was one colour from edge to edge on every platform, and logged `shader Terrain: texture param "u_paintPages" finds no free texture unit`. The baked occlusion map made the paint that shader's eighth texture, one past the seven units a material has; the eighth takes an engine unit the shader does not read, and the shader was counted as reading four of them because it keeps three colours in variables named `s0` to `s3`, which is how WGSL names a sampler unit. A name a source declares for itself (`let s2`, a `var` in a function, a parameter) is not a unit now. A criterion, `terrain-paint`, draws a ground painted in two colours on both backends: nothing in the pull-request tier drew a terrain before.
- **A loop is seamless in a packaged game.** A `.wav` is re-encoded to MP3 when a game is exported, and a decoder gives an MP3 back with 1105 samples of silence before its sound and several hundred after it: measured in a browser, a one-second loop that was seamless as a `.wav` had 21 ms of silence every time round once packaged, and every sound, looping or not, began a fortieth of a second late. No tag in the file tells a player to take the silence off, and none does. The exporter now records, for each clip it re-encodes, how long it really is and what leads it, and the runtime plays the clip from past the lead to its real end and loops it between the two: exact to the sample where the clip is decoded (the web's decoded clips and the native host), and started past the lead where it is played from its file (a streamed clip on the web, a mini-game). A decoder that takes the silence off itself is seen by the length of what it returns and is not asked to twice. A project played from the editor reads its own `.wav` files and was never affected.
- **A large ground no longer throws every frame once its pages run out.** With every page slot in use (a `detail` that wants more than the 900 there are, or a long flight), `TerrainSystem` threw `Cannot read properties of undefined (reading 'slot')` each frame: the ground stopped following the eye, and the log filled. The ground chose what to draw, made new pages, then placed its nodes, and a page was marked as in use only when placed; a page made in between took the slot of the least recently drawn one, which was a page just chosen. What is drawn is now marked before anything is made, and when every slot is drawn that frame the new page waits.
diff --git a/sdk/src/water/zone.ts b/sdk/src/water/zone.ts
index 66d7d9678..4e316104c 100644
--- a/sdk/src/water/zone.ts
+++ b/sdk/src/water/zone.ts
@@ -227,15 +227,16 @@ export class WaterField {
const grounds = new Float64Array(grid.nx * grid.nz).fill(Number.NaN);
// How much of each texel's water is a river's: the surf takes it for land.
const rivers = new Float32Array(grid.nx * grid.nz);
+ const rows = rowsAStep(grid.nx, CELLS_A_STEP);
for (let j = 0; j < grid.nz; j++) {
- if (j % 32 === 0) yield 0.3 * (j / grid.nz);
+ if (j % rows === 0) yield 0.3 * (j / grid.nz);
for (let i = 0; i < grid.nx; i++) {
const x = grid.minX + (i / Math.max(grid.nx - 1, 1)) * grid.sizeX;
const z = grid.minZ + (j / Math.max(grid.nz - 1, 1)) * grid.sizeZ;
grounds[j * grid.nx + i] = ground?.(x, z) ?? Number.NaN;
}
}
- const sea = ocean ? openSea(grid, grounds, ocean.level) : null;
+ const sea = ocean ? yield* openSea(grid, grounds, ocean.level, 0.3) : null;
// How steep the ground is at each texel: a depth over it is how far the shore is across the ground, to first order, so the
// distance to the shore is one measure (along the ground) everywhere and runs smoothly through zero between texels.
const stepX = grid.sizeX / Math.max(grid.nx - 1, 1), stepZ = grid.sizeZ / Math.max(grid.nz - 1, 1);
@@ -246,7 +247,7 @@ export class WaterField {
return Number.isFinite(m) ? Math.max(m, MIN_SLOPE) : MIN_SLOPE;
};
for (let j = 0; j < grid.nz; j++) {
- if (j % 16 === 0) yield 0.3 + 0.5 * (j / grid.nz);
+ if (j % rows === 0) yield 0.3 + 0.5 * (j / grid.nz);
for (let i = 0; i < grid.nx; i++) {
const k = j * grid.nx + i;
const x = grid.minX + (i / Math.max(grid.nx - 1, 1)) * grid.sizeX;
@@ -310,9 +311,9 @@ export class WaterField {
}
yield* levelShore(field, texel);
yield* fillDry(field);
- surfDistance(field, rivers, texel, surfLength);
+ yield* surfDistance(field, rivers, texel, surfLength, 0.95);
// The water beside a steep shore takes its steepness: a cliff's foot is a texel or two out in the water.
- blur(field.steep, grid.nx, grid.nz, 2);
+ yield* blur(field.steep, grid.nx, grid.nz, 2, 0.99);
for (let k = 0; k < field.steep.length; k++) field.steep[k] = Math.min(field.steep[k]! * 2, 1);
return field;
}
@@ -457,7 +458,7 @@ function sayOf(body: ZoneLake | ZoneRiver, x: number, z: number, g: number | nul
* Which texels the open sea reaches: those whose ground is below `level` (or unknown) and joined, through such texels, to the region's
* edge, past which the sea goes on.
*/
-function openSea(grid: ZoneGrid, grounds: Float64Array, level: number): Uint8Array {
+function* openSea(grid: ZoneGrid, grounds: Float64Array, level: number, at: number): Generator<number, Uint8Array, void> {
const { nx, nz } = grid;
const sea = new Uint8Array(nx * nz);
const low = (k: number): boolean => { const g = grounds[k]!; return Number.isNaN(g) || g < level; };
@@ -466,6 +467,7 @@ function openSea(grid: ZoneGrid, grounds: Float64Array, level: number): Uint8Arr
for (let i = 0; i < nx; i++) { reach(i); reach((nz - 1) * nx + i); }
for (let j = 0; j < nz; j++) { reach(j * nx); reach(j * nx + nx - 1); }
for (let q = 0; q < queue.length; q++) {
+ if (q % SWEPT_A_STEP === SWEPT_A_STEP - 1) yield at;
const k = queue[q]!, i = k % nx;
if (i > 0) reach(k - 1);
if (i < nx - 1) reach(k + 1);
@@ -499,6 +501,15 @@ function boundsOf(body: ZoneLake | ZoneRiver): [number, number, number, number]
* texels on either side of an edge keep it, held to a texel, and every other texel is that plus how far it is from the nearest of them
* (each texel carries its nearest edge texel, two sweeps).
*/
+/**
+ * How much of a bake is one step. A job stepped in a frame is given the frame's spare milliseconds, and takes them a step at a time: a
+ * step is as small as the smallest thing worth waiting for, by cells and not by rows, so a region of 4096 texels a side steps as often
+ * as one of 256. Reading the ground and asking every body is about a microsecond a cell; a sweep is a twentieth of that.
+ */
+const CELLS_A_STEP = 2048;
+const SWEPT_A_STEP = 32768;
+const rowsAStep = (nx: number, cells: number): number => Math.max(1, Math.floor(cells / Math.max(nx, 1)));
+
function* levelShore(field: WaterField, texel: number): Generator<number, void, void> {
const { nx, nz, sizeX, sizeZ } = field.grid;
const n = nx * nz;
@@ -508,14 +519,18 @@ function* levelShore(field: WaterField, texel: number): Generator<number, void,
const seed = new Int32Array(n).fill(-1);
const offset = new Float32Array(n);
const dist = new Float32Array(n).fill(Infinity);
+ const rows = rowsAStep(nx, SWEPT_A_STEP);
yield 0.85;
- for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) {
- const k = j * nx + i, w = wet(k);
- const edge = (i > 0 && wet(k - 1) !== w) || (i < nx - 1 && wet(k + 1) !== w) || (j > 0 && wet(k - nx) !== w) || (j < nz - 1 && wet(k + nx) !== w);
- if (!edge) continue;
- seed[k] = k;
- offset[k] = Math.min(Math.abs(field.shore[k]!), texel);
- dist[k] = offset[k]!;
+ for (let j = 0; j < nz; j++) {
+ if (j % rows === 0) yield 0.85;
+ for (let i = 0; i < nx; i++) {
+ const k = j * nx + i, w = wet(k);
+ const edge = (i > 0 && wet(k - 1) !== w) || (i < nx - 1 && wet(k + 1) !== w) || (j > 0 && wet(k - nx) !== w) || (j < nz - 1 && wet(k + nx) !== w);
+ if (!edge) continue;
+ seed[k] = k;
+ offset[k] = Math.min(Math.abs(field.shore[k]!), texel);
+ dist[k] = offset[k]!;
+ }
}
const relax = (k: number, from: number): void => {
const s = seed[from]!;
@@ -525,17 +540,23 @@ function* levelShore(field: WaterField, texel: number): Generator<number, void,
if (d < dist[k]!) { dist[k] = d; seed[k] = s; }
};
yield 0.85;
- for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) {
- const k = j * nx + i;
- if (i > 0) relax(k, k - 1);
- if (j > 0) { relax(k, k - nx); if (i > 0) relax(k, k - nx - 1); if (i < nx - 1) relax(k, k - nx + 1); }
+ for (let j = 0; j < nz; j++) {
+ if (j % rows === 0) yield 0.85 + 0.035 * (j / nz);
+ for (let i = 0; i < nx; i++) {
+ const k = j * nx + i;
+ if (i > 0) relax(k, k - 1);
+ if (j > 0) { relax(k, k - nx); if (i > 0) relax(k, k - nx - 1); if (i < nx - 1) relax(k, k - nx + 1); }
+ }
}
- yield 0.92;
- for (let j = nz - 1; j >= 0; j--) for (let i = nx - 1; i >= 0; i--) {
- const k = j * nx + i;
- if (i < nx - 1) relax(k, k + 1);
- if (j < nz - 1) { relax(k, k + nx); if (i < nx - 1) relax(k, k + nx + 1); if (i > 0) relax(k, k + nx - 1); }
+ for (let j = nz - 1; j >= 0; j--) {
+ if (j % rows === 0) yield 0.92 - 0.035 * (j / nz);
+ for (let i = nx - 1; i >= 0; i--) {
+ const k = j * nx + i;
+ if (i < nx - 1) relax(k, k + 1);
+ if (j < nz - 1) { relax(k, k + nx); if (i < nx - 1) relax(k, k + nx + 1); if (i > 0) relax(k, k + nx - 1); }
+ }
}
+ yield 0.92;
for (let k = 0; k < n; k++) {
if (!Number.isFinite(dist[k]!)) continue;
field.shore[k] = wet(k) ? dist[k]! : -dist[k]!;
@@ -557,22 +578,29 @@ function* fillDry(field: WaterField): Generator<number, void, void> {
dist[k] = dist[from]! + step;
src[k] = src[from]!;
};
- yield 0.85;
- for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) {
- const k = j * nx + i;
- if (i > 0) relax(k, k - 1, 1);
- if (j > 0) relax(k, k - nx, 1);
- if (i > 0 && j > 0) relax(k, k - nx - 1, Math.SQRT2);
- if (i < nx - 1 && j > 0) relax(k, k - nx + 1, Math.SQRT2);
- }
+ const rows = rowsAStep(nx, SWEPT_A_STEP);
yield 0.92;
- for (let j = nz - 1; j >= 0; j--) for (let i = nx - 1; i >= 0; i--) {
- const k = j * nx + i;
- if (i < nx - 1) relax(k, k + 1, 1);
- if (j < nz - 1) relax(k, k + nx, 1);
- if (i < nx - 1 && j < nz - 1) relax(k, k + nx + 1, Math.SQRT2);
- if (i > 0 && j < nz - 1) relax(k, k + nx - 1, Math.SQRT2);
+ for (let j = 0; j < nz; j++) {
+ if (j % rows === 0) yield 0.92 + 0.015 * (j / nz);
+ for (let i = 0; i < nx; i++) {
+ const k = j * nx + i;
+ if (i > 0) relax(k, k - 1, 1);
+ if (j > 0) relax(k, k - nx, 1);
+ if (i > 0 && j > 0) relax(k, k - nx - 1, Math.SQRT2);
+ if (i < nx - 1 && j > 0) relax(k, k - nx + 1, Math.SQRT2);
+ }
+ }
+ for (let j = nz - 1; j >= 0; j--) {
+ if (j % rows === 0) yield 0.95 - 0.015 * (j / nz);
+ for (let i = nx - 1; i >= 0; i--) {
+ const k = j * nx + i;
+ if (i < nx - 1) relax(k, k + 1, 1);
+ if (j < nz - 1) relax(k, k + nx, 1);
+ if (i < nx - 1 && j < nz - 1) relax(k, k + nx + 1, Math.SQRT2);
+ if (i > 0 && j < nz - 1) relax(k, k + nx - 1, Math.SQRT2);
+ }
}
+ yield 0.95;
const fallback = field.ocean ? field.ocean.level : 0;
for (let k = 0; k < n; k++) {
if (Number.isFinite(field.height[k]!)) continue;
@@ -586,7 +614,7 @@ function* fillDry(field: WaterField): Generator<number, void, void> {
* no bay the crests bend into), smoothed over the surf's length. A crest is a line of it: smoothed, the crests come in from the open
* water as lines and turn to follow the shore only as they near it, instead of tracing every turn of it from far out.
*/
-function surfDistance(field: WaterField, rivers: Float32Array, texel: number, surfLength: number): void {
+function* surfDistance(field: WaterField, rivers: Float32Array, texel: number, surfLength: number, at: number): Generator<number, void, void> {
const { nx, nz } = field.grid;
const n = nx * nz;
// Distances across the texels (8-neighbour chamfer) from the open water's edge: into the water above zero, onto the land (a river's
@@ -594,23 +622,27 @@ function surfDistance(field: WaterField, rivers: Float32Array, texel: number, su
const wet = new Uint8Array(n);
for (let k = 0; k < n; k++) wet[k] = field.shore[k]! > 0 && rivers[k]! < 0.5 ? 1 : 0;
const cap = Math.max(surfLength * 8, texel * 16);
- const toWater = chamfer(nx, nz, wet, 0), toLand = chamfer(nx, nz, wet, 1);
+ yield at;
+ const toWater = yield* chamfer(nx, nz, wet, 0, at);
+ const toLand = yield* chamfer(nx, nz, wet, 1, at);
for (let k = 0; k < n; k++) field.surf[k] = Math.min(Math.max((toLand[k]! - toWater[k]!) * texel, -cap), cap);
- blur(field.surf, nx, nz, Math.round(surfLength / Math.max(texel, 1e-6)));
+ yield* blur(field.surf, nx, nz, Math.round(surfLength / Math.max(texel, 1e-6)), at);
// A river's current carries no surf, and spreads as it runs out into open water: the surf comes back over half its length.
for (let k = 0; k < n; k++) field.still[k] = 1 - rivers[k]!;
- blur(field.still, nx, nz, Math.round((surfLength * 0.5) / Math.max(texel, 1e-6)));
+ yield* blur(field.still, nx, nz, Math.round((surfLength * 0.5) / Math.max(texel, 1e-6)), at);
// Spread out past the mouth, never in: the river itself, banks and all, keeps none (the land beside it would lend it some).
for (let k = 0; k < n; k++) field.still[k] = Math.min(field.still[k]!, 1 - rivers[k]!);
}
/** `values` (a grid nx by nz) blurred in place over `radius` texels: three box passes each way, near enough a Gaussian, at a cost that
* does not grow with the radius. */
-function blur(values: Float32Array, nx: number, nz: number, radius: number): void {
+function* blur(values: Float32Array, nx: number, nz: number, radius: number, progress: number): Generator<number, void, void> {
if (radius < 1 || nx < 2 || nz < 2) return;
const line = new Float32Array(Math.max(nx, nz));
- const pass = (count: number, length: number, at: (c: number, i: number) => number): void => {
+ function* pass(count: number, length: number, at: (c: number, i: number) => number): Generator<number, void, void> {
+ const lines = rowsAStep(length, SWEPT_A_STEP);
for (let c = 0; c < count; c++) {
+ if (c % lines === 0) yield progress;
for (let i = 0; i < length; i++) line[i] = values[at(c, i)]!;
let sum = 0;
for (let i = -radius; i <= radius; i++) sum += line[Math.min(Math.max(i, 0), length - 1)]!;
@@ -619,32 +651,39 @@ function blur(values: Float32Array, nx: number, nz: number, radius: number): voi
sum += line[Math.min(i + radius + 1, length - 1)]! - line[Math.max(i - radius, 0)]!;
}
}
- };
+ }
for (let p = 0; p < 3; p++) {
- pass(nz, nx, (j, i) => j * nx + i);
- pass(nx, nz, (i, j) => j * nx + i);
+ yield* pass(nz, nx, (j, i) => j * nx + i);
+ yield* pass(nx, nz, (i, j) => j * nx + i);
}
}
/** How many texels each texel is from the nearest one whose `wet` is not `from` (half a texel at the edge, 0 on the other side). */
-function chamfer(nx: number, nz: number, wet: Uint8Array, from: number): Float32Array {
+function* chamfer(nx: number, nz: number, wet: Uint8Array, from: number, at: number): Generator<number, Float32Array, void> {
const n = nx * nz;
const d = new Float32Array(n);
for (let k = 0; k < n; k++) d[k] = wet[k] === from ? 1e9 : 0;
const relax = (k: number, o: number, w: number): void => { if (d[o]! + w < d[k]!) d[k] = d[o]! + w; };
- for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) {
- const k = j * nx + i;
- if (i > 0) relax(k, k - 1, 1);
- if (j > 0) relax(k, k - nx, 1);
- if (i > 0 && j > 0) relax(k, k - nx - 1, Math.SQRT2);
- if (i < nx - 1 && j > 0) relax(k, k - nx + 1, Math.SQRT2);
+ const rows = rowsAStep(nx, SWEPT_A_STEP);
+ for (let j = 0; j < nz; j++) {
+ if (j % rows === 0) yield at;
+ for (let i = 0; i < nx; i++) {
+ const k = j * nx + i;
+ if (i > 0) relax(k, k - 1, 1);
+ if (j > 0) relax(k, k - nx, 1);
+ if (i > 0 && j > 0) relax(k, k - nx - 1, Math.SQRT2);
+ if (i < nx - 1 && j > 0) relax(k, k - nx + 1, Math.SQRT2);
+ }
}
- for (let j = nz - 1; j >= 0; j--) for (let i = nx - 1; i >= 0; i--) {
- const k = j * nx + i;
- if (i < nx - 1) relax(k, k + 1, 1);
- if (j < nz - 1) relax(k, k + nx, 1);
- if (i < nx - 1 && j < nz - 1) relax(k, k + nx + 1, Math.SQRT2);
- if (i > 0 && j < nz - 1) relax(k, k + nx - 1, Math.SQRT2);
+ for (let j = nz - 1; j >= 0; j--) {
+ if (j % rows === 0) yield at;
+ for (let i = nx - 1; i >= 0; i--) {
+ const k = j * nx + i;
+ if (i < nx - 1) relax(k, k + 1, 1);
+ if (j < nz - 1) relax(k, k + nx, 1);
+ if (i < nx - 1 && j < nz - 1) relax(k, k + nx + 1, Math.SQRT2);
+ if (i > 0 && j < nz - 1) relax(k, k + nx - 1, Math.SQRT2);
+ }
}
// The edge lies between two texels: half a texel from each.
for (let k = 0; k < n; k++) if (d[k]! > 0 && d[k]! < 1e9) d[k] = d[k]! - 0.5;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 32004c717..4bf3e7c4b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4712,6 +4712,8 @@ export const NOTED = {
{"note": "A large ground no longer throws every frame once its pages run out."},
"fix(terrain): a ground with no layer files is painted again - its paint is the eighth texture of its shader and found no unit, because three colours the shader keeps in s0..s3 were read as four engine units in use; a name a WGSL source declares for itself is no longer a unit, and a criterion draws a painted ground":
{"note": "A ground with no layer files is painted again."},
+ "fix(water): baking a water region no longer holds the frame - its steps were a fixed count of rows and whole sweeps of the grid, 70 to 400 ms each at 2048 texels a side; a step is a count of cells now, 20 ms at the most":
+ {"note": "Baking a water region no longer holds the frame."},
"chore(gates): what the checks asked of this week's work - a disposition for esengine/image in a native script and its namespace there, Waterfall's width and its place in the corpus, the recipe worker's bundle as not a contract, shadeLight2D's inout against its return, and a test that named App as a type":
{"internal": "tables the gates read, brought level; a natively packaged script can now import esengine/image, which no native game has run yet"},
"feat(image): a picture's source goes where a texture goes, and its maps are recipes too - a texture field takes an .esimage, the image tools write a picture's normal and surface maps as recipes beside it and answer their refs, text is set by the drawer a game sets it with, and the viewport draws a recipe as a game does":