Browse Source

Merge remote-tracking branch 'origin/master' into Environment

Sebastien Vandenberghe 8 years ago
parent
commit
ff5ec07706

BIN
Exporters/Blender/Blender2Babylon-5.3.zip


+ 1 - 1
Exporters/Blender/src/babylon-js/__init__.py

@@ -1,7 +1,7 @@
 bl_info = {
     'name': 'Babylon.js',
     'author': 'David Catuhe, Jeff Palmer',
-    'version': (5, 3, 0),
+    'version': (5, 4, 0),
     'blender': (2, 76, 0),
     'location': 'File > Export > Babylon.js (.babylon)',
     'description': 'Export Babylon.js scenes (.babylon)',

+ 35 - 21
Exporters/Blender/src/babylon-js/mesh.py

@@ -24,7 +24,7 @@ BILLBOARDMODE_NONE = 0
 #BILLBOARDMODE_Z = 4
 BILLBOARDMODE_ALL = 7
 
-# used in Mesh constructor, defined in BABYLON.PhysicsEngine
+# used in Mesh constructor, defined in BABYLON.PhysicsImpostor
 SPHERE_IMPOSTER = 1
 BOX_IMPOSTER = 2
 #PLANE_IMPOSTER = 3
@@ -32,7 +32,7 @@ MESH_IMPOSTER = 4
 CAPSULE_IMPOSTER = 5
 CONE_IMPOSTER = 6
 CYLINDER_IMPOSTER = 7
-CONVEX_HULL_IMPOSTER = 8
+PARTICLE_IMPOSTER = 8
 
 SHAPE_KEY_GROUPS_ALLOWED = False
 #===============================================================================
@@ -107,24 +107,6 @@ class Mesh(FCurveAnimatable):
             self.dataName = self.name
             self.parentId = forcedParent.name
 
-        # Get if this will be an instance of another, before processing materials, to avoid multi-bakes
-        sourceMesh = exporter.getSourceMeshInstance(self.dataName)
-        if sourceMesh is not None:
-            #need to make sure rotation mode matches, since value initially copied in InstancedMesh constructor
-            if hasattr(sourceMesh, 'rotationQuaternion'):
-                instRot = None
-                instRotq = rot
-            else:
-                instRot = scale_vector(rot.to_euler('XYZ'), -1)
-                instRotq = None
-
-            instance = MeshInstance(self, instRot, instRotq)
-            sourceMesh.instances.append(instance)
-            Logger.log('mesh is an instance of :  ' + sourceMesh.name + '.  Processing halted.', 2)
-            return
-        else:
-            self.instances = []
-
         # Physics
         if object.rigid_body != None:
             shape_items = {'SPHERE'     : SPHERE_IMPOSTER,
@@ -133,10 +115,11 @@ class Mesh(FCurveAnimatable):
                            'CAPSULE'    : CAPSULE_IMPOSTER,
                            'CONE'       : CONE_IMPOSTER,
                            'CYLINDER'   : CYLINDER_IMPOSTER,
-                           'CONVEX_HULL': CONVEX_HULL_IMPOSTER}
+                           'CONVEX_HULL': PARTICLE_IMPOSTER}
 
             shape_type = shape_items[object.rigid_body.collision_shape]
             self.physicsImpostor = shape_type
+            
             mass = object.rigid_body.mass
             if mass < 0.005:
                 mass = 0
@@ -144,6 +127,24 @@ class Mesh(FCurveAnimatable):
             self.physicsFriction = object.rigid_body.friction
             self.physicsRestitution = object.rigid_body.restitution
 
+        # Get if this will be an instance of another, before processing materials, to avoid multi-bakes
+        sourceMesh = exporter.getSourceMeshInstance(self.dataName)
+        if sourceMesh is not None:
+            #need to make sure rotation mode matches, since value initially copied in InstancedMesh constructor
+            if hasattr(sourceMesh, 'rotationQuaternion'):
+                instRot = None
+                instRotq = rot
+            else:
+                instRot = scale_vector(rot.to_euler('XYZ'), -1)
+                instRotq = None
+
+            instance = MeshInstance(self, instRot, instRotq)
+            sourceMesh.instances.append(instance)
+            Logger.log('mesh is an instance of :  ' + sourceMesh.name + '.  Processing halted.', 2)
+            return
+        else:
+            self.instances = []
+
         # process all of the materials required
         maxVerts = MAX_VERTEX_ELEMENTS if scene.force64Kmeshes else MAX_VERTEX_ELEMENTS_32Bit # change for multi-materials or shapekeys
         recipe = BakingRecipe(object)
@@ -709,6 +710,12 @@ class MeshInstance:
             self.rotationQuaternion = rotationQuaternion
         self.scaling = instancedMesh.scaling
         self.freezeWorldMatrix = instancedMesh.freezeWorldMatrix
+        
+        if hasattr(instancedMesh, 'physicsImpostor'):
+            self.physicsImpostor = instancedMesh.physicsImpostor
+            self.physicsMass = instancedMesh.physicsMass
+            self.physicsFriction = instancedMesh.physicsFriction
+            self.physicsRestitution = instancedMesh.physicsRestitution
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      def to_scene_file(self, file_handler):
         file_handler.write('{')
@@ -723,6 +730,13 @@ class MeshInstance:
         write_vector(file_handler, 'scaling', self.scaling)
         # freeze World Matrix currently ignored for instances
         write_bool(file_handler, 'freezeWorldMatrix', self.freezeWorldMatrix)
+
+        if hasattr(self, 'physicsImpostor'):
+            write_int(file_handler, 'physicsImpostor', self.physicsImpostor)
+            write_float(file_handler, 'physicsMass', self.physicsMass)
+            write_float(file_handler, 'physicsFriction', self.physicsFriction)
+            write_float(file_handler, 'physicsRestitution', self.physicsRestitution)
+            
         file_handler.write('}')
 #===============================================================================
 class Node(FCurveAnimatable):

+ 16 - 16
src/Cameras/babylon.arcRotateCamera.ts

@@ -13,8 +13,8 @@ module BABYLON {
         public radius: number;
 
         @serializeAsVector3("target")
-        private _target: Vector3;
-        private _targetHost: AbstractMesh;
+        protected _target: Vector3;
+        protected _targetHost: AbstractMesh;
 
         public get target(): Vector3 {
             return this._target;
@@ -191,24 +191,24 @@ module BABYLON {
 
         // Panning
         public panningAxis: Vector3 = new Vector3(1, 1, 0);
-        private _localDirection: Vector3;
-        private _transformedDirection: Vector3;
+        protected _localDirection: Vector3;
+        protected _transformedDirection: Vector3;
 
         // Collisions
         public onCollide: (collidedMesh: AbstractMesh) => void;
         public checkCollisions = false;
         public collisionRadius = new Vector3(0.5, 0.5, 0.5);
-        private _collider: Collider;
-        private _previousPosition = Vector3.Zero();
-        private _collisionVelocity = Vector3.Zero();
-        private _newPosition = Vector3.Zero();
-        private _previousAlpha: number;
-        private _previousBeta: number;
-        private _previousRadius: number;
+        protected _collider: Collider;
+        protected _previousPosition = Vector3.Zero();
+        protected _collisionVelocity = Vector3.Zero();
+        protected _newPosition = Vector3.Zero();
+        protected _previousAlpha: number;
+        protected _previousBeta: number;
+        protected _previousRadius: number;
         //due to async collision inspection
-        private _collisionTriggered: boolean;
+        protected _collisionTriggered: boolean;
 
-        private _targetBoundingCenter: Vector3;
+        protected _targetBoundingCenter: Vector3;
 
         constructor(name: string, alpha: number, beta: number, radius: number, target: Vector3, scene: Scene) {
             super(name, Vector3.Zero(), scene);
@@ -249,7 +249,7 @@ module BABYLON {
             this._cache.targetScreenOffset.copyFrom(this.targetScreenOffset);
         }
 
-        private _getTargetPosition(): Vector3 {
+        protected _getTargetPosition(): Vector3 {
             if (this._targetHost && this._targetHost.getAbsolutePosition) {
                 var pos : Vector3 = this._targetHost.getAbsolutePosition();
                 if (this._targetBoundingCenter) {
@@ -367,7 +367,7 @@ module BABYLON {
             super._checkInputs();
         }
 
-        private _checkLimits() {
+        protected _checkLimits() {
             if (this.lowerBetaLimit === null || this.lowerBetaLimit === undefined) {
                 if (this.allowUpsideDown && this.beta > Math.PI) {
                     this.beta = this.beta - (2 * Math.PI);
@@ -494,7 +494,7 @@ module BABYLON {
             return this._viewMatrix;
         }
 
-        private _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
+        protected _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
 
             if (this.getScene().workerCollisions && this.checkCollisions) {
                 newPosition.multiplyInPlace(this._collider.radius);

+ 67 - 1
src/Debug/babylon.physicsViewer.ts

@@ -9,6 +9,10 @@
         protected _physicsEnginePlugin:IPhysicsEnginePlugin;
         private _renderFunction: () => void;
 
+        private _debugBoxMesh:Mesh;
+        private _debugSphereMesh:Mesh;
+        private _debugMaterial:StandardMaterial;
+
         constructor(scene:Scene){
 
             this._scene = scene || Engine.LastCreatedScene;
@@ -38,7 +42,7 @@
                 }
             }
 
-            var debugMesh = this._physicsEnginePlugin.getDebugMesh(impostor, this._scene);
+            var debugMesh = this._getDebugMesh(impostor, this._scene);
 
             if(debugMesh){
                 this._impostors[this._numMeshes] = impostor;
@@ -83,12 +87,74 @@
 
         }
 
+        private _getDebugMaterial(scene:Scene):Material{
+            if(!this._debugMaterial){
+                this._debugMaterial = new StandardMaterial('', scene);
+                this._debugMaterial.wireframe = true;
+            }
+            
+            return this._debugMaterial;
+        }
+
+        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
+            if(!this._debugBoxMesh){
+                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
+                this._debugBoxMesh.renderingGroupId = 1;
+                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
+                this._debugBoxMesh.material = this._getDebugMaterial(scene);
+                scene.removeMesh(this._debugBoxMesh);             
+            }
+
+            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
+        }
+
+        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
+            if(!this._debugSphereMesh){
+                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
+                this._debugSphereMesh.renderingGroupId = 1;
+                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
+                this._debugSphereMesh.material = this._getDebugMaterial(scene);
+                scene.removeMesh(this._debugSphereMesh);
+            }
+
+            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
+        }
+
+        private _getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
+            var body = impostor.physicsBody;
+            var shape = body.shapes[0];
+            var mesh:AbstractMesh;
+            
+            if (impostor.type == PhysicsImpostor.BoxImpostor) {
+                mesh = this._getDebugBoxMesh(scene);
+                impostor.getBoxSizeToRef(mesh.scaling);
+            } else if(impostor.type == PhysicsImpostor.SphereImpostor){	
+                mesh = this._getDebugSphereMesh(scene);  
+                var radius = impostor.getRadius();          
+                mesh.scaling.x = radius * 2;
+                mesh.scaling.y = radius * 2;
+                mesh.scaling.z = radius * 2;	
+            }
+
+            return mesh;
+        }
+
         public dispose(){
             
             for (var i = 0; i < this._numMeshes; i++){
                 this.hideImpostor(this._impostors[i]);
             }
 
+            if(this._debugBoxMesh){
+                this._debugBoxMesh.dispose();
+            }
+            if(this._debugSphereMesh){
+                this._debugSphereMesh.dispose();
+            }
+            if(this._debugMaterial){
+                this._debugMaterial.dispose();
+            }
+
             this._impostors.length = 0;
             this._scene = null;
             this._physicsEnginePlugin = null;

+ 6 - 6
src/Math/babylon.math.ts

@@ -3289,13 +3289,13 @@
             translation.y = this.m[13];
             translation.z = this.m[14];
 
-            var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
-            var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
-            var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
+            scale.x = Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
+            scale.y = Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
+            scale.z = Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
 
-            scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
-            scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
-            scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
+            if (this.determinant() <= 0) {
+                scale.y *= -1;
+            }
 
             if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
                 rotation.x = 0;

+ 14 - 67
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -9,11 +9,7 @@
         private _fixedTimeStep: number = 1 / 60;
         //See https://github.com/schteppe/cannon.js/blob/gh-pages/demos/collisionFilter.html
         private _currentCollisionGroup = 2;
-
-        private _debugBoxMesh:Mesh;
-        private _debugSphereMesh:Mesh;
-        private _debugMaterial:StandardMaterial;
-
+        
         public constructor(private _useDeltaForWorldStep: boolean = true, iterations: number = 10) {
             if (!this.isSupported()) {
                 Tools.Error("CannonJS is not available. Please make sure you included the js file.");
@@ -493,59 +489,6 @@
             joint.physicsJoint.motorEquation.minForce = lowerLimit === void 0 ? -upperLimit : lowerLimit;
         }
 
-        private _getDebugMaterial(scene:Scene):Material{
-            if(!this._debugMaterial){
-                this._debugMaterial = new StandardMaterial('', scene);
-                this._debugMaterial.wireframe = true;
-            }
-            
-            return this._debugMaterial;
-        }
-
-        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
-            if(!this._debugBoxMesh){
-                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                this._debugBoxMesh.renderingGroupId = 1;
-                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugBoxMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugBoxMesh);             
-            }
-
-            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
-            if(!this._debugSphereMesh){
-                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                this._debugSphereMesh.renderingGroupId = 1;
-                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugSphereMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugSphereMesh);
-            }
-
-            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        public getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
-            var body = impostor.physicsBody;
-            var shape = body.shapes[0];
-            var mesh:AbstractMesh;
-            
-            if (shape.halfExtents) {
-                mesh = this._getDebugBoxMesh(scene);
-                mesh.scaling.x = shape.halfExtents.x * 2;
-                mesh.scaling.y = shape.halfExtents.y * 2;
-                mesh.scaling.z = shape.halfExtents.z * 2;
-            } else if(shape.boundingSphereRadius){	
-                mesh = this._getDebugSphereMesh(scene);            
-                mesh.scaling.x = shape.boundingSphereRadius * 2;
-                mesh.scaling.y = shape.boundingSphereRadius * 2;
-                mesh.scaling.z = shape.boundingSphereRadius * 2;	
-            }
-
-            return mesh;
-        }
-
         public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
             var body = impostor.physicsBody;
 
@@ -559,16 +502,20 @@
             mesh.rotationQuaternion.w = body.quaternion.w;
         }
 
+        public getRadius(impostor: PhysicsImpostor):number{
+            var shape = impostor.physicsBody.shapes[0];
+            return shape.boundingSphereRadius;
+        }
+
+        public getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3):void{
+            var shape = impostor.physicsBody.shapes[0];
+            result.x = shape.halfExtents.x * 2;
+            result.y = shape.halfExtents.y * 2;
+            result.z = shape.halfExtents.z * 2;
+        }
+
         public dispose() {
-            if(this._debugBoxMesh){
-                this._debugBoxMesh.dispose();
-            }
-            if(this._debugSphereMesh){
-                this._debugSphereMesh.dispose();
-            }
-            if(this._debugMaterial){
-                this._debugMaterial.dispose();
-            }
+            
         }
     }
 }

+ 11 - 66
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -6,10 +6,6 @@ module BABYLON {
         public world: any;
         public name: string = "OimoJSPlugin";
 
-        private _debugBoxMesh:Mesh;
-        private _debugSphereMesh:Mesh;
-        private _debugMaterial:StandardMaterial;
-        
         constructor(iterations?: number) {
             this.world = new OIMO.World(1 / 60, 2, iterations, true);
             this.world.worldscale(1);
@@ -412,59 +408,6 @@ module BABYLON {
             }
         }
 
-        private _getDebugMaterial(scene:Scene):Material{
-            if(!this._debugMaterial){
-                this._debugMaterial = new StandardMaterial('', scene);
-                this._debugMaterial.wireframe = true;
-            }
-            
-            return this._debugMaterial;
-        }
-
-        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
-            if(!this._debugBoxMesh){
-                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                this._debugBoxMesh.renderingGroupId = 1;
-                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugBoxMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugBoxMesh);             
-            }
-
-            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
-            if(!this._debugSphereMesh){
-                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                this._debugSphereMesh.renderingGroupId = 1;
-                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugSphereMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugSphereMesh);
-            }
-
-            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        public getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
-            var body = impostor.physicsBody;
-            var shape = body.shapes;
-            var mesh:AbstractMesh;
-            
-            if (shape.halfWidth) {
-                mesh = this._getDebugBoxMesh(scene);
-                mesh.scaling.x = shape.halfWidth * 2;
-                mesh.scaling.y = shape.halfHeight * 2;
-                mesh.scaling.z = shape.halfDepth * 2;
-            } else if(shape.radius){	
-                mesh = this._getDebugSphereMesh(scene);            
-                mesh.scaling.x = shape.radius * 2;
-                mesh.scaling.y = shape.radius * 2;
-                mesh.scaling.z = shape.radius * 2;	
-            }
-
-            return mesh;
-        }
-
         public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
             var body = impostor.physicsBody;
 
@@ -478,16 +421,18 @@ module BABYLON {
             mesh.rotationQuaternion.w = body.orientation.s;
         }
 
+        public getRadius(impostor: PhysicsImpostor):number{
+            return impostor.physicsBody.shapes.radius;
+        }
+
+        public getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3):void{
+            var shape = impostor.physicsBody.shapes;
+            result.x = shape.halfWidth * 2;
+            result.y = shape.halfHeight * 2;
+            result.z = shape.halfDepth * 2;
+        }
+
         public dispose() {
-            if(this._debugBoxMesh){
-                this._debugBoxMesh.dispose();
-            }
-            if(this._debugSphereMesh){
-                this._debugSphereMesh.dispose();
-            }
-            if(this._debugMaterial){
-                this._debugMaterial.dispose();
-            }
             this.world.clear();
         }
     }

+ 2 - 1
src/Physics/babylon.physicsEngine.ts

@@ -188,7 +188,8 @@
         updateDistanceJoint(joint: PhysicsJoint, maxDistance:number, minDistance?: number);
         setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number);
         setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number);
-        getDebugMesh(impostor:PhysicsImpostor, scene:Scene);
+        getRadius(impostor: PhysicsImpostor):number;
+        getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3);
         syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor);
         dispose();
     }

+ 8 - 0
src/Physics/babylon.physicsImpostor.ts

@@ -482,6 +482,14 @@ module BABYLON {
             this._deltaRotationConjugated = this._deltaRotation.conjugate();
         }
 
+        public getBoxSizeToRef(result:Vector3){
+            this._physicsEngine.getPhysicsPlugin().getBoxSizeToRef(this, result);
+        }
+
+        public getRadius():number{
+            return this._physicsEngine.getPhysicsPlugin().getRadius(this);
+        }
+
         //Impostor types
         public static NoImpostor = 0;
         public static SphereImpostor = 1;