Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebAssembly support, and new async API #167

Merged
merged 6 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
687 changes: 667 additions & 20 deletions builds/ammo.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions builds/ammo.wasm.js

Large diffs are not rendered by default.

Binary file added builds/ammo.wasm.wasm
Binary file not shown.
160 changes: 81 additions & 79 deletions examples/hello_world.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
// Adapted from HelloWorld.cpp, Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/

function main() {
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
overlappingPairCache = new Ammo.btDbvtBroadphase(),
solver = new Ammo.btSequentialImpulseConstraintSolver(),
dynamicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));

var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(50, 50, 50)),
bodies = [],
groundTransform = new Ammo.btTransform();

groundTransform.setIdentity();
groundTransform.setOrigin(new Ammo.btVector3(0, -56, 0));

(function() {
var mass = 0,
isDynamic = (mass !== 0),
localInertia = new Ammo.btVector3(0, 0, 0);

if (isDynamic)
groundShape.calculateLocalInertia(mass, localInertia);

var myMotionState = new Ammo.btDefaultMotionState(groundTransform),
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, groundShape, localInertia),
body = new Ammo.btRigidBody(rbInfo);

dynamicsWorld.addRigidBody(body);
bodies.push(body);
})();


(function() {
var colShape = new Ammo.btSphereShape(1),
startTransform = new Ammo.btTransform();
Ammo().then(function() {
// Adapted from HelloWorld.cpp, Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/

function main() {
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
overlappingPairCache = new Ammo.btDbvtBroadphase(),
solver = new Ammo.btSequentialImpulseConstraintSolver(),
dynamicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));

var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(50, 50, 50)),
bodies = [],
groundTransform = new Ammo.btTransform();

groundTransform.setIdentity();
groundTransform.setOrigin(new Ammo.btVector3(0, -56, 0));

(function() {
var mass = 0,
isDynamic = (mass !== 0),
localInertia = new Ammo.btVector3(0, 0, 0);

if (isDynamic)
groundShape.calculateLocalInertia(mass, localInertia);

var myMotionState = new Ammo.btDefaultMotionState(groundTransform),
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, groundShape, localInertia),
body = new Ammo.btRigidBody(rbInfo);

dynamicsWorld.addRigidBody(body);
bodies.push(body);
})();


(function() {
var colShape = new Ammo.btSphereShape(1),
startTransform = new Ammo.btTransform();

startTransform.setIdentity();

var mass = 1,
isDynamic = (mass !== 0),
localInertia = new Ammo.btVector3(0, 0, 0);

if (isDynamic)
colShape.calculateLocalInertia(mass,localInertia);

startTransform.setOrigin(new Ammo.btVector3(2, 10, 0));

startTransform.setIdentity();

var mass = 1,
isDynamic = (mass !== 0),
localInertia = new Ammo.btVector3(0, 0, 0);

if (isDynamic)
colShape.calculateLocalInertia(mass,localInertia);

startTransform.setOrigin(new Ammo.btVector3(2, 10, 0));

var myMotionState = new Ammo.btDefaultMotionState(startTransform),
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia),
body = new Ammo.btRigidBody(rbInfo);

dynamicsWorld.addRigidBody(body);
bodies.push(body);
})();

var trans = new Ammo.btTransform(); // taking this out of the loop below us reduces the leaking

for (var i = 0; i < 135; i++) {
dynamicsWorld.stepSimulation(1/60, 10);

bodies.forEach(function(body) {
if (body.getMotionState()) {
body.getMotionState().getWorldTransform(trans);
print("world pos = " + [trans.getOrigin().x().toFixed(2), trans.getOrigin().y().toFixed(2), trans.getOrigin().z().toFixed(2)]);
}
});
var myMotionState = new Ammo.btDefaultMotionState(startTransform),
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia),
body = new Ammo.btRigidBody(rbInfo);

dynamicsWorld.addRigidBody(body);
bodies.push(body);
})();

var trans = new Ammo.btTransform(); // taking this out of the loop below us reduces the leaking

for (var i = 0; i < 135; i++) {
dynamicsWorld.stepSimulation(1/60, 10);

bodies.forEach(function(body) {
if (body.getMotionState()) {
body.getMotionState().getWorldTransform(trans);
print("world pos = " + [trans.getOrigin().x().toFixed(2), trans.getOrigin().y().toFixed(2), trans.getOrigin().z().toFixed(2)]);
}
});
}

// Delete objects we created through |new|. We just do a few of them here, but you should do them all if you are not shutting down ammo.js
// we'll free the objects in reversed order as they were created via 'new' to avoid the 'dead' object links
Ammo.destroy(dynamicsWorld);
Ammo.destroy(solver);
Ammo.destroy(overlappingPairCache);
Ammo.destroy(dispatcher);
Ammo.destroy(collisionConfiguration);

print('ok.')
}

// Delete objects we created through |new|. We just do a few of them here, but you should do them all if you are not shutting down ammo.js
// we'll free the objects in reversed order as they were created via 'new' to avoid the 'dead' object links
Ammo.destroy(dynamicsWorld);
Ammo.destroy(solver);
Ammo.destroy(overlappingPairCache);
Ammo.destroy(dispatcher);
Ammo.destroy(collisionConfiguration);

print('ok.')
}

main();
main();
});
2 changes: 2 additions & 0 deletions examples/webgl_demo/ammo.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ <h2>ammo.js benchmark</h2>

<canvas id="canvas" width="1" height="1"></canvas>
<br>
<br>
<div id="before">
<form>
Boxes:
Expand All @@ -176,6 +177,7 @@ <h2>ammo.js benchmark</h2>
<option value="2500">2500</option>
</select>
<input type="button" value="go!" onclick="startUp(boxes.value);">
<a href="https://twitter.com/horse_js"><img src="js.png" style="width: 4em; margin-left: 300px"></a>
</form>
</div>
<div id="during"><div>Physics FPS (current / stable): <input type="text" id="out" readonly="1" size="7"></div><div id="postdiv"></div></div>
Expand Down
194 changes: 194 additions & 0 deletions examples/webgl_demo/ammo.wasm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<html>
<head>
<title>Bullet/WebGL Demo</title>
<script src="CubicVR.min.js" type="text/javascript"></script>
<style type="text/css">
body {
background-color: #ccc;
text-size-adjust: none;
}
</style>

<script src="shared.js"></script>

<script type="text/javascript">
// Main demo code

var boxes = [];

var lastFPS = 0;
var outElement = null;
var currFPS = 0, allFPS = 0;
function showFPS() {
if (!outElement) outElement = document.getElementById('out');
var now = Date.now();
if (now - lastFPS > 333) {
outElement.value = currFPS + ' / ' + allFPS;
lastFPS = now;
}
}

var FLOOR_SIZE = 100;
var FLOOR_HEIGHT = -56

var physicsWorker = null;
var nextPhysicsWorker = new Worker('worker.wasm.js');

// CubicVR code

function startUp(NUM) {
var NUMRANGE = [];
while (NUMRANGE.length < NUM) NUMRANGE.push(NUMRANGE.length+1);

document.getElementById('postdiv').innerHTML = '(' + NUM + ' cubes)';

//document.getElementById('before').style.visibility = 'hidden';
document.getElementById('during').style.visibility = 'visible';

var canvas = document.getElementById("canvas");
canvas.width = screen.width*0.75;
canvas.height = screen.height*0.50;

var gl = CubicVR.init(canvas);

if (!gl) {
alert("Sorry, no WebGL support :(");
return;
};

var quaternion = new CubicVR.Quaternion;

var scene = new CubicVR.Scene(canvas.width, canvas.height, 70);

var light = new CubicVR.Light({
type:CubicVR.enums.light.type.POINT,
intensity: 0.9,
areaCeiling: 80,
areaFloor: FLOOR_HEIGHT,
areaAxis: [15, 10],
distance: 60,
mapRes: 1024
});
scene.bindLight(light);

scene.camera.position = [0, 2.4, 17];
scene.camera.target = [0, 2.4, 0];

var boxMaterials = [];
var boxMeshes = [];

for (var i = 0; i < 5; i++) {
boxMaterials[i] = new CubicVR.Material({
textures: {
color: new CubicVR.Texture("cube" + (i+1) + ".jpg")
}
});
boxMeshes[i] = new CubicVR.primitives.box({
size: 2.0,
material: boxMaterials[i],
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.CUBIC,
scale: [2, 2, 2]
}
}).calcNormals().triangulateQuads().compile().clean();
}

for (var i = 0; i < NUM; i++) {
boxes[i] = new CubicVR.SceneObject({ mesh: boxMeshes[Math.floor(Math.random()*5)], position: [0, -10000, 0] });
scene.bindSceneObject(boxes[i], true);
}

var floorMaterial = new CubicVR.Material({
textures: {
color: new CubicVR.Texture("cube3.jpg")
}
});
var floorMesh = new CubicVR.primitives.box({
size: FLOOR_SIZE,
material: floorMaterial,
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.CUBIC,
scale: [4, 4, 4]
}
}).calcNormals().triangulateQuads().compile().clean();

var floor_ = new CubicVR.SceneObject({ mesh: floorMesh, position: [0, FLOOR_HEIGHT, 0] });
scene.bindSceneObject(floor_, true);

// Worker

if (physicsWorker) physicsWorker.terminate();
physicsWorker = nextPhysicsWorker;
nextPhysicsWorker = null;
if (!physicsWorker) physicsWorker = new Worker('worker.wasm.js');

physicsWorker.onmessage = function(event) {
var data = event.data;
if (data.objects.length != NUM) return;
for (var i = 0; i < NUM; i++) {
var physicsObject = data.objects[i];
var renderObject = boxes[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();
}
currFPS = data.currFPS;
allFPS = data.allFPS;
};
physicsWorker.postMessage(NUM);

// Main loop

var mvc = new CubicVR.MouseViewController(canvas, scene.camera);

CubicVR.MainLoop(function(timer, gl) {
var dt = timer.getLastUpdateSeconds();
scene.render();
showFPS();
});
}
</script>
</head>
<body onload="document.getElementById('during').style.visibility = 'hidden'">
<center>
<h2>ammo.js benchmark</h2>

<canvas id="canvas" width="1" height="1"></canvas>
<br>
<br>
<div id="before">
<form>
Boxes:
<select name="boxes">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="500" selected>500</option>
<option value="750">750</option>
<option value="1000">1000</option>
<option value="1500">1500</option>
<option value="2500">2500</option>
</select>
<input type="button" value="go!" onclick="startUp(boxes.value);">
<a href="https://twitter.com/horse_wasm"><img src="wasm.png" style="width: 4em; margin-left: 300px"></a>
</form>
</div>
<div id="during"><div>Physics FPS (current / stable): <input type="text" id="out" readonly="1" size="7"></div><div id="postdiv"></div></div>
<p>
This is <b><a href="https://github.com/kripken/ammo.js">ammo.js</a></b>, a port of
the <b><a href="http://www.bulletphysics.com">Bullet physics engine</a></b> from C++ to JavaScript
using <b><a href="http://emscripten.org">Emscripten</a></b> in
<b><a href="http://asmjs.org">asm.js</a></b> mode. WebGL rendering in this demo is done using
<b><a href="https://github.com/cjcliffe/CubicVR.js/">CubicVR.js</a></b>.
</p>
</center>
</body>
</html>

Binary file added examples/webgl_demo/js.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/webgl_demo/wasm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading