@margoengine/voxel 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 +1671 -3
- package/dist/bevel.d.ts +79 -0
- package/dist/bevel.js +453 -0
- package/dist/blocks-BabtpQT-.js +179 -0
- package/dist/blocks.d.ts +220 -0
- package/dist/border.d.ts +9 -0
- package/dist/chunk-BCoaKSn4.js +158 -0
- package/dist/chunk.d.ts +112 -0
- package/dist/codec-ZZFj1xvt.js +410 -0
- package/dist/codec.d.ts +125 -0
- package/dist/codec.js +1 -0
- package/dist/debris.d.ts +114 -0
- package/dist/debris.js +387 -0
- package/dist/faces-DUR7LhrN.js +146 -0
- package/dist/faces.d.ts +7 -0
- package/dist/fluids.d.ts +74 -0
- package/dist/fluids.js +226 -0
- package/dist/generate.d.ts +78 -0
- package/dist/generate.js +99 -0
- package/dist/gi.d.ts +76 -0
- package/dist/gi.js +264 -0
- package/dist/gpu-mesher.d.ts +77 -0
- package/dist/gpu-mesher.js +461 -0
- package/dist/granular-mpm.d.ts +32 -0
- package/dist/granular-mpm.js +46 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/light-rgb.d.ts +71 -0
- package/dist/light-rgb.js +287 -0
- package/dist/light-worker.d.ts +84 -0
- package/dist/light-worker.js +110 -0
- package/dist/light.d.ts +108 -0
- package/dist/light.js +332 -0
- package/dist/light.worker.d.ts +1 -0
- package/dist/lightmap.d.ts +50 -0
- package/dist/lightmap.js +68 -0
- package/dist/lod.d.ts +87 -0
- package/dist/lod.js +182 -0
- package/dist/mesher.d.ts +214 -0
- package/dist/mesher.js +539 -0
- package/dist/mesher.worker.d.ts +1 -0
- package/dist/model-data-D_rIKJCQ.js +41 -0
- package/dist/model-data.d.ts +21 -0
- package/dist/model-mesh-C4Gi6bMM.js +122 -0
- package/dist/model-mesh.d.ts +21 -0
- package/dist/model-surface.d.ts +5 -0
- package/dist/models.d.ts +74 -0
- package/dist/models.js +308 -0
- package/dist/objects.d.ts +73 -0
- package/dist/objects.js +152 -0
- package/dist/octants-C_Ic7gdn.js +10 -0
- package/dist/octants.d.ts +16 -0
- package/dist/origin-DsMelUxs.js +23 -0
- package/dist/origin.d.ts +7 -0
- package/dist/physics.d.ts +147 -0
- package/dist/physics.js +486 -0
- package/dist/query.d.ts +166 -0
- package/dist/query.js +305 -0
- package/dist/render-post.d.ts +6 -0
- package/dist/render.d.ts +296 -0
- package/dist/render.js +1613 -0
- package/dist/shared/borrowed.d.ts +44 -0
- package/dist/smooth-CEwHkXli.js +968 -0
- package/dist/smooth-clip.d.ts +24 -0
- package/dist/smooth-lod.d.ts +118 -0
- package/dist/smooth-lod.js +244 -0
- package/dist/smooth.d.ts +171 -0
- package/dist/smooth.js +1 -0
- package/dist/stream.d.ts +201 -0
- package/dist/stream.js +402 -0
- package/dist/terrain.worker.d.ts +1 -0
- package/dist/textures.d.ts +86 -0
- package/dist/ticks.d.ts +126 -0
- package/dist/ticks.js +239 -0
- package/dist/unit-light-Ch7roJgB.js +7 -0
- package/dist/vox.d.ts +237 -0
- package/dist/vox.js +780 -0
- package/dist/wind-data.d.ts +13 -0
- package/dist/wind.d.ts +68 -0
- package/dist/wind.js +170 -0
- package/dist/worker-DlGRxlc4.js +44 -0
- package/dist/worker-pool-sPyC6jCE.js +93 -0
- package/dist/workers/light.js +1 -0
- package/dist/workers/mesher.js +1 -0
- package/dist/workers/terrain.js +1 -0
- package/dist/world-CelTlft2.js +153 -0
- package/dist/world.d.ts +117 -0
- package/package.json +175 -6
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import type { Vec3 } from '@margoengine/math';
|
|
3
|
+
import { VoxelChunk } from './chunk.js';
|
|
4
|
+
import { type NoiseTerrainOptions } from './generate.js';
|
|
5
|
+
import { VoxelWorld } from './world.js';
|
|
6
|
+
/**
|
|
7
|
+
* Where chunks are saved when they unload and looked up before they are generated again. Chunk coordinates are
|
|
8
|
+
* cell >> 5. Implement it to save somewhere else (a server, OPFS); {@link IndexedDBChunkStore} and
|
|
9
|
+
* {@link MemoryChunkStore} are the built-in ones.
|
|
10
|
+
*/
|
|
11
|
+
export interface ChunkStore {
|
|
12
|
+
/** The chunk stored at chunk coordinates (cx, cy, cz), or undefined. Sees every put and delete made before it. */
|
|
13
|
+
get(cx: number, cy: number, cz: number): Promise<VoxelChunk | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Stores a copy of `chunk` as it is now (later edits are not saved); resolves once it is stored. Invalid
|
|
16
|
+
* arguments throw at the call; a storage failure rejects this promise and the next flush, so a caller that
|
|
17
|
+
* awaits flush may ignore it.
|
|
18
|
+
*/
|
|
19
|
+
put(cx: number, cy: number, cz: number, chunk: VoxelChunk): Promise<void>;
|
|
20
|
+
/** Removes the chunk stored at (cx, cy, cz); resolves once it is gone. Errors as for put. */
|
|
21
|
+
delete(cx: number, cy: number, cz: number): Promise<void>;
|
|
22
|
+
/** Resolves once every put and delete made before it is stored; rejects when one failed since the last flush. */
|
|
23
|
+
flush(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A {@link ChunkStore} in memory, holding uncompressed {@link encodeChunk} bytes: for tests, servers and sessions
|
|
27
|
+
* that need not outlive the page.
|
|
28
|
+
*/
|
|
29
|
+
export declare class MemoryChunkStore implements ChunkStore {
|
|
30
|
+
private readonly records;
|
|
31
|
+
/** @throws RangeError or TypeError (rejects) for invalid chunk coordinates. */
|
|
32
|
+
get(cx: number, cy: number, cz: number): Promise<VoxelChunk | undefined>;
|
|
33
|
+
/** @throws TypeError when `chunk` is not a VoxelChunk; RangeError or TypeError for invalid chunk coordinates. */
|
|
34
|
+
put(cx: number, cy: number, cz: number, chunk: VoxelChunk): Promise<void>;
|
|
35
|
+
/** @throws RangeError or TypeError for invalid chunk coordinates. */
|
|
36
|
+
delete(cx: number, cy: number, cz: number): Promise<void>;
|
|
37
|
+
/** Resolves at once: memory writes complete when put returns. */
|
|
38
|
+
flush(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A {@link ChunkStore} in IndexedDB: database `name`, object store `'chunks'`, one deflated {@link encodeChunk}
|
|
42
|
+
* record per chunk keyed `[cx, cy, cz]`. Puts and deletes made in one task are compressed, then written in one
|
|
43
|
+
* transaction after the previous one, in call order; get sees writes that are not committed yet.
|
|
44
|
+
*/
|
|
45
|
+
export declare class IndexedDBChunkStore implements ChunkStore {
|
|
46
|
+
/** The IndexedDB database name: one per world or save. */
|
|
47
|
+
readonly name: string;
|
|
48
|
+
private readonly db;
|
|
49
|
+
private readonly pending;
|
|
50
|
+
private readonly writing;
|
|
51
|
+
private queue;
|
|
52
|
+
private batch;
|
|
53
|
+
private last;
|
|
54
|
+
private error;
|
|
55
|
+
/**
|
|
56
|
+
* Opens database `name` (default `'margo-voxel'`), creating it on first use.
|
|
57
|
+
* @throws TypeError for a name that is not a non-empty string.
|
|
58
|
+
* @throws MargoError `'unsupported'` when the runtime lacks IndexedDB or CompressionStream.
|
|
59
|
+
*/
|
|
60
|
+
constructor(name?: string);
|
|
61
|
+
/**
|
|
62
|
+
* @throws RangeError or TypeError (rejects) for invalid chunk coordinates or a damaged record.
|
|
63
|
+
* @throws MargoError `'failed'` (rejects) when IndexedDB fails.
|
|
64
|
+
*/
|
|
65
|
+
get(cx: number, cy: number, cz: number): Promise<VoxelChunk | undefined>;
|
|
66
|
+
/**
|
|
67
|
+
* @throws TypeError when `chunk` is not a VoxelChunk, or as {@link IndexedDBChunkStore.delete}.
|
|
68
|
+
*/
|
|
69
|
+
put(cx: number, cy: number, cz: number, chunk: VoxelChunk): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* @throws RangeError or TypeError for invalid chunk coordinates.
|
|
72
|
+
* @throws MargoError `'failed'` (rejects) when IndexedDB fails to commit the batch.
|
|
73
|
+
*/
|
|
74
|
+
delete(cx: number, cy: number, cz: number): Promise<void>;
|
|
75
|
+
/** @throws MargoError `'failed'` (rejects) when a write failed since the last flush (the error is then cleared). */
|
|
76
|
+
flush(): Promise<void>;
|
|
77
|
+
private write;
|
|
78
|
+
}
|
|
79
|
+
/** Options of {@link VoxelStreamer}. */
|
|
80
|
+
export interface VoxelStreamerOptions {
|
|
81
|
+
/** The world chunks are added to and removed from. One streamer per world. */
|
|
82
|
+
world: VoxelWorld;
|
|
83
|
+
/**
|
|
84
|
+
* Where chunks missing from the store come from: {@link NoiseTerrainOptions} for the built-in terrain (run in
|
|
85
|
+
* classic workers, dist/workers/terrain.js), or the URL of your own module worker that calls
|
|
86
|
+
* `serveVoxelGenerator` from `@margoengine/voxel/generate`. Default none: such chunks load as air.
|
|
87
|
+
*/
|
|
88
|
+
generator?: NoiseTerrainOptions | URL | string;
|
|
89
|
+
/** Where edited chunks are saved when they unload, and looked for before generating. Default a MemoryChunkStore. */
|
|
90
|
+
store?: ChunkStore;
|
|
91
|
+
/** Load radius in chunks around each viewer, in x and z (a circle), an integer in 0..64. Default 8 (256 blocks). */
|
|
92
|
+
distance?: number;
|
|
93
|
+
/** Load radius in chunks above and below each viewer, an integer in 0..64. Default 4. */
|
|
94
|
+
verticalDistance?: number;
|
|
95
|
+
/** Generator workers, started as jobs arrive. Default a quarter of the logical cores, 1 to 3. */
|
|
96
|
+
workers?: number;
|
|
97
|
+
/** Bytes of loaded chunk data ({@link VoxelChunk.byteLength}) to keep to. Default 128 MiB. */
|
|
98
|
+
memoryBudget?: number;
|
|
99
|
+
/** Most chunks added to the world per {@link VoxelStreamer.update}: an integer >= 1 or Infinity. Default 8. */
|
|
100
|
+
chunksPerFrame?: number;
|
|
101
|
+
/**
|
|
102
|
+
* Main-thread milliseconds per {@link VoxelStreamer.update} for adding finished chunks (unpacking each, adding it,
|
|
103
|
+
* and the world's onChunk listeners): no chunk is started once it is spent, but one always joins, so streaming
|
|
104
|
+
* never stalls. A number >= 0 (0: one chunk per update) or Infinity. Default 0.5.
|
|
105
|
+
*/
|
|
106
|
+
frameBudget?: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Loads and unloads the chunks of a {@link VoxelWorld} around its viewers: the engine camera (with
|
|
110
|
+
* {@link VoxelStreamer.attach}) and the positions in {@link VoxelStreamer.viewers}. A chunk loads when it is within
|
|
111
|
+
* `distance` chunks of a viewer's chunk in x and z and `verticalDistance` in y, and unloads once it is more than one
|
|
112
|
+
* chunk past that for every viewer (the margin keeps chunks at the edge from loading and unloading over and over).
|
|
113
|
+
* Loads run closest first, the store first and then the generator; finished chunks join the world closest first, per
|
|
114
|
+
* update at most `chunksPerFrame` and no more once `frameBudget` milliseconds are spent (at least one). Lighting and
|
|
115
|
+
* meshing follow through the world's onChunk and dirty set. A chunk that was edited (`world.onChange`), or that
|
|
116
|
+
* someone else added, is saved to the store when it unloads; one generated or read from the store and not edited is
|
|
117
|
+
* dropped. To fit `memoryBudget` it unloads the least recently used chunks outside the load range. Each finished
|
|
118
|
+
* chunk waits until its actual byteLength fits (a budget smaller than the load range loads the closest chunks only).
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* const store = new IndexedDBChunkStore('world-1');
|
|
122
|
+
* const streamer = VoxelStreamer.attach(engine, { world, generator: { seed: 7 }, store });
|
|
123
|
+
* streamer.viewers.push(otherPlayer.position); // more positions (blocks) to load around
|
|
124
|
+
* addEventListener('pagehide', () => streamer.save()); // edits of loaded chunks too
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export declare class VoxelStreamer {
|
|
128
|
+
#private;
|
|
129
|
+
/** The streamed world. */
|
|
130
|
+
readonly world: VoxelWorld;
|
|
131
|
+
/** Where edited chunks go when they unload. */
|
|
132
|
+
readonly store: ChunkStore;
|
|
133
|
+
/** Number of generator workers (started only with a generator). */
|
|
134
|
+
readonly workers: number;
|
|
135
|
+
/** Positions (in blocks) to load around besides the engine camera; read each update. */
|
|
136
|
+
viewers: Vec3[];
|
|
137
|
+
/**
|
|
138
|
+
* Receives failures: a chunk that could not be read or generated (tried again after it leaves the load range and
|
|
139
|
+
* comes back) and a save that failed on unload (the chunk is kept, loads find it, and {@link VoxelStreamer.save}
|
|
140
|
+
* tries again). Default the engine's onError with attach, else reportError or console.error.
|
|
141
|
+
*/
|
|
142
|
+
onError: (error: unknown) => void;
|
|
143
|
+
/**
|
|
144
|
+
* A streamer the engine drives: each frame runs {@link VoxelStreamer.update} with the camera as a viewer, failures go
|
|
145
|
+
* to `engine.onError`, and disposing the engine disposes the streamer.
|
|
146
|
+
* @throws as the constructor.
|
|
147
|
+
*/
|
|
148
|
+
static attach(engine: EngineKernel, options: VoxelStreamerOptions): VoxelStreamer;
|
|
149
|
+
/**
|
|
150
|
+
* A streamer you drive: call {@link VoxelStreamer.update} once a frame. It adopts the chunks already loaded as
|
|
151
|
+
* edited (saved when they unload). Built-in terrain options and their block names are checked here.
|
|
152
|
+
* @throws TypeError when `world` is not a VoxelWorld, `store` is not a ChunkStore, `generator` is neither terrain
|
|
153
|
+
* options nor a URL, or a number option is not a number; as `noiseTerrain` for wrong terrain options.
|
|
154
|
+
* @throws RangeError for `distance` or `verticalDistance` not an integer in 0..64, `workers` not an integer >= 1,
|
|
155
|
+
* `chunksPerFrame` not an integer >= 1 or Infinity, `frameBudget` not >= 0, `memoryBudget` not > 0, or a terrain
|
|
156
|
+
* option out of range or block name the world's types lack.
|
|
157
|
+
* @throws MargoError `'attached'` when another VoxelStreamer streams `world`; `'unsupported'` for a generator in a
|
|
158
|
+
* context without Web Workers.
|
|
159
|
+
*/
|
|
160
|
+
constructor(options: VoxelStreamerOptions);
|
|
161
|
+
/** Load radius in chunks in x and z, an integer in 0..64; a change applies at the next update. */
|
|
162
|
+
get distance(): number;
|
|
163
|
+
set distance(value: number);
|
|
164
|
+
/** Load radius in chunks in y, an integer in 0..64; a change applies at the next update. */
|
|
165
|
+
get verticalDistance(): number;
|
|
166
|
+
set verticalDistance(value: number);
|
|
167
|
+
/** Most chunks added per update: an integer >= 1, or Infinity (with `frameBudget` Infinity behind a loading screen). */
|
|
168
|
+
get chunksPerFrame(): number;
|
|
169
|
+
set chunksPerFrame(value: number);
|
|
170
|
+
/**
|
|
171
|
+
* Main-thread milliseconds per update for adding finished chunks, >= 0 or Infinity: once spent, no more join that
|
|
172
|
+
* update (one always does). Applies from the next update.
|
|
173
|
+
*/
|
|
174
|
+
get frameBudget(): number;
|
|
175
|
+
set frameBudget(value: number);
|
|
176
|
+
/** Bytes of loaded chunk data to keep to, > 0 (Infinity allowed). */
|
|
177
|
+
get memoryBudget(): number;
|
|
178
|
+
set memoryBudget(value: number);
|
|
179
|
+
/** Chunks in the load range of the last update that are not loaded yet, failed ones excluded: 0 when it is full. */
|
|
180
|
+
get pending(): number;
|
|
181
|
+
/**
|
|
182
|
+
* Unloads what left the range, adds finished chunks (up to `chunksPerFrame`, within `frameBudget`), unloads to the
|
|
183
|
+
* memory budget and starts the next loads, closest first. Call it once a frame (attach does).
|
|
184
|
+
* @throws TypeError or RangeError for a viewer that is not three finite numbers.
|
|
185
|
+
* @throws MargoError `'disposed'` after {@link VoxelStreamer.dispose}.
|
|
186
|
+
*/
|
|
187
|
+
update(): void;
|
|
188
|
+
/**
|
|
189
|
+
* Saves every edited chunk (loaded, or unloaded with a failed save) to the store and flushes it; unloads save by
|
|
190
|
+
* themselves. Call it when the page hides.
|
|
191
|
+
* @throws MargoError `'disposed'` (rejects) after {@link VoxelStreamer.dispose}, which saved already.
|
|
192
|
+
* @throws (rejects) the store's error when a save failed; those chunks stay edited for the next save.
|
|
193
|
+
*/
|
|
194
|
+
save(): Promise<void>;
|
|
195
|
+
/**
|
|
196
|
+
* Stops streaming: cancels pending loads, stops the workers, then saves the edited chunks and flushes the store (the
|
|
197
|
+
* returned promise settles as {@link VoxelStreamer.save}'s). Loaded chunks stay in the world.
|
|
198
|
+
*/
|
|
199
|
+
dispose(): Promise<void>;
|
|
200
|
+
[Symbol.dispose](): void;
|
|
201
|
+
}
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e } from "./chunk-BCoaKSn4.js";
|
|
5
|
+
import { n as t, t as n } from "./world-CelTlft2.js";
|
|
6
|
+
import { i as r, o as i } from "./codec-ZZFj1xvt.js";
|
|
7
|
+
import { t as a } from "./worker-pool-sPyC6jCE.js";
|
|
8
|
+
import { noiseTerrain as o } from "./generate.js";
|
|
9
|
+
import { MargoError as s } from "@margoengine/math/errors";
|
|
10
|
+
//#region src/stream.ts
|
|
11
|
+
var c = (e, t, n) => {
|
|
12
|
+
if ([
|
|
13
|
+
e,
|
|
14
|
+
t,
|
|
15
|
+
n
|
|
16
|
+
].every((e) => e << 5 >> 5 === e)) return [
|
|
17
|
+
e,
|
|
18
|
+
t,
|
|
19
|
+
n
|
|
20
|
+
];
|
|
21
|
+
throw [
|
|
22
|
+
e,
|
|
23
|
+
t,
|
|
24
|
+
n
|
|
25
|
+
].every((e) => typeof e == "number") ? /* @__PURE__ */ RangeError(`Chunk (${e}, ${t}, ${n}) is not three integers in -2^26..2^26-1; pass cell >> 5`) : /* @__PURE__ */ TypeError("Chunk coordinates must be numbers");
|
|
26
|
+
}, l = (t) => {
|
|
27
|
+
if (t instanceof e) return t;
|
|
28
|
+
throw TypeError("ChunkStore.put expects a VoxelChunk");
|
|
29
|
+
}, u = class {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.records = /* @__PURE__ */ new Map();
|
|
32
|
+
}
|
|
33
|
+
async get(e, n, i) {
|
|
34
|
+
let a = this.records.get(t(...c(e, n, i)));
|
|
35
|
+
return a && r(await a);
|
|
36
|
+
}
|
|
37
|
+
put(e, n, r, a) {
|
|
38
|
+
let o = t(...c(e, n, r)), s = i(l(a), { compress: !1 });
|
|
39
|
+
return this.records.set(o, s), s.then(() => {});
|
|
40
|
+
}
|
|
41
|
+
delete(e, n, r) {
|
|
42
|
+
return this.records.delete(t(...c(e, n, r))), Promise.resolve();
|
|
43
|
+
}
|
|
44
|
+
async flush() {}
|
|
45
|
+
}, d = "chunks", f = (e, t, n) => n instanceof s ? n : new s("failed", `IndexedDBChunkStore "${e}" could not ${t} (${n?.name ?? "aborted"}); free storage or reopen the store`), p = (e, t, n) => new Promise((r, i) => {
|
|
46
|
+
e.onsuccess = () => r(e.result), e.onerror = () => i(f(t, n, e.error));
|
|
47
|
+
}), m = (e, t) => new Promise((n, r) => {
|
|
48
|
+
e.oncomplete = () => n(), e.onerror = e.onabort = () => r(f(t, "write", e.error));
|
|
49
|
+
}), h = class {
|
|
50
|
+
constructor(e = "margo-voxel") {
|
|
51
|
+
if (this.pending = /* @__PURE__ */ new Map(), this.writing = /* @__PURE__ */ new Set(), this.queue = [], this.batch = null, this.last = Promise.resolve(), this.error = null, typeof e != "string" || !e) throw TypeError("IndexedDBChunkStore name must be a non-empty string such as \"world-1\"");
|
|
52
|
+
if (typeof indexedDB > "u" || typeof CompressionStream != "function") throw new s("unsupported", "IndexedDBChunkStore needs IndexedDB and CompressionStream, which this runtime lacks; use MemoryChunkStore");
|
|
53
|
+
this.name = e;
|
|
54
|
+
let t = indexedDB.open(e, 1);
|
|
55
|
+
t.onupgradeneeded = () => t.result.createObjectStore(d), this.db = p(t, e, "open its database"), this.db.then((e) => {
|
|
56
|
+
e.onversionchange = () => e.close();
|
|
57
|
+
}, () => {});
|
|
58
|
+
}
|
|
59
|
+
async get(e, n, i) {
|
|
60
|
+
let a = c(e, n, i), o = this.pending.get(t(...a));
|
|
61
|
+
if (o) return o.bytes ? r(await o.bytes) : void 0;
|
|
62
|
+
let s;
|
|
63
|
+
try {
|
|
64
|
+
s = await p((await this.db).transaction(d).objectStore(d).get(a), this.name, "read");
|
|
65
|
+
} catch (e) {
|
|
66
|
+
throw f(this.name, "read", e);
|
|
67
|
+
}
|
|
68
|
+
return s === void 0 ? void 0 : r(s);
|
|
69
|
+
}
|
|
70
|
+
put(e, t, n, r) {
|
|
71
|
+
let a = c(e, t, n);
|
|
72
|
+
return this.write(a, i(l(r)));
|
|
73
|
+
}
|
|
74
|
+
delete(e, t, n) {
|
|
75
|
+
return this.write(c(e, t, n), null);
|
|
76
|
+
}
|
|
77
|
+
async flush() {
|
|
78
|
+
await Promise.allSettled(this.writing);
|
|
79
|
+
let e = this.error;
|
|
80
|
+
if (this.error = null, e) throw e;
|
|
81
|
+
}
|
|
82
|
+
write(e, n) {
|
|
83
|
+
let r = {
|
|
84
|
+
key: e,
|
|
85
|
+
bytes: n
|
|
86
|
+
};
|
|
87
|
+
if (n?.catch(() => {}), this.pending.set(t(...e), r), this.queue.push(r), this.batch) return this.batch;
|
|
88
|
+
let i = this.last, a = new Promise((e) => setTimeout(e, 0)).then(async () => {
|
|
89
|
+
let e = this.queue;
|
|
90
|
+
this.queue = [], this.batch = null;
|
|
91
|
+
try {
|
|
92
|
+
await i.catch(() => {});
|
|
93
|
+
let t = await Promise.all(e.map((e) => e.bytes)), n = (await this.db).transaction(d, "readwrite"), r = n.objectStore(d);
|
|
94
|
+
e.forEach((e, n) => e.bytes ? r.put(t[n], e.key) : r.delete(e.key)), await m(n, this.name);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
let t = f(this.name, "write", e);
|
|
97
|
+
throw this.error ??= t, t;
|
|
98
|
+
} finally {
|
|
99
|
+
for (let n of e) {
|
|
100
|
+
let e = t(...n.key);
|
|
101
|
+
this.pending.get(e) === n && this.pending.delete(e);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return this.batch = this.last = a, this.writing.add(a), a.then(() => this.writing.delete(a), () => this.writing.delete(a)), a;
|
|
106
|
+
}
|
|
107
|
+
}, g = /* @__PURE__ */ new WeakSet(), _ = 0, v = (e) => e << 5 >> 5 === e, y = (e, t, n, r) => {
|
|
108
|
+
if (typeof t != "number") throw TypeError(`VoxelStreamer ${e} must be a number`);
|
|
109
|
+
if (!(t === r || Number.isInteger(t) && t >= n && t <= r)) throw RangeError(`VoxelStreamer ${e} is ${t}; pass an integer in ${n}..${r}`);
|
|
110
|
+
return t;
|
|
111
|
+
}, b = (e) => {
|
|
112
|
+
if (typeof e != "number") throw TypeError("VoxelStreamer frameBudget must be a number of milliseconds");
|
|
113
|
+
if (!(e >= 0)) throw RangeError(`VoxelStreamer frameBudget is ${e}; pass milliseconds >= 0 or Infinity`);
|
|
114
|
+
return e;
|
|
115
|
+
}, x = (e) => {
|
|
116
|
+
if (typeof e != "number") throw TypeError("VoxelStreamer memoryBudget must be a number of bytes");
|
|
117
|
+
if (!(e > 0)) throw RangeError(`VoxelStreamer memoryBudget is ${e}; pass bytes > 0 or Infinity`);
|
|
118
|
+
return e;
|
|
119
|
+
}, S = (e) => {
|
|
120
|
+
typeof reportError == "function" ? reportError(e) : console.error(e);
|
|
121
|
+
}, C = class r {
|
|
122
|
+
#e;
|
|
123
|
+
#t;
|
|
124
|
+
#n;
|
|
125
|
+
#r;
|
|
126
|
+
#i;
|
|
127
|
+
#a;
|
|
128
|
+
#o;
|
|
129
|
+
#s;
|
|
130
|
+
#c;
|
|
131
|
+
#l;
|
|
132
|
+
#u;
|
|
133
|
+
#d;
|
|
134
|
+
#f;
|
|
135
|
+
#p;
|
|
136
|
+
#m;
|
|
137
|
+
#h;
|
|
138
|
+
#g;
|
|
139
|
+
#_;
|
|
140
|
+
#v;
|
|
141
|
+
#y;
|
|
142
|
+
#b;
|
|
143
|
+
#x;
|
|
144
|
+
#S;
|
|
145
|
+
#C;
|
|
146
|
+
#w;
|
|
147
|
+
#T;
|
|
148
|
+
#E;
|
|
149
|
+
#D;
|
|
150
|
+
#O;
|
|
151
|
+
#k;
|
|
152
|
+
#A;
|
|
153
|
+
#j;
|
|
154
|
+
#M;
|
|
155
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
156
|
+
let n = new r(t), i = `voxel-streamer-${++_}`;
|
|
157
|
+
n.#s = e.camera, n.#c = e, n.onError = (t) => e.onError(t);
|
|
158
|
+
try {
|
|
159
|
+
let t = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),
|
|
160
|
+
name: i,
|
|
161
|
+
setup() {},
|
|
162
|
+
onPreUpdate: () => n.update(),
|
|
163
|
+
dispose: () => void n.dispose()
|
|
164
|
+
};
|
|
165
|
+
n.#l = t, e.use(t);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
throw n.dispose(), e;
|
|
168
|
+
}
|
|
169
|
+
return n;
|
|
170
|
+
}
|
|
171
|
+
constructor(r) {
|
|
172
|
+
if (this.viewers = [], this.onError = S, this.#u = "\0", this.#d = [], this.#f = /* @__PURE__ */ new Map(), this.#p = [], this.#m = 0, this.#h = 0, this.#g = !1, this.#_ = 0, this.#v = 0, this.#y = /* @__PURE__ */ new Map(), this.#b = /* @__PURE__ */ new Map(), this.#x = /* @__PURE__ */ new Map(), this.#S = /* @__PURE__ */ new Map(), this.#C = /* @__PURE__ */ new Map(), this.#w = /* @__PURE__ */ new Map(), this.#E = NaN, this.#D = 0, this.#O = 0, this.#j = !1, this.#M = Promise.resolve(), !(r?.world instanceof n)) throw TypeError("VoxelStreamer needs {world}: pass {world: new VoxelWorld({blocks})}");
|
|
173
|
+
let { world: i, generator: c, store: l = new u() } = r;
|
|
174
|
+
if (typeof l?.get != "function" || typeof l.put != "function" || typeof l.flush != "function") throw TypeError("VoxelStreamer store must be a ChunkStore (get, put, delete, flush)");
|
|
175
|
+
this.world = i, this.store = l, this.#e = y("distance", r.distance ?? 8, 0, 64), this.#t = y("verticalDistance", r.verticalDistance ?? 4, 0, 64), this.#r = y("chunksPerFrame", r.chunksPerFrame ?? 8, 1, Infinity), this.#i = b(r.frameBudget ?? .5), this.#n = x(r.memoryBudget ?? 134217728);
|
|
176
|
+
let d = globalThis.navigator?.hardwareConcurrency || 4;
|
|
177
|
+
this.workers = y("workers", r.workers ?? Math.max(1, Math.min(3, d >> 2)), 1, 64);
|
|
178
|
+
let f = typeof c == "string" || c instanceof URL;
|
|
179
|
+
if (c !== void 0 && !f) {
|
|
180
|
+
if (!c || typeof c != "object") throw TypeError("VoxelStreamer generator must be NoiseTerrainOptions or the URL of a module worker");
|
|
181
|
+
this.#o = structuredClone(c), o(this.#o)(new e(), 0, 1 << 20, 0, { blocks: i.blocks.table });
|
|
182
|
+
}
|
|
183
|
+
if (c !== void 0 && typeof Worker != "function") throw new s("unsupported", "VoxelStreamer generators run in Web Workers, which this context lacks");
|
|
184
|
+
if (g.has(i)) throw new s("attached", "VoxelWorld already has a VoxelStreamer; dispose() it before creating another");
|
|
185
|
+
g.add(i), c !== void 0 && (this.#a = new a(() => f ? new Worker(c, {
|
|
186
|
+
type: "module",
|
|
187
|
+
name: "margoengine-generator"
|
|
188
|
+
}) : new Worker(new URL("./workers/terrain.js?no-inline",
|
|
189
|
+
import.meta.url
|
|
190
|
+
), { name: "margoengine-terrain" }), this.workers)), this.#k = i.onChunk((e, n, r, i) => {
|
|
191
|
+
let a = t(e, n, r);
|
|
192
|
+
this.#T = void 0, i ? this.#y.has(a) || this.#y.set(a, {
|
|
193
|
+
cx: e,
|
|
194
|
+
cy: n,
|
|
195
|
+
cz: r,
|
|
196
|
+
chunk: i,
|
|
197
|
+
edited: !0,
|
|
198
|
+
used: this.#h
|
|
199
|
+
}) : (this.#y.delete(a), this.#m = 0);
|
|
200
|
+
}), this.#A = i.onChange((e, n, r) => {
|
|
201
|
+
let i = e >> 5, a = n >> 5, o = r >> 5;
|
|
202
|
+
(!this.#T || i !== this.#E || a !== this.#D || o !== this.#O) && (this.#E = i, this.#D = a, this.#O = o, this.#T = this.#y.get(t(i, a, o))), this.#T && (this.#T.edited = !0);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
get distance() {
|
|
206
|
+
return this.#e;
|
|
207
|
+
}
|
|
208
|
+
set distance(e) {
|
|
209
|
+
this.#e = y("distance", e, 0, 64), this.#u = "\0";
|
|
210
|
+
}
|
|
211
|
+
get verticalDistance() {
|
|
212
|
+
return this.#t;
|
|
213
|
+
}
|
|
214
|
+
set verticalDistance(e) {
|
|
215
|
+
this.#t = y("verticalDistance", e, 0, 64), this.#u = "\0";
|
|
216
|
+
}
|
|
217
|
+
get chunksPerFrame() {
|
|
218
|
+
return this.#r;
|
|
219
|
+
}
|
|
220
|
+
set chunksPerFrame(e) {
|
|
221
|
+
this.#r = y("chunksPerFrame", e, 1, Infinity);
|
|
222
|
+
}
|
|
223
|
+
get frameBudget() {
|
|
224
|
+
return this.#i;
|
|
225
|
+
}
|
|
226
|
+
set frameBudget(e) {
|
|
227
|
+
this.#i = b(e);
|
|
228
|
+
}
|
|
229
|
+
get memoryBudget() {
|
|
230
|
+
return this.#n;
|
|
231
|
+
}
|
|
232
|
+
set memoryBudget(e) {
|
|
233
|
+
this.#n = x(e);
|
|
234
|
+
}
|
|
235
|
+
get pending() {
|
|
236
|
+
let e = 0;
|
|
237
|
+
for (let t of this.#f.keys()) !this.#y.has(t) && !this.#S.has(t) && e++;
|
|
238
|
+
return e;
|
|
239
|
+
}
|
|
240
|
+
update() {
|
|
241
|
+
if (this.#j) throw new s("disposed", "VoxelStreamer is disposed; create a new VoxelStreamer");
|
|
242
|
+
let e = [];
|
|
243
|
+
for (let t of this.#s ? [this.#s.position, ...this.viewers] : this.viewers) {
|
|
244
|
+
if (typeof t?.[0] != "number" || typeof t[1] != "number" || typeof t[2] != "number") throw TypeError("VoxelStreamer viewers must be [x, y, z] positions in blocks");
|
|
245
|
+
if (!Number.isFinite(t[0] + t[1] + t[2])) throw RangeError(`VoxelStreamer viewer [${t[0]}, ${t[1]}, ${t[2]}] is not finite; pass finite positions`);
|
|
246
|
+
e.push(Math.floor(t[0] / 32), Math.floor(t[1] / 32), Math.floor(t[2] / 32));
|
|
247
|
+
}
|
|
248
|
+
let t = e.join();
|
|
249
|
+
t !== this.#u && (this.#u = t, this.#d = e, this.#P()), this.#I(), this.#F(), this.#L();
|
|
250
|
+
}
|
|
251
|
+
save() {
|
|
252
|
+
return this.#j ? Promise.reject(new s("disposed", "VoxelStreamer is disposed; its dispose() saved already")) : this.#V();
|
|
253
|
+
}
|
|
254
|
+
dispose() {
|
|
255
|
+
if (this.#j) return this.#M;
|
|
256
|
+
this.#j = !0, this.#k(), this.#A(), g.delete(this.world);
|
|
257
|
+
for (let e of this.#b.values()) e.abort();
|
|
258
|
+
return this.#b.clear(), this.#x.clear(), this.#a?.dispose(), this.#M = this.#V(), this.#l && this.#c?.sceneOf(this.#l)?.removePlugin(this.#l.name), this.#M;
|
|
259
|
+
}
|
|
260
|
+
[Symbol.dispose]() {
|
|
261
|
+
this.dispose();
|
|
262
|
+
}
|
|
263
|
+
#N(e, t, n) {
|
|
264
|
+
let r = this.#e + 1, i = this.#t + 1, a = this.#d;
|
|
265
|
+
for (let o = 0; o < a.length; o += 3) {
|
|
266
|
+
let s = e - a[o], c = n - a[o + 2];
|
|
267
|
+
if (s * s + c * c <= r * r && Math.abs(t - a[o + 1]) <= i) return !0;
|
|
268
|
+
}
|
|
269
|
+
return !1;
|
|
270
|
+
}
|
|
271
|
+
#P() {
|
|
272
|
+
let e = this.#e, n = this.#t, r = this.#d, { minY: i, maxY: a } = this.world, o = /* @__PURE__ */ new Map();
|
|
273
|
+
for (let s = 0; s < r.length; s += 3) for (let c = -e; c <= e; c++) for (let l = -e; l <= e; l++) {
|
|
274
|
+
let u = r[s] + c, d = r[s + 2] + l;
|
|
275
|
+
if (!(c * c + l * l > e * e || !v(u) || !v(d))) for (let e = -n; e <= n; e++) {
|
|
276
|
+
let n = r[s + 1] + e;
|
|
277
|
+
if (n * 32 > a || n * 32 + 31 < i) continue;
|
|
278
|
+
let f = t(u, n, d), p = c * c + e * e + l * l, m = o.get(f);
|
|
279
|
+
m ? p < m.d && (m.d = p) : o.set(f, {
|
|
280
|
+
key: f,
|
|
281
|
+
cx: u,
|
|
282
|
+
cy: n,
|
|
283
|
+
cz: d,
|
|
284
|
+
d: p
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
this.#f = o, this.#p = [...o.values()].sort((e, t) => e.d - t.d), this.#m = 0;
|
|
289
|
+
let s = ++this.#h;
|
|
290
|
+
for (let [e, t] of this.#y) o.has(e) ? t.used = s : this.#N(t.cx, t.cy, t.cz) || this.#z(e, t);
|
|
291
|
+
for (let [e, t] of this.#b) {
|
|
292
|
+
let [n, r, i] = e.split(",").map(Number);
|
|
293
|
+
this.#N(n, r, i) || (t.abort(), this.#b.delete(e));
|
|
294
|
+
}
|
|
295
|
+
for (let [e, t] of this.#x) this.#N(t.cx, t.cy, t.cz) || this.#x.delete(e);
|
|
296
|
+
for (let [e, t] of this.#S) this.#N(t.cx, t.cy, t.cz) || this.#S.delete(e);
|
|
297
|
+
let c = (e + 1) * (e + 1);
|
|
298
|
+
for (let e of this.#w.keys()) {
|
|
299
|
+
let [t, n] = e.split(",").map(Number), i = !1;
|
|
300
|
+
for (let e = 0; e < r.length && !i; e += 3) i = (t - r[e]) ** 2 + (n - r[e + 2]) ** 2 <= c;
|
|
301
|
+
i || this.#w.delete(e);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
#F(e = 0) {
|
|
305
|
+
let t = 0;
|
|
306
|
+
for (let e of this.#y.values()) t += e.chunk.byteLength;
|
|
307
|
+
if (this.#v = this.#y.size ? t / this.#y.size : 0, t + e > this.#n) for (let [n, r] of [...this.#y].filter(([e]) => !this.#f.has(e)).sort((e, t) => e[1].used - t[1].used)) {
|
|
308
|
+
if (t + e <= this.#n) break;
|
|
309
|
+
t -= r.chunk.byteLength, this.#z(n, r);
|
|
310
|
+
}
|
|
311
|
+
this.#g = t > this.#n, this.#_ = this.#n - t;
|
|
312
|
+
}
|
|
313
|
+
#I() {
|
|
314
|
+
if (!this.#x.size) return;
|
|
315
|
+
this.#F();
|
|
316
|
+
let t = (e) => this.#f.get(e)?.d ?? Infinity, n = [...this.#x].sort((e, n) => t(e[0]) - t(n[0])), r = performance.now(), i = 0;
|
|
317
|
+
for (let [t, a] of n) {
|
|
318
|
+
if (i === this.#r || i && performance.now() - r >= this.#i) break;
|
|
319
|
+
let { cx: n, cy: o, cz: s, edited: c } = a;
|
|
320
|
+
if (this.world.getChunk(n, o, s)) {
|
|
321
|
+
this.#x.delete(t);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
let l;
|
|
325
|
+
try {
|
|
326
|
+
l = a.chunk instanceof e ? a.chunk : e.unpack(a.chunk);
|
|
327
|
+
} catch (e) {
|
|
328
|
+
this.#x.delete(t), this.#S.set(t, a), this.onError(e);
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (a.chunk = l, l.byteLength > this.#_ && this.#F(l.byteLength), l.byteLength > this.#_) break;
|
|
332
|
+
this.#x.delete(t), c && this.#C.delete(t), this.#y.set(t, {
|
|
333
|
+
cx: n,
|
|
334
|
+
cy: o,
|
|
335
|
+
cz: s,
|
|
336
|
+
chunk: l,
|
|
337
|
+
edited: c,
|
|
338
|
+
used: this.#f.has(t) ? this.#h : 0
|
|
339
|
+
}), this.world.addChunk(n, o, s, l), this.#_ -= l.byteLength, i++;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
#L(e = !1) {
|
|
343
|
+
if (this.#g || this.#j || e && !this.#v) return;
|
|
344
|
+
let t = Math.max(4 * this.workers, Math.min(this.#r, 64));
|
|
345
|
+
for (let n = this.#p; this.#m < n.length; this.#m++) {
|
|
346
|
+
if (this.#b.size >= 4 * this.workers || this.#x.size >= t) return;
|
|
347
|
+
let r = n[this.#m], { key: i } = r;
|
|
348
|
+
if (this.#y.has(i) || this.#b.has(i) || this.#x.has(i) || this.#S.has(i)) continue;
|
|
349
|
+
let a = (this.#b.size + this.#x.size + 1) * this.#v;
|
|
350
|
+
if (a > this.#_ && !e && this.#F(a), a > this.#_) return;
|
|
351
|
+
let o = this.#C.get(i);
|
|
352
|
+
o ? this.#x.set(i, o) : this.#R(r);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
#R(t) {
|
|
356
|
+
let { key: n, cx: r, cy: i, cz: a, d: o } = t, s = new AbortController(), c = this.#a, l = `${r},${a}`;
|
|
357
|
+
this.#b.set(n, s), (async () => {
|
|
358
|
+
let t = await this.store.get(r, i, a);
|
|
359
|
+
return t || !c || i * 32 > (this.#w.get(l) ?? Infinity) ? t ?? new e() : (s.signal.throwIfAborted(), c.run(() => ({
|
|
360
|
+
cx: r,
|
|
361
|
+
cy: i,
|
|
362
|
+
cz: a,
|
|
363
|
+
blocks: this.world.blocks.table,
|
|
364
|
+
terrain: this.#o
|
|
365
|
+
}), {
|
|
366
|
+
priority: o,
|
|
367
|
+
signal: s.signal
|
|
368
|
+
}));
|
|
369
|
+
})().then((t) => {
|
|
370
|
+
this.#b.get(n) === s && (this.#b.delete(n), !(t instanceof e) && Number.isInteger(t.ceiling) && this.#w.set(l, t.ceiling), this.#x.set(n, {
|
|
371
|
+
cx: r,
|
|
372
|
+
cy: i,
|
|
373
|
+
cz: a,
|
|
374
|
+
chunk: t,
|
|
375
|
+
edited: !1,
|
|
376
|
+
used: 0
|
|
377
|
+
}), this.#L(!0));
|
|
378
|
+
}, (e) => {
|
|
379
|
+
this.#b.get(n) === s && (this.#b.delete(n), this.#S.set(n, t), this.onError(e), this.#L(!0));
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
#z(e, t) {
|
|
383
|
+
this.#y.delete(e), t.edited && this.#B(e, t).catch((e) => this.onError(e)), this.world.removeChunk(t.cx, t.cy, t.cz);
|
|
384
|
+
}
|
|
385
|
+
#B(e, t) {
|
|
386
|
+
return this.#C.set(e, t), this.store.put(t.cx, t.cy, t.cz, t.chunk).then(() => {
|
|
387
|
+
this.#C.get(e) === t && this.#C.delete(e);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
async #V() {
|
|
391
|
+
let e = [];
|
|
392
|
+
for (let t of this.#y.values()) t.edited && (t.edited = !1, e.push(this.store.put(t.cx, t.cy, t.cz, t.chunk).catch((e) => {
|
|
393
|
+
throw t.edited = !0, e;
|
|
394
|
+
})));
|
|
395
|
+
for (let [t, n] of this.#C) e.push(this.#B(t, n));
|
|
396
|
+
let t = await Promise.allSettled(e);
|
|
397
|
+
await this.store.flush();
|
|
398
|
+
for (let e of t) if (e.status === "rejected") throw e.reason;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
//#endregion
|
|
402
|
+
export { h as IndexedDBChunkStore, u as MemoryChunkStore, C as VoxelStreamer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|