浏览代码

new physics tab for Inspector

Adam Bowman 8 年之前
父节点
当前提交
30d1f7424e

+ 3 - 0
inspector/src/Inspector.ts

@@ -308,6 +308,9 @@ module INSPECTOR {
          */
         public dispose() {
             if (!this._popupMode) {
+                     
+                this._tabbar.getActiveTab().dispose();
+
                 // Get canvas
                 let canvas = this._scene.getEngine().getRenderingCanvas();
 

+ 61 - 0
inspector/src/adapters/PhysicsImpostorAdapter.ts

@@ -0,0 +1,61 @@
+module INSPECTOR {
+
+    export class PhysicsImpostorAdapter
+        extends Adapter
+        implements IToolVisible {
+
+        private _viewer:BABYLON.Debug.PhysicsViewer;
+        private _isVisible = false;
+
+        constructor(obj: BABYLON.PhysicsImpostor, viewer:BABYLON.Debug.PhysicsViewer) {
+            super(obj);
+            this._viewer = viewer;
+        }
+
+        /** Returns the name displayed in the tree */
+        public id(): string {
+            let str = '';
+            let physicsImposter = (<BABYLON.PhysicsImpostor>this._obj);
+            if (physicsImposter && physicsImposter.object) {
+                str = (<BABYLON.AbstractMesh>physicsImposter.object).name;
+            } // otherwise nothing displayed        
+            return str;
+        }
+
+        /** Returns the type of this object - displayed in the tree */
+        public type(): string {
+            return Helpers.GET_TYPE(this._obj);
+        }
+
+        /** Returns the list of properties to be displayed for this adapter */
+        public getProperties(): Array<PropertyLine> {
+            let propertiesLines: Array<PropertyLine> = [];
+
+            for (let dirty of PROPERTIES['PhysicsImpostor'].properties) {
+                let infos = new Property(dirty, this._obj);
+                propertiesLines.push(new PropertyLine(infos));
+            }
+            return propertiesLines;
+        }
+
+        public getTools(): Array<AbstractTreeTool> {
+            let tools = [];
+            tools.push(new Checkbox(this));
+            return tools;
+        }
+
+        public setVisible(b: boolean) {
+            this._isVisible = b;
+            if(b){
+                this._viewer.showImpostor(this._obj);
+            }else{
+                this._viewer.hideImpostor(this._obj);
+            }
+        }
+        
+        public isVisible(): boolean {
+            return this._isVisible;
+        }
+        
+    }
+}

+ 8 - 2
inspector/src/details/PropertyLine.ts

@@ -145,9 +145,15 @@ module INSPECTOR {
         private _validateInput(e: KeyboardEvent) {
             if (e.keyCode == 13) {
                 // Enter : validate the new value
-                let newValue = this._input.value;
+                let newValue:any = this._input.value;
+
                 this.updateObject();
-                this._property.value = newValue;
+
+                if(typeof this._property.value === 'number'){
+                    this._property.value = parseFloat(newValue);
+                }else{
+                    this._property.value = newValue;
+                }
                 // Remove input
                 this.update();
                 // resume scheduler

+ 9 - 1
inspector/src/properties.ts

@@ -405,7 +405,15 @@ module INSPECTOR {
         },
         'WorldSpaceCanvas2DNode': {
             type: BABYLON.WorldSpaceCanvas2DNode
-        }
+        },
+        'PhysicsImpostor': {
+            type: BABYLON.PhysicsImpostor,
+            properties: [
+                'friction',
+                'mass',
+                'restitution',
+            ]
+        },
     }
 
 }

+ 37 - 0
inspector/src/tabs/PhysicsTab.ts

@@ -0,0 +1,37 @@
+module INSPECTOR{
+    
+    export class PhysicsTab extends PropertyTab {
+
+        public viewer:BABYLON.Debug.PhysicsViewer;
+                
+        constructor(tabbar:TabBar, inspector:Inspector) {
+            super(tabbar, 'Physics', inspector); 
+        }
+
+        /* Overrides super */
+        protected _getTree() : Array<TreeItem> {
+            let arr = [];
+
+            let scene = this._inspector.scene;
+            
+            if(!scene.isPhysicsEnabled()){
+                return arr;
+            }
+
+            if(!this.viewer){
+                this.viewer = new BABYLON.Debug.PhysicsViewer(scene);
+            }
+
+            for (let mesh of scene.meshes) {
+                if (mesh.physicsImpostor) {
+                    arr.push(new TreeItem(this, new PhysicsImpostorAdapter(mesh.physicsImpostor, this.viewer)));
+                }
+            }
+            return arr;
+        }
+
+        public dispose() {
+            this._detailsPanel.dispose();
+        }
+    }
+}

+ 1 - 1
inspector/src/tabs/TabBar.ts

@@ -34,7 +34,7 @@ module INSPECTOR {
             this._tabs.push(new LightTab(this, this._inspector));
             this._tabs.push(new MaterialTab(this, this._inspector));
             this._tabs.push(new GUITab(this, this._inspector));
-
+            this._tabs.push(new PhysicsTab(this, this._inspector));
             this._tabs.push(new CameraTab(this, this._inspector));
             this._tabs.push(new SoundTab(this, this._inspector));
 

+ 20 - 0
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -434,6 +434,26 @@
             impostor.physicsBody.updateMassProperties();
         }
 
+        public getBodyMass(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.mass;
+        }
+
+        public getBodyFriction(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.material.friction;
+        }
+
+        public setBodyFriction(impostor: PhysicsImpostor, friction:number) {
+            impostor.physicsBody.material.friction = friction;
+        }
+
+        public getBodyRestitution(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.material.restitution;
+        }
+
+        public setBodyRestitution(impostor: PhysicsImpostor, restitution:number) {
+            impostor.physicsBody.material.restitution = restitution;
+        }
+
         public sleepBody(impostor: PhysicsImpostor) {
             impostor.physicsBody.sleep();
         }

+ 20 - 0
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -361,6 +361,26 @@ module BABYLON {
             impostor.physicsBody.setupMass(staticBody ? 0x2 : 0x1);
         }
 
+        public getBodyMass(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.shapes.density;
+        }
+
+        public getBodyFriction(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.shapes.friction;
+        }
+
+        public setBodyFriction(impostor: PhysicsImpostor, friction:number) {
+            impostor.physicsBody.shapes.friction = friction;
+        }
+
+        public getBodyRestitution(impostor: PhysicsImpostor):number {
+            return impostor.physicsBody.shapes.restitution;
+        }
+
+        public setBodyRestitution(impostor: PhysicsImpostor, restitution:number) {
+            impostor.physicsBody.shapes.restitution = restitution;
+        }
+
         public sleepBody(impostor: PhysicsImpostor) {
             impostor.physicsBody.sleep();
         }

+ 5 - 0
src/Physics/babylon.physicsEngine.ts

@@ -177,6 +177,11 @@
         getLinearVelocity(impostor: PhysicsImpostor) : Vector3;
         getAngularVelocity(impostor: PhysicsImpostor) : Vector3;
         setBodyMass(impostor: PhysicsImpostor, mass: number);
+        getBodyMass(impostor: PhysicsImpostor);
+        getBodyFriction(impostor: PhysicsImpostor);
+        setBodyFriction(impostor: PhysicsImpostor, friction: number);
+        getBodyRestitution(impostor: PhysicsImpostor);
+        setBodyRestitution(impostor: PhysicsImpostor, restitution: number);
         sleepBody(impostor: PhysicsImpostor);
         wakeUpBody(impostor: PhysicsImpostor);
         //Joint Update

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

@@ -50,6 +50,30 @@ module BABYLON {
             return this._isDisposed;
         }
 
+        get mass():number{
+            return this._physicsEngine.getPhysicsPlugin().getBodyMass(this);
+        }
+
+        set mass(value:number){
+            this.setMass(value);
+        }
+
+        get friction():number{
+            return this._physicsEngine.getPhysicsPlugin().getBodyFriction(this);
+        }
+
+        set friction(value:number){
+            this._physicsEngine.getPhysicsPlugin().setBodyFriction(this, value);
+        }
+
+        get restitution():number {
+            return this._physicsEngine.getPhysicsPlugin().getBodyRestitution(this);
+        }
+
+        set restitution(value:number){
+            this._physicsEngine.getPhysicsPlugin().setBodyRestitution(this, value);
+        }
+
         //set by the physics engine when adding this impostor to the array.
         public uniqueId: number;