fabmax / physx-js-webidl

Javascript WASM bindings for Nvidia PhysX
MIT License
117 stars 27 forks source link

Add an option to provide the Wasm URL #13

Closed LeXXik closed 2 years ago

LeXXik commented 2 years ago

Hello, could you, please, add an option to allow a full path URL of the Wasm file to be provided at runtime? It is not always available before that. Something like:

PhysX({  wasmPath: 'https://.../physx.wasm.wasm' }).then('...');

If the options object is not provided, it would default to whatever it is doing at the moment.

fabmax commented 2 years ago

Hmm might be not that easy. The javascript is entirely generated by the emscripten compiler, so that one would need to have an option to do that. I did a quick google search but found nothing to achieve that.

LeXXik commented 2 years ago

Back in the days, when prestomation first made his initial project public, I used it to test an integration with PlayCanvas: https://playcanvas.com/project/737359/overview/playcanvas-with-nvidia-physx

However, I had to modify the original sources to point to the Wasm file path. Similar with your library, I had to modify the original js file:

image

This is because the PlayCanvas servers add a hash to the filenames of the project assets, so the default name will fail to fetch.

Edit:

Adding my findings that may help: https://stackoverflow.com/a/46340091/3411075

Hmm, potentially, I could use a service worker to intercept a fetch request for the named wasm file and instead return a custom response: https://medium.com/@maulanamaleek/intercept-http-request-using-serviceworker-b6ef23f97d1f Haven't tried that before though...

fabmax commented 2 years ago

Ok, does the intercept approach work? I guess that would be the easiest solution.

I don't want to add manual changes to the 'official' javascript file I distribute, because that makes it very difficult to reproduce the build and to publish updates. It might be an option to write some kind of script, which changes the javascript automatically as part of the build process but that would also require some work.

LeXXik commented 2 years ago

I haven't tried the service worker yet. Will do a test tonight and let you know.

LeXXik commented 2 years ago

Hmm, so after fighting with it, I wasn't able to use a service worker. After some research, I think I found the default way of doing it in Emscripten - Module.instantiateWasm: https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm

You can consider adding support for it. I will close the issue, as I my toolchain replaces instantiateAsync() with my custom function instead in the final bundle.