question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[html5 (JS)] WASM Ammo.js + WebWorker

See original GitHub issue

Hi all, there is a way to speed up Armory3D when exporting to html5.

The speed up will come from the physic module. The physic module ammo.js can be used as a JS-compiled WASM module and sync physics info using a webWorker. The module then run in its own thread and the 3D objects properties are then updated using the webWorker. Meaning that for each frame there is no need to wait anymore for the physics to be calculated. (no slow FPS due to Physics) in theory : ![html5 (JS)] WASM Ammo.js + WebWorker Examples : with only Javascript Ammo.js with WASM Ammo.js + WebWorker

  1. To run the ammo.js module as a WASM module you need first to compile ammo.js to WASM. More info about compiling it yourself here : https://github.com/kripken/ammo.js/#building

    Here is the compiled file : ammo.wasm.wasm

  2. You first need to launch a webWorker using javascript that will load our module, something like :

var physicsWorker = null;
var nextPhysicsWorker = new Worker('worker.wasm.js');
if (physicsWorker) physicsWorker.terminate();
physicsWorker = nextPhysicsWorker;
nextPhysicsWorker = null;
if (!physicsWorker) physicsWorker = new Worker('worker.wasm.js');
  1. Here is what a WebWorker will look like : worker.wasm.js We probably need to rewrite the “Bullet-interfacing code” for Armory.

  2. Armory is loading the normal JS ammo.js inside the kha.js script by calling this function here : https://github.com/armory3d/armory/blob/58e4744aa10b197c34c5e929578fce4c76427003/Sources/armory/system/Starter.hx#L77

and it seems that Krom at line 80 is already using the WASM version of ammo.js - so we just need to rewrite this function for the html5 export.

  1. Finally : we need to sync 3D objects using the worker. We need to access and update all 3D objects with their transforms generated by the worker and get them update in the 3D canvas. The function will look like this :
physicsWorker.onmessage = function(event) {
var data = event.data;
if (data.objects.length != TotalNumberOfObjectsWithPhysics) return;
for (var i = 0; i < TotalNumberOfObjectsWithPhysics; i++) {
	var physicsObject = data.objects[i];
	renderObject.position[0] = physicsObject[0];
	renderObject.position[1] = physicsObject[1];
	renderObject.position[2] = physicsObject[2];
	quaternion.x = physicsObject[3];
	quaternion.y = physicsObject[4];
	quaternion.z = physicsObject[5];
	quaternion.w = physicsObject[6];
	renderObject.rotation = quaternion.toEuler();
}
};
physicsWorker.postMessage(TotalNumberOfObjectsWithPhysics);

Also here is a 3D scene with cube and added physics that can be used as a benchmark. https://github.com/onelsonic/armory_tests/raw/master/mesh_generate.7z

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tongcommented, Aug 2, 2021
1reaction
onelsoniccommented, Aug 1, 2021

I see that Kha has an HTML5 backend with workers which might work for this : https://github.com/Kode/Kha/tree/master/Backends/HTML5-Worker/kha

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - [html5 (JS)] WASM Ammo.js + WebWorker -
The physic module ammo.js can be used as a JS-compiled WASM module and sync physics info using a webWorker. The module then run...
Read more >
Three.js and ammo.js WebWorker Test - YouTube
On the left: ammo.jsOn the right: ammo. wasm. js + WebWorkerThis will soon be implemented into https://enable3d.io/For now enable3d uses the ...
Read more >
ammo.js | Direct port of the Bullet physics engine to JavaScript ...
ammo.js is a direct port of the Bullet physics engine to JavaScript, using Emscripten. The source code is translated directly to JavaScript, without...
Read more >
Seamless offloading of web app computations from mobile ...
One example from the paper is an application using the ammo.js physics ... The design of the HTML5 Web Worker interface turns out...
Read more >
Jerome Etienne - CD2H gitForager
Bio: Making WebAR a reality - Around Javascript and WebGL - 8th most active user ... using modern web technics - wasm, webworker,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found