@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
88 lines (87 loc) • 2.57 kB
JavaScript
;
import { CadLoaderSync } from "./CadLoaderSync";
import opencascadeCustomBuild from "./build/polygonjs-occt.js";
const initOpenCascade = (options) => {
const module = options.module || {};
return new Promise((resolve, reject) => {
new options.mainJS({
locateFile(path) {
if (path.endsWith(".wasm")) {
return options.mainWasm;
}
if (path.endsWith(".worker.js") && !!options.worker) {
return options.worker;
}
return path;
},
...module
}).then(async (oc) => {
if (options.libs) {
for (const lib of options.libs) {
await oc.loadDynamicLibrary(lib, {
loadAsync: true,
global: true,
nodelete: true,
allowUndefined: false
});
}
}
resolve(oc);
});
});
};
import { Poly } from "../../../../engine/Poly";
import { sanitizeUrl } from "../../../UrlHelper";
import { LIBRARY_INSTALL_HINT } from "./../../../loader/common";
import "three";
let _resolves = [];
let _importStarted = false;
let _oc;
export class CadLoader {
static async core(_) {
if (_oc) {
return _oc;
}
return new Promise(async (resolve, reject) => {
if (_importStarted) {
_resolves.push(resolve);
return;
}
_importStarted = true;
const onError = () => {
const message = `failed to load OpenCascade library. Make sure to install it to use CAD nodes (${LIBRARY_INSTALL_HINT})`;
reject(new Error(message));
};
const root = Poly.libs.root();
const OCCTPath = Poly.libs.OCCTPath();
if (root || OCCTPath) {
const version = Poly.version().replace(/\./g, "-");
const id = false ? MathUtils.generateUUID() : version;
const wasmUrl = sanitizeUrl(`${root || ""}${OCCTPath || ""}/polygonjs-occt.wasm?v=${id}`);
try {
const response = await fetch(wasmUrl, { method: "HEAD" });
if (!response.ok) {
onError();
return;
}
const oc = await initOpenCascade({
mainJS: opencascadeCustomBuild,
mainWasm: wasmUrl
});
_oc = oc;
CadLoaderSync.__setOC(oc);
Poly.blobs.fetchBlobGlobal(wasmUrl);
resolve(oc);
if (_resolves.length > 0) {
for (const _resolve of _resolves) {
_resolve(oc);
}
_resolves.length = 0;
}
} catch (err) {
onError();
}
}
});
}
}