Sfoglia il codice sorgente

tweaks to Debug.PhysicsViewer

Adam Bowman 8 anni fa
parent
commit
a52836a745

+ 18 - 18
src/Debug/babylon.physicsViewer.ts

@@ -2,7 +2,7 @@
 
     export class PhysicsViewer {
         
-        protected _imposters:Array<PhysicsImpostor> = [];
+        protected _impostors:Array<PhysicsImpostor> = [];
         protected _meshes:Array<AbstractMesh> = [];
         protected _scene:Scene;
         protected _numMeshes = 0;
@@ -21,30 +21,30 @@
             var plugin = this._physicsEnginePlugin;
 
             for (var i = 0; i < this._numMeshes; i++){
-                if(this._imposters[i].isDisposed){
-                    this.hideImposter(this._imposters[i--]);
+                if(this._impostors[i].isDisposed){
+                    this.hideImpostor(this._impostors[i--]);
                 }else{
-                    plugin.syncMeshWithImposter(this._meshes[i], this._imposters[i]);
+                    plugin.syncMeshWithImpostor(this._meshes[i], this._impostors[i]);
                 }
             }
 
         }
 
-        public showImposter(imposter:PhysicsImpostor):void{
+        public showImpostor(impostor:PhysicsImpostor):void{
 
             for (var i = 0; i < this._numMeshes; i++){
-                if(this._imposters[i] == imposter){
+                if(this._impostors[i] == impostor){
                     return;
                 }
             }
 
-            var debugMesh = this._physicsEnginePlugin.getDebugMesh(imposter, this._scene);
+            var debugMesh = this._physicsEnginePlugin.getDebugMesh(impostor, this._scene);
 
             if(debugMesh){
-                this._imposters[this._numMeshes] = imposter;
+                this._impostors[this._numMeshes] = impostor;
                 this._meshes[this._numMeshes] = debugMesh;
 
-                if(this._numMeshes == 0){
+                if(this._numMeshes === 0){
                     this._renderFunction = this._updateDebugMeshes.bind(this);
                     this._scene.registerBeforeRender(this._renderFunction);
                 }
@@ -54,31 +54,31 @@
 
         }
 
-        public hideImposter(imposter:PhysicsImpostor){
+        public hideImpostor(impostor:PhysicsImpostor){
 
             var removed = false;
 
             for (var i = 0; i < this._numMeshes; i++){
-                if(this._imposters[i] == imposter){
+                if(this._impostors[i] == impostor){
                     this._scene.removeMesh(this._meshes[i]);
                     this._meshes[i].dispose();
                     this._numMeshes--;
                     if(this._numMeshes > 0){
                         this._meshes[i] = this._meshes[this._numMeshes];
-                        this._imposters[i] = this._imposters[this._numMeshes];
+                        this._impostors[i] = this._impostors[this._numMeshes];
                         this._meshes[this._numMeshes] = null;
-                        this._imposters[this._numMeshes] = null;
+                        this._impostors[this._numMeshes] = null;
                     }else{
                         this._meshes[0] = null;
-                        this._imposters[0] = null;
+                        this._impostors[0] = null;
                     }
                     removed = true;
                     break;
                 }
             }
 
-            if(removed && this._numMeshes == 0){
-                this._scene.registerBeforeRender(this._renderFunction);
+            if(removed && this._numMeshes === 0){
+                this._scene.unregisterBeforeRender(this._renderFunction);
             }
 
         }
@@ -86,10 +86,10 @@
         public dispose(){
             
             for (var i = 0; i < this._numMeshes; i++){
-                this.hideImposter(this._imposters[i]);
+                this.hideImpostor(this._impostors[i]);
             }
 
-            this._imposters.length = 0;
+            this._impostors.length = 0;
             this._scene = null;
             this._physicsEnginePlugin = null;
             

+ 43 - 25
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -12,6 +12,7 @@
 
         private _debugBoxMesh:Mesh;
         private _debugSphereMesh:Mesh;
+        private _debugMaterial:StandardMaterial;
 
         public constructor(private _useDeltaForWorldStep: boolean = true, iterations: number = 10) {
             if (!this.isSupported()) {
@@ -472,35 +473,51 @@
             joint.physicsJoint.motorEquation.minForce = lowerLimit === void 0 ? -upperLimit : lowerLimit;
         }
 
-        public getDebugMesh(imposter:PhysicsImpostor, scene:Scene):AbstractMesh{
+        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');
+        }
 
-            var body = imposter.physicsBody;
+        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:InstancedMesh;
+            var mesh:AbstractMesh;
             
             if (shape.halfExtents) {
-                if(!this._debugBoxMesh){
-                    this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                    this._debugBoxMesh.renderingGroupId = 1;
-                    this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                    this._debugBoxMesh.material = new StandardMaterial('', scene);
-                    this._debugBoxMesh.material.wireframe = true;
-                    scene.removeMesh(this._debugBoxMesh);             
-                }
-                mesh = this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
+                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){	
-                if(!this._debugSphereMesh){
-                    this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                    this._debugSphereMesh.renderingGroupId = 1;
-                    this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                    this._debugSphereMesh.material = new StandardMaterial('', scene);
-                    this._debugSphereMesh.material.wireframe = true;
-                    scene.removeMesh(this._debugSphereMesh);
-                }	
-                mesh = this._debugSphereMesh.createInstance('physicsBodySphereViewInstance');            
+                mesh = this._getDebugSphereMesh(scene);            
                 mesh.scaling.x = shape.boundingSphereRadius * 2;
                 mesh.scaling.y = shape.boundingSphereRadius * 2;
                 mesh.scaling.z = shape.boundingSphereRadius * 2;	
@@ -509,9 +526,8 @@
             return mesh;
         }
 
-        public syncMeshWithImposter(mesh:AbstractMesh, imposter:PhysicsImpostor){
-
-            var body = imposter.physicsBody;
+        public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
+            var body = impostor.physicsBody;
 
             mesh.position.x = body.position.x;
             mesh.position.y = body.position.y;
@@ -521,7 +537,6 @@
             mesh.rotationQuaternion.y = body.quaternion.y;
             mesh.rotationQuaternion.z = body.quaternion.z;
             mesh.rotationQuaternion.w = body.quaternion.w;
-
         }
 
         public dispose() {
@@ -531,6 +546,9 @@
             if(this._debugSphereMesh){
                 this._debugSphereMesh.dispose();
             }
+            if(this._debugMaterial){
+                this._debugMaterial.dispose();
+            }
         }
     }
 }

+ 43 - 25
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -8,6 +8,7 @@ module BABYLON {
 
         private _debugBoxMesh:Mesh;
         private _debugSphereMesh:Mesh;
+        private _debugMaterial:StandardMaterial;
         
         constructor(iterations?: number) {
             this.world = new OIMO.World(1 / 60, 2, iterations, true);
@@ -391,35 +392,51 @@ module BABYLON {
             }
         }
 
-        public getDebugMesh(imposter:PhysicsImpostor, scene:Scene):AbstractMesh{
+        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);
+            }
 
-            var body = imposter.physicsBody;
+            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
+        }
+
+        public getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
+            var body = impostor.physicsBody;
             var shape = body.shapes;
-            var mesh:InstancedMesh;
+            var mesh:AbstractMesh;
             
             if (shape.halfWidth) {
-                if(!this._debugBoxMesh){
-                    this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                    this._debugBoxMesh.renderingGroupId = 1;
-                    this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                    this._debugBoxMesh.material = new StandardMaterial('', scene);
-                    this._debugBoxMesh.material.wireframe = true;
-                    scene.removeMesh(this._debugBoxMesh);             
-                }
-                mesh = this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
+                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){	
-                if(!this._debugSphereMesh){
-                    this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                    this._debugSphereMesh.renderingGroupId = 1;
-                    this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                    this._debugSphereMesh.material = new StandardMaterial('', scene);
-                    this._debugSphereMesh.material.wireframe = true;
-                    scene.removeMesh(this._debugSphereMesh);
-                }	
-                mesh = this._debugSphereMesh.createInstance('physicsBodySphereViewInstance');            
+                mesh = this._getDebugSphereMesh(scene);            
                 mesh.scaling.x = shape.radius * 2;
                 mesh.scaling.y = shape.radius * 2;
                 mesh.scaling.z = shape.radius * 2;	
@@ -428,9 +445,8 @@ module BABYLON {
             return mesh;
         }
 
-        public syncMeshWithImposter(mesh:AbstractMesh, imposter:PhysicsImpostor){
-
-            var body = imposter.physicsBody;
+        public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
+            var body = impostor.physicsBody;
 
             mesh.position.x = body.position.x;
             mesh.position.y = body.position.y;
@@ -440,7 +456,6 @@ module BABYLON {
             mesh.rotationQuaternion.y = body.orientation.y;
             mesh.rotationQuaternion.z = body.orientation.z;
             mesh.rotationQuaternion.w = body.orientation.s;
-
         }
 
         public dispose() {
@@ -450,6 +465,9 @@ module BABYLON {
             if(this._debugSphereMesh){
                 this._debugSphereMesh.dispose();
             }
+            if(this._debugMaterial){
+                this._debugMaterial.dispose();
+            }
             this.world.clear();
         }
     }

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

@@ -183,8 +183,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(imposter:PhysicsImpostor, scene:Scene);
-        syncMeshWithImposter(mesh:AbstractMesh, imposter:PhysicsImpostor);
+        getDebugMesh(impostor:PhysicsImpostor, scene:Scene);
+        syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor);
         dispose();
     }
 }