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(water): an ocean reaches the horizon - a flat apron carries the surface on from its rings, so the sea no longer ends in a line where its mesh does

yhh122af2f4b82e
diff --git a/sdk/src/water/waterMesh.ts b/sdk/src/water/waterMesh.ts
index 2f585b605..c2b12e2e7 100644
--- a/sdk/src/water/waterMesh.ts
+++ b/sdk/src/water/waterMesh.ts
@@ -24,9 +24,10 @@ interface Grid {
/**
* An ocean of `rings` rings of `cells` cells a side (a multiple of four), the innermost with `cellSize` cells. It reaches
- * `cells * cellSize * 2^(rings - 1) / 2` out from its centre.
+ * `cells * cellSize * 2^(rings - 1) / 2` out from its centre, and with `skirt` a flat apron runs on from there to that
+ * half-extent: the sea out to the horizon, at the cost of one cell's quads along each side.
*/
-export function buildOceanMesh(cells: number, rings: number, cellSize: number): MeshData {
+export function buildOceanMesh(cells: number, rings: number, cellSize: number, skirt = 0): MeshData {
const n = Math.max(4, Math.round(cells / 4) * 4);
const levels = Math.min(8, Math.max(1, Math.floor(rings)));
const grids: Grid[] = [];
@@ -53,6 +54,24 @@ export function buildOceanMesh(cells: number, rings: number, cellSize: number):
}
}
}
+ const outer = grids[grids.length - 1]!;
+ if (skirt > outer.half) {
+ // Each cell of the outermost ring's edge is carried out to the apron's edge along its own side; the corners take a quad each.
+ const base = positions.length / 3, k = skirt / outer.half;
+ const edge: Array<[number, number]> = [];
+ for (let i = 0; i <= n; i++) edge.push([-outer.half + i * outer.cell, -outer.half]);
+ for (let i = 1; i <= n; i++) edge.push([outer.half, -outer.half + i * outer.cell]);
+ for (let i = 1; i <= n; i++) edge.push([outer.half - i * outer.cell, outer.half]);
+ for (let i = 1; i < n; i++) edge.push([-outer.half, outer.half - i * outer.cell]);
+ for (const [x, z] of edge) { positions.push(x, 0, z); colors.push(outer.ring / 7, 1, 0, 1); }
+ for (const [x, z] of edge) { positions.push(x * k, 0, z * k); colors.push(outer.ring / 7, 1, 0, 1); }
+ const m = edge.length;
+ for (let i = 0; i < m; i++) {
+ const a = base + i, b = base + ((i + 1) % m), c = base + m + i, d = base + m + ((i + 1) % m);
+ // The edge runs clockwise seen from above (+x, then +z): the apron is on its left, and faces up in this order.
+ indices.push(a, b, c, b, d, c);
+ }
+ }
return pack(positions, colors, indices);
}
diff --git a/sdk/tests/water.test.ts b/sdk/tests/water.test.ts
index 3f957a032..9b9a911c9 100644
--- a/sdk/tests/water.test.ts
+++ b/sdk/tests/water.test.ts
@@ -110,6 +110,27 @@ describe('the waves', () => {
});
});
+describe('an ocean\'s apron', () => {
+ it('runs from the rings to the horizon, facing up, with no gap and no overlap', () => {
+ const cells = 16, rings = 3, cell = 1, far = 1000;
+ const plain = buildOceanMesh(cells, rings, cell), mesh = buildOceanMesh(cells, rings, cell, far);
+ const p = positions(mesh), reach = oceanReach(cells, rings, cell);
+ expect(mesh.aabbMax[0]).toBeCloseTo(far, 3);
+ expect(mesh.aabbMin[2]).toBeCloseTo(-far, 3);
+ let area = 0;
+ for (let t = plain.indices.length; t < mesh.indices.length; t += 3) {
+ const f = facing(p, mesh.indices[t]!, mesh.indices[t + 1]!, mesh.indices[t + 2]!);
+ expect(f).toBeGreaterThan(0);
+ area += f / 2;
+ }
+ expect(area).toBeCloseTo((2 * far) ** 2 - (2 * reach) ** 2, 1);
+ });
+
+ it('is not there when it would end inside the rings', () => {
+ expect(buildOceanMesh(16, 3, 1, 10).indices.length).toBe(buildOceanMesh(16, 3, 1).indices.length);
+ });
+});
+
describe('the surface\'s mesh', () => {
const cells = 16, rings = 4, cell = 1;
const mesh = buildOceanMesh(cells, rings, cell);