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(water): the shore distance near the edge is a distance across the ground, not the water's depth
yhh102aa163c6b3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9e6b9219b..108ee6b3e 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.+- **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.- **Rivers cut their beds.** A river's `WaterBody.bedDepth` cuts its bed into the terrain, a trough under its surface with banks that blend back into the ground, laid as terrain modifiers along it so what is drawn, the queries and collision follow, and editing the spline remakes only the ground along the river.- **Whether a place can be walked to.** `Terrains.findPath(from, to, { maxSlope, step, blocked })` searches the ground for a way a walker held to a slope can take, reading the ground where it goes (modifiers too, nothing baked), and returns it pulled straight with its length, or says whether there is no way, the search ran out, or an end stands on no ground: what a game checks a spawn or a goal against.diff --git a/docs/WORLD_GENERATION_PLAN.md b/docs/WORLD_GENERATION_PLAN.mdindex 14e5f6d43..4cba2f099 100644--- a/docs/WORLD_GENERATION_PLAN.md+++ b/docs/WORLD_GENERATION_PLAN.md@@ -93,6 +93,7 @@- 雨天/阴天时 CPU 渲染提交阻塞约 45 ms(GPU 计时或同步读回导致),需定位。- 编辑器三维视图套用后处理(天空/雾)后画面上下翻转;改动已撤回,需修呈现路径。- 河口水流纹理拉伸。+- 岸边锯齿(河岸、入海口近看时水边与冲浪白边呈台阶状):已排除区域纹理过滤与岸线距离量化(后者顺带修正:岸线距离原在岸边用垂直水深,现按地面坡度换算为水平距离,`Waters.shoreAt`/`shoreDistance` 单位因此正确);齿距接近地形网格格距,疑为水面与地形三角网格的交线及冲浪薄膜跟随网格三角形,待查。- 无限地形:地貌参数(导数衰减侵蚀下呈台地状)与远处一条直线接缝需排查。- 动态分辨率在 GPU 计时下不降分辨率。- 编辑器启动时的 `NaN` CSS 高度报错(无调用栈)。diff --git a/sdk/src/water/zone.ts b/sdk/src/water/zone.tsindex 0ecc6575b..a92228530 100644--- a/sdk/src/water/zone.ts+++ b/sdk/src/water/zone.ts@@ -147,6 +147,8 @@ export function currentShare(width: number, widest: number): number {/** The depth over ground nothing is known of. */const DEEP = 1e6;+/** The gentlest slope a depth is turned into a distance by: flatter ground than this is deep water or dry land a long way from a shore. */+const MIN_SLOPE = 0.01;/** The baked water of a zone. */export class WaterField {@@ -214,6 +216,15 @@ export class WaterField {}}const sea = ocean ? openSea(grid, grounds, ocean.level) : 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);+ const slopeAt = (i: number, j: number): number => {+ const at = (ii: number, jj: number): number => grounds[Math.min(Math.max(jj, 0), grid.nz - 1) * grid.nx + Math.min(Math.max(ii, 0), grid.nx - 1)]!;+ const gx = (at(i + 1, j) - at(i - 1, j)) / (2 * stepX), gz = (at(i, j + 1) - at(i, j - 1)) / (2 * stepZ);+ const m = Math.hypot(gx, gz);+ 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);for (let i = 0; i < grid.nx; i++) {@@ -221,11 +232,12 @@ export class WaterField {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;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;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;- const say = sayOf(bounded[b]!, x, z, g);+ const say = sayOf(bounded[b]!, x, z, g, slope);if (!say) continue;best = Math.max(best, say.shore);const inside = smooth(-blend, blend, say.shore);@@ -239,11 +251,11 @@ export class WaterField {}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;+ const deep = g === null ? DEEP : g < ocean.level && !sea![k] ? -texel : (ocean.level - g) / slope;best = Math.max(best, deep);const weight = smooth(-blend, blend, deep) * (1 - held);if (weight > 0) {- const calm = shoreDamp(deep, ocean.shoreWidth);+ const calm = shoreDamp(g === null ? deep : Math.min(deep, ocean.level - g), ocean.shoreWidth);w += weight; h += ocean.level * weight; a += ocean.waveHeight * calm * weight; c += calm * weight; sw += ocean.waveHeight * weight;ex += (ocean.exposure ?? 1) * weight;if (weight > ownerW) { ownerW = weight; owner = ocean.key; }@@ -352,14 +364,16 @@ 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):+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 {if (body.kind === 'lake') {const [lx, lz] = body.toLocal(x, z);- const deep = g === null ? DEEP : body.level - g;+ // The depth as a distance across the ground to where the water meets it (the ground's slope there sets how far that is).+ const depth = g === null ? DEEP : body.level - g;// Water only where its outline is and its surface is over the ground: land inside the outline, above the water, is land.- const shore = Math.min(polygonSdf(body.outline, lx, lz), deep);- const calm = shoreDamp(shore, body.shoreWidth);+ 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 };}const st = body.stations;@@ -380,14 +394,14 @@ function sayOf(body: ZoneLake | ZoneRiver, x: number, z: number, g: number | nullet dx = mix(a.dx, b.dx), dz = mix(a.dz, b.dz);const len = Math.hypot(dx, dz) || 1;dx /= len; dz /= len;- const deep = g === null ? DEEP : y - g;+ const depth = g === null ? DEEP : y - g;// Within its banks and over its bed: a bank inside the river's width that stands above its surface is dry.- const shore = Math.min(half - best, deep);+ const shore = Math.min(half - best, depth / slope);// The current fades in over a width from the spring and out over a width into whatever it runs into.const along = (at + t) / (st.length - 1);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(shore, body.shoreWidth);+ 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 };}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex d55720bb7..195a641fe 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -2808,4 +2808,6 @@ export const NOTED = {{"note":"Rivers cut their beds."},"feat(terrain): heightmaps name their format, and 16-bit grey PNGs are read at full depth":{"note":"Heightmaps say how they hold their heights."},+ "fix(water): the shore distance near the edge is a distance across the ground, not the water's depth":+ {"note":"The distance to the shore is a distance."},};