@margoengine/terrain 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +4 -4
  3. package/README.md +2367 -1808
  4. package/dist/bed-mesh.d.ts +40 -0
  5. package/dist/bed-mesh.js +95 -0
  6. package/dist/blend.js +4 -4
  7. package/dist/colors.d.ts +6 -0
  8. package/dist/deform.d.ts +41 -10
  9. package/dist/deform.js +169 -87
  10. package/dist/displace.js +4 -4
  11. package/dist/elevation.d.ts +182 -0
  12. package/dist/elevation.js +155 -0
  13. package/dist/far.d.ts +66 -0
  14. package/dist/far.js +191 -0
  15. package/dist/furrows.d.ts +74 -0
  16. package/dist/furrows.js +157 -0
  17. package/dist/grid.d.ts +6 -3
  18. package/dist/ground-change.d.ts +1 -1
  19. package/dist/ground-change.js +232 -111
  20. package/dist/ground-contact-settings.d.ts +27 -0
  21. package/dist/ground-contact-settings.js +22 -0
  22. package/dist/ground-contact.d.ts +91 -0
  23. package/dist/ground-contact.js +241 -0
  24. package/dist/ground-model.d.ts +83 -1
  25. package/dist/imagery.js +4 -4
  26. package/dist/index.d.ts +120 -9
  27. package/dist/index.js +1 -1
  28. package/dist/layers-pbr.js +9 -9
  29. package/dist/overlays.js +4 -4
  30. package/dist/planet-complex.d.ts +31 -0
  31. package/dist/planet-craters.d.ts +28 -3
  32. package/dist/planet-rays.d.ts +37 -0
  33. package/dist/planet-recipes.d.ts +10 -1
  34. package/dist/planet-recipes.js +341 -115
  35. package/dist/planet.d.ts +5 -0
  36. package/dist/planet.js +112 -104
  37. package/dist/relief-lighting.d.ts +7 -0
  38. package/dist/relief-lighting.js +36 -23
  39. package/dist/rock-surface.d.ts +3 -0
  40. package/dist/rock-surface.js +28 -16
  41. package/dist/rvt.js +5 -5
  42. package/dist/scour.d.ts +90 -0
  43. package/dist/scour.js +153 -0
  44. package/dist/sculpt.js +4 -4
  45. package/dist/shader.d.ts +120 -5
  46. package/dist/shared/settings.d.ts +89 -89
  47. package/dist/{src-C6dtSaAb.js → src-Cr7Gm4n0.js} +359 -148
  48. package/dist/stream-window.d.ts +123 -0
  49. package/dist/stream.d.ts +95 -26
  50. package/dist/stream.js +440 -248
  51. package/dist/wet-ground.js +4 -4
  52. package/package.json +175 -148
@@ -0,0 +1,123 @@
1
+ /** One loaded tile: `tileTexels` x `tileTexels` samples, row by row along +X from the tile's minimum corner, rows along +Z. */
2
+ export interface TerrainStreamTile {
3
+ /** Heights in heightmap units (world Y is `height * heightScale + heightOffset`), finite. */
4
+ readonly heights: Float32Array;
5
+ /**
6
+ * With `TerrainStreamOptions.weights` above 0: that many arrays of RGBA bytes (4 per texel), the painted layer
7
+ * weights of `TerrainTextures.weights` for this tile (layer i is channel i % 4 of array i / 4).
8
+ */
9
+ readonly weights?: readonly Uint8Array[];
10
+ /**
11
+ * With `TerrainStreamOptions.normals`: the tile's normals as RGBA bytes (4 per texel), XYZ encoded 0..1 (`n * 0.5 +
12
+ * 0.5`), the `TerrainTextures.normalmap` texels for this tile. Required on every tile with data then.
13
+ */
14
+ readonly normals?: Uint8Array;
15
+ /**
16
+ * With `TerrainStreamOptions.tint`: the tile's tint as RGBA bytes (4 per texel, sRGB colour and A how far tinted
17
+ * layers move toward it), the `TerrainTextures.tint` texels for this tile. Optional: a tile without it is untinted.
18
+ */
19
+ readonly tint?: Uint8Array;
20
+ }
21
+ /** What changed in the heightmap: a band of rows uploaded, or the whole window moved (`shifted`). Texel rectangle. */
22
+ export interface TerrainStreamChange {
23
+ readonly shifted: boolean;
24
+ readonly u: number;
25
+ readonly v: number;
26
+ readonly width: number;
27
+ readonly depth: number;
28
+ }
29
+ /** Loads tile (x, z); see `TerrainStreamOptions.load`. */
30
+ export type TerrainStreamLoad = (x: number, z: number, signal: AbortSignal) => Promise<TerrainStreamTile | null | undefined>;
31
+ /** The streamed tiles of one window: what `TerrainStream` and its far ring show of their data. */
32
+ export interface TerrainTiles {
33
+ /** Texels per window side (`window * tileTexels`). */
34
+ readonly columns: number;
35
+ /** CPU copy of the heightmap, `columns` x `columns`, row by row from `origin` (kept in place: the same array always). */
36
+ readonly heights: Float32Array;
37
+ /** CPU copies of the weight textures, RGBA bytes, as `heights`. */
38
+ readonly weights: readonly Uint8Array[];
39
+ /** CPU copy of the normal map with `normals`, RGBA bytes as `heights`; else undefined. */
40
+ readonly normals?: Uint8Array;
41
+ /** CPU copy of the tint with `tint`, RGBA bytes as `heights`; else undefined. */
42
+ readonly tint?: Uint8Array;
43
+ /** World XZ of the window's first texel; moves by whole tiles. */
44
+ readonly origin: readonly [number, number];
45
+ /** Tile index of the window's minimum corner. */
46
+ readonly tile: readonly [number, number];
47
+ /** Rises on every change to the heightmap: an upload or a window move. */
48
+ readonly version: number;
49
+ /** Whether tile (x, z) is in the window and fully uploaded (loaded data, or no data at `fill`). */
50
+ resident(x: number, z: number): boolean;
51
+ /** Calls `callback` for every change to the heightmap. Returns the remover. */
52
+ onChange(callback: (change: TerrainStreamChange) => void): () => void;
53
+ }
54
+ /** What a window needs: `TerrainStreamOptions` with every default filled in. */
55
+ export interface TileWindowConfig {
56
+ readonly tileSize: number;
57
+ readonly tileTexels: number;
58
+ readonly window: number;
59
+ /** World XZ of tile (0, 0)'s minimum corner. */
60
+ readonly lattice: readonly [number, number];
61
+ readonly load: TerrainStreamLoad;
62
+ readonly weights: number;
63
+ readonly normals: boolean;
64
+ readonly tint: boolean;
65
+ /** The weight textures as the layers of one array texture. */
66
+ readonly weightArray: boolean;
67
+ readonly fill: number;
68
+ readonly retries: number;
69
+ readonly retryDelay: number;
70
+ readonly signal?: AbortSignal;
71
+ }
72
+ export declare class TileWindow implements TerrainTiles {
73
+ #private;
74
+ readonly columns: number;
75
+ readonly heights: Float32Array;
76
+ readonly weights: readonly Uint8Array[];
77
+ readonly normals?: Uint8Array;
78
+ readonly tint?: Uint8Array;
79
+ /** The arrays `tile` and `origin` return, moved in place with the window (the terrain reads `origin` every frame). */
80
+ readonly tile: [number, number];
81
+ readonly origin: [number, number];
82
+ /** Loads in flight. */
83
+ loading: number;
84
+ version: number;
85
+ /** `x`, `z`: the world position the window is first placed around. `changed` and `settle` tell the owner. */
86
+ constructor(config: TileWindowConfig, x: number, z: number, changed: () => void, settle: () => void);
87
+ /** The textures made by `create`: the heights, the weights (one entry per texture), the normal map and the tint. */
88
+ get textures(): {
89
+ height: GPUTexture;
90
+ weights: GPUTexture[];
91
+ normals?: GPUTexture;
92
+ tint?: GPUTexture;
93
+ };
94
+ resident(x: number, z: number): boolean;
95
+ /** Whether tile (x, z) is resident with loaded data, not just `fill`. */
96
+ holds(x: number, z: number): boolean;
97
+ onChange(callback: (change: TerrainStreamChange) => void): () => void;
98
+ /** Starts every failed tile, and every tile waiting for a retry, over; returns how many. */
99
+ retry(): number;
100
+ /** Whether some tile of the window is not resident yet, and the first failed tile's error as `failed`. */
101
+ state(): {
102
+ missing: boolean;
103
+ failed?: {
104
+ error: unknown;
105
+ };
106
+ };
107
+ /**
108
+ * Moves the window a whole tile or more when world (x, z) is more than 0.75 of a tile from its centre. Returns
109
+ * (x, z) in tiles from the lattice, the window's centre for a position that is not finite.
110
+ */
111
+ follow(x: number, z: number): [number, number];
112
+ /** Starts up to `free` loads for window tiles without one, nearest to (fx, fz) in tiles first. */
113
+ schedule(fx: number, fz: number, free: number): void;
114
+ /** Uploads bands of loaded tiles within `budget` texels, nearest to (fx, fz) first; returns the texels uploaded. */
115
+ upload(fx: number, fz: number, budget: number): number;
116
+ /** Makes the textures on `device` from the CPU copies, at attachment and after device restoration. */
117
+ create(device: GPUDevice): void;
118
+ /** Destroys the window's own scratch and fill textures (a terrain given the others owns and destroys them). */
119
+ release(): void;
120
+ /** Destroys the heightmap and planes too: for a window whose textures no terrain owns (the far ring). */
121
+ destroyTextures(): void;
122
+ dispose(): void;
123
+ }
package/dist/stream.d.ts CHANGED
@@ -1,25 +1,9 @@
1
1
  /// <reference lib="esnext.disposable" preserve="true" />
2
2
  import type { EngineKernel } from '@margoengine/core';
3
3
  import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
4
- import { Terrain, type TerrainOptions } from './index.js';
5
- /** One loaded tile: `tileTexels` x `tileTexels` samples, row by row along +X from the tile's minimum corner, rows along +Z. */
6
- export interface TerrainStreamTile {
7
- /** Heights in heightmap units (world Y is `height * heightScale + heightOffset`), finite. */
8
- readonly heights: Float32Array;
9
- /**
10
- * With `TerrainStreamOptions.weights` above 0: that many arrays of RGBA bytes (4 per texel), the painted layer
11
- * weights of `TerrainTextures.weights` for this tile (layer i is channel i % 4 of array i / 4).
12
- */
13
- readonly weights?: readonly Uint8Array[];
14
- }
15
- /** What changed in the heightmap: a band of rows uploaded, or the whole window moved (`shifted`). Texel rectangle. */
16
- export interface TerrainStreamChange {
17
- readonly shifted: boolean;
18
- readonly u: number;
19
- readonly v: number;
20
- readonly width: number;
21
- readonly depth: number;
22
- }
4
+ import { Terrain, type TerrainOptions, type TerrainSource } from './index.js';
5
+ import { type TerrainStreamChange, type TerrainStreamLoad, type TerrainStreamTile, type TerrainTiles } from './stream-window.js';
6
+ export type { TerrainStreamChange, TerrainStreamLoad, TerrainStreamTile, TerrainTiles, } from './stream-window.js';
23
7
  /** {@link TerrainStream} options: the terrain's own options without `textures`, `origin` and `size`, plus these.
24
8
  * The clipmap window defaults to `'camera'`, on the same world lattice as its tiles. */
25
9
  export interface TerrainStreamOptions extends Omit<TerrainOptions, 'textures' | 'origin' | 'size'> {
@@ -33,7 +17,8 @@ export interface TerrainStreamOptions extends Omit<TerrainOptions, 'textures' |
33
17
  readonly origin?: readonly [number, number];
34
18
  /**
35
19
  * Loads tile (x, z) (any integers; tile (x, z) starts at `origin + [x, z] * tileSize`). Resolve undefined or null
36
- * for no data there (the tile keeps `fill`). `signal` aborts when the tile leaves the window or the stream is disposed.
20
+ * for no data there (the tile keeps `fill`; with `far` the far ring's ground shows there). `signal` aborts when the
21
+ * tile leaves the window or the stream is disposed.
37
22
  */
38
23
  readonly load: (x: number, z: number, signal: AbortSignal) => Promise<TerrainStreamTile | null | undefined>;
39
24
  /** Makes the layer colour array (`TerrainTextures.layers`), at attachment and again after device restoration. */
@@ -42,19 +27,88 @@ export interface TerrainStreamOptions extends Omit<TerrainOptions, 'textures' |
42
27
  readonly weights?: number;
43
28
  /** Texels uploaded per frame (one tile row at least). Default 65,536 (one 256 x 256 tile). */
44
29
  readonly budget?: number;
45
- /** Tile loads in flight at once, 1 to 64. Default 4. */
30
+ /** Tile loads in flight at once, 1 to 64, the far ring's included. Default 4. */
46
31
  readonly concurrency?: number;
47
32
  /** Height of texels whose tile is not loaded yet, finite. Default 0. */
48
33
  readonly fill?: number;
49
34
  /** Aborts every load; no new ones start, and `ready` rejects with its reason. */
50
35
  readonly signal?: AbortSignal;
36
+ /**
37
+ * Times a tile whose `load` rejects is loaded again before it counts as failed, 0 to 16. Default 2. A tile of the
38
+ * wrong shape and an aborted load are not retried. `retry()` starts every failed tile over.
39
+ */
40
+ readonly retries?: number;
41
+ /**
42
+ * Base wait before a retry in milliseconds, 0 or more. Default 250. Retry k (from 0) waits a random time up to
43
+ * `retryDelay * 2^k`, capped at 30 s (exponential backoff with full jitter), so tiles that failed together do not
44
+ * all ask again at once.
45
+ */
46
+ readonly retryDelay?: number;
47
+ /**
48
+ * Stream a normal map with the heights: each tile with data gives `normals`, and the terrain shades with them in place
49
+ * of normals from neighbouring heights (`TerrainTextures.normalmap`), so lighting keeps detail finer than the height
50
+ * samples. Texels not loaded yet face straight up. Default false.
51
+ */
52
+ readonly normals?: boolean;
53
+ /**
54
+ * Stream a tint texture with the heights (`TerrainTextures.tint`, as `rgba8unorm-srgb`; needs `weights`): layers with
55
+ * `tint: true` move toward each tile's `tint` colour by its A, a draped photograph or a season map. Texels not loaded
56
+ * yet, and tiles without `tint`, are untinted. Default false.
57
+ */
58
+ readonly tint?: boolean;
59
+ /**
60
+ * Draw coarser ground past the window: `terrainFar({ scale, load })` from `@margoengine/terrain/far`, a second window
61
+ * of bigger tiles the heights blend into with distance. With it, `levels` defaults to enough rings to reach the far
62
+ * ring's edge. Default: none (nothing past the window is drawn).
63
+ */
64
+ readonly far?: TerrainStreamFar;
65
+ }
66
+ /** A tile window the far ring reads: its tiles, which hold data, and its textures. */
67
+ export interface TerrainStreamWindow extends TerrainTiles {
68
+ /** Whether tile (x, z) is resident with loaded data, not just `fill`. */
69
+ holds(x: number, z: number): boolean;
70
+ /** The window's textures, made for the current device. */
71
+ readonly textures: {
72
+ readonly height: GPUTexture;
73
+ readonly weights: readonly GPUTexture[];
74
+ readonly normals?: GPUTexture;
75
+ readonly tint?: GPUTexture;
76
+ };
77
+ }
78
+ /** What a {@link TerrainStream} needs of its far ring; `terrainFar` (`@margoengine/terrain/far`) makes one. */
79
+ export interface TerrainStreamFar {
80
+ /** Loads far tile (x, z), which starts at the stream's `origin + [x, z] * tileSize` of the far ring. */
81
+ readonly load: TerrainStreamLoad;
82
+ /**
83
+ * The far window for a stream with these tiles: its tile side in world units, texels and tiles per side, and how far
84
+ * from the camera on each axis it draws (`reach`).
85
+ * @throws RangeError when the two windows do not fit together.
86
+ */
87
+ plan(near: {
88
+ tileSize: number;
89
+ tileTexels: number;
90
+ window: number;
91
+ }): {
92
+ tileSize: number;
93
+ tileTexels: number;
94
+ window: number;
95
+ reach: number;
96
+ };
97
+ /** Joins the two windows: returns what draws and updates the blend between them. */
98
+ bind(near: TerrainStreamWindow, far: TerrainStreamWindow, terrain: Pick<TerrainOptions, 'heightScale' | 'heightOffset'>): {
99
+ /** The shader additions on `device`, at attachment and after device restoration (after both windows' textures). */
100
+ source(device: GPUDevice): TerrainSource;
101
+ /** One frame: the camera's world XZ and the seconds since the last. Returns whether the drawn ground changed. */
102
+ update(delta: number, x: number, z: number): boolean;
103
+ dispose(): void;
104
+ };
51
105
  }
52
106
  /**
53
107
  * A {@link Terrain} whose heightmap (and painted weights) stream in tiles around the camera, so the world can be far
54
108
  * bigger than one heightmap. Attach with {@link TerrainStream.attach}; it makes and owns the terrain (`terrain`).
55
109
  * The window follows the engine camera: it moves a whole tile when the camera is more than 0.75 of a tile from its
56
110
  * centre, so ground at least `(window / 2 - 0.75) * tileSize` from the camera on each axis is always in it (the
57
- * terrain draws nothing past the window, as past any heightmap).
111
+ * terrain draws nothing past the window, as past any heightmap, unless `far` adds a coarser ring there).
58
112
  */
59
113
  export declare class TerrainStream implements EnginePlugin {
60
114
  #private;
@@ -67,6 +121,12 @@ export declare class TerrainStream implements EnginePlugin {
67
121
  readonly heights: Float32Array;
68
122
  /** CPU copies of the weight textures, RGBA bytes, as `heights`. */
69
123
  readonly weights: readonly Uint8Array[];
124
+ /** CPU copy of the normal map with `normals`, RGBA bytes as `heights`; else undefined. */
125
+ readonly normals?: Uint8Array;
126
+ /** CPU copy of the tint with `tint`, RGBA bytes as `heights`; else undefined. */
127
+ readonly tint?: Uint8Array;
128
+ /** With `far`: the far ring's own window of tiles. */
129
+ readonly far?: TerrainTiles;
70
130
  disposed: boolean;
71
131
  private constructor();
72
132
  /**
@@ -83,18 +143,27 @@ export declare class TerrainStream implements EnginePlugin {
83
143
  get version(): number;
84
144
  /** Whether tile (x, z) is in the window and fully uploaded (loaded data, or no data at `fill`). */
85
145
  resident(x: number, z: number): boolean;
146
+ /**
147
+ * Starts every failed tile of the window (and of the far ring) over, and every tile waiting for a retry at once, each
148
+ * with its full `retries` again (after the network comes back, say: `addEventListener('online', () =>
149
+ * stream.retry())`). Returns how many tiles it started over; they load as frames are drawn.
150
+ */
151
+ retry(): number;
86
152
  /**
87
153
  * Calls `callback` for every change to the heightmap, for a `TerrainQuery` over `heights`: refresh it over a band's
88
- * rectangle, or build a new one (`origin` moved) when `shifted`. Returns the remover.
154
+ * rectangle, or build a new one (`origin` moved) when `shifted`. Returns the remover. The far ring's changes come
155
+ * from `far.onChange`.
89
156
  */
90
157
  onChange(callback: (change: TerrainStreamChange) => void): () => void;
91
158
  /**
92
- * Resolves once every tile of the current window is loaded and uploaded (uploads go in as frames are drawn).
93
- * Rejects with a tile's load error, `signal`'s or the stream's abort reason, or MargoError `'disposed'`.
159
+ * Resolves once every tile of the current window, and of the far ring, is loaded and uploaded (uploads go in as frames
160
+ * are drawn). Rejects with a tile's load error once its `retries` are spent, `signal`'s or the stream's abort reason,
161
+ * or MargoError `'disposed'`.
94
162
  */
95
163
  ready(signal?: AbortSignal): Promise<void>;
96
164
  setup(engine: Engine): void;
97
- onPreUpdate(): void;
165
+ /** Follows the camera, loads and uploads; `delta`, the frame's seconds, drives the far ring's fades. */
166
+ onPreUpdate(delta?: number): void;
98
167
  dispose(): void;
99
168
  [Symbol.dispose]: () => void;
100
169
  }