Skip to content

Commit 0c94176

Browse files
committed
(deps): require Three + Physijs via Webpack + NPM
- use physijs-webpack, a fork I created a few years ago and finally took some time to debug and get working - use via github since someone took the NPM name a few years ago and referenced my repo in it even though it never worked until today... - maybe will update once I get that resolved - now we don't have to vendor in all those scripts anymore and don't need to manually configure Physijs either! - remove the vendored physijs and three scripts - had to debug and modify ammo to get that version working with webpack / bundlers in general - newer versions of Emscripten can target specific envs - c.f. kripken/ammo.js#109 (comment) , webpack/webpack#7352 , and emscripten-core/emscripten#6542 - and then made the worker config more flexible - add worker-loader as a devDep per physijs-webpack instructions - fix publicPath location as this is now actually used - the worker is loaded based on the publicPath - add three-window-resize and three-trackballcontrols as deps as well - since three's examples/js/ folder doesn't quite work with bundlers - c.f. mrdoob/three.js#9562 - maybe if I made some modifications, updated to newer Three revision, and used imports-loader it might work 🤷 TBD - upgrade physijs to latest and Three from r60 to r73 - latest physijs uses r73, so remain consistent - also physijs-webpack has a peerDep for that specific version - [email protected] also requires r73 as dep - had to refactor a bit due to upgrade - WebGLRenderer() -> WebGLRenderer({alpha: true}) - the canvas now defaults to black without this, which was extremely disorienting - shadowMapEnabled -> shadowMap.enabled - CubeGeometry -> BoxGeometry - quaternion._euler -> rotation - I probably could've just used this before, couldn't have I...? . . - TODO: improve webpack perf (CPU + Memory) and build speed - this change slows down initial build times quite a bit (~20s), since Three, Physijs, and Ammo are all parsed by Webpack now - will want to update webpack to get a dev-server (wepback-serve) running - webpack itself is faster in later versions as well - and perhaps add HardSource for caching otherwise or split out vendored stuff into a DLL - probably can't update until decaffeinate'd since I believe the loaders used here are no longer maintained :/ - and then would need to figure out the literate programming and its sourcemaps - will want to output a separate vendor bundle per best practice - may also want to output HTML via webpack too while at it - the templates don't do any templating so would be nbd - and would allow for hashing and therefore cache-busting - no need to manually clean cache then - and prod build / uglification is even _slower_ (~80s) - may want to exclude some files from Uglify - i.e. Ammo and use three.min so it can be excluded too
1 parent 204cee3 commit 0c94176

File tree

11 files changed

+32
-4193
lines changed

11 files changed

+32
-4193
lines changed

app/static/plugins/physijs/ammo.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/static/plugins/physijs/physi.js

Lines changed: 0 additions & 1400 deletions
This file was deleted.

app/static/plugins/physijs/physijs_worker.js

Lines changed: 0 additions & 1399 deletions
This file was deleted.

app/static/plugins/threejs/three.min.js

Lines changed: 0 additions & 705 deletions
This file was deleted.

app/static/plugins/threejs/three.trackballcontrols.js

Lines changed: 0 additions & 611 deletions
This file was deleted.

app/static/plugins/threejs/threex.windowresize.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

app/templates/simulator.html

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ <h1>LTLMoPWeb3D Simulator</h1>
1818
<div id='viewport'></div>
1919
</body>
2020

21-
<script type='text/javascript' src='static/plugins/threejs/three.min.js'></script>
22-
<script type='text/javascript' src='static/plugins/threejs/three.trackballcontrols.js'></script>
23-
<script type='text/javascript' src='static/plugins/threejs/threex.windowresize.js'></script>
24-
<script type='text/javascript' src='static/plugins/physijs/physi.js'></script>
25-
<script type='text/javascript' src='static/build/simulator.bundle.js'></script>
21+
<script type='text/javascript' src='static/build/simulator.bundle.js'></script>
2622

27-
</html>
23+
</html>

static/js/simulator/physijs/initializePhysics.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
// plenty of refactoring done and some changes as well
33
'use strict';
44

5-
// set worker and ammo
6-
Physijs.scripts.worker = 'static/plugins/physijs/physijs_worker.js';
7-
Physijs.scripts.ammo = 'ammo.js';
5+
// External Dependencies
6+
var Physijs = require('physijs-webpack');
7+
var THREE = require('three');
8+
var TrackballControls = require('three-trackballcontrols');
9+
var WindowResize = require('three-window-resize');
810

911
// declare objects
10-
var controls, projector, renderer, scene, camera, car = {};
12+
var controls, renderer, scene, camera, car = {};
1113

1214
// initialize the scene
1315
var initScene = function() {
14-
projector = new THREE.Projector;
15-
1616
// create the renderer
17-
renderer = new THREE.WebGLRenderer({antialias: true});
17+
renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
1818
var viewportElem = document.getElementById('viewport')
1919
console.log(viewportElem.clientWidth)
2020
console.log(viewportElem.clientHeight)
2121
renderer.setSize(viewportElem.clientWidth, viewportElem.clientHeight)
22-
renderer.shadowMapEnabled = true;
22+
renderer.shadowMap.enabled = true;
2323
renderer.shadowMapSoft = true;
2424
viewportElem.appendChild(renderer.domElement)
2525

@@ -47,7 +47,7 @@ var initScene = function() {
4747
scene.add( camera );
4848

4949
// add trackball controls
50-
controls = new THREE.TrackballControls( camera );
50+
controls = new TrackballControls( camera );
5151
controls.rotateSpeed = 1.0;
5252
controls.zoomSpeed = 1.2;
5353
controls.panSpeed = 1.2;
@@ -77,7 +77,7 @@ var initScene = function() {
7777
// scene.add(light);
7878

7979
// add window resizer
80-
THREEx.WindowResize(renderer, camera, function () {
80+
WindowResize(renderer, camera, function () {
8181
return {width: viewportElem.clientWidth, height: viewportElem.clientHeight}
8282
});
8383

@@ -102,7 +102,7 @@ var create3DCar = function(startX, startY, startZ) {
102102
0 // no restitution
103103
);
104104
car.body = new Physijs.BoxMesh(
105-
new THREE.CubeGeometry( 10, 5, 7 ),
105+
new THREE.BoxGeometry( 10, 5, 7 ),
106106
car_material,
107107
1000
108108
);

static/js/simulator/physijs/poseHandler.litcoffee

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
External Dependencies
2+
---------------------
3+
4+
THREE = require('three')
5+
Physijs = require('physijs-webpack')
6+
17
Internal Dependencies
28
---------------------
39

@@ -81,9 +87,9 @@ Set the velocity and theta
8187
currentVelocity = velocity
8288
currentTheta = theta
8389
#console.log('velocity: ' + velocity)
84-
#console.log('car x: ' + car.body.quaternion._euler.x)
85-
#console.log('car y: ' + car.body.quaternion._euler.y)
86-
#console.log('car z: ' + car.body.quaternion._euler.z)
90+
#console.log('car x: ' + car.body.rotation.x)
91+
#console.log('car y: ' + car.body.rotation.y)
92+
#console.log('car z: ' + car.body.rotation.z)
8793
#console.log('wheel theta: ' + theta)
8894
8995

@@ -126,8 +132,8 @@ Starts moving the car toward the destination
126132
targetTheta = Math.atan2(targetPosition[1] - currentPosition[1], targetPosition[0] - currentPosition[0])
127133
#console.log('target theta: ' + targetTheta)
128134
# get proper car theta via transformations of euler angles
129-
carTheta = car.body.quaternion._euler.y
130-
if Math.abs(car.body.quaternion._euler.x) < Math.PI/2
135+
carTheta = car.body.rotation.y
136+
if Math.abs(car.body.rotation.x) < Math.PI/2
131137
carTheta = -(carTheta + Math.PI)
132138
#console.log('car theta: ' + carTheta)
133139
# theta = diff b/t car body's theta and target theta

static/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@
1111
"history": "^1.13.0",
1212
"immutable": "^3.7.5",
1313
"isomorphic-fetch": "^2.2.0",
14+
"physijs-webpack": "github:agilgur5/physijs-webpack#2ddf4f6",
1415
"promise-file-reader": "^1.0.2",
1516
"react": "^0.14.2",
1617
"react-dom": "^0.14.2",
1718
"react-router": "^1.0.0",
1819
"react-tabs": "^0.5.1",
19-
"roslib": "^0.18.0"
20+
"roslib": "^0.18.0",
21+
"three": "^0.73.0",
22+
"three-trackballcontrols": "0.0.3",
23+
"three-window-resize": "^1.0.4"
2024
},
2125
"devDependencies": {
2226
"cjsx-loader": "^2.1.0",
2327
"coffee-loader": "~0.7.2",
2428
"coffee-script": "~1.9.2",
2529
"css-loader": "^0.22.0",
2630
"style-loader": "^0.13.0",
27-
"webpack": "^1.8.11"
31+
"webpack": "^1.8.11",
32+
"worker-loader": "^0.7.0"
2833
},
2934
"scripts": {
3035
"test": "echo \"Error: no test specified\" && exit 1",

static/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
},
1111
output: {
1212
path: './build', // This is where images AND js will go
13-
publicPath: 'http://0.0.0.0:5000', // This is used to generate URLs to e.g. images
13+
publicPath: './static/build/', // This is used to generate URLs to e.g. images
1414
filename: '[name].bundle.js'
1515
},
1616
resolveLoader: {

0 commit comments

Comments
 (0)