Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update physijs, fixes FF18 support.

  • Loading branch information...
commit fea5fd7c3f3a16be76b7acb1e62c6508dd2c1012 1 parent 77a16db
@tapio authored
Showing with 80 additions and 42 deletions.
  1. +41 −30 libs/physi.js
  2. +39 −12 libs/physijs_worker.js
View
71 libs/physi.js
@@ -3,6 +3,7 @@
window.Physijs = (function() {
var THREE_REVISION = parseInt( THREE.REVISION, 10 ),
+ SUPPORT_TRANSFERABLE,
_matrix = new THREE.Matrix4, _is_simulating = false,
_Physijs = Physijs, // used for noConflict method
Physijs = {}, // object assigned to window.Physijs
@@ -18,7 +19,7 @@ window.Physijs = (function() {
_temp_matrix4_1 = new THREE.Matrix4,
_quaternion_1 = new THREE.Quaternion,
- // constants
+ // constants
MESSAGE_TYPES = {
WORLDREPORT: 0,
COLLISIONREPORT: 1,
@@ -114,7 +115,7 @@ window.Physijs = (function() {
}
// Invert rotation matrix in order to "unrotate" a point back to object space
- _temp_matrix4_1.getInverse( _temp_matrix4_1 );
+ _temp_matrix4_1.getInverse( _temp_matrix4_1 );
// Yay! Temp vars!
_temp_vector3_1.copy( position );
@@ -393,43 +394,53 @@ window.Physijs = (function() {
THREE.Scene.call( this );
this._worker = new Worker( Physijs.scripts.worker || 'physijs_worker.js' );
+ this._worker.transferableMessage = this._worker.webkitPostMessage || this._worker.postMessage;
this._materials = {};
this._objects = {};
this._vehicles = {};
this._constraints = {};
-
+
+ var ab = new ArrayBuffer( 1 );
+ this._worker.transferableMessage( ab, [ab] );
+ SUPPORT_TRANSFERABLE = ( ab.byteLength === 0 );
+
this._worker.onmessage = function ( event ) {
- var _temp;
+ var _temp,
+ data = event.data;
+
+ if ( data instanceof ArrayBuffer && data.byteLength !== 1 ) { // byteLength === 1 is the worker making a SUPPORT_TRANSFERABLE test
+ data = new Float32Array( data );
+ }
- if ( event.data instanceof Float32Array ) {
+ if ( data instanceof Float32Array ) {
// transferable object
- switch ( event.data[0] ) {
+ switch ( data[0] ) {
case MESSAGE_TYPES.WORLDREPORT:
- self._updateScene( event.data );
+ self._updateScene( data );
break;
case MESSAGE_TYPES.COLLISIONREPORT:
- self._updateCollisions( event.data );
+ self._updateCollisions( data );
break;
case MESSAGE_TYPES.VEHICLEREPORT:
- self._updateVehicles( event.data );
+ self._updateVehicles( data );
break;
case MESSAGE_TYPES.CONSTRAINTREPORT:
- self._updateConstraints( event.data );
+ self._updateConstraints( data );
break;
}
} else {
- if ( event.data.cmd ) {
+ if ( data.cmd ) {
// non-transferable object
- switch ( event.data.cmd ) {
+ switch ( data.cmd ) {
case 'objectReady':
- _temp = event.data.params;
+ _temp = data.params;
if ( self._objects[ _temp ] ) {
self._objects[ _temp ].dispatchEvent( 'ready' );
}
@@ -440,33 +451,33 @@ window.Physijs = (function() {
break;
case 'vehicle':
- window.test = event.data;
+ window.test = data;
break;
default:
// Do nothing, just show the message
- console.debug('Received: ' + event.data.cmd);
- console.dir(event.data.params);
+ console.debug('Received: ' + data.cmd);
+ console.dir(data.params);
break;
}
} else {
- switch ( event.data[0] ) {
+ switch ( data[0] ) {
case MESSAGE_TYPES.WORLDREPORT:
- self._updateScene( event.data );
+ self._updateScene( data );
break;
case MESSAGE_TYPES.COLLISIONREPORT:
- self._updateCollisions( event.data );
+ self._updateCollisions( data );
break;
case MESSAGE_TYPES.VEHICLEREPORT:
- self._updateVehicles( event.data );
+ self._updateVehicles( data );
break;
case MESSAGE_TYPES.CONSTRAINTREPORT:
- self._updateConstraints( event.data );
+ self._updateConstraints( data );
break;
}
@@ -539,9 +550,9 @@ window.Physijs = (function() {
}
- if ( this._worker.webkitPostMessage ) {
+ if ( SUPPORT_TRANSFERABLE ) {
// Give the typed array back to the worker
- this._worker.webkitPostMessage( data, [data.buffer] );
+ this._worker.transferableMessage( data.buffer, [data.buffer] );
}
_is_simulating = false;
@@ -586,9 +597,9 @@ window.Physijs = (function() {
}
- if ( this._worker.webkitPostMessage ) {
+ if ( SUPPORT_TRANSFERABLE ) {
// Give the typed array back to the worker
- this._worker.webkitPostMessage( data, [data.buffer] );
+ this._worker.transferableMessage( data.buffer, [data.buffer] );
}
};
@@ -617,9 +628,9 @@ window.Physijs = (function() {
constraint.appliedImpulse = data[ offset + 5 ] ;
}
- if ( this._worker.webkitPostMessage ) {
+ if ( SUPPORT_TRANSFERABLE ) {
// Give the typed array back to the worker
- this._worker.webkitPostMessage( data, [data.buffer] );
+ this._worker.transferableMessage( data.buffer, [data.buffer] );
}
};
@@ -688,9 +699,9 @@ window.Physijs = (function() {
}
- if ( this._worker.webkitPostMessage ) {
+ if ( SUPPORT_TRANSFERABLE ) {
// Give the typed array back to the worker
- this._worker.webkitPostMessage( data, [data.buffer] );
+ this._worker.transferableMessage( data.buffer, [data.buffer] );
}
};
@@ -1102,7 +1113,7 @@ window.Physijs = (function() {
this._physijs.points = points;
};
Physijs.HeightfieldMesh.prototype = new Physijs.Mesh;
- Physijs.HeightfieldMesh.prototype.constructor = Physijs.HeightfieldMesh;
+ Physijs.HeightfieldMesh.prototype.constructor = Physijs.HeightfieldMesh;
// Physijs.BoxMesh
Physijs.BoxMesh = function( geometry, material, mass ) {
View
51 libs/physijs_worker.js
@@ -60,9 +60,9 @@ var
CONSTRAINTREPORT_ITEMSIZE = 6, // constraint id, offset object, offset, applied impulse
constraintreport;
-var ab = new ArrayBuffer(1);
-transferableMessage(ab, [ab]);
-var SUPPORT_TRANSFERABLE = (ab.byteLength === 0);
+var ab = new ArrayBuffer( 1 );
+transferableMessage( ab, [ab] );
+var SUPPORT_TRANSFERABLE = ( ab.byteLength === 0 );
getShapeFromCache = function ( cache_key ) {
if ( _object_shapes[ cache_key ] !== undefined ) {
@@ -200,13 +200,19 @@ public_functions.init = function( params ) {
// Transferable messages are supported, take advantage of them with TypedArrays
worldreport = new Float32Array(2 + REPORT_CHUNKSIZE * WORLDREPORT_ITEMSIZE); // message id + # of objects to report + chunk size * # of values per object
collisionreport = new Float32Array(2 + REPORT_CHUNKSIZE * COLLISIONREPORT_ITEMSIZE); // message id + # of collisions to report + chunk size * # of values per object
+ vehiclereport = new Float32Array(2 + REPORT_CHUNKSIZE * VEHICLEREPORT_ITEMSIZE); // message id + # of vehicles to report + chunk size * # of values per object
+ constraintreport = new Float32Array(2 + REPORT_CHUNKSIZE * CONSTRAINTREPORT_ITEMSIZE); // message id + # of constraints to report + chunk size * # of values per object
} else {
// Transferable messages are not supported, send data as normal arrays
worldreport = [];
collisionreport = [];
+ vehiclereport = [];
+ constraintreport = [];
}
worldreport[0] = MESSAGE_TYPES.WORLDREPORT;
collisionreport[0] = MESSAGE_TYPES.COLLISIONREPORT;
+ vehiclereport[0] = MESSAGE_TYPES.VEHICLEREPORT;
+ constraintreport[0] = MESSAGE_TYPES.CONSTRAINTREPORT;
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration,
dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration ),
@@ -904,8 +910,8 @@ reportWorld = function() {
worldreport[ offset + 13 ] = _vector.z();
}
}
-
- transferableMessage( worldreport, [worldreport.buffer] );
+
+ transferableMessage( worldreport.buffer, [worldreport.buffer] );
};
reportCollisions = function() {
@@ -946,7 +952,7 @@ reportCollisions = function() {
}
}
- transferableMessage( collisionreport, [collisionreport.buffer] );
+ transferableMessage( collisionreport.buffer, [collisionreport.buffer] );
};
reportVehicles = function() {
@@ -954,6 +960,17 @@ reportVehicles = function() {
transform = new Ammo.btTransform, origin, rotation,
offset = 0,
i = 0, j = 0;
+
+ if ( SUPPORT_TRANSFERABLE ) {
+ if ( vehiclereport.length < 2 + _num_wheels * VEHICLEREPORT_ITEMSIZE ) {
+ vehiclereport = new Float32Array(
+ 2 + // message id & # objects in report
+ ( Math.ceil( _num_wheels / REPORT_CHUNKSIZE ) * REPORT_CHUNKSIZE ) * VEHICLEREPORT_ITEMSIZE // # of values needed * item size
+ );
+ vehiclereport[0] = MESSAGE_TYPES.VEHICLEREPORT;
+ }
+ }
+
for ( index in _vehicles ) {
if ( _vehicles.hasOwnProperty( index ) ) {
vehicle = _vehicles[index];
@@ -989,7 +1006,7 @@ reportVehicles = function() {
}
if ( j !== 0 ) {
- transferableMessage( vehiclereport, [vehiclereport.buffer] );
+ transferableMessage( vehiclereport.buffer, [vehiclereport.buffer] );
}
};
@@ -1000,6 +1017,16 @@ reportConstraints = function() {
offset = 0,
i = 0;
+ if ( SUPPORT_TRANSFERABLE ) {
+ if ( constraintreport.length < 2 + _num_constraints * CONSTRAINTREPORT_ITEMSIZE ) {
+ constraintreport = new Float32Array(
+ 2 + // message id & # objects in report
+ ( Math.ceil( _num_constraints / REPORT_CHUNKSIZE ) * REPORT_CHUNKSIZE ) * CONSTRAINTREPORT_ITEMSIZE // # of values needed * item size
+ );
+ constraintreport[0] = MESSAGE_TYPES.CONSTRAINTREPORT;
+ }
+ }
+
for ( index in _constraints ) {
if ( _constraints.hasOwnProperty( index ) ) {
constraint = _constraints[index];
@@ -1020,7 +1047,7 @@ reportConstraints = function() {
}
if ( i !== 0 ) {
- transferableMessage( constraintreport, [constraintreport.buffer] );
+ transferableMessage( constraintreport.buffer, [constraintreport.buffer] );
}
};
@@ -1031,19 +1058,19 @@ self.onmessage = function( event ) {
switch ( event.data[0] ) {
case MESSAGE_TYPES.WORLDREPORT:
- worldreport = event.data;
+ worldreport = new Float32Array( event.data );
break;
case MESSAGE_TYPES.COLLISIONREPORT:
- collisionreport = event.data;
+ collisionreport = new Float32Array( event.data );
break;
case MESSAGE_TYPES.VEHICLEREPORT:
- vehiclereport = event.data;
+ vehiclereport = new Float32Array( event.data );
break;
case MESSAGE_TYPES.CONSTRAINTREPORT:
- constraintreport = event.data;
+ constraintreport = new Float32Array( event.data );
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.