minerender 2.1.0-alpha.85 → 2.1.0-alpha.87
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/dist/browser/index.d.mts +161 -3
- package/dist/browser/index.d.ts +161 -3
- package/dist/browser/index.js +469 -4
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/index.mjs +469 -4
- package/dist/browser/index.mjs.map +1 -1
- package/dist/bundle.js +483 -4
- package/dist/bundle.js.map +1 -1
- package/dist/node/index.d.mts +161 -3
- package/dist/node/index.d.ts +161 -3
- package/dist/node/index.js +472 -4
- package/dist/node/index.js.map +1 -1
- package/dist/node/index.mjs +469 -4
- package/dist/node/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/browser/index.mjs
CHANGED
|
@@ -22024,6 +22024,131 @@ function copyDataTexture(source) {
|
|
|
22024
22024
|
return new Texture3().copy(copySource);
|
|
22025
22025
|
}
|
|
22026
22026
|
|
|
22027
|
+
// src/export/VideoExporter.ts
|
|
22028
|
+
var VideoExporter = class {
|
|
22029
|
+
constructor(source, options) {
|
|
22030
|
+
this.source = source;
|
|
22031
|
+
this.chunks = [];
|
|
22032
|
+
this.settled = false;
|
|
22033
|
+
this.stopping = false;
|
|
22034
|
+
this.onStart = () => {
|
|
22035
|
+
this.timer = setTimeout(() => {
|
|
22036
|
+
this.stopping = true;
|
|
22037
|
+
try {
|
|
22038
|
+
this.recorder.stop();
|
|
22039
|
+
} catch (error) {
|
|
22040
|
+
this.cancel(error);
|
|
22041
|
+
}
|
|
22042
|
+
}, this.duration * 1e3);
|
|
22043
|
+
};
|
|
22044
|
+
this.onData = (event) => {
|
|
22045
|
+
if (event.data.size) this.chunks.push(event.data);
|
|
22046
|
+
};
|
|
22047
|
+
this.onStop = () => {
|
|
22048
|
+
if (!this.stopping) return this.cancel(new Error("Video recording stopped before its duration elapsed"));
|
|
22049
|
+
if (!this.chunks.length) return this.cancel(new Error("The browser produced no video data"));
|
|
22050
|
+
const mimeType = this.chunks.find((chunk) => chunk.type)?.type || this.recorder.mimeType;
|
|
22051
|
+
const blob = new Blob(this.chunks, { type: mimeType });
|
|
22052
|
+
this.settled = true;
|
|
22053
|
+
this.cleanup();
|
|
22054
|
+
this.resolve(blob);
|
|
22055
|
+
};
|
|
22056
|
+
this.onError = (event) => {
|
|
22057
|
+
this.cancel(event.error ?? new Error("Video recording failed"));
|
|
22058
|
+
};
|
|
22059
|
+
this.onAbort = () => {
|
|
22060
|
+
this.cancel(this.signal.reason);
|
|
22061
|
+
};
|
|
22062
|
+
this.onTrackEnded = () => {
|
|
22063
|
+
this.cancel(new Error("The video capture stream stopped producing frames"));
|
|
22064
|
+
};
|
|
22065
|
+
const fps = options.fps ?? 30;
|
|
22066
|
+
if (!Number.isFinite(options.duration) || options.duration <= 0 || options.duration * 1e3 > 2147483647) {
|
|
22067
|
+
throw new RangeError("Video duration must be greater than zero and at most 2147483.647 seconds");
|
|
22068
|
+
}
|
|
22069
|
+
if (!Number.isFinite(fps) || fps <= 0) throw new RangeError("Video fps must be a finite positive number");
|
|
22070
|
+
const bitrate = options.videoBitsPerSecond;
|
|
22071
|
+
if (bitrate !== void 0 && (!Number.isInteger(bitrate) || bitrate <= 0 || bitrate > 4294967295)) {
|
|
22072
|
+
throw new RangeError("Video bitrate must be a positive unsigned 32-bit integer");
|
|
22073
|
+
}
|
|
22074
|
+
if (!source.width || !source.height) throw new Error("Cannot export video from an empty canvas");
|
|
22075
|
+
options.signal?.throwIfAborted();
|
|
22076
|
+
if (typeof document === "undefined" || typeof MediaRecorder === "undefined" || typeof MediaRecorder.isTypeSupported !== "function") {
|
|
22077
|
+
throw new Error("Video export requires a browser with MediaRecorder support");
|
|
22078
|
+
}
|
|
22079
|
+
const mimeTypes = options.mimeType === void 0 ? ["video/webm;codecs=vp9", "video/webm;codecs=vp8", "video/webm", "video/mp4"] : [options.mimeType];
|
|
22080
|
+
const mimeType = mimeTypes.find((type) => type.startsWith("video/") && MediaRecorder.isTypeSupported(type));
|
|
22081
|
+
if (!mimeType) throw new Error("The browser does not support the requested video format");
|
|
22082
|
+
this.canvas = document.createElement("canvas");
|
|
22083
|
+
this.canvas.width = source.width;
|
|
22084
|
+
this.canvas.height = source.height;
|
|
22085
|
+
if (typeof this.canvas.captureStream !== "function") throw new Error("Video export requires canvas captureStream support");
|
|
22086
|
+
const context = this.canvas.getContext("2d", { alpha: false });
|
|
22087
|
+
if (!context) throw new Error("Could not create a canvas for video export");
|
|
22088
|
+
this.context = context;
|
|
22089
|
+
this.signal = options.signal;
|
|
22090
|
+
this.duration = options.duration;
|
|
22091
|
+
this.result = new Promise((resolve, reject) => {
|
|
22092
|
+
this.resolve = resolve;
|
|
22093
|
+
this.reject = reject;
|
|
22094
|
+
});
|
|
22095
|
+
try {
|
|
22096
|
+
this.stream = this.canvas.captureStream(fps);
|
|
22097
|
+
this.recorder = new MediaRecorder(this.stream, { mimeType, videoBitsPerSecond: bitrate });
|
|
22098
|
+
this.recorder.addEventListener("start", this.onStart);
|
|
22099
|
+
this.recorder.addEventListener("dataavailable", this.onData);
|
|
22100
|
+
this.recorder.addEventListener("stop", this.onStop);
|
|
22101
|
+
this.recorder.addEventListener("error", this.onError);
|
|
22102
|
+
for (const track of this.stream.getTracks()) {
|
|
22103
|
+
track.addEventListener("ended", this.onTrackEnded);
|
|
22104
|
+
}
|
|
22105
|
+
this.signal?.addEventListener("abort", this.onAbort, { once: true });
|
|
22106
|
+
this.recorder.start();
|
|
22107
|
+
} catch (error) {
|
|
22108
|
+
this.cancel(error);
|
|
22109
|
+
}
|
|
22110
|
+
}
|
|
22111
|
+
/** Copies a freshly drawn source frame before WebGL can clear its drawing buffer. */
|
|
22112
|
+
capture() {
|
|
22113
|
+
if (this.settled || this.stopping) return;
|
|
22114
|
+
try {
|
|
22115
|
+
this.context.fillStyle = "#000000";
|
|
22116
|
+
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
22117
|
+
this.context.drawImage(this.source, 0, 0, this.canvas.width, this.canvas.height);
|
|
22118
|
+
} catch (error) {
|
|
22119
|
+
this.cancel(error);
|
|
22120
|
+
}
|
|
22121
|
+
}
|
|
22122
|
+
/** Cancels recording, releases its capture stream, and rejects {@link result}. */
|
|
22123
|
+
cancel(reason = new DOMException("Video export cancelled", "AbortError")) {
|
|
22124
|
+
if (this.settled) return;
|
|
22125
|
+
this.settled = true;
|
|
22126
|
+
this.cleanup();
|
|
22127
|
+
this.reject(reason);
|
|
22128
|
+
}
|
|
22129
|
+
cleanup() {
|
|
22130
|
+
if (this.timer !== void 0) clearTimeout(this.timer);
|
|
22131
|
+
this.signal?.removeEventListener("abort", this.onAbort);
|
|
22132
|
+
if (this.recorder) {
|
|
22133
|
+
this.recorder.removeEventListener("start", this.onStart);
|
|
22134
|
+
this.recorder.removeEventListener("dataavailable", this.onData);
|
|
22135
|
+
this.recorder.removeEventListener("stop", this.onStop);
|
|
22136
|
+
this.recorder.removeEventListener("error", this.onError);
|
|
22137
|
+
if (this.recorder.state !== "inactive") {
|
|
22138
|
+
try {
|
|
22139
|
+
this.recorder.stop();
|
|
22140
|
+
} catch {
|
|
22141
|
+
}
|
|
22142
|
+
}
|
|
22143
|
+
}
|
|
22144
|
+
for (const track of this.stream?.getTracks() ?? []) {
|
|
22145
|
+
track.removeEventListener("ended", this.onTrackEnded);
|
|
22146
|
+
track.stop();
|
|
22147
|
+
}
|
|
22148
|
+
this.chunks.length = 0;
|
|
22149
|
+
}
|
|
22150
|
+
};
|
|
22151
|
+
|
|
22027
22152
|
// src/gui/GuiHelper.ts
|
|
22028
22153
|
init_AssetKey();
|
|
22029
22154
|
init_ModelTextures();
|
|
@@ -27341,6 +27466,9 @@ var _Renderer = class _Renderer {
|
|
|
27341
27466
|
this._inAnimationLoop = true;
|
|
27342
27467
|
try {
|
|
27343
27468
|
this.animate(time);
|
|
27469
|
+
} catch (error) {
|
|
27470
|
+
this._videoExporter?.cancel(error);
|
|
27471
|
+
throw error;
|
|
27344
27472
|
} finally {
|
|
27345
27473
|
this._inAnimationLoop = false;
|
|
27346
27474
|
}
|
|
@@ -27535,16 +27663,18 @@ var _Renderer = class _Renderer {
|
|
|
27535
27663
|
if (this._frameCallbacks.get(callback) === subscription) this._frameCallbacks.delete(callback);
|
|
27536
27664
|
};
|
|
27537
27665
|
}
|
|
27538
|
-
/** Starts or resumes the animation loop and registered frame callbacks. */
|
|
27666
|
+
/** Starts or resumes the animation loop and registered frame callbacks. Has no effect while running. */
|
|
27539
27667
|
start() {
|
|
27540
|
-
if (this._disposed) return;
|
|
27668
|
+
if (this._disposed || this._running) return;
|
|
27541
27669
|
this.stop();
|
|
27542
27670
|
this._running = true;
|
|
27543
27671
|
if (!this._inAnimationLoop) this.renderer.setAnimationLoop(this._animationLoop);
|
|
27544
27672
|
}
|
|
27545
|
-
/** Pauses rendering and frame callbacks. Call {@link start} to resume. */
|
|
27673
|
+
/** Pauses rendering and frame callbacks, and cancels video export. Call {@link start} to resume. */
|
|
27546
27674
|
stop() {
|
|
27547
27675
|
if (this._disposed) return;
|
|
27676
|
+
this._videoExporter?.cancel();
|
|
27677
|
+
this._videoExporter = void 0;
|
|
27548
27678
|
this._running = false;
|
|
27549
27679
|
if (this._inAnimationLoop) {
|
|
27550
27680
|
queueMicrotask(() => {
|
|
@@ -27606,7 +27736,7 @@ var _Renderer = class _Renderer {
|
|
|
27606
27736
|
this._controls.update();
|
|
27607
27737
|
}
|
|
27608
27738
|
if (!this._running) return;
|
|
27609
|
-
if (!this.dirty && !this.options.render.renderAlways && !this._frameCallbacks.size) return;
|
|
27739
|
+
if (!this.dirty && !this.options.render.renderAlways && !this._frameCallbacks.size && !this._videoExporter) return;
|
|
27610
27740
|
const interval = this._frameInterval;
|
|
27611
27741
|
if (interval) {
|
|
27612
27742
|
const next = this._nextFrameTime;
|
|
@@ -27631,6 +27761,7 @@ var _Renderer = class _Renderer {
|
|
|
27631
27761
|
} else {
|
|
27632
27762
|
this.renderer.render(this.scene, this.camera);
|
|
27633
27763
|
}
|
|
27764
|
+
this._videoExporter?.capture();
|
|
27634
27765
|
this.dirty = false;
|
|
27635
27766
|
if (this._stats) {
|
|
27636
27767
|
this._stats.end();
|
|
@@ -27651,6 +27782,35 @@ var _Renderer = class _Renderer {
|
|
|
27651
27782
|
const canvas = trim ? trimCanvas(this.renderer.domElement) : this.renderer.domElement;
|
|
27652
27783
|
return canvas.toDataURL(mime, quality);
|
|
27653
27784
|
}
|
|
27785
|
+
/**
|
|
27786
|
+
* Records a silent browser video in real time, including frame callbacks and animated textures.
|
|
27787
|
+
* Keep the tab visible; browser scheduling and `render.fpsLimit` limit the capture rate.
|
|
27788
|
+
* The video keeps the initial drawing-buffer size and fills transparent pixels with black.
|
|
27789
|
+
* Temporarily starts a stopped renderer. Stop, disposal, or `options.signal` cancels the export.
|
|
27790
|
+
* Only one export can run at a time. Use the returned Blob's type to select a file extension.
|
|
27791
|
+
*/
|
|
27792
|
+
async toVideo(options) {
|
|
27793
|
+
if (this._disposed) throw new Error("Cannot export a video from a disposed renderer");
|
|
27794
|
+
if (this._videoExporter) throw new Error("A video export is already running");
|
|
27795
|
+
const exporter = new VideoExporter(this.renderer.domElement, options);
|
|
27796
|
+
const wasRunning = this._running;
|
|
27797
|
+
try {
|
|
27798
|
+
try {
|
|
27799
|
+
if (!wasRunning) this.start();
|
|
27800
|
+
this._videoExporter = exporter;
|
|
27801
|
+
if (this._controls?.enabled) this._controls.update();
|
|
27802
|
+
this.drawFrame();
|
|
27803
|
+
} catch (error) {
|
|
27804
|
+
exporter.cancel(error);
|
|
27805
|
+
}
|
|
27806
|
+
return await exporter.result;
|
|
27807
|
+
} finally {
|
|
27808
|
+
if (this._videoExporter === exporter) {
|
|
27809
|
+
this._videoExporter = void 0;
|
|
27810
|
+
if (!wasRunning) this.stop();
|
|
27811
|
+
}
|
|
27812
|
+
}
|
|
27813
|
+
}
|
|
27654
27814
|
/** Exports visible scene meshes as OBJ text, without texture images. */
|
|
27655
27815
|
toObj() {
|
|
27656
27816
|
return SceneExporter.toObj(this.scene);
|
|
@@ -28602,6 +28762,134 @@ var AnvilParser = class {
|
|
|
28602
28762
|
}
|
|
28603
28763
|
};
|
|
28604
28764
|
|
|
28765
|
+
// src/world/AnvilWorldSource.ts
|
|
28766
|
+
init_MineRenderError();
|
|
28767
|
+
async function abortable2(promise, signal) {
|
|
28768
|
+
if (!signal) return promise;
|
|
28769
|
+
let abort;
|
|
28770
|
+
const cancelled = new Promise((_, reject) => {
|
|
28771
|
+
abort = () => reject(signal.reason);
|
|
28772
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
28773
|
+
if (signal.aborted) abort();
|
|
28774
|
+
});
|
|
28775
|
+
try {
|
|
28776
|
+
return await Promise.race([promise, cancelled]);
|
|
28777
|
+
} finally {
|
|
28778
|
+
signal.removeEventListener("abort", abort);
|
|
28779
|
+
}
|
|
28780
|
+
}
|
|
28781
|
+
var AnvilWorldSource = class {
|
|
28782
|
+
constructor(readRegion, options = {}) {
|
|
28783
|
+
this.readRegion = readRegion;
|
|
28784
|
+
this.regions = /* @__PURE__ */ new Map();
|
|
28785
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
28786
|
+
this.cachedBytes = 0;
|
|
28787
|
+
this.maxCachedRegions = options.maxCachedRegions ?? 4;
|
|
28788
|
+
this.maxCachedBytes = options.maxCachedBytes ?? 64 * 1024 * 1024;
|
|
28789
|
+
for (const [name, value] of Object.entries({ maxCachedRegions: this.maxCachedRegions, maxCachedBytes: this.maxCachedBytes })) {
|
|
28790
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a nonnegative safe integer`);
|
|
28791
|
+
}
|
|
28792
|
+
}
|
|
28793
|
+
/** Decodes one column at absolute chunk coordinates. Aborting a caller leaves other shared readers active. */
|
|
28794
|
+
async getChunk(x, z, signal) {
|
|
28795
|
+
if (!Number.isSafeInteger(x) || !Number.isSafeInteger(z)) {
|
|
28796
|
+
throw new RangeError("Chunk column coordinates must be safe integers");
|
|
28797
|
+
}
|
|
28798
|
+
const regionX = Math.floor(x / 32), regionZ = Math.floor(z / 32);
|
|
28799
|
+
const data = await this.getRegion(regionX, regionZ, signal);
|
|
28800
|
+
signal?.throwIfAborted();
|
|
28801
|
+
if (!data) return void 0;
|
|
28802
|
+
const chunk = await abortable2(AnvilParser.parseChunk(data, x - regionX * 32, z - regionZ * 32), signal);
|
|
28803
|
+
signal?.throwIfAborted();
|
|
28804
|
+
if (chunk && (chunk.x !== x || chunk.z !== z)) {
|
|
28805
|
+
throw new MineRenderError(`Anvil chunk coordinates ${chunk.x},${chunk.z} do not match requested column ${x},${z}`);
|
|
28806
|
+
}
|
|
28807
|
+
return chunk;
|
|
28808
|
+
}
|
|
28809
|
+
/** Releases cached regions. Reads already in progress finish without repopulating this cache. */
|
|
28810
|
+
clearCache() {
|
|
28811
|
+
this.regions.clear();
|
|
28812
|
+
this.pending.clear();
|
|
28813
|
+
this.cachedBytes = 0;
|
|
28814
|
+
}
|
|
28815
|
+
getRegion(x, z, signal) {
|
|
28816
|
+
signal?.throwIfAborted();
|
|
28817
|
+
const key = `${x},${z}`;
|
|
28818
|
+
const cached2 = this.regions.get(key);
|
|
28819
|
+
if (cached2) {
|
|
28820
|
+
this.regions.delete(key);
|
|
28821
|
+
this.regions.set(key, cached2);
|
|
28822
|
+
return Promise.resolve(cached2.data);
|
|
28823
|
+
}
|
|
28824
|
+
let loading = this.pending.get(key);
|
|
28825
|
+
if (!loading) {
|
|
28826
|
+
const controller = new AbortController();
|
|
28827
|
+
loading = {
|
|
28828
|
+
controller,
|
|
28829
|
+
waiters: 0,
|
|
28830
|
+
settled: false,
|
|
28831
|
+
promise: Promise.resolve().then(() => {
|
|
28832
|
+
controller.signal.throwIfAborted();
|
|
28833
|
+
return this.readRegion(x, z, controller.signal);
|
|
28834
|
+
}).then((data) => {
|
|
28835
|
+
controller.signal.throwIfAborted();
|
|
28836
|
+
let bytes = data instanceof Uint8Array ? data : data === void 0 ? void 0 : new Uint8Array(data);
|
|
28837
|
+
if (bytes && bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
|
|
28838
|
+
if (bytes) AnvilParser.getChunkList(bytes);
|
|
28839
|
+
loading.settled = true;
|
|
28840
|
+
if (this.pending.get(key) === loading) this.cacheRegion(key, bytes);
|
|
28841
|
+
return bytes;
|
|
28842
|
+
}).finally(() => {
|
|
28843
|
+
loading.settled = true;
|
|
28844
|
+
if (this.pending.get(key) === loading) this.pending.delete(key);
|
|
28845
|
+
})
|
|
28846
|
+
};
|
|
28847
|
+
this.pending.set(key, loading);
|
|
28848
|
+
}
|
|
28849
|
+
const read = loading;
|
|
28850
|
+
read.waiters++;
|
|
28851
|
+
return new Promise((resolve, reject) => {
|
|
28852
|
+
let finished = false;
|
|
28853
|
+
const release = () => {
|
|
28854
|
+
if (finished) return false;
|
|
28855
|
+
finished = true;
|
|
28856
|
+
signal?.removeEventListener("abort", abort);
|
|
28857
|
+
read.waiters--;
|
|
28858
|
+
return true;
|
|
28859
|
+
};
|
|
28860
|
+
const abort = () => {
|
|
28861
|
+
if (!release()) return;
|
|
28862
|
+
if (!read.waiters && !read.settled) {
|
|
28863
|
+
if (this.pending.get(key) === read) this.pending.delete(key);
|
|
28864
|
+
read.controller.abort(signal.reason);
|
|
28865
|
+
}
|
|
28866
|
+
reject(signal.reason);
|
|
28867
|
+
};
|
|
28868
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
28869
|
+
read.promise.then(
|
|
28870
|
+
(data) => {
|
|
28871
|
+
if (release()) resolve(data);
|
|
28872
|
+
},
|
|
28873
|
+
(error) => {
|
|
28874
|
+
if (release()) reject(error);
|
|
28875
|
+
}
|
|
28876
|
+
);
|
|
28877
|
+
if (signal?.aborted) abort();
|
|
28878
|
+
});
|
|
28879
|
+
}
|
|
28880
|
+
cacheRegion(key, data) {
|
|
28881
|
+
const size = data?.byteLength ?? 0;
|
|
28882
|
+
if (!this.maxCachedRegions || size > this.maxCachedBytes) return;
|
|
28883
|
+
this.regions.set(key, { data, size });
|
|
28884
|
+
this.cachedBytes += size;
|
|
28885
|
+
while (this.regions.size > this.maxCachedRegions || this.cachedBytes > this.maxCachedBytes) {
|
|
28886
|
+
const oldest = this.regions.keys().next().value;
|
|
28887
|
+
this.cachedBytes -= this.regions.get(oldest).size;
|
|
28888
|
+
this.regions.delete(oldest);
|
|
28889
|
+
}
|
|
28890
|
+
}
|
|
28891
|
+
};
|
|
28892
|
+
|
|
28605
28893
|
// src/world/Chunk.ts
|
|
28606
28894
|
import { Vector3 as Vector317 } from "three";
|
|
28607
28895
|
init_BlockStates();
|
|
@@ -29276,9 +29564,184 @@ var Chunk = class _Chunk {
|
|
|
29276
29564
|
);
|
|
29277
29565
|
}
|
|
29278
29566
|
};
|
|
29567
|
+
|
|
29568
|
+
// src/world/WorldStreamer.ts
|
|
29569
|
+
var WorldStreamer = class {
|
|
29570
|
+
constructor(world, source, options = {}) {
|
|
29571
|
+
this.world = world;
|
|
29572
|
+
this.source = source;
|
|
29573
|
+
this.loaded = /* @__PURE__ */ new Map();
|
|
29574
|
+
this.owned = /* @__PURE__ */ new Map();
|
|
29575
|
+
this.missing = /* @__PURE__ */ new Map();
|
|
29576
|
+
this.failed = /* @__PURE__ */ new Map();
|
|
29577
|
+
this.desired = [];
|
|
29578
|
+
this.disposed = false;
|
|
29579
|
+
this.loadRadius = options.loadRadius ?? 2;
|
|
29580
|
+
this.unloadRadius = options.unloadRadius ?? Math.min(32, this.loadRadius + 1);
|
|
29581
|
+
if (![this.loadRadius, this.unloadRadius].every((value) => Number.isInteger(value) && value >= 0 && value <= 32) || this.unloadRadius < this.loadRadius) {
|
|
29582
|
+
throw new RangeError("Chunk radii must be integers from 0 to 32, with unloadRadius at least loadRadius");
|
|
29583
|
+
}
|
|
29584
|
+
}
|
|
29585
|
+
/** Returns absolute coordinates of successfully placed columns, including retained columns. */
|
|
29586
|
+
get loadedChunks() {
|
|
29587
|
+
return [...this.loaded.values()].map((position) => ({ ...position }));
|
|
29588
|
+
}
|
|
29589
|
+
/** Returns source failures retained within the unload radius. Failed columns do not block other loads. */
|
|
29590
|
+
get failedChunks() {
|
|
29591
|
+
return [...this.failed.values()].map((failure) => ({ ...failure }));
|
|
29592
|
+
}
|
|
29593
|
+
/** Counts desired columns still waiting for a source read or placement, including the active column. */
|
|
29594
|
+
get pendingChunks() {
|
|
29595
|
+
return this.desired.filter((position) => this.needsLoad(position)).length;
|
|
29596
|
+
}
|
|
29597
|
+
/**
|
|
29598
|
+
* Updates the center in absolute chunk coordinates. Repeated calls share the active drain.
|
|
29599
|
+
* Source failures are recorded in failedChunks; other columns continue loading. Missing and failed
|
|
29600
|
+
* columns are remembered within the retention radius. Call retryFailedChunks to retry source failures.
|
|
29601
|
+
* Placement and unload failures reject the drain; call update again to retry those operations.
|
|
29602
|
+
* Reads outside the latest load radius receive an abort signal; placement finishes before unloading.
|
|
29603
|
+
* Sources should represent an unchanged world during streaming.
|
|
29604
|
+
*/
|
|
29605
|
+
async update(x, z) {
|
|
29606
|
+
if (this.disposed) throw new Error("WorldStreamer is disposed");
|
|
29607
|
+
if (![x, z].every((value) => Number.isSafeInteger(value) && Number.isSafeInteger(value - this.unloadRadius) && Number.isSafeInteger(value + this.unloadRadius))) {
|
|
29608
|
+
throw new RangeError("Chunk coordinates must be safe integers within the streaming radius");
|
|
29609
|
+
}
|
|
29610
|
+
if (!this.center || this.center.x !== x || this.center.z !== z) {
|
|
29611
|
+
this.center = { x, z };
|
|
29612
|
+
this.desired = [];
|
|
29613
|
+
for (let dz = -this.loadRadius; dz <= this.loadRadius; dz++) {
|
|
29614
|
+
for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
|
|
29615
|
+
this.desired.push({ x: x + dx, z: z + dz });
|
|
29616
|
+
}
|
|
29617
|
+
}
|
|
29618
|
+
this.desired.sort((a, b) => (a.x - x) ** 2 + (a.z - z) ** 2 - (b.x - x) ** 2 - (b.z - z) ** 2);
|
|
29619
|
+
if (this.loading && !this.within(this.loading.position, this.loadRadius)) {
|
|
29620
|
+
this.loading.controller.abort();
|
|
29621
|
+
}
|
|
29622
|
+
}
|
|
29623
|
+
return this.running ?? (this.running = Promise.resolve().then(() => this.drain()));
|
|
29624
|
+
}
|
|
29625
|
+
/** Updates from a camera or view-center position in scene units. One chunk spans 256 scene units. */
|
|
29626
|
+
async updatePosition(position) {
|
|
29627
|
+
if (![position.x, position.z].every(Number.isFinite)) throw new RangeError("Scene coordinates must be finite");
|
|
29628
|
+
return this.update(Math.floor(position.x / 256), Math.floor(position.z / 256));
|
|
29629
|
+
}
|
|
29630
|
+
/** Forgets recorded source failures and retries desired columns at the latest center. */
|
|
29631
|
+
async retryFailedChunks() {
|
|
29632
|
+
if (this.disposed) throw new Error("WorldStreamer is disposed");
|
|
29633
|
+
this.failed.clear();
|
|
29634
|
+
if (this.center) await this.update(this.center.x, this.center.z);
|
|
29635
|
+
}
|
|
29636
|
+
/** Aborts source reads, waits for active work, then unloads owned columns. The world and source remain caller-owned. */
|
|
29637
|
+
dispose() {
|
|
29638
|
+
this.disposed = true;
|
|
29639
|
+
this.desired = [];
|
|
29640
|
+
this.center = void 0;
|
|
29641
|
+
this.loading?.controller.abort();
|
|
29642
|
+
return this.disposal ?? (this.disposal = Promise.resolve().then(async () => {
|
|
29643
|
+
await this.running?.catch(() => void 0);
|
|
29644
|
+
let failure;
|
|
29645
|
+
let failed = false;
|
|
29646
|
+
for (const position of this.owned.values()) {
|
|
29647
|
+
try {
|
|
29648
|
+
await this.unload(position);
|
|
29649
|
+
} catch (error) {
|
|
29650
|
+
failed = true;
|
|
29651
|
+
failure ?? (failure = error);
|
|
29652
|
+
}
|
|
29653
|
+
}
|
|
29654
|
+
this.missing.clear();
|
|
29655
|
+
this.failed.clear();
|
|
29656
|
+
if (failed) throw failure;
|
|
29657
|
+
}).catch((error) => {
|
|
29658
|
+
this.disposal = void 0;
|
|
29659
|
+
throw error;
|
|
29660
|
+
}));
|
|
29661
|
+
}
|
|
29662
|
+
async drain() {
|
|
29663
|
+
try {
|
|
29664
|
+
while (!this.disposed) {
|
|
29665
|
+
for (const [key, position2] of this.missing) {
|
|
29666
|
+
if (!this.within(position2, this.unloadRadius)) this.missing.delete(key);
|
|
29667
|
+
}
|
|
29668
|
+
for (const [key, position2] of this.failed) {
|
|
29669
|
+
if (!this.within(position2, this.unloadRadius)) this.failed.delete(key);
|
|
29670
|
+
}
|
|
29671
|
+
const obsolete = [...this.owned.values()].find((position2) => !this.loaded.has(this.key(position2)) || !this.within(position2, this.unloadRadius));
|
|
29672
|
+
if (obsolete) {
|
|
29673
|
+
await this.unload(obsolete);
|
|
29674
|
+
continue;
|
|
29675
|
+
}
|
|
29676
|
+
const position = this.desired.find((position2) => this.needsLoad(position2));
|
|
29677
|
+
if (!position) return;
|
|
29678
|
+
let chunk;
|
|
29679
|
+
const controller = new AbortController();
|
|
29680
|
+
this.loading = { position, controller };
|
|
29681
|
+
try {
|
|
29682
|
+
chunk = await this.source.getChunk(position.x, position.z, controller.signal);
|
|
29683
|
+
if (chunk) this.validateChunk(chunk, position);
|
|
29684
|
+
} catch (error) {
|
|
29685
|
+
if (!controller.signal.aborted && !this.disposed && this.within(position, this.loadRadius)) {
|
|
29686
|
+
this.failed.set(this.key(position), { ...position, error });
|
|
29687
|
+
}
|
|
29688
|
+
continue;
|
|
29689
|
+
} finally {
|
|
29690
|
+
this.loading = void 0;
|
|
29691
|
+
}
|
|
29692
|
+
if (controller.signal.aborted || this.disposed || !this.within(position, this.loadRadius)) continue;
|
|
29693
|
+
if (!chunk) {
|
|
29694
|
+
this.missing.set(this.key(position), position);
|
|
29695
|
+
continue;
|
|
29696
|
+
}
|
|
29697
|
+
this.owned.set(this.key(position), position);
|
|
29698
|
+
try {
|
|
29699
|
+
await this.world.placeChunk(chunk);
|
|
29700
|
+
this.loaded.set(this.key(position), position);
|
|
29701
|
+
} catch (error) {
|
|
29702
|
+
try {
|
|
29703
|
+
await this.unload(position);
|
|
29704
|
+
} catch (cleanupError) {
|
|
29705
|
+
const failure = error instanceof Error ? error : new Error(String(error));
|
|
29706
|
+
try {
|
|
29707
|
+
Object.defineProperty(failure, "cause", { value: cleanupError, configurable: true });
|
|
29708
|
+
} catch {
|
|
29709
|
+
throw Object.assign(new Error(failure.message), { cause: cleanupError });
|
|
29710
|
+
}
|
|
29711
|
+
throw failure;
|
|
29712
|
+
}
|
|
29713
|
+
throw error;
|
|
29714
|
+
}
|
|
29715
|
+
}
|
|
29716
|
+
} finally {
|
|
29717
|
+
this.running = void 0;
|
|
29718
|
+
}
|
|
29719
|
+
}
|
|
29720
|
+
needsLoad(position) {
|
|
29721
|
+
const key = this.key(position);
|
|
29722
|
+
return !this.loaded.has(key) && !this.missing.has(key) && !this.failed.has(key);
|
|
29723
|
+
}
|
|
29724
|
+
async unload(position) {
|
|
29725
|
+
await this.world.unloadChunkColumn(position.x, position.z);
|
|
29726
|
+
this.owned.delete(this.key(position));
|
|
29727
|
+
this.loaded.delete(this.key(position));
|
|
29728
|
+
}
|
|
29729
|
+
validateChunk(chunk, position) {
|
|
29730
|
+
if (chunk.x !== position.x || chunk.z !== position.z) {
|
|
29731
|
+
throw new Error(`World source returned chunk ${chunk.x},${chunk.z} for ${position.x},${position.z}`);
|
|
29732
|
+
}
|
|
29733
|
+
}
|
|
29734
|
+
within(position, radius) {
|
|
29735
|
+
return !!this.center && Math.abs(position.x - this.center.x) <= radius && Math.abs(position.z - this.center.z) <= radius;
|
|
29736
|
+
}
|
|
29737
|
+
key(position) {
|
|
29738
|
+
return `${position.x},${position.z}`;
|
|
29739
|
+
}
|
|
29740
|
+
};
|
|
29279
29741
|
export {
|
|
29280
29742
|
AXES,
|
|
29281
29743
|
AnvilParser,
|
|
29744
|
+
AnvilWorldSource,
|
|
29282
29745
|
ArchiveAssetSource,
|
|
29283
29746
|
AssetKey,
|
|
29284
29747
|
AssetLoadError,
|
|
@@ -29381,6 +29844,8 @@ export {
|
|
|
29381
29844
|
Ticker,
|
|
29382
29845
|
TintColors,
|
|
29383
29846
|
UVMapper,
|
|
29847
|
+
VideoExporter,
|
|
29848
|
+
WorldStreamer,
|
|
29384
29849
|
WrappedImage,
|
|
29385
29850
|
addBox3WireframeToObject,
|
|
29386
29851
|
addWireframeToMesh,
|