@margoengine/terrain 0.0.0-stage → 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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +2366 -2
- package/dist/bed-mesh.d.ts +40 -0
- package/dist/bed-mesh.js +95 -0
- package/dist/blend.d.ts +70 -0
- package/dist/blend.js +159 -0
- package/dist/colors.d.ts +170 -0
- package/dist/deform.d.ts +134 -0
- package/dist/deform.js +404 -0
- package/dist/displace.d.ts +60 -0
- package/dist/displace.js +254 -0
- package/dist/dunes.d.ts +86 -0
- package/dist/dunes.js +88 -0
- package/dist/elevation.d.ts +182 -0
- package/dist/elevation.js +155 -0
- package/dist/errors-fc4i1pSL.js +7 -0
- package/dist/far.d.ts +66 -0
- package/dist/far.js +191 -0
- package/dist/furrows.d.ts +74 -0
- package/dist/furrows.js +157 -0
- package/dist/grid.d.ts +14 -0
- package/dist/ground-change-settings.d.ts +29 -0
- package/dist/ground-change-settings.js +69 -0
- package/dist/ground-change.d.ts +227 -0
- package/dist/ground-change.js +1412 -0
- package/dist/ground-contact-settings.d.ts +27 -0
- package/dist/ground-contact-settings.js +22 -0
- package/dist/ground-contact.d.ts +91 -0
- package/dist/ground-contact.js +241 -0
- package/dist/ground-model.d.ts +372 -0
- package/dist/hdr-format-DRhzGS7G.js +30 -0
- package/dist/height-grid.d.ts +65 -0
- package/dist/height-grid.js +74 -0
- package/dist/heightfield.d.ts +38 -0
- package/dist/holes.d.ts +37 -0
- package/dist/holes.js +97 -0
- package/dist/imagery.d.ts +109 -0
- package/dist/imagery.js +472 -0
- package/dist/index.d.ts +408 -0
- package/dist/index.js +1 -0
- package/dist/layers-pbr.d.ts +78 -0
- package/dist/layers-pbr.js +128 -0
- package/dist/lighting-layout-CaDtDa5G.js +34 -0
- package/dist/lighting-layout.d.ts +5 -0
- package/dist/mesh-patch-BThCHCeS.js +21 -0
- package/dist/mesh-patch.d.ts +14 -0
- package/dist/noise3-sQdnbz4B.js +11 -0
- package/dist/noise3.d.ts +3 -0
- package/dist/overlays.d.ts +74 -0
- package/dist/overlays.js +297 -0
- package/dist/planet-complex.d.ts +31 -0
- package/dist/planet-craters.d.ts +80 -0
- package/dist/planet-rays.d.ts +37 -0
- package/dist/planet-recipes.d.ts +84 -0
- package/dist/planet-recipes.js +442 -0
- package/dist/planet.d.ts +144 -0
- package/dist/planet.js +610 -0
- package/dist/query.d.ts +150 -0
- package/dist/query.js +243 -0
- package/dist/relief-lighting.d.ts +53 -0
- package/dist/relief-lighting.js +221 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/rock-settings.d.ts +39 -0
- package/dist/rock-settings.js +84 -0
- package/dist/rock-surface.d.ts +114 -0
- package/dist/rock-surface.js +380 -0
- package/dist/rocks.d.ts +258 -0
- package/dist/rocks.js +488 -0
- package/dist/rvt.d.ts +136 -0
- package/dist/rvt.js +518 -0
- package/dist/scour.d.ts +90 -0
- package/dist/scour.js +153 -0
- package/dist/sculpt.d.ts +103 -0
- package/dist/sculpt.js +361 -0
- package/dist/settings-B9BSdnhD.js +72 -0
- package/dist/settings.d.ts +81 -0
- package/dist/settings.js +152 -0
- package/dist/shader.d.ts +176 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/src-Cr7Gm4n0.js +1366 -0
- package/dist/stream-window.d.ts +123 -0
- package/dist/stream.d.ts +169 -0
- package/dist/stream.js +531 -0
- package/dist/surface-height.d.ts +16 -0
- package/dist/wet-ground.d.ts +82 -0
- package/dist/wet-ground.js +147 -0
- package/package.json +173 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph, type PassContext } from '@margoengine/core/extension';
|
|
4
|
+
export { clipmapGrid } from './grid.js';
|
|
5
|
+
export { DRY_SAND, TerrainColors, wetSandAfter, type SandSurface, type TerrainColorOptions, type TerrainLook, type TerrainLookUpdate, } from './colors.js';
|
|
6
|
+
export { heightfield, heightfieldGeometry, TerrainHeightmap, type HeightField, type HeightfieldOptions, } from './heightfield.js';
|
|
7
|
+
type TerrainPassContext = Extract<PassContext, {
|
|
8
|
+
backend: 'webgpu';
|
|
9
|
+
}>;
|
|
10
|
+
/** The textures `TerrainOptions.textures` returns; Terrain owns and destroys them. All sampled, single-sample 2D. */
|
|
11
|
+
export interface TerrainTextures {
|
|
12
|
+
/** Heights in the R channel: `r32float`, `rgba16float` or `rgba8unorm`, one layer. */
|
|
13
|
+
heightmap: GPUTexture;
|
|
14
|
+
/** Optional XYZ normals encoded 0..1: `rgba8unorm` or `rgba16float`. Default: from neighbouring heights. */
|
|
15
|
+
normalmap?: GPUTexture;
|
|
16
|
+
/** Layer colours, one array layer per `TerrainLayer`: `rgba8unorm-srgb`, `rgba8unorm` or `rgba16float`. */
|
|
17
|
+
layers: GPUTexture;
|
|
18
|
+
/**
|
|
19
|
+
* Optional painted layer weights over the heightmap's bounds, 2 to 4 `rgba8unorm` 2D textures, enough for the layers
|
|
20
|
+
* (4 per texture, up to 16): layer i's weight is channel i % 4 of texture i / 4 (pass the same texture twice for 4
|
|
21
|
+
* layers). When present they replace the height and slope rules (each rule's `scale` still sets its texture repeat),
|
|
22
|
+
* as `@margoengine/biomes/view` uses for biome ground layers; a layer whose weight is 0 at a pixel skips its texture
|
|
23
|
+
* samples there. Without them the terrain shader and bindings are unchanged. Default: none.
|
|
24
|
+
*/
|
|
25
|
+
weights?: readonly GPUTexture[];
|
|
26
|
+
/**
|
|
27
|
+
* Optional tint over the heightmap's bounds, with `weights` only: one `rgba8unorm` or `rgba8unorm-srgb` 2D texture of
|
|
28
|
+
* any size, sampled bilinearly. Each layer with `tint: true` has its colour mixed toward the texel's RGB by its A:
|
|
29
|
+
* `mix(layer, tint.rgb, tint.a)`, in linear light (an `-srgb` texture holds sRGB colours). Layers without `tint` are
|
|
30
|
+
* drawn as they are. `@margoengine/biomes/view` draws seasons through it. Without it the shader and bindings are
|
|
31
|
+
* unchanged. Default: none.
|
|
32
|
+
*/
|
|
33
|
+
tint?: GPUTexture;
|
|
34
|
+
/**
|
|
35
|
+
* Optional holes over the heightmap's bounds: one `r8unorm` or `rgba8unorm` 2D texture of any size, a texel per
|
|
36
|
+
* cell (texel (u, v) covers cell (u, v) of the bounds split into its width by height); where its R is below 0.5 the
|
|
37
|
+
* terrain draws nothing, in its colour, SSR capture, shadow and occluder passes alike. `TerrainHoles`
|
|
38
|
+
* (`@margoengine/terrain/holes`) makes and edits one. Without it the shaders and bindings are unchanged. Default: none.
|
|
39
|
+
*/
|
|
40
|
+
holes?: GPUTexture;
|
|
41
|
+
/**
|
|
42
|
+
* Optional rock structure over the heightmap's bounds, read with `TerrainOptions.rock`: one `rgba8unorm` 2D texture of
|
|
43
|
+
* any size, sampled bilinearly. R is drainage, 0 to 1 (gullies and couloirs are carved along it), G loose debris such
|
|
44
|
+
* as talus fans below rock walls, 0 to 1. `@margoengine/biomes/hydrology` `drainageStructure` makes the values from a
|
|
45
|
+
* height grid. Without `rock` it is not read. Default: none (rock draws ledges and facets only).
|
|
46
|
+
*/
|
|
47
|
+
structure?: GPUTexture;
|
|
48
|
+
/** More data the terrain's shaders read, such as a stream's far ring (`TerrainStreamOptions.far`). Default: none. */
|
|
49
|
+
source?: TerrainSource;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Data added to every terrain shader (colour, capture, shadow, occluder and surface-height passes alike), in bind group
|
|
53
|
+
* 0 after the terrain's own bindings. `@margoengine/terrain/far` makes one for a streamed terrain.
|
|
54
|
+
*/
|
|
55
|
+
export interface TerrainSource {
|
|
56
|
+
/** The layout entries, bound from `binding` on. */
|
|
57
|
+
entries(binding: number): GPUBindGroupLayoutEntry[];
|
|
58
|
+
/** The resources of those entries, in binding order. */
|
|
59
|
+
resources(): GPUBindingResource[];
|
|
60
|
+
/** Returns `code`, a terrain shader, reading the data bound from `binding` on. */
|
|
61
|
+
shader(code: string, binding: number): string;
|
|
62
|
+
}
|
|
63
|
+
/** A splat rule: where layer i's texture shows. Rules blend smoothly around their bounds; layer 0 is the fallback. */
|
|
64
|
+
export interface TerrainLayer {
|
|
65
|
+
/** World Y range [min, max), min < max. */
|
|
66
|
+
height: readonly [number, number];
|
|
67
|
+
/** Slope range (1 - normal.y), 0 <= min < max <= 1. */
|
|
68
|
+
slope: readonly [number, number];
|
|
69
|
+
/** Texture repeats per world unit, > 0. */
|
|
70
|
+
scale: number;
|
|
71
|
+
/**
|
|
72
|
+
* Relief from the layer's own texture, 0 to 1: its luma becomes a height of up to `bump` halves of one texture repeat
|
|
73
|
+
* (2 m at scale 0.25), and the shading normal follows that height's slope (derivative bump mapping, Mikkelsen 2010).
|
|
74
|
+
* Where a pixel covers less than 1/128 of a repeat (3 cm at `scale` 0.25) the height is the layer's 128-texel mip
|
|
75
|
+
* through a cubic filter (16 texel loads a projection), so a magnified texture draws no speckle or facets. The mips
|
|
76
|
+
* flatten it with distance, and its slope falls once a pixel covers over 1/64 of a repeat on the ground.
|
|
77
|
+
* Default 0 (no bump).
|
|
78
|
+
*/
|
|
79
|
+
bump?: number;
|
|
80
|
+
/**
|
|
81
|
+
* Large-scale variation, 0 to 1: the same layer at 1/24 of `scale` (96 m at 0.25) scales the colour by
|
|
82
|
+
* `1 - macro + macro * luma / meanLuma`, so far slopes keep a pattern where the fine texture has averaged to its mean.
|
|
83
|
+
* A smooth sample about 318 m wide picks that repeat's offset, so it draws no grid. The mean comes from the layer's
|
|
84
|
+
* top mip: give the layers texture a full mip chain.
|
|
85
|
+
* Default 0 (no macro sample).
|
|
86
|
+
*/
|
|
87
|
+
macro?: number;
|
|
88
|
+
/** Whether the layer takes `TerrainTextures.tint` (plant cover, not rock). Ignored without a tint. Default false. */
|
|
89
|
+
tint?: boolean;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* What `TerrainOptions.floatingOrigin` takes: the float64 world point at local zero and its rebase event. A
|
|
93
|
+
* `FloatingOrigin` from `@margoengine/cameras/origin` fits as is.
|
|
94
|
+
*/
|
|
95
|
+
export interface TerrainOrigin {
|
|
96
|
+
/** Float64 world position of local [0, 0, 0]. */
|
|
97
|
+
readonly origin: ArrayLike<number>;
|
|
98
|
+
/** Calls `listener` after each rebase with the new origin second; returns an unsubscribe. */
|
|
99
|
+
onRebase(listener: (shift: ArrayLike<number>, origin: ArrayLike<number>) => void): () => void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The ring corners of a clipmap: for each level i, the eye's float64 world XZ snapped down to the level's spacing
|
|
103
|
+
* (`cell * 2^i`), less `shift` (the world point at local zero), as [x0, z0, x1, z1, ...]. Snapping the world point keeps
|
|
104
|
+
* the lattice, and so the vertices and the LOD, the same wherever the origin sits; only the small local result reaches
|
|
105
|
+
* float32. Exported for the tests.
|
|
106
|
+
*/
|
|
107
|
+
export declare function planRings(eye: ArrayLike<number>, shift: ArrayLike<number>, cell: number, levels: number, out?: Float64Array<ArrayBuffer>): Float64Array;
|
|
108
|
+
/** Clipmap terrain settings, fixed at construction. Invalid values throw RangeError. */
|
|
109
|
+
export interface TerrainOptions {
|
|
110
|
+
/** Creates the textures, at attach and again after device restoration (recreate them from retained data). */
|
|
111
|
+
textures: (device: GPUDevice) => TerrainTextures;
|
|
112
|
+
/** 4 to 16 splat rules, one per `layers` texture layer. */
|
|
113
|
+
layers: readonly TerrainLayer[];
|
|
114
|
+
/** World XZ of the heightmap's minimum corner. Default [0, 0]. */
|
|
115
|
+
origin?: readonly [number, number];
|
|
116
|
+
/** World XZ extent the heightmap covers, each > 0. */
|
|
117
|
+
size: readonly [number, number];
|
|
118
|
+
/** World Y per height sample unit. Default 100. */
|
|
119
|
+
heightScale?: number;
|
|
120
|
+
/** World Y added to every height. Default 0. */
|
|
121
|
+
heightOffset?: number;
|
|
122
|
+
/** Finest grid spacing in world units, > 0; doubles per level. Default 1. */
|
|
123
|
+
cellSize?: number;
|
|
124
|
+
/** Cells per ring side, a multiple of 4 from 8 to 256. Default 64. */
|
|
125
|
+
resolution?: number;
|
|
126
|
+
/** Clipmap rings, 1-12. Default 6. */
|
|
127
|
+
levels?: number;
|
|
128
|
+
/** World XZ centre for the rings. Defaults to the main camera's initial world XZ and stays fixed. Use `'camera'`
|
|
129
|
+
* for an unbounded route that needs to select a moving window on the world lattice. */
|
|
130
|
+
readonly clipmapAnchor?: readonly [number, number] | 'camera';
|
|
131
|
+
/** Rasterize the drawn clipmap heights before vegetation placement. Default false. */
|
|
132
|
+
readonly captureSurfaceHeight?: boolean;
|
|
133
|
+
/** Surface roughness (GGX, 0-1), for the sun's highlight and screen-space reflections (`ssr.add(terrain)`). Default
|
|
134
|
+
* 0.9; leaving it out keeps the shader as it was. */
|
|
135
|
+
roughness?: number;
|
|
136
|
+
/**
|
|
137
|
+
* A floating origin (`FloatingOrigin` from `@margoengine/cameras/origin`) for terrain far from world zero: the camera
|
|
138
|
+
* is local to it and the terrain plans its rings, layer texture phases and cache windows in float64 world space, then
|
|
139
|
+
* sends the GPU only small local numbers. A terrain 10,000 km out then draws the same vertices, levels and texels as
|
|
140
|
+
* at zero, and a rebase moves nothing on screen. `origin`, `size` and every CPU call (`TerrainQuery`, holes, decals,
|
|
141
|
+
* overlays, sculpt) stay in world coordinates. Default: none (the shaders, uniforms and rules stay as they were).
|
|
142
|
+
*/
|
|
143
|
+
floatingOrigin?: TerrainOrigin;
|
|
144
|
+
/**
|
|
145
|
+
* With `TerrainTextures.weights` only: resolves layers that share a texel into patches with crisp edges instead of
|
|
146
|
+
* their average, the way an alpine slope is turf, scree and rock side by side. Each layer gets its own value noise
|
|
147
|
+
* of `size`-metre cells (default 14) times `contrast` (0 to 1, default 0.6) added to its weight, and only the layers
|
|
148
|
+
* near the highest sum show (height-based splatting); weights of 0 and 1 stay as painted, and far ground, where a
|
|
149
|
+
* pixel covers a cell, hands back to the painted weights. Default: none (the shader is unchanged).
|
|
150
|
+
* @example
|
|
151
|
+
* Terrain.attach(engine, { ...options, patches: { size: 18, contrast: 0.7 } });
|
|
152
|
+
*/
|
|
153
|
+
patches?: TerrainPatches;
|
|
154
|
+
/**
|
|
155
|
+
* Structure for bare rock at 10 to 100 m, where the heightmap draws a steep face as one smooth plane: ledges along
|
|
156
|
+
* beds, jointed facets that catch and lose the light, and (with `TerrainTextures.structure`) gullies carved along the
|
|
157
|
+
* drainage with debris fans below. Shading only: the normal and the colour's tone change, the heights do not. Every
|
|
158
|
+
* part fades in between `slope` degrees and fades out where a pixel covers a good share of it. Default: none (the
|
|
159
|
+
* shader is unchanged).
|
|
160
|
+
* @example
|
|
161
|
+
* Terrain.attach(engine, { ...options, rock: { strata: 8, facets: 20 } });
|
|
162
|
+
*/
|
|
163
|
+
rock?: TerrainRock;
|
|
164
|
+
}
|
|
165
|
+
/** `TerrainOptions.patches`. */
|
|
166
|
+
export interface TerrainPatches {
|
|
167
|
+
/** Patch cell in metres, 0.5 to 10,000. Default 14. */
|
|
168
|
+
readonly size?: number;
|
|
169
|
+
/** How far each layer's noise lifts it over the others, above 0 to 1. Default 0.6. */
|
|
170
|
+
readonly contrast?: number;
|
|
171
|
+
/**
|
|
172
|
+
* 0 to 1: displaces the patch noise by a broader noise and adds an octave of 3.3 cells, so patches come in clusters
|
|
173
|
+
* and strands of several sizes rather than one blob size. Default 0.
|
|
174
|
+
*/
|
|
175
|
+
readonly warp?: number;
|
|
176
|
+
/**
|
|
177
|
+
* Metres of height per contour band, 0 (none) or 0.5 to 1,000: patches stretch along the contours (turf banks, gravel
|
|
178
|
+
* strips). Default 0.
|
|
179
|
+
*/
|
|
180
|
+
readonly terraces?: number;
|
|
181
|
+
}
|
|
182
|
+
/** `TerrainOptions.rock`. Every field has a default. */
|
|
183
|
+
export interface TerrainRock {
|
|
184
|
+
/** Slope in degrees where the structure fades in, [start, full], 0 <= start < full <= 90. Default [28, 44]. */
|
|
185
|
+
readonly slope?: readonly [number, number];
|
|
186
|
+
/** Bed thickness in metres for ledges, 0 (none) or 0.5 to 500. Default 9. */
|
|
187
|
+
readonly strata?: number;
|
|
188
|
+
/** How far beds step into benches and risers, 0 to 1. Default 0.6. */
|
|
189
|
+
readonly ledges?: number;
|
|
190
|
+
/** Tilt of the beds: metres of rise per metre along x and z, each -4 to 4. Default [0, 0] (level beds). */
|
|
191
|
+
readonly dip?: readonly [number, number];
|
|
192
|
+
/** Joint block size in metres, 0 (none) or 0.5 to 500. Default 18. */
|
|
193
|
+
readonly facets?: number;
|
|
194
|
+
/** How far a block's face tilts from the slope, 0 to 1. Default 0.45. */
|
|
195
|
+
readonly relief?: number;
|
|
196
|
+
/** Gully depth in metres along `TerrainTextures.structure`'s drainage, 0 to 100. Default 6. */
|
|
197
|
+
readonly gullies?: number;
|
|
198
|
+
/** Tone variation between beds and blocks and in gullies, 0 to 1. Default 0.35. */
|
|
199
|
+
readonly tone?: number;
|
|
200
|
+
/**
|
|
201
|
+
* Relief in metres of the 5 to 50 m ground forms (felsenmeer, turf banks, solifluction lobes, frost hummocks) shaded
|
|
202
|
+
* into gentle ground below `slope`'s start, where a heightmap of several metres a texel is smooth and a repeating
|
|
203
|
+
* layer texture reads as streaks at grazing angles; 0 to 10. Three octaves of 48, 20 and 8 m tilt the shading
|
|
204
|
+
* normal and shade their crests and hollows, each fading out as the pixel's footprint nears its wavelength. Default 0
|
|
205
|
+
* (none).
|
|
206
|
+
*/
|
|
207
|
+
readonly undulation?: number;
|
|
208
|
+
/**
|
|
209
|
+
* The layer (index) that benches, gully floors below the faces and `structure` debris fans take, with
|
|
210
|
+
* `TerrainTextures.weights`. Default: none.
|
|
211
|
+
*/
|
|
212
|
+
readonly debris?: number;
|
|
213
|
+
}
|
|
214
|
+
/** What `setCapture` takes: `@margoengine/post` SSR's material capture hook (its `SSRCapture`), declared here so terrain
|
|
215
|
+
* does not import post. */
|
|
216
|
+
export interface TerrainCapture {
|
|
217
|
+
/** Module-scope WGSL for the lit shader `code`: `struct MargoSSR` and `fn margoSSR(n,v,base,metal,rough)->MargoSSR`. */
|
|
218
|
+
declarations(code: string): string;
|
|
219
|
+
/** Colour targets of the capture pass, in `MargoSSR` location order. */
|
|
220
|
+
readonly targets: readonly GPUColorTargetState[];
|
|
221
|
+
/** This frame's capture pass: these targets and a depth attachment of the context's format; ended by the owner. */
|
|
222
|
+
begin(): GPURenderPassEncoder;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* A lighting effect patched into the terrain shader, such as shadows or clustered lights (`terrain.addLighting`).
|
|
226
|
+
* Its bindings live in group 1.
|
|
227
|
+
*/
|
|
228
|
+
export interface TerrainLighting {
|
|
229
|
+
/** The engine the effect belongs to; a different engine is rejected. */
|
|
230
|
+
readonly owner?: Engine;
|
|
231
|
+
/** Returns `source` with the effect's WGSL added, using bind group `group`. */
|
|
232
|
+
shader(source: string, group: number): string;
|
|
233
|
+
/** The group-1 entries the patched shader reads, asked every frame. */
|
|
234
|
+
bindings(): GPUBindGroupEntry[];
|
|
235
|
+
/**
|
|
236
|
+
* Optional: runs on the whole lit shader after every effect's `shader`, in the same order, so an effect can adjust
|
|
237
|
+
* what effects attached after it added (displacement moves their sun shadow lookups). Returns the shader.
|
|
238
|
+
*/
|
|
239
|
+
finish?(source: string, group: number): string;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Geometry-clipmap terrain: concentric rings that follow the camera, heights sampled from a texture in the vertex
|
|
243
|
+
* shader, 4 to 16 slope/height texture layers with triplanar cliffs, GGX sun lighting. Works with `Shadows` and
|
|
244
|
+
* `Clustered` (`shadows.add(terrain)`, `clustered.add(terrain)`). `Terrain.attach(engine, options)`; WebGPU only.
|
|
245
|
+
* @throws RangeError from the constructor when an option is out of range.
|
|
246
|
+
* @throws TypeError when the textures have the wrong dimension, format, usage or layer count.
|
|
247
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`, `'attached'` when a lighting
|
|
248
|
+
* effect belongs to another engine.
|
|
249
|
+
*/
|
|
250
|
+
export declare class Terrain implements EnginePlugin {
|
|
251
|
+
readonly options: TerrainOptions;
|
|
252
|
+
readonly name: string;
|
|
253
|
+
shadowVersion: number;
|
|
254
|
+
disposed: boolean;
|
|
255
|
+
private engine?;
|
|
256
|
+
private device?;
|
|
257
|
+
private textures?;
|
|
258
|
+
private vertex?;
|
|
259
|
+
private center?;
|
|
260
|
+
private ring?;
|
|
261
|
+
private rules?;
|
|
262
|
+
private sampler?;
|
|
263
|
+
private layout?;
|
|
264
|
+
private shadowPipeline?;
|
|
265
|
+
/** Reversed-depth variant of the shadow pipeline for local light maps, built on first use. */
|
|
266
|
+
/** The local shadow pipelines: reversed depth (`greater`) at 0, forward (`less-equal`) at 1. */
|
|
267
|
+
private localShadowPipelines;
|
|
268
|
+
private shadowModule?;
|
|
269
|
+
private ringGroup?;
|
|
270
|
+
private occluder?;
|
|
271
|
+
private rings;
|
|
272
|
+
/** The bits each uniform buffer last received, so an unchanged frame uploads nothing. Buffers made anew start empty. */
|
|
273
|
+
private readonly sent;
|
|
274
|
+
private readonly bitViews;
|
|
275
|
+
private pipelines;
|
|
276
|
+
private lightingGroups;
|
|
277
|
+
private lighting;
|
|
278
|
+
/** Bind-group entries for the current draw. Lighting providers may return fresh wrappers; the group cache compares them. */
|
|
279
|
+
private readonly lightingEntries;
|
|
280
|
+
private centerCount;
|
|
281
|
+
private ringCount;
|
|
282
|
+
private capture?;
|
|
283
|
+
private captureCode;
|
|
284
|
+
private readonly disposers;
|
|
285
|
+
/** The float64 world point at local zero (zeros without `floatingOrigin`); `shift` reads it. */
|
|
286
|
+
private readonly shiftValues;
|
|
287
|
+
/** The shift the layer rules were last written with, and the scratch eye and ring plan of `prepare`. */
|
|
288
|
+
private readonly rulesShift;
|
|
289
|
+
/** The world eye the layer rules' `eyeWrap` was last written for (NaN: never), and the local eye of `prepare`. */
|
|
290
|
+
private readonly rulesEye;
|
|
291
|
+
private readonly eyeLocal;
|
|
292
|
+
private readonly eye;
|
|
293
|
+
private anchoredEye?;
|
|
294
|
+
private capturedSurface?;
|
|
295
|
+
private plan;
|
|
296
|
+
private unfollow?;
|
|
297
|
+
/**
|
|
298
|
+
* With `floatingOrigin`, `options.origin` as it was at construction (a stream moves its array): local + `Frame.anchor`
|
|
299
|
+
* is world XZ less this, the coordinate cache surfaces, overlays and imagery draw in. Undefined without the option.
|
|
300
|
+
*/
|
|
301
|
+
readonly anchor?: readonly [number, number];
|
|
302
|
+
constructor(options: TerrainOptions);
|
|
303
|
+
/**
|
|
304
|
+
* The float64 world point at local zero: the floating origin's, or zeros without `floatingOrigin`. Every terrain entry
|
|
305
|
+
* reads it per frame and compares it with the last one to see a rebase. Read only.
|
|
306
|
+
*/
|
|
307
|
+
get shift(): ArrayLike<number>;
|
|
308
|
+
/** World heights of the rendered triangles, with the heightmap's UVs. Available after the first captured frame. */
|
|
309
|
+
get surfaceHeightTexture(): GPUTexture | undefined;
|
|
310
|
+
static attach(instance: EngineKernel, options: TerrainOptions): Terrain;
|
|
311
|
+
private validate;
|
|
312
|
+
addLighting(effect: TerrainLighting): () => void;
|
|
313
|
+
invalidate(): void;
|
|
314
|
+
/**
|
|
315
|
+
* The heightmap texture the `textures` factory returned, while attached (undefined before and after disposal; a new
|
|
316
|
+
* one after device restoration). Terrain owns it. `TerrainQuery.read` (`@margoengine/terrain/query`) reads it back.
|
|
317
|
+
*/
|
|
318
|
+
get heightmap(): GPUTexture | undefined;
|
|
319
|
+
/** The painted weight textures the factory returned (as given, a texture may repeat), while attached; else undefined. */
|
|
320
|
+
get weights(): readonly GPUTexture[] | undefined;
|
|
321
|
+
/** The holes texture the factory returned, while attached; else undefined. `TerrainHoles.upload` writes into it. */
|
|
322
|
+
get holes(): GPUTexture | undefined;
|
|
323
|
+
setup(engine: Engine): void;
|
|
324
|
+
/**
|
|
325
|
+
* The layer rules: 8 floats a layer; 12 with `bump` or `macro` (surfaceStruct: bump metres per unit of luma, macro,
|
|
326
|
+
* macro scale); with `floatingOrigin` 16 more (originShader: `wrap` and `tone` per axis for the shift, `eyeWrap` and
|
|
327
|
+
* `eyeTone` for the eye) and the height bounds less the shift's Y, as positions reach the shader local.
|
|
328
|
+
*/
|
|
329
|
+
private ruleData;
|
|
330
|
+
registerPasses(graph: FrameGraph): void;
|
|
331
|
+
private prepare;
|
|
332
|
+
/** The lit shader: terrain's own (with `roughness`, when given) through every lighting effect. */
|
|
333
|
+
private shader;
|
|
334
|
+
/**
|
|
335
|
+
* The terrain's own shader, with its edge and hole tests and no lighting effect, with the bind group layout and the
|
|
336
|
+
* finest ring's group 0 it draws with, while attached: what `@margoengine/terrain/rvt` bakes the ground colour from.
|
|
337
|
+
*/
|
|
338
|
+
colorSource(): {
|
|
339
|
+
code: string;
|
|
340
|
+
layout: GPUBindGroupLayout;
|
|
341
|
+
group: GPUBindGroup;
|
|
342
|
+
} | undefined;
|
|
343
|
+
/** The terrain's own shader (with `roughness`, when given) and its edge and hole tests, before any effect. */
|
|
344
|
+
private base;
|
|
345
|
+
/**
|
|
346
|
+
* With `TerrainTextures.holes`: declares the mask after the weights and tint and adds its test to the edge test. With
|
|
347
|
+
* `TerrainTextures.source`: its shader additions, after the mask's binding.
|
|
348
|
+
*/
|
|
349
|
+
private clip;
|
|
350
|
+
/** The group-0 binding of `TerrainTextures.holes`: after the weights and tint. */
|
|
351
|
+
private holesBinding;
|
|
352
|
+
/** The first group-0 binding after the weights, tint, holes and structure: where a `source` binds. */
|
|
353
|
+
private sourceBinding;
|
|
354
|
+
/** `TerrainOptions.rock` resolved for the shader (the structure texture binds before any source), or undefined. */
|
|
355
|
+
private rockShader;
|
|
356
|
+
/** Whether any layer sets `bump` or `macro`; without, the shader and rules are what they were before both. */
|
|
357
|
+
private surface;
|
|
358
|
+
private drawRings;
|
|
359
|
+
/**
|
|
360
|
+
* Lets a capture hook (`@margoengine/post` SSR: `ssr.add(terrain)`) draw the terrain's material with
|
|
361
|
+
* `draw(c, 'capture', ...)`: its normal, layer colour and roughness through the hook's `margoSSR`, depth tested
|
|
362
|
+
* (greater-equal) and written. Returns a remover. Without a hook the terrain draws exactly as before.
|
|
363
|
+
*/
|
|
364
|
+
setCapture(hook: TerrainCapture): () => void;
|
|
365
|
+
/** Calls `callback` once when the terrain is disposed; returns a remover. */
|
|
366
|
+
onDispose(callback: () => void): () => void;
|
|
367
|
+
/** `mode` 'capture': draws the terrain into the capture hook's pass (`setCapture`); `color` and `depth` are unused (the
|
|
368
|
+
* hook's pass has its own). Does nothing without a hook or after disposal. */
|
|
369
|
+
draw(c: TerrainPassContext, mode: 'capture', color?: GPUTextureView, depth?: GPUTextureView): void;
|
|
370
|
+
/** Whether `camera`'s matrix or eye differs, as float32, from the one the rings were prepared with. */
|
|
371
|
+
private otherCamera;
|
|
372
|
+
private lightingGroup;
|
|
373
|
+
/**
|
|
374
|
+
* Draws the terrain's depth with the frame's camera into an occluder pass of @margoengine/culling
|
|
375
|
+
* (`parts.addOccluder(terrain)`): the rings the main pass draws, on the same uniforms.
|
|
376
|
+
*/
|
|
377
|
+
drawOccluder({ pass, samples }: {
|
|
378
|
+
pass: GPURenderPassEncoder;
|
|
379
|
+
samples: number;
|
|
380
|
+
}): void;
|
|
381
|
+
drawShadow({ context, pass, matrix, cascade, }: {
|
|
382
|
+
context: TerrainPassContext;
|
|
383
|
+
pass: GPURenderPassEncoder;
|
|
384
|
+
matrix: Float32Array;
|
|
385
|
+
cascade: number;
|
|
386
|
+
}): void;
|
|
387
|
+
/**
|
|
388
|
+
* Casts the terrain into a point, spot, directional or area light's map of clustered `LocalShadows` (pass the
|
|
389
|
+
* terrain to `LocalShadows.add`) or of Clustered's integrated `shadows` maps (`addShadowCaster`). The depth test is
|
|
390
|
+
* the draw's `depthCompare`: `greater` (reversed depth, LocalShadows and the default) or `less-equal` (the integrated
|
|
391
|
+
* maps). One uniform set per ring, layer and depth convention, made on first use, so both systems' layers of one
|
|
392
|
+
* frame never share one.
|
|
393
|
+
*/
|
|
394
|
+
drawLocalShadow({ context, pass, matrix, layer, depthCompare, }: {
|
|
395
|
+
context: TerrainPassContext;
|
|
396
|
+
pass: GPURenderPassEncoder;
|
|
397
|
+
matrix: Float32Array;
|
|
398
|
+
layer: number;
|
|
399
|
+
depthCompare?: 'greater' | 'less-equal';
|
|
400
|
+
}): void;
|
|
401
|
+
/** Writes `values` to `buffer` unless the buffer already holds exactly these bits (compared bit for bit, so -0 and NaN count). */
|
|
402
|
+
private upload;
|
|
403
|
+
private release;
|
|
404
|
+
onDeviceRestored(engine: Engine): void;
|
|
405
|
+
dispose(): void;
|
|
406
|
+
[Symbol.dispose]: () => void;
|
|
407
|
+
destroy(): void;
|
|
408
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { o as DRY_SAND, t as Terrain, s as TerrainColors, r as TerrainHeightmap, l as clipmapGrid, i as heightfield, a as heightfieldGeometry, n as planRings, c as wetSandAfter } from "./src-Cr7Gm4n0.js";
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import type { Terrain, TerrainLighting } from './index.js';
|
|
5
|
+
/** One layer's material settings, in the order of `TerrainOptions.layers`. Every field has a default. */
|
|
6
|
+
export interface TerrainLayerMaterial {
|
|
7
|
+
/** How strongly the normal map tilts the shading normal, 0 to 2. Default 1 (as authored). */
|
|
8
|
+
normal?: number;
|
|
9
|
+
/** How much of the map's ambient occlusion applies, 0 to 1. Default 1. */
|
|
10
|
+
occlusion?: number;
|
|
11
|
+
/** Multiplies the map's roughness, 0 to 2 (the result is clamped to 0.02 to 1). Default 1. */
|
|
12
|
+
roughness?: number;
|
|
13
|
+
}
|
|
14
|
+
/** The textures `TerrainMaterialOptions.textures` returns; the materials own and destroy them. */
|
|
15
|
+
export interface TerrainMaterialTextures {
|
|
16
|
+
/**
|
|
17
|
+
* Tangent-space normal maps, one array layer per terrain layer, XYZ encoded 0..1 with +Y up (the OpenGL convention,
|
|
18
|
+
* Poly Haven's `nor_gl`): `rgba8unorm` or `rgba16float`, the size of the layers' colour texture.
|
|
19
|
+
*/
|
|
20
|
+
normals: GPUTexture;
|
|
21
|
+
/**
|
|
22
|
+
* Surface maps, one array layer per terrain layer: R ambient occlusion, G roughness (glTF's ORM order; Poly Haven's
|
|
23
|
+
* `arm`), B unused, A height 0..1 (Poly Haven's `disp`): `rgba8unorm` or `rgba16float`.
|
|
24
|
+
*/
|
|
25
|
+
surface: GPUTexture;
|
|
26
|
+
}
|
|
27
|
+
/** Settings of {@link TerrainMaterials.attach}. Invalid values throw RangeError. */
|
|
28
|
+
export interface TerrainMaterialOptions {
|
|
29
|
+
/** Creates the textures, at attachment and again after device restoration (recreate them from retained data). */
|
|
30
|
+
textures: (device: GPUDevice) => TerrainMaterialTextures;
|
|
31
|
+
/** One entry per terrain layer, or fewer (the rest take the defaults). Default: all defaults. */
|
|
32
|
+
layers?: readonly TerrainLayerMaterial[];
|
|
33
|
+
/**
|
|
34
|
+
* Height blending where layers' weights overlap, 0 to 1: 0 crossfades by weight as the terrain does; 1 lets the
|
|
35
|
+
* higher surface win almost outright (weights scaled by 2^(8 (height - 1))). Default 0.5.
|
|
36
|
+
*/
|
|
37
|
+
heightBlend?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Full PBR layer materials for a `Terrain`: per layer normal, ambient occlusion, roughness and height maps, triplanar
|
|
41
|
+
* like the layer colours, with height blending between layers. `TerrainMaterials.attach(engine, terrain, options)`;
|
|
42
|
+
* WebGPU only. Works with painted weights, `bump` and `macro`, shadows and clustered lights (they read its normal and
|
|
43
|
+
* roughness); SSR's capture keeps the terrain's own normal and roughness.
|
|
44
|
+
* @throws RangeError when an option is out of range.
|
|
45
|
+
* @throws TypeError when the textures have the wrong dimension, format, usage or layer count.
|
|
46
|
+
* @throws MargoError `'unsupported'` when the terrain's shader lacks the lines it patches, `'disposed'` after dispose.
|
|
47
|
+
*/
|
|
48
|
+
export declare class TerrainMaterials implements EnginePlugin, TerrainLighting {
|
|
49
|
+
private readonly terrain;
|
|
50
|
+
readonly options: TerrainMaterialOptions;
|
|
51
|
+
readonly name: string;
|
|
52
|
+
disposed: boolean;
|
|
53
|
+
owner?: Engine;
|
|
54
|
+
private device?;
|
|
55
|
+
private textures?;
|
|
56
|
+
private rules?;
|
|
57
|
+
private entries;
|
|
58
|
+
private remove?;
|
|
59
|
+
private readonly cache;
|
|
60
|
+
private constructor();
|
|
61
|
+
/** Attaches materials to `terrain`, which must already be attached to `engine`. Returns them; `dispose()` removes them. */
|
|
62
|
+
static attach(engine: EngineKernel, terrain: Terrain, options: TerrainMaterialOptions): TerrainMaterials;
|
|
63
|
+
setup(engine: Engine): void;
|
|
64
|
+
/**
|
|
65
|
+
* The surface maps texture the `textures` factory returned, while attached (undefined before and after disposal; a
|
|
66
|
+
* new one after device restoration). The materials own it. `TerrainDisplacement` (`@margoengine/terrain/displace`)
|
|
67
|
+
* reads its height channel.
|
|
68
|
+
*/
|
|
69
|
+
get surface(): GPUTexture | undefined;
|
|
70
|
+
/** The group-1 entries the patched shader reads. */
|
|
71
|
+
bindings(): GPUBindGroupEntry[];
|
|
72
|
+
/** The terrain's lit shader with the layer materials patched in (bindings in `group`). */
|
|
73
|
+
shader(source: string, group: number): string;
|
|
74
|
+
onDeviceRestored(engine: Engine): void;
|
|
75
|
+
private release;
|
|
76
|
+
dispose(): void;
|
|
77
|
+
[Symbol.dispose]: () => void;
|
|
78
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const e=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
5
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
6
|
+
//#region src/layers-pbr.ts
|
|
7
|
+
var n = 90, r = 0, i = class i {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
8
|
+
constructor(e, t) {
|
|
9
|
+
this.terrain = e, this.options = t, this.name = "terrain-materials:" + r++, this.disposed = !1, this.entries = [], this.cache = /* @__PURE__ */ new Map();
|
|
10
|
+
let n = e?.options?.layers?.length;
|
|
11
|
+
if (!n || typeof t?.textures != "function") throw TypeError("Invalid terrain materials; pass a Terrain and options with a textures factory");
|
|
12
|
+
let i = t.heightBlend ?? .5, a = t.layers ?? [];
|
|
13
|
+
if (!(i >= 0 && i <= 1) || !Array.isArray(a) || a.length > n || !a.every((e) => (e.normal ?? 1) >= 0 && (e.normal ?? 1) <= 2 && (e.occlusion ?? 1) >= 0 && (e.occlusion ?? 1) <= 1 && (e.roughness ?? 1) >= 0 && (e.roughness ?? 1) <= 2)) throw RangeError("Invalid terrain materials; give heightBlend 0..1 and at most one entry per terrain layer with normal 0..2, occlusion 0..1 and roughness 0..2.");
|
|
14
|
+
}
|
|
15
|
+
static attach(t, n, r) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
16
|
+
let a = new i(n, r);
|
|
17
|
+
return e(t).use(a), a;
|
|
18
|
+
}
|
|
19
|
+
setup(e) {
|
|
20
|
+
if (this.disposed || !e.gpu) throw new t(this.disposed ? "disposed" : "unsupported", "Terrain materials require WebGPU and a live module; attach new TerrainMaterials to a WebGPU engine");
|
|
21
|
+
this.owner = e, this.device = e.gpu;
|
|
22
|
+
let r = this.textures = this.options.textures(this.device), i = this.terrain.options.layers.length;
|
|
23
|
+
for (let e of [r?.normals, r?.surface]) if (!e || e.dimension !== "2d" || !(e.usage & GPUTextureUsage.TEXTURE_BINDING) || e.sampleCount !== 1 || e.depthOrArrayLayers !== i || !["rgba8unorm", "rgba16float"].includes(e.format)) throw TypeError(`Invalid terrain material textures; return normals and surface as rgba8unorm or rgba16float 2D arrays with ${i} layers, one per terrain layer`);
|
|
24
|
+
let a = /* @__PURE__ */ new Float32Array(64), o = 8 * (this.options.heightBlend ?? .5);
|
|
25
|
+
for (let e = 0; e < i; e++) {
|
|
26
|
+
let t = this.options.layers?.[e] ?? {};
|
|
27
|
+
a.set([
|
|
28
|
+
t.normal ?? 1,
|
|
29
|
+
t.occlusion ?? 1,
|
|
30
|
+
t.roughness ?? 1,
|
|
31
|
+
o
|
|
32
|
+
], e * 4);
|
|
33
|
+
}
|
|
34
|
+
this.rules = this.device.createBuffer({
|
|
35
|
+
size: 256,
|
|
36
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
37
|
+
}), this.device.queue.writeBuffer(this.rules, 0, a), this.entries = [
|
|
38
|
+
{
|
|
39
|
+
binding: n,
|
|
40
|
+
resource: r.normals.createView({ dimension: "2d-array" })
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
binding: 91,
|
|
44
|
+
resource: r.surface.createView({ dimension: "2d-array" })
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
binding: 92,
|
|
48
|
+
resource: { buffer: this.rules }
|
|
49
|
+
}
|
|
50
|
+
], this.remove ??= this.terrain.addLighting(this);
|
|
51
|
+
}
|
|
52
|
+
get surface() {
|
|
53
|
+
return this.textures?.surface;
|
|
54
|
+
}
|
|
55
|
+
bindings() {
|
|
56
|
+
return this.entries;
|
|
57
|
+
}
|
|
58
|
+
shader(e, n) {
|
|
59
|
+
let r = n + e, i = this.cache.get(r);
|
|
60
|
+
if (i) return i;
|
|
61
|
+
let a = "@fragment fn fragment(i:Output)->@location(0) vec4f {", s = "let n=normalize(i.normal);let base=terrainColor(i.position,n);", c = "let n=terrainRock(i.position,normalize(i.normal));let base=terrainColor(i.position,normalize(i.normal))*terrainRockShade;", l = "let n=terrainBump(i.position,terrainN);", u = "let weight=smoothstep(", d = "let weight=packed[layer/4u][layer%4u];if(weight<=0.0){continue;}", f = "+base*frame.radiance.w*frame.ambient.rgb";
|
|
62
|
+
if ([
|
|
63
|
+
a,
|
|
64
|
+
f,
|
|
65
|
+
"total+=weight;"
|
|
66
|
+
].some((t) => e.split(t).length !== 2) || (e.includes(s) || e.includes(c)) === e.includes(l) || e.includes(u) === e.includes(d)) throw new t("unsupported", "Terrain materials need the terrain's own lit shader; attach them before any effect that replaces it");
|
|
67
|
+
let p = "i.material.x=terrainPbrRoughness(i.material.x);", m = o(n), h = (e.includes("wrap:vec4f") ? m.split("position*s,").join("(position+layerRules[layer].wrap.xyz)*s,") : m) + e.replace(a, "fn terrainPbrShade(terrainIn:Output)->vec4f {var i=terrainIn;").replace(s, `let terrainN0=normalize(i.normal);let base=terrainColor(i.position,terrainN0);let n=terrainPbrNormal(terrainN0);${p}`).replace(c, `let terrainN0=terrainRock(i.position,normalize(i.normal));let base=terrainColor(i.position,normalize(i.normal))*terrainRockShade;let n=terrainPbrNormal(terrainN0);${p}`).replace(l, `let n=terrainPbrNormal(terrainBump(i.position,terrainN));${p}`).replace(u, "let weight=terrainPbrHeight(position,projection,dx,dy,layer)*smoothstep(").replace(d, "let painted=packed[layer/4u][layer%4u];if(painted<=0.0){continue;}let weight=painted*terrainPbrHeight(position,projection,dx,dy,layer);").replace("total+=weight;", "total+=weight;terrainPbrLayer(position,projection,dx,dy,layer,weight);").replace(f, f + "*terrainPbrOcclusion()");
|
|
68
|
+
return h += `\n${a}return terrainPbrShade(i);}\n`, this.cache.size >= 16 && this.cache.clear(), this.cache.set(r, h), h;
|
|
69
|
+
}
|
|
70
|
+
onDeviceRestored(e) {
|
|
71
|
+
this.release(), this.setup(e);
|
|
72
|
+
}
|
|
73
|
+
release() {
|
|
74
|
+
this.textures?.normals.destroy(), this.textures?.surface.destroy(), this.rules?.destroy(), this.textures = this.rules = void 0, this.entries = [];
|
|
75
|
+
}
|
|
76
|
+
dispose() {
|
|
77
|
+
this.disposed || (this.disposed = !0, this.remove?.(), this.remove = void 0, this.owner?.sceneOf(this)?.removePlugin(this.name), this.release(), this.cache.clear(), this.owner = this.device = void 0);
|
|
78
|
+
}
|
|
79
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
80
|
+
this.dispose();
|
|
81
|
+
}
|
|
82
|
+
}, a = (e, t) => `
|
|
83
|
+
fn ${t}(p:vec3f,q:vec3f,dx:vec3f,dy:vec3f,s:f32,layer:u32)->array<vec4f,3> {
|
|
84
|
+
var r=array<vec4f,3>(vec4f(0),vec4f(0),vec4f(0));
|
|
85
|
+
if(q.x>0.0){r[0]=textureSampleGrad(${e},terrainSampler,p.zy,i32(layer),dx.zy*s,dy.zy*s);}
|
|
86
|
+
if(q.y>0.0){r[1]=textureSampleGrad(${e},terrainSampler,p.xz,i32(layer),dx.xz*s,dy.xz*s);}
|
|
87
|
+
if(q.z>0.0){r[2]=textureSampleGrad(${e},terrainSampler,p.xy,i32(layer),dx.xy*s,dy.xy*s);}
|
|
88
|
+
return r;
|
|
89
|
+
}`, o = (e) => `
|
|
90
|
+
@group(${e}) @binding(${n}) var terrainPbrNormals:texture_2d_array<f32>;
|
|
91
|
+
@group(${e}) @binding(91) var terrainPbrSurfaces:texture_2d_array<f32>;
|
|
92
|
+
@group(${e}) @binding(92) var<uniform> terrainPbrRules:array<vec4f,16>;
|
|
93
|
+
var<private> terrainPbrGradient:vec4f;var<private> terrainPbrSurface:vec3f;var<private> terrainPbrLast:vec4f;
|
|
94
|
+
${a("terrainPbrNormals", "terrainPbrTriN")}${a("terrainPbrSurfaces", "terrainPbrTriS")}
|
|
95
|
+
fn terrainPbrHeight(position:vec3f,projection:vec3f,dx:vec3f,dy:vec3f,layer:u32)->f32 {
|
|
96
|
+
let s=layerRules[layer].params.x;let t=terrainPbrTriS(position*s,projection,dx,dy,s,layer);
|
|
97
|
+
terrainPbrLast=t[0]*projection.x+t[1]*projection.y+t[2]*projection.z;
|
|
98
|
+
return exp2(terrainPbrRules[layer].w*(terrainPbrLast.a-1.0));
|
|
99
|
+
}
|
|
100
|
+
fn terrainPbrLayer(position:vec3f,projection:vec3f,dx:vec3f,dy:vec3f,layer:u32,weight:f32) {
|
|
101
|
+
if(weight<=0.0){return;}
|
|
102
|
+
let r=terrainPbrRules[layer];let s=layerRules[layer].params.x;
|
|
103
|
+
let t=terrainPbrTriN(position*s,projection,dx,dy,s,layer);
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
var g=vec3f(0);
|
|
107
|
+
if(projection.x>0.0){let m=t[0].xyz*2.0-1.0;let d=vec2f(-m.x,m.y)/max(m.z,0.05);g+=projection.x*vec3f(0.0,d.y,d.x);}
|
|
108
|
+
if(projection.y>0.0){let m=t[1].xyz*2.0-1.0;let d=vec2f(-m.x,m.y)/max(m.z,0.05);g+=projection.y*vec3f(d.x,0.0,d.y);}
|
|
109
|
+
if(projection.z>0.0){let m=t[2].xyz*2.0-1.0;let d=vec2f(-m.x,m.y)/max(m.z,0.05);g+=projection.z*vec3f(d.x,d.y,0.0);}
|
|
110
|
+
terrainPbrGradient+=vec4f(g*r.x*weight,weight);
|
|
111
|
+
terrainPbrSurface+=vec3f(mix(1.0,terrainPbrLast.r,r.y),terrainPbrLast.g*r.z,0.0)*weight;
|
|
112
|
+
}
|
|
113
|
+
fn terrainPbrNormal(n:vec3f)->vec3f {
|
|
114
|
+
if(terrainPbrGradient.w<=0.00001){return n;}
|
|
115
|
+
let g=terrainPbrGradient.xyz/terrainPbrGradient.w;let surface=g-dot(g,n)*n;let tilted=n-surface;
|
|
116
|
+
return select(n,normalize(tilted),dot(tilted,tilted)>0.0);
|
|
117
|
+
}
|
|
118
|
+
fn terrainPbrRoughness(fallback:f32)->f32 {
|
|
119
|
+
if(terrainPbrGradient.w<=0.00001){return fallback;}
|
|
120
|
+
return clamp(terrainPbrSurface.y/terrainPbrGradient.w,0.02,1.0);
|
|
121
|
+
}
|
|
122
|
+
fn terrainPbrOcclusion()->f32 {
|
|
123
|
+
if(terrainPbrGradient.w<=0.00001){return 1.0;}
|
|
124
|
+
return terrainPbrSurface.x/terrainPbrGradient.w;
|
|
125
|
+
}
|
|
126
|
+
`;
|
|
127
|
+
//#endregion
|
|
128
|
+
export { i as TerrainMaterials };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/lighting-layout.ts
|
|
2
|
+
var e = /@group\((\d+)\)\s*@binding\((\d+)\)\s*var(?:<([^>]*)>)?\s+(\w+)\s*:\s*([^;]+);/g, t = /^texture_(2d_array|2d|3d|cube_array|cube)<(f32|i32|u32)>$/, n = /^texture_depth_(2d_array|2d|cube_array|cube)$/, r = (e) => e.replace("_", "-");
|
|
3
|
+
function i(e, i, a, o) {
|
|
4
|
+
if (i === "uniform") return { buffer: { type: "uniform" } };
|
|
5
|
+
if (i.startsWith("storage")) return { buffer: { type: i.includes("read_write") ? "storage" : "read-only-storage" } };
|
|
6
|
+
if (o === "sampler_comparison") return { sampler: { type: "comparison" } };
|
|
7
|
+
if (o === "sampler") return { sampler: { type: "filtering" } };
|
|
8
|
+
let s = n.exec(o);
|
|
9
|
+
if (s) return { texture: {
|
|
10
|
+
sampleType: "depth",
|
|
11
|
+
viewDimension: r(s[1])
|
|
12
|
+
} };
|
|
13
|
+
let c = t.exec(o);
|
|
14
|
+
if (!c) throw TypeError(`Terrain lighting declares ${a} as ${o}, which a lighting group cannot hold; declare a sampled texture, sampler or buffer`);
|
|
15
|
+
let l = new RegExp(String.raw`textureSample\w*\(\s*${a}\b`).test(e);
|
|
16
|
+
return { texture: {
|
|
17
|
+
sampleType: c[2] === "f32" ? l ? "float" : "unfilterable-float" : c[2] === "u32" ? "uint" : "sint",
|
|
18
|
+
viewDimension: r(c[1])
|
|
19
|
+
} };
|
|
20
|
+
}
|
|
21
|
+
function a(t, n, r = 1) {
|
|
22
|
+
let a = /* @__PURE__ */ new Map();
|
|
23
|
+
for (let [, n, o, s = "", c, l] of t.matchAll(e)) +n === r && a.set(+o, i(t, s.replace(/\s/g, ""), c, l.trim()));
|
|
24
|
+
return n.flatMap(({ binding: e }) => {
|
|
25
|
+
let t = a.get(e);
|
|
26
|
+
return t ? [{
|
|
27
|
+
binding: e,
|
|
28
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
29
|
+
...t
|
|
30
|
+
}] : [];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { a as t };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The layout entries of `entries`, each as `code` declares it at `group` (default 1). One it does not declare has no entry, so
|
|
3
|
+
* WebGPU's `createBindGroup` names it as a validation error.
|
|
4
|
+
*/
|
|
5
|
+
export declare function lightingLayout(code: string, entries: readonly GPUBindGroupEntry[], group?: number): GPUBindGroupLayoutEntry[];
|