Hi everyone !
Unfortunately I cannot provide a playground to reproduce the issue.
Calling InitializeCSG2Async
while in a worker thread leads to an error du to the use of importScripts()
inside the method LoadScript
of the class Tools
.
It is pretty obvious from the method comment that it needs to access the DOM, which is not possible in a worker context.
This function is used internally by babylon components to load a script (identified by an url). When the url returns, the content of this file is added into a new script element, attached to the DOM (body element)
Then, because the script was not loaded, trying to instanciate a ManifoldMesh
into the CSG.FromMesh
method leads to a crash.
As CSG
is now deprecated, any help to fix the issue would be greatly appreciated !
Thanks a lot for the amazing work !
You have analyzed it wonderfully - this is exactly the reason why it doesn’t work in webworkers.
We should be able to make _LoadScriptModuleAsync
work if no document object is available. Want to create a github issue for that? You can reference this page when creating it.
I initialize it this way in my worker and it works fine:
async function initBabylon(canvas: OffscreenCanvas) {
engine = new Engine(canvas, true);
scene = new Scene(engine);
try {
const wasmUrl = "https://unpkg.com/manifold-3d@3.0.1/manifold.wasm";
const response = await fetch(wasmUrl);
const wasmBinary = await response.arrayBuffer();
const wasm = await Module({
wasmBinary,
locateFile: (path) => `https://unpkg.com/manifold-3d@3.0.1/${path}`,
});
wasm.setup();
Manifold = wasm.Manifold;
ManifoldMesh = wasm.Mesh;
await InitializeCSG2Async({
manifoldInstance: Manifold,
manifoldMeshInstance: ManifoldMesh,
});
console.log("CSG2 initialized successfully");
} catch (error) {
console.error("Failed to initialize CSG2:", error);
}
}
2 Likes