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): a river fades into the sea or lake it runs into, so the surf is not cut along its sides

yhh9540e7192574
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d6799c1be..9612222b6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ published separately; it ships inside the editor.
- **Rays against the terrain, anywhere.** `Terrains.raycast(origin, direction, maxDistance)` meets the ground's triangles exactly (the ones collision and `heightAt` use) wherever the ray goes, with no physics world and no collision tile there, for a line of sight, a pick or a landing spot.
- **Far versions for generated decoration.** A recipe member can name its far version (`far`: crossed cards, or a tent for a conifer, showing a rectangle of its texture), drawn past its layer's `lodDistance`, and decoration that leans with the ground (`attach: 'align'`) now leans in its merged mesh too.
- **Terrain modifiers, and zones that level their ground.** `Terrains.setModifier` lays a change over the ground (`flattenModifier` levels a circle or a turned box, blending back over its edge) that what is drawn, the queries, rays and collision all follow, remaking only the pages and collision tiles it reaches; `heightBeneathAt` reads the ground as it was made. A `WorldZone` with `flatten` levels its plot to the ground's height at its centre.
+- **A river gives way where it runs into the sea or a lake.** Over half its width past their shore it fades into them, its current with it, where it used to stop within a texel and a half and leave its sides running on into the open water: the surf, which no current carries, broke off along those lines. The surf now runs on along the coast and round into the mouth.
- **Water meets the shore along a smooth line.** Close up, a river's banks and the surf on a beach ran in steps a texel of the water zone wide: the baked shore distance is now stored finely near the shore (a signed square root of it), and past the shore the surf's film and the shallows' foam are told dry from wet by the picture's depth at each pixel rather than by the baked depth, which only knew whole texels.
- **The distance to the shore is a distance.** Near the water's edge the baked shore distance was the water's depth (or, on land, the height above the water), so `Waters.shoreAt`, a generated world's `shoreDistance` and the surf's reach mixed heights with distances; the depth is now turned into the distance across the ground by the ground's slope there. Waves still calm as the water shallows.
- **Heightmaps say how they hold their heights.** `Terrain.heightmapFormat` is Grey (the grey or red channel at the file's depth, so a 16-bit grey PNG, what terrain tools export, is read at full precision) or Red + green (sixteen bits across two 8-bit channels); the format is no longer guessed from the pixels. A heightmap that cannot be read as data says it must ship as the PNG it is. **Upgrading:** a terrain whose heightmap packs sixteen bits into red and green sets `heightmapFormat` to Red + green.
diff --git a/docs/WORLD_GENERATION_PLAN.md b/docs/WORLD_GENERATION_PLAN.md
index 5cc0bd3bb..c6bfd20c5 100644
--- a/docs/WORLD_GENERATION_PLAN.md
+++ b/docs/WORLD_GENERATION_PLAN.md
@@ -93,7 +93,7 @@
- 雨天/阴天时 CPU 渲染提交阻塞约 45 ms(GPU 计时或同步读回导致),需定位。
- 编辑器三维视图套用后处理(天空/雾)后画面上下翻转;改动已撤回,需修呈现路径。
- 河口水流纹理拉伸。
-- ~~岸边锯齿~~:已修复。三处叠加:岸线距离在岸边用垂直水深(改为按坡度换算的水平距离);岸线距离 8 位线性编码在 ±3500 量程上每档约 27 单位(改为带符号平方根编码,岸边每档约 0.2 单位);冲浪薄膜与泡沫用按纹素截断的烘焙水深判断干湿(有深度缓冲时改用逐像素水柱,水深通道也改为平方根编码)。仍可见:湖口处河与湖交界有一条短横边(河道浅水与湖深水的颜色过渡),待查。
+- ~~岸边锯齿~~:已修复。三处叠加:岸线距离在岸边用垂直水深(改为按坡度换算的水平距离);岸线距离 8 位线性编码在 ±3500 量程上每档约 27 单位(改为带符号平方根编码,岸边每档约 0.2 单位);冲浪薄膜与泡沫用按纹素截断的烘焙水深判断干湿(有深度缓冲时改用逐像素水柱,水深通道也改为平方根编码)。河流入海/入湖后原在 1.5 纹素内硬切、两条侧边伸进开阔水面,冲浪遇水流即停而被截成直线;现于对方岸线内半个河宽渐隐让位、水流随之减弱,浪沿岸连续并绕入河口。湖口剩下的横边是示例地形的水下陡坎(湖盆边缘深于河床),属地形不属水体。
- 无限地形:地貌参数(导数衰减侵蚀下呈台地状)与远处一条直线接缝需排查。
- 动态分辨率在 GPU 计时下不降分辨率。
- 编辑器启动时的 `NaN` CSS 高度报错(无调用栈)。
diff --git a/sdk/src/water/zone.ts b/sdk/src/water/zone.ts
index d56d1f37d..2c49791f5 100644
--- a/sdk/src/water/zone.ts
+++ b/sdk/src/water/zone.ts
@@ -233,7 +233,9 @@ export class WaterField {
const z = grid.minZ + (j / Math.max(grid.nz - 1, 1)) * grid.sizeZ;
const g = Number.isNaN(grounds[k]!) ? null : grounds[k]!;
const slope = g === null ? MIN_SLOPE : slopeAt(i, j);
- let w = 0, h = 0, a = 0, c = 0, sw = 0, ex = 0, fx = 0, fz = 0, best = -Infinity, owner = -1, ownerW = 0, held = 0, lakeHeld = 0;
+ let w = 0, h = 0, a = 0, c = 0, sw = 0, ex = 0, fx = 0, fz = 0, best = -Infinity, owner = -1, ownerW = 0, held = 0, lakeShore = -Infinity;
+ // Ground below the sea's level that the open sea does not reach is a dry basin: as far above the sea as a texel.
+ const seaDeep = !ocean ? -DEEP : g === null ? DEEP : g < ocean.level && !sea![k] ? -texel : (ocean.level - g) / slope;
for (let b = 0; b < bounded.length; b++) {
const box = boxes[b]!;
if (x < box[0] - blend || x > box[2] + blend || z < box[1] - blend || z > box[3] + blend) continue;
@@ -241,8 +243,12 @@ export class WaterField {
if (!say) continue;
best = Math.max(best, say.shore);
const inside = smooth(-blend, blend, say.shore);
- const weight = bounded[b]!.kind === 'river' ? inside * (1 - lakeHeld) : inside;
- if (bounded[b]!.kind === 'lake') lakeHeld = Math.max(lakeHeld, inside);
+ // A river gives way to the lake or the sea it runs into over half its width past their shore, its current fading
+ // with it: cut off at their shore, its sides would run on into the open water as lines the surf stops at.
+ const fade = Math.max(say.half, blend);
+ const weight = bounded[b]!.kind === 'river'
+ ? inside * (1 - smooth(0, fade, lakeShore)) * (1 - smooth(0, fade, seaDeep)) : inside;
+ if (bounded[b]!.kind === 'lake') lakeShore = Math.max(lakeShore, say.shore);
if (weight <= 0) continue;
held = Math.max(held, weight);
w += weight; h += say.height * weight; a += say.amp * weight; c += say.calm * weight; sw += bounded[b]!.waveHeight * weight; fx += say.fx * weight; fz += say.fz * weight;
@@ -250,8 +256,7 @@ export class WaterField {
if (weight > ownerW) { ownerW = weight; owner = bounded[b]!.key; }
}
if (ocean) {
- // Ground below the sea's level that the open sea does not reach is a dry basin: as far above the sea as a texel.
- const deep = g === null ? DEEP : g < ocean.level && !sea![k] ? -texel : (ocean.level - g) / slope;
+ const deep = seaDeep;
best = Math.max(best, deep);
const weight = smooth(-blend, blend, deep) * (1 - held);
if (weight > 0) {
@@ -367,7 +372,7 @@ export class WaterField {
/** What one body says about a point: its height there, how far inside it the point is, its waves and its current. Null where it says nothing. */
function sayOf(body: ZoneLake | ZoneRiver, x: number, z: number, g: number | null, slope: number):
- { height: number; shore: number; amp: number; calm: number; fx: number; fz: number } | null {
+ { height: number; shore: number; amp: number; calm: number; fx: number; fz: number; half: number } | null {
if (body.kind === 'lake') {
const [lx, lz] = body.toLocal(x, z);
// The depth as a distance across the ground to where the water meets it (the ground's slope there sets how far that is).
@@ -376,7 +381,7 @@ function sayOf(body: ZoneLake | ZoneRiver, x: number, z: number, g: number | nul
const shore = Math.min(polygonSdf(body.outline, lx, lz), depth / slope);
// The waves calm toward the shore and as the water shallows, which on a flat shelf far from any shore is the depth alone.
const calm = shoreDamp(Math.min(shore, depth), body.shoreWidth);
- return { height: body.level, shore, amp: body.waveHeight * calm, calm, fx: 0, fz: 0 };
+ return { height: body.level, shore, amp: body.waveHeight * calm, calm, fx: 0, fz: 0, half: 0 };
}
const st = body.stations;
if (st.length < 2) return null;
@@ -404,7 +409,7 @@ function sayOf(body: ZoneLake | ZoneRiver, x: number, z: number, g: number | nul
const length = st.reduce((n, p, i) => (i ? n + Math.hypot(p.x - st[i - 1]!.x, p.z - st[i - 1]!.z) : 0), 0);
const run = smooth(0, 2 * half, along * length) * smooth(0, 2 * half, (1 - along) * length);
const calm = shoreDamp(Math.min(shore, depth), body.shoreWidth);
- return { height: y, shore, amp: body.waveHeight * calm, calm, fx: dx * speed * run, fz: dz * speed * run };
+ return { height: y, shore, amp: body.waveHeight * calm, calm, fx: dx * speed * run, fz: dz * speed * run, half };
}
/**
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 8d4d7e2f8..6526dfaca 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2812,4 +2812,6 @@ export const NOTED = {
{"note":"The distance to the shore is a distance."},
"fix(water): water meets the shore along a smooth line, not the zone's texel steps":
{"note":"Water meets the shore along a smooth line."},
+ "fix(water): a river fades into the sea or lake it runs into, so the surf is not cut along its sides":
+ {"note":"A river gives way where it runs into the sea or a lake."},
};