Loading of Havok from a script loading standpoint

I need enough information in order to be able to load Havok. Did not really see something at this level in Docs, so from reverse engineering of the playground, have determined there are 2 js Urls:

A playground that actually uses Havok also has:

while a pg that does not run Havok, only has the js file loaded. I am concluding from that, the js file has a script tag append inside of it.

If trying to make a self contained BJS Native app, is there a way to specify where to get the wasm, like APP:///HavokPhysics.wasm ?

Edit:
Did just find Using Havok and the Havok Plugin | Babylon.js Documentation that shows the full process, but wasm explicit url not there.

In my experience, putting them in the same path works. He seems to use relative paths to load wasm files

I changed my HavokPhysics script to require the loaded WASM module as an argument instead of automatically loading it, so I could use fetch or whatever to load it myself. This was needed in my NodeJS(ESM) environment, as I would otherwise have been required to run a webserver and serve the WASM from within the same directory as the module needing the physics engine.

I believe an optional parameter with either a URL or the loaded module would help with this, to allow for external hosting
@Cedric

While that would be great to just use the directory of the JS script, but the more I think about it, there really isn’t a formal script tag for the JS file to get a path from. There is an AppendScript() in C, which is a cover for an Eval() operation to do the load. There really is not any place to get a path from.

Moreover, since there is no Dom in Native, the JS code that dynamically creates the script tag for the .wasm is just going to error. Guess Havok is not going to be implementable in Native at this time. I was just thinking this out, not actually going to do right now, so I’ll just table this.

I’m not 100% sure but I think the .wasm is loaded from the .js and it expects the .wasm to be in the same folder? I don’t think it’s possible to specify where to load the .wasm only. Am I right @RaananW ?

The wasm is loaded by the .js, The default path is the same as the js script’s path, BUT you can still override this behavior.
This is standard wasm behavior (at least ones that are exported by emscripten), but maybe we should document it :slight_smile:

If you pass a function when initializing the wasm file you can change the default location. The function takes two variables - the file path to load (i.e. filename.wasm) and the script directory (http path). It needs to return the location of the wasm file (or better yet - the file it is requesting, which is probably the wasm file :-))

For example:

const locateFile = (path, scriptDirectory) => {
    return scriptDirectory + path; // default behavior
}

const locateFileUsingExternalCDN = (path, scriptDirectory) => {
    return "https://my.wonderful.cdn.io/" + path;
}

Now, pass this function when initializing the plugin:

const havok = await HavokPhysics({locateFile: myLocateFileFunction});

Small note - this will not work in the playground of course, because we initialize the plugin under the hood for you. It will work in a custom implementation

4 Likes