This is the first time I lay hands on Web Workers, I have recreated the webgl worker offscreencanvas example locally to experiment with Web Workers using modules and a bundler and it works, but is it possible to eliminate the external Web Worker JS file completely and embed its code to the main file?
eg, instead of:
const worker = new Worker('./offscreen.js', { type: 'module' } );
…convert the offscreen.js code to stringUrl and pass that to the Worker function.
I know how to stringify objects, but that’s JS code, not just objects, so JSON.stringify() doesn’t work.
Or perhaps there is a more direct alternative.
The reason:
On this example 3 files are required: a scene.js file, an offscreen.js (worker) file and the main JS file. Both the offscreen.js and the main JS file have to import the scene.js file.
If the offscreen.js code will be embedded on the main js file, then the 3 JS files can merge to a single JS file.
The Web Worker API does require a script that it can find at some given URL, at least until/unless proposals like GitHub - tc39/proposal-js-module-blocks are accepted. If you don’t want to, or can’t, include a separate file, the only other option I’m aware of is to stringify a specific function:
@puxiao Nice work, congrats!
Although In my case I’m not sure whether I’ll use OrbitContols in my non-VR version yet (I have a different, custom approach in mind), it’s definitely a useful contribution and example, thanks!