@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
57 lines (56 loc) • 1.78 kB
JavaScript
;
import { SDFLoaderSync } from "./SDFLoaderSync";
import { Poly } from "../../../../engine/Poly";
import { sanitizeUrl } from "../../../UrlHelper";
import { LIBRARY_INSTALL_HINT } from "./../../../loader/common";
import { Module } from "./SDFCommon";
let _resolves = [];
let _importStarted = false;
let _manifold;
export class SDFLoader {
static async core() {
if (_manifold) {
return _manifold;
}
return new Promise(async (resolve, reject) => {
if (_importStarted) {
_resolves.push(resolve);
return;
}
_importStarted = true;
const onError = () => {
const message = `failed to load Manifold library. Make sure to install it to use the SDFBuilder node (${LIBRARY_INSTALL_HINT})`;
reject(new Error(message));
};
const root = Poly.libs.root();
const ManifoldPath = Poly.libs.ManifoldPath();
if (root || ManifoldPath) {
const version = Poly.version().replace(/\./g, "-");
const wasmUrl = sanitizeUrl(`${root || ""}${ManifoldPath || ""}/manifold.wasm?v=${version}`);
try {
const response = await fetch(wasmUrl, { method: "HEAD" });
if (!response.ok) {
onError();
return;
}
const manifold = await Module({
locateFile: () => wasmUrl
});
manifold.setup();
_manifold = manifold;
SDFLoaderSync.__setManifold(manifold);
resolve(manifold);
if (_resolves.length > 0) {
for (const _resolve of _resolves) {
_resolve(manifold);
}
_resolves.length = 0;
}
} catch (err) {
console.error(err);
onError();
}
}
});
}
}