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(terrain): an edited page keeps drawing as it was until it is made again, so a brush stroke leaves no hole
yhhe8198bbb9ae5
diff --git a/sdk/src/terrain/pages.ts b/sdk/src/terrain/pages.tsindex 7e12b7366..f2dbb88c9 100644--- a/sdk/src/terrain/pages.ts+++ b/sdk/src/terrain/pages.ts@@ -28,6 +28,8 @@ export interface Page {max: number;/** When it was last drawn, for choosing which to give up. */used: number;+ /** The ground changed under it: it is still drawn, as it was, until a new image takes its slot. */+ stale?: boolean;}/** The pixels of one page. */@@ -140,6 +142,9 @@ export class PageTable {/** A slot for a new page under `key`: a free one, or the least recently drawn one not drawn this frame. Null when every slot is in use now. */claim(key: string, min: number, max: number): Page | null {+ // A page the ground changed under is made again in its own slot, so what is drawn there changes in place and never goes missing.+ const had = this.pages_.get(key);+ if (had) { had.min = min; had.max = max; had.used = this.clock_; had.stale = false; return had; }let slot = this.free_.pop();if (slot === undefined) {let oldest: string | null = null, at = Infinity;@@ -162,6 +167,19 @@ export class PageTable {}}+ /**+ * Marks the pages `test` names as out of date without taking them away: they go on being drawn as they were until each is made again+ * (`claim` on its key), so an edit never leaves a hole in the ground while its pages are rebuilt over the next frames.+ */+ markStale(test: (key: string) => boolean): void {+ for (const [key, page] of this.pages_) if (test(key)) page.stale = true;+ }++ /** Whether the page under `key` is out of date. */+ isStale(key: string): boolean {+ return this.pages_.get(key)?.stale === true;+ }+/** Forgets every page (the device lost the atlas, or the ground changed). */clear(): void {this.pages_.clear();diff --git a/sdk/src/terrain/terrainPlugin.ts b/sdk/src/terrain/terrainPlugin.tsindex a48b050aa..330d1b713 100644--- a/sdk/src/terrain/terrainPlugin.ts+++ b/sdk/src/terrain/terrainPlugin.ts@@ -498,7 +498,7 @@ export class Terrains {const source = built.source;if (!(source instanceof ModifiedSource)) continue;const o = source.origin, leaf = PATCH_CELLS * Math.max(built.data.cellSize, 1e-3);- built.table.forget((key) => {+ built.table.markStale((key) => {const [level, ix, iz] = key.split(':').map(Number) as [number, number, number];const size = leaf * 2 ** level, x0 = ix * size + o.x, z0 = iz * size + o.z;return x0 <= b[2] && x0 + size >= b[0] && z0 <= b[3] && z0 + size >= b[1];@@ -527,7 +527,7 @@ export class Terrains {for (const built of this.built_.values()) {built.edits++;built.pageWork = undefined;- built.table.clear();+ built.table.markStale(() => true);}}@@ -946,13 +946,15 @@ export class TerrainPlugin implements Plugin {for (const node of chosen) {let n: TerrainNode | null = node;const own = nodeKey(node.level, node.ix, node.iz);- if (!table.get(own)) built.wanted.set(own, node);+ if (!table.get(own) || table.isStale(own)) built.wanted.set(own, node);while (n && !table.get(nodeKey(n.level, n.ix, n.iz))) {n = n.level + 1 < tree.levels ? parentOf(n, tree.leafSize) : null;if (n && !table.get(nodeKey(n.level, n.ix, n.iz))) built.wanted.set(nodeKey(n.level, n.ix, n.iz), n);}if (!n) continue;const key = nodeKey(n.level, n.ix, n.iz);+ // An ancestor drawn in place of a missing page and out of date is made again too.+ if (n !== node && table.isStale(key)) built.wanted.set(key, n);if (n !== node) covered.add(key);drawn.set(key, n);}diff --git a/sdk/tests/terrain-quadtree.test.ts b/sdk/tests/terrain-quadtree.test.tsindex b0376514e..4a60e9e04 100644--- a/sdk/tests/terrain-quadtree.test.ts+++ b/sdk/tests/terrain-quadtree.test.ts@@ -89,4 +89,21 @@ describe('the pages', () => {expect(table.get('p0')).toBeUndefined();expect(page.slot).toBeGreaterThanOrEqual(0);});++ it('keeps a page the ground changed under, drawn as it was, until it is made again in its own slot', () => {+ const table = new PageTable(PAGE_SIDE * 2);+ const slot = table.claim('a', 0, 1)!.slot;+ table.claim('b', 0, 1);+ table.markStale((key) => key === 'a');+ // Still there to draw: an edit leaves no hole while its pages are rebuilt.+ expect(table.get('a')).toBeDefined();+ expect(table.isStale('a')).toBe(true);+ expect(table.isStale('b')).toBe(false);+ const again = table.claim('a', 0.25, 2)!;+ expect(again.slot).toBe(slot);+ expect(again.min).toBe(0.25);+ expect(again.max).toBe(2);+ expect(table.isStale('a')).toBe(false);+ expect(table.size).toBe(2);+ });});