@margoengine/terrain 0.0.0-stage → 0.1.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 +1808 -3
- package/dist/blend.d.ts +70 -0
- package/dist/blend.js +159 -0
- package/dist/colors.d.ts +164 -0
- package/dist/deform.d.ts +103 -0
- package/dist/deform.js +322 -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/errors-fc4i1pSL.js +7 -0
- package/dist/grid.d.ts +11 -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 +1291 -0
- package/dist/ground-model.d.ts +290 -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 +297 -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-craters.d.ts +55 -0
- package/dist/planet-recipes.d.ts +75 -0
- package/dist/planet-recipes.js +216 -0
- package/dist/planet.d.ts +139 -0
- package/dist/planet.js +602 -0
- package/dist/query.d.ts +150 -0
- package/dist/query.js +243 -0
- package/dist/relief-lighting.d.ts +46 -0
- package/dist/relief-lighting.js +208 -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 +111 -0
- package/dist/rock-surface.js +368 -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/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 +61 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/src-C6dtSaAb.js +1155 -0
- package/dist/stream.d.ts +100 -0
- package/dist/stream.js +339 -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 +148 -6
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { Terrain, TerrainLighting } from './index.js';
|
|
5
|
+
import type { PlanetTerrain } from './planet.js';
|
|
6
|
+
import type { TerrainVirtualTexture } from './rvt.js';
|
|
7
|
+
/** Rectangle [minimum X, minimum Z, maximum X, maximum Z], or planet [west,south,east,north] in degrees. */
|
|
8
|
+
export type ImageryBounds = readonly [number, number, number, number];
|
|
9
|
+
/** A north-origin tile address. */
|
|
10
|
+
export interface ImageryTile {
|
|
11
|
+
/** Zoom. */ level: number /** Column. */;
|
|
12
|
+
x: number /** Row. */;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
/** Tiled imagery, decoded in the browser; a custom loader may return an ImageBitmap or RGBA bytes. */
|
|
16
|
+
export interface TiledImagerySource {
|
|
17
|
+
/** Tile URL, with optional {z}, {x}, {y} placeholders, or a URL function. */
|
|
18
|
+
url: string | ((tile: ImageryTile) => string);
|
|
19
|
+
/** Tile side in pixels. Default 256. */ tileSize?: number;
|
|
20
|
+
/** Projection on planets. Default mercator. */ projection?: 'mercator' | 'geographic';
|
|
21
|
+
/** Columns at level zero (geographic WMTS commonly uses 2). Default 1. */ columns?: number;
|
|
22
|
+
/** Lowest available zoom. Default 0. */ minLevel?: number;
|
|
23
|
+
/** Highest available zoom. Default 18. */ maxLevel?: number;
|
|
24
|
+
/** Credit retained by the collection for display by the application. */ attribution?: string;
|
|
25
|
+
/** Custom authenticated or cached fetch/decode, honoring signal; byte data is straight RGBA in sRGB. */
|
|
26
|
+
load?: (tile: ImageryTile, signal: AbortSignal) => Promise<ImageBitmap | Uint8Array>;
|
|
27
|
+
}
|
|
28
|
+
/** A borrowed GPU texture. The factory runs again on restoration; the collection never destroys it. */
|
|
29
|
+
export interface TextureImagerySource {
|
|
30
|
+
/** A single-layer filterable RGBA 2D texture with TEXTURE_BINDING. */ texture: (device: GPUDevice) => GPUTexture;
|
|
31
|
+
/** Credit for application display. */ attribution?: string;
|
|
32
|
+
}
|
|
33
|
+
/** Source of one imagery layer. */
|
|
34
|
+
export type ImagerySource = TiledImagerySource | TextureImagerySource;
|
|
35
|
+
/** Live display properties; call set() to apply a change. */
|
|
36
|
+
export interface ImageryLayerOptions {
|
|
37
|
+
/** Source; fixed for the lifetime of this layer. */ source: ImagerySource;
|
|
38
|
+
/** Clip bounds, also the texture extent for GPU sources. Default the terrain bounds, or the whole planet. */ bounds?: ImageryBounds;
|
|
39
|
+
/** Straight alpha multiplier 0..1. Default 1. */ alpha?: number;
|
|
40
|
+
/** Linear-light brightness multiplier, 0 or greater. Default 1. */ brightness?: number;
|
|
41
|
+
/** Visibility. Default true. */ show?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Fixed collection options. */
|
|
44
|
+
export interface TerrainImageryOptions {
|
|
45
|
+
/** Bake imagery through this borrowed clipmap RVT, before later surface providers such as vector overlays. Default none. */
|
|
46
|
+
virtualTexture?: TerrainVirtualTexture;
|
|
47
|
+
/** Clipmap extent corresponding to the full tile pyramid. Default terrain origin/size. */ bounds?: ImageryBounds;
|
|
48
|
+
/** Maximum layers, 1..8 (also subject to device resource limits). Default 4. */ maxLayers?: number;
|
|
49
|
+
/** Resident tiles per layer, 4..256. Default 64. */ maxTiles?: number;
|
|
50
|
+
/** Simultaneous requests per collection, 1..16. Default 4. */ concurrency?: number;
|
|
51
|
+
/** Camera-driven tile selection. Default true; update() remains available for deterministic/offline use. */ automatic?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** WMTS GetTile parameters. REST templates may use {TileMatrix}, {TileRow}, {TileCol}. */
|
|
54
|
+
export interface WMTSImageryOptions extends Omit<TiledImagerySource, 'url'> {
|
|
55
|
+
/** Service URL or REST tile template. */ url: string;
|
|
56
|
+
/** Layer identifier. */ layer: string;
|
|
57
|
+
/** Tile matrix set identifier. */ matrixSet: string;
|
|
58
|
+
/** Explicit matrix identifiers in zoom order. Default decimal zoom strings. */ matrices?: readonly string[];
|
|
59
|
+
/** Style identifier. Default default. */ style?: string;
|
|
60
|
+
/** MIME type. Default image/png. */ format?: string;
|
|
61
|
+
/** Extra dimensions such as TIME, included in every request. */ dimensions?: Readonly<Record<string, string>>;
|
|
62
|
+
}
|
|
63
|
+
/** XYZ source using north-origin rows. No network work occurs before attachment/update. */
|
|
64
|
+
export declare function xyzImagery(url: string, options?: Omit<TiledImagerySource, 'url'>): TiledImagerySource;
|
|
65
|
+
/** OGC WMTS REST or KVP source, including named tile matrices, styles and dimensions. */
|
|
66
|
+
export declare function wmtsImagery(options: WMTSImageryOptions): TiledImagerySource;
|
|
67
|
+
/**
|
|
68
|
+
* Bounded nested tile windows with coarse parents first; rows clamp at the poles. Columns wrap in longitude, or with
|
|
69
|
+
* `wrap` false (a clipmap rectangle) clamp at the east and west edges.
|
|
70
|
+
*/
|
|
71
|
+
export declare function imageryTiles(center: readonly [number, number], level: number, capacity?: number, columns?: number, minLevel?: number, wrap?: boolean): ImageryTile[];
|
|
72
|
+
/** Ordered draped imagery on clipmap or planet terrain. No geometry offsets, extra terrain draws or core changes. */
|
|
73
|
+
export declare class TerrainImagery implements EnginePlugin, TerrainLighting, Disposable {
|
|
74
|
+
#private;
|
|
75
|
+
private readonly terrain;
|
|
76
|
+
readonly name: string;
|
|
77
|
+
/** Engine owning the collection while attached. */ owner?: Engine;
|
|
78
|
+
/** Whether resources and registration have been released. */ disposed: boolean;
|
|
79
|
+
/** Most recent asynchronous load error; update() also rejects with it. No automatic retries. */ error?: unknown;
|
|
80
|
+
constructor(terrain: Terrain | PlanetTerrain, options?: TerrainImageryOptions);
|
|
81
|
+
/** Attaches the collection to the same engine as its terrain. */
|
|
82
|
+
static attach(engine: EngineKernel, terrain: Terrain | PlanetTerrain, options?: TerrainImageryOptions): TerrainImagery;
|
|
83
|
+
/** Number of ordered layers. */ get length(): number;
|
|
84
|
+
/** Layer IDs from bottom to top. */ get order(): readonly number[];
|
|
85
|
+
/** Unique nonempty source credits for the application to display. */
|
|
86
|
+
get credits(): readonly string[];
|
|
87
|
+
/** Latest camera/update load, rejecting on a failed request. */ get ready(): Promise<void>;
|
|
88
|
+
/** Adds a layer at index (default top), returning its stable ID. */
|
|
89
|
+
add(options: ImageryLayerOptions, index?: number): number;
|
|
90
|
+
/** Changes bounds, alpha, brightness or visibility atomically. */
|
|
91
|
+
set(id: number, changes: Partial<Omit<ImageryLayerOptions, 'source'>>): void;
|
|
92
|
+
/** Moves a layer to index, 0 is bottom. */
|
|
93
|
+
move(id: number, index: number): void;
|
|
94
|
+
/** Removes a layer, cancels its requests and releases owned textures. */
|
|
95
|
+
remove(id: number): boolean;
|
|
96
|
+
setup(engine: Engine): void;
|
|
97
|
+
/** Loads nested windows about normalized XZ, or longitude/latitude (north=0) on planets, at zoom level. */
|
|
98
|
+
update(center?: readonly [number, number], level?: number, signal?: AbortSignal): Promise<void>;
|
|
99
|
+
/** Shader effect resources in the terrain's group 1. */
|
|
100
|
+
bindings(): GPUBindGroupEntry[];
|
|
101
|
+
/** Adds ordered linear-light alpha composition immediately before terrain lighting. */
|
|
102
|
+
shader(source: string, group: number): string;
|
|
103
|
+
registerPasses(graph: FrameGraph): void;
|
|
104
|
+
/** Recreates owned caches and reacquires borrowed textures, rejecting stale in-flight results. */
|
|
105
|
+
onDeviceRestored(engine: Engine): void;
|
|
106
|
+
/** Cancels requests, removes the effect/scene registration and releases only owned resources. */
|
|
107
|
+
dispose(): void;
|
|
108
|
+
[Symbol.dispose](): void;
|
|
109
|
+
}
|
package/dist/imagery.js
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./require-webgpu--tUXPsmA.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
import { MargoError as n } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/imagery.ts
|
|
8
|
+
var r = [
|
|
9
|
+
0,
|
|
10
|
+
0,
|
|
11
|
+
0
|
|
12
|
+
];
|
|
13
|
+
function i(e, t = {}) {
|
|
14
|
+
return {
|
|
15
|
+
...t,
|
|
16
|
+
url: e
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function a(e) {
|
|
20
|
+
let { url: t, layer: n, matrixSet: r, matrices: i, style: a = "default", format: o = "image/png", dimensions: s = {}, ...c } = e;
|
|
21
|
+
if (!t || !n || !r || i && !i.length) throw TypeError("Invalid WMTS source; give url, layer, matrixSet and nonempty matrices when supplied.");
|
|
22
|
+
return {
|
|
23
|
+
...c,
|
|
24
|
+
maxLevel: c.maxLevel ?? (i ? i.length - 1 : 18),
|
|
25
|
+
url: (e) => {
|
|
26
|
+
let c = {
|
|
27
|
+
...s,
|
|
28
|
+
Layer: n,
|
|
29
|
+
Style: a,
|
|
30
|
+
TileMatrixSet: r,
|
|
31
|
+
TileMatrix: i?.[e.level] ?? String(e.level),
|
|
32
|
+
TileRow: String(e.y),
|
|
33
|
+
TileCol: String(e.x)
|
|
34
|
+
};
|
|
35
|
+
if (t.includes("{Tile")) return t.replace(/\{([^}]+)\}/g, (e, t) => encodeURIComponent(c[t] ?? t));
|
|
36
|
+
let l = new URL(t);
|
|
37
|
+
for (let [e, t] of Object.entries({
|
|
38
|
+
SERVICE: "WMTS",
|
|
39
|
+
REQUEST: "GetTile",
|
|
40
|
+
VERSION: "1.0.0",
|
|
41
|
+
FORMAT: o,
|
|
42
|
+
...c
|
|
43
|
+
})) l.searchParams.set(e.toUpperCase(), t);
|
|
44
|
+
return l.href;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function o(e) {
|
|
49
|
+
if (!Array.isArray(e) || e.length !== 4 || !e.every(Number.isFinite) || e[0] >= e[2] || e[1] >= e[3]) throw RangeError("Invalid imagery bounds; give four finite increasing rectangle coordinates.");
|
|
50
|
+
return [
|
|
51
|
+
e[0],
|
|
52
|
+
e[1],
|
|
53
|
+
e[2],
|
|
54
|
+
e[3]
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
function s(e, t, n) {
|
|
58
|
+
return Number.isInteger(e) && e >= t && e <= n;
|
|
59
|
+
}
|
|
60
|
+
function c(e) {
|
|
61
|
+
return !("texture" in e);
|
|
62
|
+
}
|
|
63
|
+
function l(e) {
|
|
64
|
+
return `${e.level}/${e.x}/${e.y}`;
|
|
65
|
+
}
|
|
66
|
+
function u(e, t, n = 64, r = 1, i = 0, a = !0) {
|
|
67
|
+
if (e.length !== 2 || !e.every(Number.isFinite) || !s(t, 0, 22) || !s(i, 0, t) || !s(n, 4, 256) || !s(r, 1, 2)) throw RangeError("Invalid imagery window; give finite center, levels 0..22, capacity 4..256 and columns 1 or 2.");
|
|
68
|
+
let o = /* @__PURE__ */ new Map();
|
|
69
|
+
for (let s = t; s >= i; s--) {
|
|
70
|
+
let i = 2 ** s, c = i * r, u = a ? Math.floor(e[0] * c) : Math.min(c - 1, Math.max(0, Math.floor(e[0] * c))), d = Math.min(i - 1, Math.max(0, Math.floor(e[1] * i))), f = s === t ? [
|
|
71
|
+
0,
|
|
72
|
+
-1,
|
|
73
|
+
1,
|
|
74
|
+
-2,
|
|
75
|
+
2
|
|
76
|
+
] : [
|
|
77
|
+
0,
|
|
78
|
+
-1,
|
|
79
|
+
1
|
|
80
|
+
];
|
|
81
|
+
for (let e of f) for (let t of f) {
|
|
82
|
+
let f = d + e, p = a ? ((u + t) % c + c) % c : u + t;
|
|
83
|
+
if (f < 0 || f >= i || p < 0 || p >= c) continue;
|
|
84
|
+
let m = {
|
|
85
|
+
level: s,
|
|
86
|
+
x: p,
|
|
87
|
+
y: f
|
|
88
|
+
}, h = l(m);
|
|
89
|
+
!o.has(h) && o.size < n - r && o.set(h, m);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
let c = r * 2 ** i, u = Math.floor(e[0] * c), d = a ? u : Math.min(c - 1, Math.max(0, u)), f = Math.min(2 ** i - 1, Math.max(0, Math.floor(e[1] * 2 ** i)));
|
|
93
|
+
for (let e = 0; e < r; e++) {
|
|
94
|
+
let t = {
|
|
95
|
+
level: i,
|
|
96
|
+
x: ((!a && d + e >= c ? d - e : d + e) % c + c) % c,
|
|
97
|
+
y: f
|
|
98
|
+
};
|
|
99
|
+
o.set(l(t), t);
|
|
100
|
+
}
|
|
101
|
+
return [...o.values()].sort((e, t) => e.level - t.level || e.y - t.y || e.x - t.x);
|
|
102
|
+
}
|
|
103
|
+
var d = 0, f = class i {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
104
|
+
#e;
|
|
105
|
+
#t;
|
|
106
|
+
#n;
|
|
107
|
+
#r;
|
|
108
|
+
#i;
|
|
109
|
+
#a;
|
|
110
|
+
#o;
|
|
111
|
+
#s;
|
|
112
|
+
#c;
|
|
113
|
+
#l;
|
|
114
|
+
#u;
|
|
115
|
+
#d;
|
|
116
|
+
#f;
|
|
117
|
+
#p;
|
|
118
|
+
constructor(e, t = {}) {
|
|
119
|
+
if (this.terrain = e, this.name = "terrain-imagery:" + d++, this.disposed = !1, this.#e = [], this.#c = 0, this.#l = /* @__PURE__ */ new Map(), this.#u = "", this.#d = Promise.resolve(), this.#f = [.5, .5], this.#p = 0, !e || typeof e.addLighting != "function") throw TypeError("Invalid imagery terrain; pass Terrain or PlanetTerrain.");
|
|
120
|
+
if (this.#r = "radius" in e, this.#n = t.virtualTexture, this.#n && (this.#r || typeof this.#n.addSurface != "function")) throw TypeError("Imagery virtualTexture requires a clipmap RVT; use a clipmap terrain and live TerrainVirtualTexture.");
|
|
121
|
+
let n = "options" in e ? e.options : void 0, r = n?.origin ?? [0, 0];
|
|
122
|
+
if (this.#t = {
|
|
123
|
+
bounds: o(t.bounds ?? (this.#r ? [
|
|
124
|
+
-180,
|
|
125
|
+
-90,
|
|
126
|
+
180,
|
|
127
|
+
90
|
|
128
|
+
] : [
|
|
129
|
+
r[0],
|
|
130
|
+
r[1],
|
|
131
|
+
r[0] + n.size[0],
|
|
132
|
+
r[1] + n.size[1]
|
|
133
|
+
])),
|
|
134
|
+
maxLayers: t.maxLayers ?? 4,
|
|
135
|
+
maxTiles: t.maxTiles ?? 64,
|
|
136
|
+
concurrency: t.concurrency ?? 4,
|
|
137
|
+
automatic: t.automatic ?? !0
|
|
138
|
+
}, !s(this.#t.maxLayers, 1, 8) || !s(this.#t.maxTiles, 4, 256) || !s(this.#t.concurrency, 1, 16)) throw RangeError("Invalid imagery capacity; give maxLayers 1..8, maxTiles 4..256 and concurrency 1..16.");
|
|
139
|
+
}
|
|
140
|
+
static attach(e, n, r = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
141
|
+
let a = new i(n, r);
|
|
142
|
+
return t(e).use(a), a;
|
|
143
|
+
}
|
|
144
|
+
get length() {
|
|
145
|
+
return this.#e.length;
|
|
146
|
+
}
|
|
147
|
+
get order() {
|
|
148
|
+
return this.#e.map((e) => e.id);
|
|
149
|
+
}
|
|
150
|
+
get credits() {
|
|
151
|
+
return [...new Set(this.#e.map((e) => e.options.source.attribution).filter((e) => !!e))];
|
|
152
|
+
}
|
|
153
|
+
get ready() {
|
|
154
|
+
return this.#d;
|
|
155
|
+
}
|
|
156
|
+
#m() {
|
|
157
|
+
if (this.disposed) throw new n("disposed", "Imagery is disposed; attach a new collection.");
|
|
158
|
+
}
|
|
159
|
+
#h(e) {
|
|
160
|
+
let t = this.#e.find((t) => t.id === e);
|
|
161
|
+
if (!t) throw RangeError("Unknown imagery layer; use an ID returned by add().");
|
|
162
|
+
return t;
|
|
163
|
+
}
|
|
164
|
+
add(e, t = this.length) {
|
|
165
|
+
if (this.#m(), this.length === this.#t.maxLayers || !s(t, 0, this.length)) throw RangeError("Imagery collection is full or index invalid; remove a layer or use an index in 0..length.");
|
|
166
|
+
let n = e?.source;
|
|
167
|
+
if (!n || (c(n) ? typeof n.url != "string" && typeof n.url != "function" : typeof n.texture != "function")) throw TypeError("Invalid imagery source; give an XYZ/WMTS source or a texture factory.");
|
|
168
|
+
if (c(n) && (!s(n.tileSize ?? 256, 1, 4096) || !s(n.columns ?? 1, 1, 2) || !s(n.minLevel ?? 0, 0, 22) || !s(n.maxLevel ?? 18, n.minLevel ?? 0, 22) || !["mercator", "geographic"].includes(n.projection ?? "mercator"))) throw RangeError("Invalid tile source; give tileSize 1..4096, columns 1 or 2 and increasing levels in 0..22, with mercator or geographic projection.");
|
|
169
|
+
let r = {
|
|
170
|
+
id: this.#c++,
|
|
171
|
+
options: {
|
|
172
|
+
source: n,
|
|
173
|
+
bounds: this.#t.bounds,
|
|
174
|
+
alpha: 1,
|
|
175
|
+
brightness: 1,
|
|
176
|
+
show: !0
|
|
177
|
+
},
|
|
178
|
+
cache: /* @__PURE__ */ new Map(),
|
|
179
|
+
generation: 0,
|
|
180
|
+
owned: c(n)
|
|
181
|
+
};
|
|
182
|
+
return this.#g(r, e), this.#i && this.#_(r), this.#e.splice(t, 0, r), this.#v(), this.#x(), r.id;
|
|
183
|
+
}
|
|
184
|
+
#g(e, t) {
|
|
185
|
+
let n = {
|
|
186
|
+
...e.options,
|
|
187
|
+
...t,
|
|
188
|
+
bounds: o(t.bounds ?? e.options.bounds)
|
|
189
|
+
};
|
|
190
|
+
if (!(n.alpha >= 0 && n.alpha <= 1) || !Number.isFinite(n.brightness) || n.brightness < 0 || typeof n.show != "boolean") throw RangeError("Invalid imagery style; give alpha 0..1, finite brightness >=0 and boolean show.");
|
|
191
|
+
if (this.#r && (n.bounds[0] < -180 || n.bounds[2] > 180 || n.bounds[1] < -90 || n.bounds[3] > 90)) throw RangeError("Invalid planet bounds; give west/east in -180..180 and south/north in -90..90. Split antimeridian rectangles into two layers.");
|
|
192
|
+
e.options = n;
|
|
193
|
+
}
|
|
194
|
+
set(e, t) {
|
|
195
|
+
this.#m(), this.#g(this.#h(e), t), this.#b();
|
|
196
|
+
}
|
|
197
|
+
move(e, t) {
|
|
198
|
+
if (this.#m(), !s(t, 0, this.length - 1)) throw RangeError("Invalid layer index; use 0..length-1.");
|
|
199
|
+
let n = this.#h(e);
|
|
200
|
+
this.#e.splice(this.#e.indexOf(n), 1), this.#e.splice(t, 0, n), this.#v();
|
|
201
|
+
}
|
|
202
|
+
remove(e) {
|
|
203
|
+
this.#m();
|
|
204
|
+
let t = this.#e.findIndex((t) => t.id === e);
|
|
205
|
+
if (t < 0) return !1;
|
|
206
|
+
let n = this.#e[t];
|
|
207
|
+
return n.controller?.abort(), n.owned && n.texture?.destroy(), this.#e.splice(t, 1), this.#v(), !0;
|
|
208
|
+
}
|
|
209
|
+
setup(t) {
|
|
210
|
+
if (this.#m(), e(t, "TerrainImagery"), "owner" in this.terrain && this.terrain.owner && this.terrain.owner !== t) throw new n("attached", "Imagery and terrain must belong to the same engine; attach them together.");
|
|
211
|
+
if (this.owner = t, this.#i = t.gpu, this.#t.maxLayers + 9 > t.gpu.limits.maxSampledTexturesPerShaderStage) throw RangeError("Imagery layer capacity exceeds device sampled-texture limits; lower maxLayers.");
|
|
212
|
+
this.#a = t.gpu.createBuffer({
|
|
213
|
+
size: this.#t.maxLayers * (2 + this.#t.maxTiles) * 16,
|
|
214
|
+
usage: 136
|
|
215
|
+
}), this.#o = t.gpu.createSampler({
|
|
216
|
+
magFilter: "linear",
|
|
217
|
+
minFilter: "linear",
|
|
218
|
+
mipmapFilter: "linear"
|
|
219
|
+
});
|
|
220
|
+
for (let e of this.#e) this.#_(e);
|
|
221
|
+
this.#s ??= this.#n ? this.#n.addSurface(this) : this.terrain.addLighting(this), this.#v(), this.#x();
|
|
222
|
+
}
|
|
223
|
+
#_(e) {
|
|
224
|
+
let t = this.#i, n = e.options.source;
|
|
225
|
+
if (c(n)) {
|
|
226
|
+
let r = n.tileSize ?? 256;
|
|
227
|
+
if (r > t.limits.maxTextureDimension2D || this.#t.maxTiles > t.limits.maxTextureArrayLayers) throw RangeError("Imagery cache exceeds texture limits; lower tileSize or maxTiles.");
|
|
228
|
+
e.texture = t.createTexture({
|
|
229
|
+
size: [
|
|
230
|
+
r,
|
|
231
|
+
r,
|
|
232
|
+
this.#t.maxTiles
|
|
233
|
+
],
|
|
234
|
+
format: "rgba8unorm-srgb",
|
|
235
|
+
usage: 22
|
|
236
|
+
});
|
|
237
|
+
} else {
|
|
238
|
+
let r = n.texture(t);
|
|
239
|
+
if (!r || r.dimension !== "2d" || r.sampleCount !== 1 || r.depthOrArrayLayers !== 1 || !(r.usage & 4) || ![
|
|
240
|
+
"rgba8unorm",
|
|
241
|
+
"rgba8unorm-srgb",
|
|
242
|
+
"rgba16float",
|
|
243
|
+
"bgra8unorm",
|
|
244
|
+
"bgra8unorm-srgb"
|
|
245
|
+
].includes(r.format)) throw TypeError("Invalid imagery texture; return a single-sample, single-layer filterable RGBA 2D texture with TEXTURE_BINDING.");
|
|
246
|
+
e.texture = r;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
#v() {
|
|
250
|
+
this.#l.clear(), this.#b(), this.#n?.refreshSurfaces();
|
|
251
|
+
}
|
|
252
|
+
#y() {
|
|
253
|
+
return this.#r ? void 0 : this.terrain.anchor;
|
|
254
|
+
}
|
|
255
|
+
#b() {
|
|
256
|
+
if (!this.#i || !this.#a) return;
|
|
257
|
+
let e = (2 + this.#t.maxTiles) * 4, t = new Float32Array(e * this.#t.maxLayers), [n, r] = this.#y() ?? [0, 0];
|
|
258
|
+
this.#e.forEach((i, a) => {
|
|
259
|
+
let o = a * e, s = i.options, l = s.bounds;
|
|
260
|
+
if (t.set(this.#y() ? [
|
|
261
|
+
l[0] - n,
|
|
262
|
+
l[1] - r,
|
|
263
|
+
l[2] - n,
|
|
264
|
+
l[3] - r
|
|
265
|
+
] : l, o), t.set([
|
|
266
|
+
s.show ? s.alpha : 0,
|
|
267
|
+
s.brightness,
|
|
268
|
+
+!!c(s.source),
|
|
269
|
+
c(s.source) && s.source.projection !== "geographic" ? 1 : 0
|
|
270
|
+
], o + 4), !c(s.source)) {
|
|
271
|
+
let e = s.bounds, n = this.#t.bounds;
|
|
272
|
+
t.set(this.#r ? [
|
|
273
|
+
(e[0] + 180) / 360,
|
|
274
|
+
(90 - e[3]) / 180,
|
|
275
|
+
(e[2] - e[0]) / 360,
|
|
276
|
+
(e[3] - e[1]) / 180
|
|
277
|
+
] : [
|
|
278
|
+
(e[0] - n[0]) / (n[2] - n[0]),
|
|
279
|
+
(e[1] - n[1]) / (n[3] - n[1]),
|
|
280
|
+
(e[2] - e[0]) / (n[2] - n[0]),
|
|
281
|
+
(e[3] - e[1]) / (n[3] - n[1])
|
|
282
|
+
], o + 8);
|
|
283
|
+
}
|
|
284
|
+
for (let { tile: e, slot: n } of i.cache.values()) {
|
|
285
|
+
let r = 2 ** e.level, i = r * (s.source.columns ?? 1);
|
|
286
|
+
t.set([
|
|
287
|
+
e.x / i,
|
|
288
|
+
e.y / r,
|
|
289
|
+
1 / i,
|
|
290
|
+
1 / r
|
|
291
|
+
], o + 8 + n * 4);
|
|
292
|
+
}
|
|
293
|
+
}), this.#i.queue.writeBuffer(this.#a, 0, t), this.#n?.invalidate();
|
|
294
|
+
}
|
|
295
|
+
#x() {
|
|
296
|
+
this.#i && !this.disposed && this.update(this.#f, this.#p);
|
|
297
|
+
}
|
|
298
|
+
update(e = this.#f, t = this.#p, n) {
|
|
299
|
+
return this.#d = this.#S(e, t, n), this.#d.catch((e) => {
|
|
300
|
+
this.error = e;
|
|
301
|
+
}), this.#d;
|
|
302
|
+
}
|
|
303
|
+
async #S(e, t, r) {
|
|
304
|
+
if (this.#m(), e.length !== 2 || !e.every(Number.isFinite) || !s(t, 0, 22)) throw RangeError("Invalid imagery view; give finite normalized center and zoom in 0..22.");
|
|
305
|
+
if (r?.throwIfAborted(), this.#f = [...e], this.#p = t, !this.#i) return;
|
|
306
|
+
let i = this.#i, a = [], o = [];
|
|
307
|
+
for (let s of this.#e) {
|
|
308
|
+
let d = s.options.source;
|
|
309
|
+
if (!c(d)) continue;
|
|
310
|
+
s.controller?.abort();
|
|
311
|
+
let f = new AbortController();
|
|
312
|
+
s.controller = f;
|
|
313
|
+
let p = ++s.generation, m = () => f.abort(r?.reason);
|
|
314
|
+
r?.addEventListener("abort", m, { once: !0 });
|
|
315
|
+
let h = Math.max(d.minLevel ?? 0, Math.min(d.maxLevel ?? 18, t)), g = [e[0], e[1]];
|
|
316
|
+
if (this.#r && d.projection !== "geographic") {
|
|
317
|
+
let t = Math.max(-85.05112878, Math.min(85.05112878, 90 - e[1] * 180)) * Math.PI / 180;
|
|
318
|
+
g[1] = (1 - Math.log(Math.tan(Math.PI / 4 + t / 2)) / Math.PI) / 2;
|
|
319
|
+
}
|
|
320
|
+
let _ = u(g, h, this.#t.maxTiles, d.columns ?? 1, d.minLevel ?? 0, this.#r), v = new Set(_.map(l));
|
|
321
|
+
for (let e of s.cache.keys()) v.has(e) || s.cache.delete(e);
|
|
322
|
+
let y = Array.from({ length: this.#t.maxTiles }, (e, t) => t).filter((e) => ![...s.cache.values()].some((t) => t.slot === e)), b = [];
|
|
323
|
+
for (let e of _) {
|
|
324
|
+
let t = l(e);
|
|
325
|
+
if (s.cache.has(t)) continue;
|
|
326
|
+
let o = y.shift(), c, u;
|
|
327
|
+
b.push(new Promise((e, t) => {
|
|
328
|
+
c = e, u = t;
|
|
329
|
+
})), a.push(async () => {
|
|
330
|
+
let a;
|
|
331
|
+
try {
|
|
332
|
+
if (f.signal.throwIfAborted(), d.load) a = await d.load(e, f.signal);
|
|
333
|
+
else {
|
|
334
|
+
let t = typeof d.url == "function" ? d.url(e) : d.url.replace(/\{([zxy])\}/g, (t, n) => String(n === "z" ? e.level : n === "x" ? e.x : e.y)), r = await fetch(t, { signal: f.signal });
|
|
335
|
+
if (!r.ok) throw new n("failed", `Imagery HTTP ${r.status} for ${t}; check the provider URL and permissions.`);
|
|
336
|
+
a = await createImageBitmap(await r.blob(), {
|
|
337
|
+
colorSpaceConversion: "none",
|
|
338
|
+
premultiplyAlpha: "none"
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (f.signal.throwIfAborted(), s.generation !== p || this.disposed || this.#i !== i) {
|
|
342
|
+
c();
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
let r = d.tileSize ?? 256;
|
|
346
|
+
if (a instanceof Uint8Array) {
|
|
347
|
+
if (a.length !== r * r * 4) throw RangeError("Invalid imagery tile bytes; return tileSize squared RGBA pixels.");
|
|
348
|
+
i.queue.writeTexture({
|
|
349
|
+
texture: s.texture,
|
|
350
|
+
origin: [
|
|
351
|
+
0,
|
|
352
|
+
0,
|
|
353
|
+
o
|
|
354
|
+
]
|
|
355
|
+
}, a, { bytesPerRow: r * 4 }, [r, r]);
|
|
356
|
+
} else {
|
|
357
|
+
if (a.width !== r || a.height !== r) throw RangeError("Invalid imagery tile dimensions; return a square image matching tileSize.");
|
|
358
|
+
i.queue.copyExternalImageToTexture({ source: a }, {
|
|
359
|
+
texture: s.texture,
|
|
360
|
+
origin: [
|
|
361
|
+
0,
|
|
362
|
+
0,
|
|
363
|
+
o
|
|
364
|
+
]
|
|
365
|
+
}, [r, r]);
|
|
366
|
+
}
|
|
367
|
+
s.cache.set(t, {
|
|
368
|
+
tile: e,
|
|
369
|
+
slot: o
|
|
370
|
+
}), this.#b(), c();
|
|
371
|
+
} catch (e) {
|
|
372
|
+
f.signal.aborted && !r?.aborted ? c() : u(e);
|
|
373
|
+
} finally {
|
|
374
|
+
a && !(a instanceof Uint8Array) && a.close();
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
let x = Promise.all(b).finally(() => r?.removeEventListener("abort", m));
|
|
379
|
+
x.catch(() => {}), o.push(x);
|
|
380
|
+
}
|
|
381
|
+
this.#b();
|
|
382
|
+
let d = 0;
|
|
383
|
+
await Promise.all(Array.from({ length: Math.min(a.length, this.#t.concurrency) }, async () => {
|
|
384
|
+
for (; d < a.length;) await a[d++]();
|
|
385
|
+
})), await Promise.all(o), this.error = void 0;
|
|
386
|
+
}
|
|
387
|
+
bindings() {
|
|
388
|
+
return !this.length || !this.#a || !this.#o ? [] : [
|
|
389
|
+
{
|
|
390
|
+
binding: 98,
|
|
391
|
+
resource: this.#o
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
binding: 99,
|
|
395
|
+
resource: { buffer: this.#a }
|
|
396
|
+
},
|
|
397
|
+
...this.#e.map((e, t) => ({
|
|
398
|
+
binding: 100 + t,
|
|
399
|
+
resource: e.texture.createView({ dimension: "2d-array" })
|
|
400
|
+
}))
|
|
401
|
+
];
|
|
402
|
+
}
|
|
403
|
+
shader(e, t) {
|
|
404
|
+
if (!this.length) return e;
|
|
405
|
+
let r = t + e, i = this.#l.get(r);
|
|
406
|
+
if (i) return i;
|
|
407
|
+
let a = this.#r ? "let nl=clamp(dot(n,planet.light.xyz)" : "let base=terrainColor(i.position,", o;
|
|
408
|
+
if (this.#n) {
|
|
409
|
+
if (!e.includes("return terrainSurfaceColor;")) throw new n("unsupported", "Cached imagery needs the RVT surface hook; attach it through virtualTexture.");
|
|
410
|
+
o = e.replace("return terrainSurfaceColor;", "terrainSurfaceColor=terrainImagery(terrainSurfaceColor,vec3f(position.x,0.0,position.y));return terrainSurfaceColor;");
|
|
411
|
+
} else if (this.#r) {
|
|
412
|
+
if (!e.includes(a)) throw new n("unsupported", "Imagery needs the planet surface shader; attach it before a replacement shader.");
|
|
413
|
+
o = e.replace(a, "base=terrainImagery(base,i.position);" + a);
|
|
414
|
+
} else {
|
|
415
|
+
let t = /let base=(terrainColor|terrainVirtualGround)\(i\.position,([^;]+);/.exec(e);
|
|
416
|
+
if (!t) throw new n("unsupported", "Imagery needs the terrain color call; attach after RVT and before a replacement shader.");
|
|
417
|
+
o = e.replace(t[0], `let base=terrainImagery(${t[1]}(i.position,${t[2]},i.position${this.#y() ? "+vec3f(frame.anchor.x,0.0,frame.anchor.y)" : ""});`);
|
|
418
|
+
}
|
|
419
|
+
let s = this.#y(), c = this.#t.bounds, l = s ? [
|
|
420
|
+
c[0] - s[0],
|
|
421
|
+
c[1] - s[1],
|
|
422
|
+
c[2] - s[0],
|
|
423
|
+
c[3] - s[1]
|
|
424
|
+
] : c, u = [c[2] - c[0], c[3] - c[1]], d = this.#r ? "let direction=normalize(position-planet.center.xyz);let geographic=vec2f(atan2(direction.z,direction.x)*57.295779513,asin(clamp(direction.y,-1.0,1.0))*57.295779513);let pyramid=vec2f((geographic.x+180.0)/360.0,(90.0-geographic.y)/180.0);" : `let geographic=position.xz;let pyramid=(geographic-vec2f(${l[0]},${l[1]}))/vec2f(${u[0]},${u[1]});`, f = 2 + this.#t.maxTiles, p = `@group(${t}) @binding(98) var imagerySampler:sampler;\n@group(${t}) @binding(99) var<storage,read> imageryRules:array<vec4f>;\n`;
|
|
425
|
+
return this.#e.forEach((e, n) => {
|
|
426
|
+
p += `@group(${t}) @binding(${100 + n}) var imagery${n}:texture_2d_array<f32>;\n`;
|
|
427
|
+
}), p += `fn terrainImagery(base:vec3f,position:vec3f)->vec3f {${d}let dx=dpdx(pyramid);let dy=dpdy(pyramid);var result=base;`, this.#e.forEach((e, t) => {
|
|
428
|
+
let n = t * f;
|
|
429
|
+
p += `{let box=imageryRules[${n}];let style=imageryRules[${n + 1}];var uv=pyramid;var sx=dx;var sy=dy;
|
|
430
|
+
if(style.w>0.5&&${this.#r ? "true" : "false"}){let lat=clamp(geographic.y,-85.05112878,85.05112878)*0.01745329252;uv.y=(1.0-log(tan(0.7853981634+lat*0.5))/3.14159265359)*0.5;let scale=0.5/max(cos(lat),0.086);sx.y*=scale;sy.y*=scale;}
|
|
431
|
+
if(style.x>0.0&&all(geographic>=box.xy)&&all(geographic<=box.zw)){
|
|
432
|
+
var slot=0;var tile=imageryRules[${n + 2}];var found=style.z<0.5;var smallest=2.0;
|
|
433
|
+
if(style.z>0.5){for(var k=0;k<${this.#t.maxTiles};k++){let p=imageryRules[${n + 2}+k];if(p.z>0.0&&p.z<smallest&&all(uv>=p.xy)&&all(uv<=p.xy+p.zw)){slot=k;tile=p;smallest=p.z;found=true;}}}
|
|
434
|
+
if(found){let d=vec2f(textureDimensions(imagery${t}));let tuv=clamp((uv-tile.xy)/tile.zw,vec2f(0.5)/d,vec2f(1)-vec2f(0.5)/d);let sample=textureSampleGrad(imagery${t},imagerySampler,tuv,slot,sx/tile.zw,sy/tile.zw);result=mix(result,sample.rgb*style.y,sample.a*style.x);}
|
|
435
|
+
}} `;
|
|
436
|
+
}), p += "return result;}\n", this.#n && (p = p.replace("let dx=dpdx(pyramid);let dy=dpdy(pyramid);", `let dx=terrainDx.xz/vec2f(${u[0]},${u[1]});let dy=terrainDy.xz/vec2f(${u[0]},${u[1]});`)), p += o, this.#l.set(r, p), p;
|
|
437
|
+
}
|
|
438
|
+
registerPasses(e) {
|
|
439
|
+
e.add({
|
|
440
|
+
name: this.name + ":view",
|
|
441
|
+
order: -4,
|
|
442
|
+
execute: (e) => {
|
|
443
|
+
if (!this.#t.automatic || e.backend !== "webgpu" || !this.#e.some((e) => c(e.options.source))) return;
|
|
444
|
+
let t = this.#t.bounds, n, i, a, o;
|
|
445
|
+
if ("radius" in this.terrain) {
|
|
446
|
+
let t = this.terrain.localCenter, r = e.camera.position, s = r[0] - t[0], c = r[1] - t[1], l = r[2] - t[2], u = Math.hypot(s, c, l);
|
|
447
|
+
n = (Math.atan2(l, s) / Math.PI + 1) / 2, i = (1 - Math.asin(Math.max(-1, Math.min(1, c / (u || 1)))) / (Math.PI / 2)) / 2, a = Math.max(1, u - this.terrain.radius), o = 2 * Math.PI * this.terrain.radius;
|
|
448
|
+
} else {
|
|
449
|
+
let s = e.camera.position, c = this.#y() ? this.terrain.shift : r;
|
|
450
|
+
n = (s[0] + c[0] - t[0]) / (t[2] - t[0]), i = (s[2] + c[2] - t[1]) / (t[3] - t[1]), a = Math.max(1, Math.abs(s[1] + c[1])), o = t[2] - t[0];
|
|
451
|
+
}
|
|
452
|
+
let s = Math.max(0, Math.min(22, Math.floor(Math.log2(o / a * 2)))), l = `${Math.floor(n * 2 ** s)}/${Math.floor(i * 2 ** s)}/${s}`;
|
|
453
|
+
l !== this.#u && (this.#u = l, this.#f = [n, i], this.#p = s, this.#x());
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
onDeviceRestored(e) {
|
|
458
|
+
this.#C(), this.#u = "", this.setup(e);
|
|
459
|
+
}
|
|
460
|
+
#C() {
|
|
461
|
+
for (let e of this.#e) e.controller?.abort(), e.generation++, e.owned && e.texture?.destroy(), e.texture = void 0, e.cache.clear();
|
|
462
|
+
this.#a?.destroy(), this.#a = void 0, this.#i = void 0;
|
|
463
|
+
}
|
|
464
|
+
dispose() {
|
|
465
|
+
this.disposed || (this.disposed = !0, this.#s?.(), this.#s = void 0, this.#C(), this.#l.clear(), this.owner?.sceneOf(this)?.removePlugin(this.name), this.owner = void 0);
|
|
466
|
+
}
|
|
467
|
+
[Symbol.dispose]() {
|
|
468
|
+
this.dispose();
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
//#endregion
|
|
472
|
+
export { f as TerrainImagery, u as imageryTiles, a as wmtsImagery, i as xyzImagery };
|