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 anchor on a waterfall - a child of a Waterfall with a WaterfallAnchor is kept on the middle of the fall, at its foot wherever the water lands or a share of the time down, so the mist a ParticleEmitter makes there follows the fall when what is under it changes

yhh5459c95ca22c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45f2d92b0..6fdb38e39 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Added
+- **An anchor on a waterfall.** A `Waterfall` lands wherever the ground or the water under it is, and nothing said where that was, so the mist at its foot had to be placed by hand and stayed behind when the fall moved. A child of the fall's with a `WaterfallAnchor` is kept on the middle of its path: at the foot (`at` 1), at the lip (0) or a share of the time down, `lift` above it. A `ParticleEmitter` on that child is the mist.
- **A waterfall.** A river's surface lies over the ground and stopped at the edge of a cliff with nothing after it. A `Waterfall` on an entity at the lip, facing the way the water leaves, throws sheets of water level at `speed` and lets gravity take them: each ends where it meets the ground, or water that is not the water it left, or `maxDrop` under the lip, and is made again when what is under it changes. It is the entity's own `MeshRenderer`; with no material it takes the stock Waterfall shader, glass at the lip that tears into strands and then veils, its pattern riding down with the water at the water's own speed.
- **A sky of your own shades the ground under its clouds.** Sky Atmosphere's clouds cast their shadow on the scene; a sky written with `defineSkyEffect` could not, so a stylised sky lit a world with no cloud shadow on it. With `groundShadow: true` the sky's source holds a second function, `skyShadow(world, sun)`, what is left of the light at a world position, and every surface the sky pass finds is multiplied by it.
- **A texture says whether it is a picture or numbers.** Import settings have a **Use**: Colour, or Data for an image read a number a texel (a heightmap, a mask of measured values, a lookup table). A build treats Data as what it is: never compressed, never decoded as sRGB or filled, kept at its depth, and at its own size unless Max Size (which now offers Source size) says otherwise, in which case it is resampled at 16 bits and not through 8. Nobody has to know to set it: a PNG of 16 bits a sample is imported as Data from the editor and the CLI alike, a texture put in a `Terrain`'s heightmap or splatmap is marked Data there and then (a splatmap cooked as a picture had its weights overwritten wherever the fourth was 0, and the ground fell back to grass everywhere), and Project Health lists textures a component reads as numbers that are still set up as pictures, with the fix on the row.
diff --git a/desktop b/desktop
index 5389d416f..f5cd3df2e 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 5389d416f7a4e448169cff8d34b32072213a49fe
+Subproject commit f5cd3df2e19d3c85210776267229ede4c6ca9db8
diff --git a/docs/astro/src/content/docs/graphics/water.mdx b/docs/astro/src/content/docs/graphics/water.mdx
index f9ff4952f..0aba3e5dd 100644
--- a/docs/astro/src/content/docs/graphics/water.mdx
+++ b/docs/astro/src/content/docs/graphics/water.mdx
@@ -176,7 +176,21 @@ fast they run (`u_run`), how much of the sheet is gone at its foot (`u_veil`) an
`u_opacity` and `u_roughness`. A material of your own reads what the mesh carries: `uv.x` across the sheet, `uv.y` the seconds the water
there has been falling, the colour's red the share of the fall done, its green a number of the sheet's own.
-The fall is the sheet alone: the mist at its foot and the white water where it lands are a `ParticleEmitter`'s and the pool's own to draw.
+The fall is the sheet alone. What belongs at a place on it is a child entity with a **`WaterfallAnchor`**: the fall keeps that child on
+the middle of its path, at the foot wherever the water lands (`at` 1), at the lip (0), or a share of the time down between them, `lift`
+above the path. A [`ParticleEmitter`](/docs/world/particles/) on the child is the mist, and it moves with the foot when the ground or the
+water under the fall changes:
+
+```json
+{ "name": "Foot Mist", "parent": 31, "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "WaterfallAnchor", "data": { "at": 1 } },
+ { "type": "ParticleEmitter", "data": { "shape": 5, "shapeRadius": 700, "rate": 22, "speedMin": 150, "speedMax": 600,
+ "lifetimeMin": 4, "lifetimeMax": 8, "startSizeMin": 400, "startSizeMax": 800, "endSizeMin": 1400, "endSizeMax": 2400,
+ "blendMode": 0, "startColor": { "r": 1, "g": 1, "b": 1, "a": 0.12 }, "endColor": { "r": 1, "g": 1, "b": 1, "a": 0 } } } ] }
+```
+
+The anchor sets the child's position only, in the fall's own space: its turn and scale stay what they were given.
## Floating
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 e59959b29..56fe4f408 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/water.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/water.mdx
@@ -118,7 +118,20 @@ description: 若干 WaterBody 组件说明海洋、湖泊或河流在哪里,一
及其流速(`u_run`)、底部少掉多少(`u_veil`)和落到多深撕成那样(`u_tearBy`)、`u_opacity` 和 `u_roughness`。自己写材质时可以读网格
带的数据:`uv.x` 是横向位置,`uv.y` 是那里的水已经下落的秒数,颜色的红是下落完成的比例,绿是每片水帘自己的编号。
-瀑布只是水帘本身:底部的水雾和落水处的白沫由 `ParticleEmitter` 和水潭自己来画。
+瀑布只画水帘本身。要放在瀑布某处的东西,做成带 **`WaterfallAnchor`** 的子实体:瀑布会把这个子实体保持在水流路径的中线上,
+`at` 为 1 时在落点(水落到哪里就在哪里),0 在崖口,中间的值按下落时间的比例取点,`lift` 是高出路径多少。子实体上放一个
+[`ParticleEmitter`](/docs/zh-cn/world/particles/) 就是水雾,瀑布下方的地形或水域变了,它会跟着落点走:
+
+```json
+{ "name": "Foot Mist", "parent": 31, "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "WaterfallAnchor", "data": { "at": 1 } },
+ { "type": "ParticleEmitter", "data": { "shape": 5, "shapeRadius": 700, "rate": 22, "speedMin": 150, "speedMax": 600,
+ "lifetimeMin": 4, "lifetimeMax": 8, "startSizeMin": 400, "startSizeMax": 800, "endSizeMin": 1400, "endSizeMax": 2400,
+ "blendMode": 0, "startColor": { "r": 1, "g": 1, "b": 1, "a": 0.12 }, "endColor": { "r": 1, "g": 1, "b": 1, "a": 0 } } } ] }
+```
+
+锚点只设置子实体的位置(在瀑布自己的空间里),旋转和缩放保持原样。
## 漂浮
diff --git a/docs/astro/src/data/componentDocs.ts b/docs/astro/src/data/componentDocs.ts
index 1a702c693..14d62aa56 100644
--- a/docs/astro/src/data/componentDocs.ts
+++ b/docs/astro/src/data/componentDocs.ts
@@ -112,6 +112,7 @@ export const COMPONENT_DOCS: Record<string, ComponentDoc> = {
Floating: { category: 'graphics', summary: 'Holds a RigidBody3D up in water and carries it with the current, by forces at a few points of the body.', doc: 'graphics/water', anchor: 'floating', anchorZh: '漂浮' },
Spline: { category: 'graphics', summary: 'A curve through points, each with a width, read by what follows it (a river\'s WaterBody on the same entity); measured by the distance along it.', doc: 'graphics/water', anchor: 'splines', anchorZh: '样条' },
Waterfall: { category: 'graphics', summary: 'Water falling from a lip: sheets thrown level at a speed and taken by gravity until they meet the ground or other water, drawn as the entity's own mesh.', doc: 'graphics/water', anchor: 'waterfalls', anchorZh: '瀑布' },
+ WaterfallAnchor: { category: 'graphics', summary: 'Keeps a child of a Waterfall on the middle of the fall: at its foot wherever the water lands, or a share of the time down. Where the mist goes.', doc: 'graphics/water', anchor: 'waterfalls', anchorZh: '瀑布' },
WaterBody: { category: 'graphics', summary: 'An ocean, a lake or a river: where the water is, how high its waves stand and how it flows; drawn as part of its zone\'s one surface.', doc: 'graphics/water', anchor: 'the-component', anchorZh: '组件' },
WaterZone: { category: 'graphics', summary: 'The region the bodies of water are baked over and drawn from as one surface: the waves they share, the material, the mesh and the ripples.', doc: 'graphics/water', anchor: 'the-zone', anchorZh: '水体区域' },
Billboard: { category: 'graphics', summary: 'Turns the MeshRenderer beside it to face each view that draws it: flat in the view plane, or upright about world up.', doc: 'graphics/drawing', anchor: 'cards-that-face-the-camera-billboard', anchorZh: '朝向相机的卡片billboard' },
diff --git a/docs/astro/src/data/components.generated.json b/docs/astro/src/data/components.generated.json
index 0e98ce5af..191b54dcd 100644
--- a/docs/astro/src/data/components.generated.json
+++ b/docs/astro/src/data/components.generated.json
@@ -10750,6 +10750,29 @@
"across"
]
},
+ {
+ "name": "WaterfallAnchor",
+ "source": "ts",
+ "fields": [
+ {
+ "key": "at",
+ "type": "number",
+ "default": "1",
+ "doc": "Where on the fall, as a share of the time the water takes to come down: 0 the lip, 1 the foot.",
+ "range": "0–1, step 0.01"
+ },
+ {
+ "key": "lift",
+ "type": "number",
+ "default": "0",
+ "doc": "How far above the path of the fall there."
+ }
+ ],
+ "animatable": [
+ "at",
+ "lift"
+ ]
+ },
{
"name": "Weather",
"source": "ts",
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index d63cd1443..2f5eb5e50 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 115 public · 166 beta · 2176 experimental · 3 deprecated
+Symbols: 115 public · 166 beta · 2179 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -13833,6 +13833,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -18593,6 +18604,11 @@ VoxelPlugin
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 87f314345..5e4ffe971 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index.native
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 115 public · 171 beta · 2196 experimental · 3 deprecated
+Symbols: 115 public · 171 beta · 2199 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -14055,6 +14055,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -18865,6 +18876,11 @@ VoxelPlugin
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 869bfded6..cd326ce32 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index.node
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 115 public · 168 beta · 2175 experimental · 3 deprecated
+Symbols: 115 public · 168 beta · 2178 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -13828,6 +13828,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -18598,6 +18609,11 @@ VoxelPlugin
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 7beec4a45..998a7ec3e 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/minigame
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 115 public · 166 beta · 2197 experimental · 3 deprecated
+Symbols: 115 public · 166 beta · 2200 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -14131,6 +14131,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -18906,6 +18917,11 @@ VoxelPlugin
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/etc/water.api.md b/sdk/etc/water.api.md
index 2209ffa2c..285136f8c 100644
--- a/sdk/etc/water.api.md
+++ b/sdk/etc/water.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/water
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 0 public · 0 beta · 47 experimental · 0 deprecated
+Symbols: 0 public · 0 beta · 50 experimental · 0 deprecated
## Basin — interface @experimental
```
@@ -230,6 +230,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -359,6 +370,11 @@ speed: number
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index ee212fe0a..46851c0f6 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/wechat
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 115 public · 166 beta · 2188 experimental · 3 deprecated
+Symbols: 115 public · 166 beta · 2191 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -13835,6 +13835,17 @@ waveSpeed: number
ComponentDef<WaterfallData>
```
+## WaterfallAnchor — const @experimental
+```
+ComponentDef<WaterfallAnchorData>
+```
+
+## WaterfallAnchorData — interface @experimental
+```
+at: number
+lift: number
+```
+
## WaterfallData — interface @experimental
```
across: number
@@ -18632,6 +18643,11 @@ VoxelPlugin
Plugin
```
+## waterfallAnchorPoint — function @experimental
+```
+(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number]
+```
+
## waterfallFlight — function @experimental
```
(fall: WaterfallData, lip: WaterfallLip, landing: WaterfallLanding | null, ahead?: number): { seconds: number; drop: number; }
diff --git a/sdk/src/core-content.ts b/sdk/src/core-content.ts
index cb558e6f2..b456c150f 100644
--- a/sdk/src/core-content.ts
+++ b/sdk/src/core-content.ts
@@ -891,7 +891,10 @@ export {
export { WAVE_SET, waveFade, waveHeight, waveSlope, type WaveParams } from './water/waves';
export { WaterZone, WaterField, currentShare, type WaterZoneData, type FieldSample } from './water/zone';
export { buildOceanMesh, oceanReach, oceanSnap } from './water/waterMesh';
-export { Waterfall, buildWaterfall, waterfallFlight, waterfallHeading, type WaterfallData, type WaterfallLanding, type WaterfallLip } from './water/waterfall';
+export {
+ Waterfall, WaterfallAnchor, buildWaterfall, waterfallAnchorPoint, waterfallFlight, waterfallHeading,
+ type WaterfallAnchorData, type WaterfallData, type WaterfallLanding, type WaterfallLip,
+} from './water/waterfall';
export { WATERFALL_SHADER_TEMPLATE } from './water/waterfallShader';
export { polygonSdf, shoreDamp, type Point2 } from './water/shore';
export { findBasins, drainPath, type Basin, type BasinMap, type BasinOptions } from './water/basins';
diff --git a/sdk/src/water/index.ts b/sdk/src/water/index.ts
index 82234f49b..417f65db0 100644
--- a/sdk/src/water/index.ts
+++ b/sdk/src/water/index.ts
@@ -9,7 +9,10 @@ export { RippleField } from './ripples';
export { Floating, FloatingPlugin, floatingForce, wakeStrength, type FloatingBody, type FloatingData, type FloatingForce } from './floating';
export { WaterZone, WaterField, currentShare, type WaterZoneData, type FieldSample } from './zone';
export { buildOceanMesh, oceanReach, oceanSnap } from './waterMesh';
-export { Waterfall, buildWaterfall, waterfallFlight, waterfallHeading, type WaterfallData, type WaterfallLanding, type WaterfallLip } from './waterfall';
+export {
+ Waterfall, WaterfallAnchor, buildWaterfall, waterfallAnchorPoint, waterfallFlight, waterfallHeading,
+ type WaterfallAnchorData, type WaterfallData, type WaterfallLanding, type WaterfallLip,
+} from './waterfall';
export { WATERFALL_SHADER_TEMPLATE } from './waterfallShader';
export { polygonSdf, shoreDamp, type Point2 } from './shore';
export { findBasins, drainPath, type Basin, type BasinMap, type BasinOptions } from './basins';
diff --git a/sdk/src/water/waterfall.ts b/sdk/src/water/waterfall.ts
index 142436287..59cce0e3e 100644
--- a/sdk/src/water/waterfall.ts
+++ b/sdk/src/water/waterfall.ts
@@ -14,10 +14,13 @@
* What a vertex carries for a material: uv.x across the sheet (0 to 1), uv.y the seconds the water there has been falling, colour
* red the share of the fall done (0 at the lip, 1 at the foot), green a number of the sheet's own (so sheets do not move as one),
* and alpha 0 at the sheet's two edges.
+ *
+ * The sheet is all a fall draws. What belongs at a place on it (the mist at its foot, the sound of it) is a child entity
+ * with a `WaterfallAnchor`, kept on the middle of the fall at a share of the way down wherever the fall lands.
*/
import type { App } from '../app/app';
import type { Entity } from '../types';
-import { defineComponent, MeshRenderer, Transform, type ComponentDef, type MeshRendererData, type TransformData } from '../ecs/component';
+import { Children, defineComponent, MeshRenderer, Transform, type ComponentDef, type MeshRendererData, type TransformData } from '../ecs/component';
import { defineSystem, Schedule } from '../ecs/system';
import { worldUnitsPerMeter } from '../ecs/worldUnits';
import { Assets } from '../asset/AssetPlugin';
@@ -67,6 +70,35 @@ export const Waterfall: ComponentDef<WaterfallData> = /* @__PURE__ */ defineComp
},
});
+/** The fields of the `WaterfallAnchor` component. @experimental */
+export interface WaterfallAnchorData {
+ /** Where on the fall, as a share of the time the water takes to come down: 0 the lip, 1 the foot. */
+ at: number;
+ /** How far above the fall's path there, in world units. */
+ lift: number;
+}
+
+/**
+ * Keeps the entity, a child of a `Waterfall`'s, on the middle of the fall: at its foot wherever that is, or a share of the way down.
+ *
+ * @experimental
+ */
+export const WaterfallAnchor: ComponentDef<WaterfallAnchorData> = /* @__PURE__ */ defineComponent<WaterfallAnchorData>('WaterfallAnchor', {
+ at: 1, lift: 0,
+}, {
+ worldUnitFields: ['lift'],
+ fields: {
+ at: { min: 0, max: 1, step: 0.01, tooltip: 'Where on the fall, as a share of the time the water takes to come down: 0 the lip, 1 the foot.' },
+ lift: { unit: 'wu', tooltip: 'How far above the path of the fall there.' },
+ },
+});
+
+/** Where an anchor sits in the fall's own space (the throw along -z), on a fall that takes `seconds` to come down. */
+export function waterfallAnchorPoint(fall: WaterfallData, seconds: number, anchor: WaterfallAnchorData): [number, number, number] {
+ const t = Math.max(0, Math.min(1, anchor.at)) * seconds;
+ return [0, anchor.lift - 0.5 * GRAVITY * worldUnitsPerMeter() * t * t, -fall.speed * t];
+}
+
/**
* What a fall lands on: the ground's height at a place, and the water there (which body's it is, and how far `y` is above its surface,
* negative under it), null where there is none. Either may be absent.
@@ -207,7 +239,7 @@ function landingOf(app: App): { query: WaterfallLanding; revision: string; waiti
/** Keeps every `Waterfall` falling to what is under it. Called once by the water plugin. */
export function installWaterfalls(app: App): void {
- const built = new Map<Entity, { key: string; handle: number }>();
+ const built = new Map<Entity, { key: string; handle: number; seconds: number }>();
let stock = 0, asked = false;
const letGo = (entity: Entity): void => {
const held = built.get(entity);
@@ -215,6 +247,15 @@ export function installWaterfalls(app: App): void {
if (held?.handle && app.hasResource(Assets)) app.getResource(Assets).releaseBuiltMesh(held.handle);
};
app.world.onDespawn(letGo);
+ const anchor = (entity: Entity, fall: WaterfallData, seconds: number): void => {
+ const world = app.world;
+ for (const child of (world.tryGet(entity, Children) as { entities?: Entity[] } | null)?.entities ?? []) {
+ if (!world.has(child, WaterfallAnchor) || !world.has(child, Transform)) continue;
+ const [x, y, z] = waterfallAnchorPoint(fall, seconds, world.get(child, WaterfallAnchor) as WaterfallAnchorData);
+ const t = world.get(child, Transform) as TransformData;
+ if (t.position.x !== x || t.position.y !== y || t.position.z !== z) world.set(child, Transform, { ...t, position: { x, y, z } });
+ }
+ };
app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {
const world = app.world;
if (!app.hasResource(Assets)) return;
@@ -244,11 +285,13 @@ export function installWaterfalls(app: App): void {
const lip = { x: t.worldPosition.x, y: t.worldPosition.y, z: t.worldPosition.z, dirX, dirZ };
const key = JSON.stringify([fall, lip, landing.revision, worldUnitsPerMeter(), own ? renderer!.material : stock]);
const held = built.get(entity);
- if (held?.key === key) continue;
+ if (held?.key === key) { anchor(entity, fall, held.seconds); continue; }
const mesh = buildWaterfall(fall, lip, landing.query);
const handle = mesh ? assets.createMesh(mesh) : 0;
if (held?.handle) assets.releaseBuiltMesh(held.handle);
- built.set(entity, { key, handle });
+ const { seconds } = waterfallFlight(fall, lip, landing.query);
+ built.set(entity, { key, handle, seconds });
+ anchor(entity, fall, seconds);
world.insert(entity, MeshRenderer, {
...renderer, enabled: true, mesh: handle, material: own ? renderer!.material : stock, opaque: false, castShadows: false, cullBackfaces: false,
} as Partial<MeshRendererData>);
diff --git a/sdk/tests/waterfall.test.ts b/sdk/tests/waterfall.test.ts
index cdb5c226d..1082b2693 100644
--- a/sdk/tests/waterfall.test.ts
+++ b/sdk/tests/waterfall.test.ts
@@ -7,7 +7,7 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { setWorldUnitsPerMeter } from '../src/ecs/worldUnits';
import { MeshChannel, type MeshData } from '../src/asset/meshFormat';
-import { Waterfall, buildWaterfall, waterfallFlight, waterfallHeading, type WaterfallData, type WaterfallLip } from '../src/water/waterfall';
+import { Waterfall, WaterfallAnchor, buildWaterfall, waterfallAnchorPoint, waterfallFlight, waterfallHeading, type WaterfallData, type WaterfallLip } from '../src/water/waterfall';
import { WATERFALL_SHADER_TEMPLATE } from '../src/water/waterfallShader';
beforeAll(() => setWorldUnitsPerMeter(1));
@@ -105,3 +105,24 @@ describe('the Waterfall component and its shader', () => {
for (const p of ['u_water', 'u_foam', 'u_strands', 'u_run', 'u_veil', 'u_tearBy', 'u_opacity', 'u_roughness']) expect(source).toContain(`#pragma param ${p} `);
});
});
+
+describe('a WaterfallAnchor', () => {
+ it('sits at the lip, at the foot, or a share of the time down, on the path the water takes', () => {
+ const f = fall({ speed: 5 });
+ const { seconds, drop } = waterfallFlight(f, lip, { heightAt: () => 20 });
+ expect(waterfallAnchorPoint(f, seconds, { at: 0, lift: 0 }).map(Math.abs)).toEqual([0, 0, 0]);
+ const foot = waterfallAnchorPoint(f, seconds, { at: 1, lift: 0 });
+ expect(foot[1]).toBeCloseTo(-drop, 6);
+ expect(foot[2]).toBeCloseTo(-5 * seconds, 6);
+ const half = waterfallAnchorPoint(f, seconds, { at: 0.5, lift: 0 });
+ expect(half[1]).toBeCloseTo(-drop / 4, 6);
+ expect(half[2]).toBeCloseTo(-2.5 * seconds, 6);
+ });
+
+ it('is lifted off the path by its own lift, and holds a share past either end to that end', () => {
+ const f = fall();
+ expect(waterfallAnchorPoint(f, 2, { at: 1, lift: 3 })[1]).toBeCloseTo(waterfallAnchorPoint(f, 2, { at: 1, lift: 0 })[1] + 3, 6);
+ expect(waterfallAnchorPoint(f, 2, { at: 4, lift: 0 })).toEqual(waterfallAnchorPoint(f, 2, { at: 1, lift: 0 }));
+ expect(WaterfallAnchor._default).toEqual({ at: 1, lift: 0 });
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index d4a98bd41..4484e0782 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4574,6 +4574,8 @@ export const NOTED = {
{"note": "The sea past the rings lies at the sea's level."},
"feat(water): a waterfall - a Waterfall on an entity at a lip throws sheets of water level at a speed and lets gravity take them, each ending where it meets the ground or water that is not the water it left; it is the entity's own MeshRenderer, and with no material it takes the stock Waterfall shader, glass at the lip tearing into strands that ride down with the water":
{"note": "A waterfall."},
+ "feat(water): an anchor on a waterfall - a child of a Waterfall with a WaterfallAnchor is kept on the middle of the fall, at its foot wherever the water lands or a share of the time down, so the mist a ParticleEmitter makes there follows the fall when what is under it changes":
+ {"note": "An anchor on a waterfall."},
"fix(terrain): a detail the pages cannot hold no longer cracks the ground - a tree that wanted more nodes than the atlas has pages was drawn in part by coarser pages standing in, which do not meet their finer neighbours' edges, and the pages were fought over every frame; the ground is now drawn at the share of its detail's range that fits, and says so once":
{"note": "A detail the pages cannot hold no longer cracks the ground."},
"fix(foliage): a far plant grows across the ground, not taller - a mesh plant kept in a far version was grown the same every way to cover the ground of those left out, so a flower that stood for ten stood ten times as high and a far meadow was a field of poles; it now grows across the ground only, and Foliage.farGrowUp gives its height a share of the growth back":