Can we run Havok inside a Web Worker?

I’m able to load Havok WASM on the main thread using await HavokPhysics(). But, this throws an error inside a Web Worker:

RuntimeError: Aborted(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f @+0). Build with -sASSERTIONS for more info.

Could anyone please share if they were able to run Havok inside a Web Worker?

For additional context, I’m able to load Ammo WASM successfully on both the main thread and in a Web Worker

I think this is indeed not possible currently, let me cc @eoin for confirmation

1 Like

There’s a couple of difficulties if you’re trying to use Havok from multiple web workers simultaneously, but I think it should be possible to use from just one (though, I haven’t tried.)

The error you’re seeing is because WASM modules expect to begin with a special identifier (\0asm) which does not appear in yours - instead, it has <!DO which looks like you’re trying to instantiate a WASM with an HTML page, rather than the module data?


Thanks, @carolhmj and @eoin !

@eoin Does running importScripts(<PATH_TO_HavokPhysics_umd.js>) and await HavokPhysics() inside a Web Worker script mean that I’m trying to instantiate a WASM with an HTML page, rather than the module data? Could you please share more details on how I can instantiate a WASM with the module data instead?

my two cents - it is trying to download the wasm file, but it is 404-ing, so you get an html page. When initializing the havok module you can pass the URL from which the wasm will be downloaded. might be the way to go here

1 Like

Ooh, thank you, @RaananW ! After passing in the wasm path, it seems to be working:

const HK = await HavokPhysics({ locateFile: () => <PATH_TO_HavokPhysics.wasm>});

awesome! :slight_smile:

1 Like

@regna I’m curious what your experience is with putting Havok into a web worker? I see Havok performance issues when generating large physics meshes, but presumably sending mesh info to a web worker will have some overhead. The querying, on the other hand, runs very performantly for me.

So I’m curious, how much benefit have you seen from running Havok in a web worker and what exactly were you optimizing for?

@jvanderdoes I’ve been slow on getting to this :frowning: and will update back here after experimenting. I’m planning to first create a comparison of falling blocks with and without Web Worker using Ammo and Havok

I don’t know yet if a Web Worker will boost performance significantly for Havok, though I remember it did for Ammo

I also haven’t gotten to a point yet where I’ve tried generating large physics meshes. Have you seen if a SharedArrayBuffer can help here?