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): the sea past the rings lies at the sea's level - the apron that carries an ocean to the horizon took its inner edge's height from the water under it, so with a river on high ground under that edge a strip of sea eighty kilometres long rose to the river's height; the apron's vertices are marked and the shader lays them at the ocean's level

yhh5e71f8998923
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 50dfd4c03..8c23e1622 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@ published separately; it ships inside the editor.
### Fixed
+- **The sea past the rings lies at the sea's level.** The apron that carries an ocean from its mesh to the horizon took its inner edge's height from whatever water lay under it. With a river on high ground under that edge (a river along a cliff top, seen from across the island), a strip of sea eighty kilometres long rose to the river's height and stood on the horizon as a block with straight sides, moving as the camera did. The apron is now laid at the ocean's level.
- **A colour map and an occlusion map lie the way the heightmap does.** `Terrain.colorMap`, `Terrain.occlusionMap` and `Foliage.colorMap` were sampled with their rows turned over: a picture painted to match the heightmap, the splatmap or a density map came out mirrored north to south under them, flowers coloured for the far side of an island and gullies shaded where the ridges were. An image over an area now has its first row along the least z whoever reads it. A map that was drawn turned over to make up for this needs turning back.
- **The ground's lengths follow the project's units.** A `Terrain`'s size, height, cell, draw distance and collision distance were plain numbers, and so were its shaders' layer tiles, relief and wet line and a layer file's default tile: a new terrain in a default project was ten metres of ground on 2 cm cells with collision built 64 cm round a body. They are lengths now (`worldUnitFields`, `unit(length)`), written at 100 units a metre: a kilometre of ground on 2 m cells, collision 64 m round a body, a layer 24 m across, in a centimetre project and a metre one. What a scene, a material or a layer file states is untouched; a scene that left `collisionDistance` or `drawDistance` to the default gets the new one, so state the old (64, 4000) to keep it. Ground with `layers` is also no longer sent the four layer colours of the plain shader, which warned four times a load.
- **Water mirrors only what is there.** Over open sea with no ground behind it the water's reflection went black: the ray through the picture took a sky pixel for something it had met, and the sky is painted after that picture is taken. From above, the sea darkened along the edge of the terrain under it, a line across the water. And a ripple steep enough to send the ray down met the sea bed in the picture, undimmed by its water: white flecks all over a shallow sea. The copy of the scene's distances now marks where the picture holds nothing, a ray sent down goes on up as off the next wave. (A first version also widened the highlight by how far the normal turns across a pixel; that made a distant sea rough and took most of its glancing reflection of the sky away, and is not in this release: the ripple map's mips keep far water from flashing.) The ray's step and the thickness of what it meets are shares of the distance to the water, where they were a number of units (a centimetre in one project, a metre in another).
diff --git a/sdk/src/water/waterMesh.ts b/sdk/src/water/waterMesh.ts
index c2b12e2e7..828159e24 100644
--- a/sdk/src/water/waterMesh.ts
+++ b/sdk/src/water/waterMesh.ts
@@ -63,8 +63,9 @@ export function buildOceanMesh(cells: number, rings: number, cellSize: number, s
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); }
+ // Green 0 marks the apron: the Water shader lays it at the open sea's level whatever water its inner edge stands over.
+ for (const [x, z] of edge) { positions.push(x, 0, z); colors.push(outer.ring / 7, 0, 0, 1); }
+ for (const [x, z] of edge) { positions.push(x * k, 0, z * k); colors.push(outer.ring / 7, 0, 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);
diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.ts
index d03045fd2..8dfbb18b8 100644
--- a/sdk/src/water/waterShader.ts
+++ b/sdk/src/water/waterShader.ts
@@ -261,7 +261,10 @@ void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
vec4 field = u_zoneOn > 0.5 ? waterZoneField(p) : vec4(1e6, 0.0, 0.0, 1.0);
float still = u_zoneOn > 0.5 ? waterZoneStill(p) : 1.0;
float surf = waterSurf(u_zoneOn > 0.5 ? waterZoneSurf(p) : 1e6, field.x, u_surfHeight * u_waveHeight * zone.z * zone.w * still, u_time.x).x;
- local.y += zone.x + (waterWaveHeight(p, u_time.x, cell) * zone.z * field.w + surf) * u_geoWaves;
+ // The apron past the rings (green 0, see waterMesh.ts) is the open sea out to the horizon: an edge vertex that stands over a river on a
+ // cliff, or on the land beside one, would lift a strip of sea eighty kilometres long to the river's height.
+ float rest = es_vertexColor.g < 0.5 && u_oceanOn > 0.5 ? u_oceanLevel : zone.x;
+ local.y += rest + (waterWaveHeight(p, u_time.x, cell) * zone.z * field.w + surf) * u_geoWaves;
}
#pragma end
@@ -371,7 +374,9 @@ fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, ve
var surfAt = 1e6;
if (mc.u_zoneOn > 0.5) { surfAt = waterZoneSurf(p); }
let surf = waterSurf(surfAt, field.x, mc.u_surfHeight * mc.u_waveHeight * zone.z * zone.w * still, tc.u_time.x).x;
- (*local).y = (*local).y + zone.x + (waterWaveHeight(p, tc.u_time.x, cell) * zone.z * field.w + surf) * mc.u_geoWaves;
+ var rest = zone.x;
+ if (es_vertexColor.g < 0.5 && mc.u_oceanOn > 0.5) { rest = mc.u_oceanLevel; }
+ (*local).y = (*local).y + rest + (waterWaveHeight(p, tc.u_time.x, cell) * zone.z * field.w + surf) * mc.u_geoWaves;
}
#pragma end
diff --git a/sdk/tests/water.test.ts b/sdk/tests/water.test.ts
index 7a366ea12..cb1f8df5f 100644
--- a/sdk/tests/water.test.ts
+++ b/sdk/tests/water.test.ts
@@ -126,6 +126,18 @@ describe('an ocean\'s apron', () => {
expect(area).toBeCloseTo((2 * far) ** 2 - (2 * reach) ** 2, 1);
});
+ it('is marked (green 0) and the rings are not, and the shader lays what is marked at the open sea’s level', () => {
+ const plain = buildOceanMesh(16, 3, 1), mesh = buildOceanMesh(16, 3, 1, 1000);
+ const channel = mesh.channels.find((c) => c.semantic === MeshChannel.Color)!;
+ const green = (i: number): number => mesh.vertices[i * mesh.vertexStride + channel.offset + 1]!;
+ for (let i = 0; i < plain.vertexCount; i++) expect(green(i)).toBe(255);
+ for (let i = plain.vertexCount; i < mesh.vertexCount; i++) expect(green(i)).toBe(0);
+ // A river over a cliff under the rings' edge otherwise lifts its strip of the apron, out to the horizon, to the river's height.
+ const source = WATER_SHADER_TEMPLATE.source;
+ expect(source).toContain('float rest = es_vertexColor.g < 0.5 && u_oceanOn > 0.5 ? u_oceanLevel : zone.x;');
+ expect(source).toContain('if (es_vertexColor.g < 0.5 && mc.u_oceanOn > 0.5) { rest = mc.u_oceanLevel; }');
+ });
+
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);
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 46478cf52..5bd9f597b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4570,4 +4570,6 @@ export const NOTED = {
{"note": "A sky of your own shades the ground under its clouds."},
"fix(terrain): a colour map and an occlusion map lie the way the heightmap does - Terrain.colorMap, Terrain.occlusionMap and Foliage.colorMap were sampled with their rows turned over, so a picture painted to match a heightmap, a splatmap or a density map came out mirrored north to south under them; an image over an area now has its first row along the least z whoever reads it":
{"note": "A colour map and an occlusion map lie the way the heightmap does."},
+ "fix(water): the sea past the rings lies at the sea's level - the apron that carries an ocean to the horizon took its inner edge's height from the water under it, so with a river on high ground under that edge a strip of sea eighty kilometres long rose to the river's height; the apron's vertices are marked and the shader lays them at the ocean's level":
+ {"note": "The sea past the rings lies at the sea's level."},
};