🐣 Three.js + WebAssembly

just curious :slight_smile:

  • What did you write the algorithm in?
  • What tools did you use?
  • What was the gain in %?

I think it’s also worth noting the browser you compared performance in. If I recall some team members saw much larger performance gains in Firefox with their benchmarks than Chrome. It’s unclear whether or not this is because WASM is faster in FF or the JS is slower.

3 Likes

Just stumbled across this thread and wanted to throw out there that ammo.js has excellent collision/raycasting/volume query support…

I think internally it uses spanning interval trees to sort/index both static and dynamic objects…
You could probably run it in a webworker if performance is a concern… It is an emscripten port of the bullet physics library to webasm/asmjs.

The bullet physics library has been developed over a long period and is hyper optimized for these kinds of operations.

You could spend a long implementing something less flexible and less performant, by hand, for all but the most constrained situations. /2c

1 Like

We can stay in one language with AssemblyScript (a TypeScript to WebAssembly compiler).

The WebAssembly reference types proposal is already out behind flags in browser(s). The WebAssembly GC proposal is next, which will allow WebAssembly code to directly manipulate JS and DOM objects.

Soon enough we’ll be able to import JS modules from WebAssembly, and vice versa. So, imagine: JS one one side (TypeScript), and WebAssembly on the other (TypeScript).

Once we can reference everything from WASM, we may not even need JS anymore.

EDIT: Oh! And multi-threading is already in WASM too!

3 Likes

I am looking into that right now and this is very interresting.
Assemblyscritp is indeed a great way to get into webassembly if you come from js or ts.
But it is still a different language ( different approach, different types…)

I would really like to be able to use AssemblyScript to compute vertex positions and such.
That would allow for great use, such as phisic engines.
For now, if you want to morph a mesh (with bones or by moving vertex yourself) the only valid way to do it is on the GPU, but then you loose the information (as you cannot access it from the CPU).

So if you want to make it responsive to the mouse (currently ray casting detects the mesh before ths skinning deformation) or to have a phisic engine that answers to the actual geometry (for now and although it is often the best course anyway, you have to create a simpler representation) or simply for all the matematical operations of Three.js I am looking forward to the evolution of webassembly.

1 Like

I see what you mean. We may be able run these calculations on separate WASM threads. It’d be neat to eventually support threads in our our port of Three.js to AssemblyScript (f.e. physics running on one thread, scene on another thread, and finally the DOM in the UI thread).

2 Likes