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): how far a dry place is from the water's edge, as a query and a field generated worlds read

yhh91d215b55898
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2adfd7da2..25919475f 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.
+- **How far a place is from the water.** `Waters.shoreAt(x, z)` gives the distance across the ground to the water's edge (above zero on dry land, below on the water), and generated worlds read it as the `shoreDistance` field, so a beach or a band of reeds can follow the shore.
- **A vertex hook can hand the fragment a colour.** What a material's vertex hook leaves in `es_vertexColor` is now the colour its fragment receives (times the instance's tint), on both backends.
- **Bakes run over frames, with progress.** Work that used to hold a frame until it was done (a terrain worn by rain, a water zone baked, terrain chunks built, plants grown) is now spread over frames within a budget, and listed with its progress in the new `Bakes` resource (`bakesOf(app).active`). The editor's status bar shows it as a bar, for the edit world and for the game running in Play, and a game can show it on a loading screen. Plants wait for the ground and the water they stand on instead of being grown twice.
- **Eroded and warped terrain.** `Terrain.erosion` wears the ground with rain (a heightmap's as well as the noise's): gullies down the slopes, sharper ridges, silt in the valleys, and dirt painted where the water ran; the same seed wears the same ground. `Terrain.warp` bends the noise by noise of its own for winding valleys and bent ridges. `erodeHeightfield` and `erosionJob` do the same for a tool.
diff --git a/docs/WORLD_GENERATION_PLAN.md b/docs/WORLD_GENERATION_PLAN.md
index f72b641e0..9aae555db 100644
--- a/docs/WORLD_GENERATION_PLAN.md
+++ b/docs/WORLD_GENERATION_PLAN.md
@@ -59,7 +59,8 @@
- 测试:`sdk/tests/worldgen-plan.test.ts`、`worldgen-plugin.test.ts`。
- 后续已补:装饰远景版本(成员 `far` 卡片 + 层 `lodDistance`,LODGroup 切换);贴地装饰合并时随法线倾斜。
- 后续已补:有限整地——地形修改器(`Terrains.setModifier`,世界空间函数,所有读取方一致,仅重做触及的页与碰撞块),区域 `flatten` 压平(地面未知时挂起重试)。
- - 仍欠:陆地离岸距离场、配方编辑器、可达性/安全落点等验收由游戏用查询接口实现(引擎可后续提供寻路连通查询)。
+ - 后续已补:陆地离岸距离——`Waters.shoreAt` 与环境场 `shoreDistance`(陆上为到水边距离,水中为负)。
+ - 仍欠:配方编辑器、可达性/安全落点等验收由游戏用查询接口实现(引擎可后续提供寻路连通查询)。
- 验收:林地建房后草木不穿地板;采集后卸载重进不重生;同种子不同加载顺序结果相同;安全落点、早期资源、入口可达。
## 水域补充
diff --git a/docs/astro/src/content/docs/graphics/water.mdx b/docs/astro/src/content/docs/graphics/water.mdx
index 9c03a17a2..304919d09 100644
--- a/docs/astro/src/content/docs/graphics/water.mdx
+++ b/docs/astro/src/content/docs/graphics/water.mdx
@@ -275,7 +275,8 @@ The lite water costs no vertex work for the waves, and the shading still moves,
It reads the same formula the shader draws, at the time of the frame, with the height the waves stand to there and the ripples. `Waters.heightAt(x, z)` is the
surface's height alone. A point over no water (outside every shore, or past the zone with no ocean) answers `null`. `Waters.bodyAt(x, z)` says which body a
-place belongs to: its entity, its `bodyId`, its form and its level at rest.
+place belongs to: its entity, its `bodyId`, its form and its level at rest. `Waters.shoreAt(x, z)` is how far a place is from the water's edge
+across the ground: above zero on dry land, below it on the water.
The surface the code reads is the full wave sum; a coarse far ring draws less of it (see above), so near the horizon the two can
differ by the short waves, which are small.
diff --git a/docs/astro/src/content/docs/world/generation.mdx b/docs/astro/src/content/docs/world/generation.mdx
index 41193aba1..ea3bc8e3d 100644
--- a/docs/astro/src/content/docs/world/generation.mdx
+++ b/docs/astro/src/content/docs/world/generation.mdx
@@ -55,7 +55,7 @@ registerWorldRules({
### Fields
Every decision is made from **fields**: numbers read at any world position. `height`, `up` (how much the ground faces up) and `slope`
-come from the terrain's surface, `waterDepth` from the water; `moisture`, `temperature` and `rock` are seeded noise, slow across the
+come from the terrain's surface, `waterDepth` and `shoreDistance` (how far a dry place is from the water's edge, negative on the water) from the water; `moisture`, `temperature` and `rock` are seeded noise, slow across the
world (their scales are the rules' `noise`). A game adds fields of its own with `fields: { name: (x, z, at) => number }`, reading the
built-in ones from `at`. A zone can set any field over its area.
@@ -151,8 +151,6 @@ Declare the folders they live in always included, in `.esengine/asset-groups.jso
## Limits
-- **No distance from the shore on land.** `waterDepth` says how deep water stands; a field of how far a dry place is from water is a
- game's own for now.
- **Decoration is merged a chunk at a time, not GPU-instanced.** The renderer's instance records are written each frame, which for a
field of thousands of plants would upload megabytes a frame; merged static meshes upload nothing, and the chunks bound their memory.
- **Rules are code.** Recipes are plain data and can be read from JSON, but there is no editor for them yet.
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx b/docs/astro/src/content/docs/zh-cn/graphics/water.mdx
index a635953ef..1846e2c8e 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/water.mdx
@@ -188,7 +188,7 @@ Details 只显示水体这种形态用得到的字段:湖有尺寸和边界,河
- `inside`:这个点是否在水上:只要回答不是 null,就总是;
- `flow`:水在那里怎么流动,世界单位每秒(河流的水流;静水为零)。
-它读的是着色器画的同一个公式,取这一帧的时间,带上那里波浪的高度和涟漪。`Waters.heightAt(x, z)` 只给出水面的高度。点上方没有水(在所有岸线之外,或在区域之外且没有海洋)则返回 `null`。`Waters.bodyAt(x, z)` 说明一个地方属于哪个水体:它的实体、`bodyId`、形态和静止水位。
+它读的是着色器画的同一个公式,取这一帧的时间,带上那里波浪的高度和涟漪。`Waters.heightAt(x, z)` 只给出水面的高度。点上方没有水(在所有岸线之外,或在区域之外且没有海洋)则返回 `null`。`Waters.bodyAt(x, z)` 说明一个地方属于哪个水体:它的实体、`bodyId`、形态和静止水位。`Waters.shoreAt(x, z)` 是一个地方沿地面到水边的距离:干燥的陆地上为正,水面上为负。
代码读到的是完整的波浪和;远处的粗圈画得少一些(见上),所以在接近地平线处两者可能相差那些短波,它们本来就很小。
diff --git a/docs/astro/src/content/docs/zh-cn/world/generation.mdx b/docs/astro/src/content/docs/zh-cn/world/generation.mdx
index f5cd8f1f8..d2cde365d 100644
--- a/docs/astro/src/content/docs/zh-cn/world/generation.mdx
+++ b/docs/astro/src/content/docs/zh-cn/world/generation.mdx
@@ -49,7 +49,7 @@ registerWorldRules({
### 环境场
-每个决定都来自**环境场**:在任意世界位置读到的数值。`height`、`up`(地面朝上的程度)和 `slope` 来自地形的地表,`waterDepth` 来自水体;`moisture`、`temperature` 和 `rock` 是带种子的噪声,在世界尺度上缓慢变化(尺度在规则的 `noise` 里设)。游戏可以用 `fields: { name: (x, z, at) => number }` 加入自己的场,并从 `at` 读取内置场。区域可以在它的范围内设置任意场的值。
+每个决定都来自**环境场**:在任意世界位置读到的数值。`height`、`up`(地面朝上的程度)和 `slope` 来自地形的地表,`waterDepth` 和 `shoreDistance`(干燥处离水边多远,水面上为负)来自水体;`moisture`、`temperature` 和 `rock` 是带种子的噪声,在世界尺度上缓慢变化(尺度在规则的 `noise` 里设)。游戏可以用 `fields: { name: (x, z, at) => number }` 加入自己的场,并从 `at` 读取内置场。区域可以在它的范围内设置任意场的值。
### 群落
@@ -125,7 +125,6 @@ gens.diffs().load(saved); // 以及读回
## 限制
-- **陆地上没有离岸距离。** `waterDepth` 说明水有多深;干燥的地方离水多远,目前由游戏自己提供场。
- **装饰按区块合并绘制,而不是 GPU 实例化。** 渲染器的实例记录每帧重写,成千上万株植物每帧要上传数兆字节;合并的静态网格每帧不上传任何东西,内存由区块驻留限制住。
- **规则是代码。** 配方是普通数据,可以从 JSON 读取,但还没有编辑器。
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index f65b3dce0..cae19e419 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -11850,6 +11850,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 3b72aa83b..bb48b6b7c 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -12063,6 +12063,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index ca4beb357..f27dc462d 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -11845,6 +11845,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 2f40f4b2a..39af13f09 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -12144,6 +12144,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/water.api.md b/sdk/etc/water.api.md
index a192265ae..71d1260d1 100644
--- a/sdk/etc/water.api.md
+++ b/sdk/etc/water.api.md
@@ -228,6 +228,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 12bde0c6b..c3e6903b1 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -11852,6 +11852,7 @@ heightAt: (x: number, z: number) => number | null
quality: WaterQuality
rippleAt: (x: number, _y: number, z: number) => number
sample: (x: number, y: number, z: number) => WaterSample | null
+shoreAt: (x: number, z: number) => number | null
static new (): Waters
static prototype: Waters
```
diff --git a/sdk/etc/worldgen.api.md b/sdk/etc/worldgen.api.md
index a4fa93ff9..94c2774ed 100644
--- a/sdk/etc/worldgen.api.md
+++ b/sdk/etc/worldgen.api.md
@@ -44,6 +44,7 @@ ground: (x: number, z: number) => { y: number; up: number; normal?: [number, num
noise: Partial<Record<string, NoiseField>> | undefined
overrides: ((x: number, z: number) => { field: string; value: number; weight: number; }[]) | undefined
seed: number
+shoreDistance: ((x: number, z: number) => number | null) | undefined
waterDepth: ((x: number, z: number, y: number) => number | null) | undefined
```
diff --git a/sdk/src/water/waterPlugin.ts b/sdk/src/water/waterPlugin.ts
index d91cbcdb8..3017be6ab 100644
--- a/sdk/src/water/waterPlugin.ts
+++ b/sdk/src/water/waterPlugin.ts
@@ -206,6 +206,18 @@ export class Waters {
return { entity, bodyId: body?.bodyId || `entity:${entity}`, form: body?.form ?? WaterForm.Ocean, level: s.height };
}
+ /**
+ * How far (x, z) is from the water's edge, across the ground: above zero on dry land (how far to the nearest water), below it on the
+ * water (how far in from its shore). Past the zone's region it is the open sea's, deep in; null where no water is baked at all.
+ */
+ shoreAt(x: number, z: number): number | null {
+ const field = this.field_;
+ if (!field) return null;
+ const at = field.at(x, z);
+ if (at) return -at.shore;
+ return field.ocean ? -1e6 : null;
+ }
+
private bodyIdOf(entity: Entity): string {
const world = this.app_?.world;
const body = world?.valid(entity) && world.has(entity, WaterBody) ? world.get(entity, WaterBody) as WaterBodyData : null;
diff --git a/sdk/src/worldgen/fields.ts b/sdk/src/worldgen/fields.ts
index 8603ceffd..0f5e0d594 100644
--- a/sdk/src/worldgen/fields.ts
+++ b/sdk/src/worldgen/fields.ts
@@ -4,7 +4,8 @@
* @file fields.ts
* @brief The environment a world is laid out by: numbers read anywhere in it, the same for the same seed on every machine.
*
- * @details A field is a function of the world position. The ground's (`height`, `up`, `slope`) and the water's (`waterDepth`) are
+ * @details A field is a function of the world position. The ground's (`height`, `up`, `slope`) and the water's (`waterDepth`, and
+ * `shoreDistance`: how far a dry place is from the water's edge, negative in the water) are
* read from the terrain and the water; `moisture`, `temperature` and `rock` are seeded noise, slow across the world; a game
* adds fields of its own. A zone may set a field over its area, blended in by its weight, so a designer can make a wet hollow
* or a dry clearing by hand. Nothing here depends on what is loaded or in what order it was asked.
@@ -27,6 +28,8 @@ export interface EnvironmentInputs {
ground(x: number, z: number): { y: number; up: number; normal?: [number, number, number] } | null;
/** How deep water stands over the world height `y` at (x, z): above zero under water, null where there is none. */
waterDepth?(x: number, z: number, y: number): number | null;
+ /** How far (x, z) is from the water's edge across the ground: above zero on land, below in the water, null where no water is known. */
+ shoreDistance?(x: number, z: number): number | null;
/** The noise fields; absent ones keep their defaults (moisture 900, temperature 2400, rock 500 world units). */
noise?: Partial<Record<string, NoiseField>>;
/** Fields of the game's own. */
@@ -91,6 +94,7 @@ export class Environment {
up: g ? g.up : 1,
slope: g ? 1 - g.up : 0,
waterDepth: g && this.inputs.waterDepth ? this.inputs.waterDepth(x, z, g.y) ?? -Infinity : -Infinity,
+ shoreDistance: this.inputs.shoreDistance?.(x, z) ?? Infinity,
};
for (const [name, f] of Object.entries(this.noise_)) {
at[name] = fbm(x / Math.max(f.scale, 1e-6), z / Math.max(f.scale, 1e-6), this.seeds_[name]!, Math.max(1, f.octaves ?? 3));
diff --git a/sdk/src/worldgen/worldgenPlugin.ts b/sdk/src/worldgen/worldgenPlugin.ts
index d417544ec..0a514ef1f 100644
--- a/sdk/src/worldgen/worldgenPlugin.ts
+++ b/sdk/src/worldgen/worldgenPlugin.ts
@@ -158,7 +158,7 @@ interface PlacedChunk {
}
/** The water, as worldgen asks it (the water module is optional: read by name, not imported). */
-interface WaterQuery { depthAt(x: number, z: number, y: number): number | null; revision_: number; baking: boolean }
+interface WaterQuery { depthAt(x: number, z: number, y: number): number | null; shoreAt?(x: number, z: number): number | null; revision_: number; baking: boolean }
function waterOf(app: App): WaterQuery | null {
const w = (app as unknown as { getResourceByName?: (name: string) => unknown }).getResourceByName?.('Waters') as Partial<WaterQuery> | undefined;
@@ -321,6 +321,7 @@ export class WorldGenPlugin implements Plugin {
return { y: t?.worldPosition.y ?? 0, up: 1, normal: [0, 1, 0] };
},
waterDepth: (x, z, y) => waterOf(app)?.depthAt(x, z, y) ?? null,
+ shoreDistance: (x, z) => waterOf(app)?.shoreAt?.(x, z) ?? null,
overrides: (x, z) => {
const out: { field: string; value: number; weight: number }[] = [];
for (const { zone, weight } of zones.at(x, z)) for (const [field, value] of Object.entries(zone.fields ?? {})) out.push({ field, value, weight });
diff --git a/sdk/tests/water-bodies.test.ts b/sdk/tests/water-bodies.test.ts
index c5514de92..a21cc7407 100644
--- a/sdk/tests/water-bodies.test.ts
+++ b/sdk/tests/water-bodies.test.ts
@@ -144,5 +144,8 @@ describe('the water in a running app with no renderer', () => {
expect(waters.bodyAt(0, 0)!.level).toBeCloseTo(2, 3);
expect(waters.sample(0, 0, 0)?.bodyId).toBe('mirror-lake');
expect(waters.bodyAt(90, 90)).toBeNull();
+ // How far from the water's edge: across the dry ground beside it, and in from the shore on it.
+ expect(waters.shoreAt(45, 0)).toBeCloseTo(15, -0.5);
+ expect(waters.shoreAt(0, 0)).toBeLessThan(-20);
});
});
diff --git a/sdk/tests/worldgen-plan.test.ts b/sdk/tests/worldgen-plan.test.ts
index c214a2f67..a1b94991f 100644
--- a/sdk/tests/worldgen-plan.test.ts
+++ b/sdk/tests/worldgen-plan.test.ts
@@ -149,3 +149,11 @@ describe('placement diffs', () => {
expect(told).toEqual(['groups@1:3,4/grove@1/oak.0', 'detail@1:9,9/ore@1/copper.0']);
});
});
+
+describe('the environment', () => {
+ it('reads how far a place is from the water, and none where no water is known', () => {
+ const env = new Environment({ seed: 1, ground: () => ({ y: 0, up: 1 }), shoreDistance: (x) => (x > 0 ? x : null) });
+ expect(env.sample(12, 0).shoreDistance).toBe(12);
+ expect(env.sample(-5, 0).shoreDistance).toBe(Infinity);
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0e3c343a6..b67df1d1b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2794,4 +2794,6 @@ export const NOTED = {
{"note":"Far versions for generated decoration."},
"feat(terrain): modifiers change the ground for every reader and remake only what they reach, and a zone can level its plot":
{"note":"Terrain modifiers, and zones that level their ground."},
+ "feat(water): how far a dry place is from the water's edge, as a query and a field generated worlds read":
+ {"note":"How far a place is from the water."},
};