浏览代码

use drag behavior with bounding box in gizmo manager

Trevor Baron 7 年之前
父节点
当前提交
18231cc8e2

+ 1 - 1
dist/preview release/what's new.md

@@ -7,7 +7,7 @@
 - New GUI 3D controls toolset. [Complete doc + demos](http://doc.babylonjs.com/how_to/gui3d) ([Deltakosh](https://github.com/deltakosh))
 - Added [Environment Texture Tools](https://doc.babylonjs.com/how_to/physically_based_rendering#creating-a-compressed-environment-texture) to reduce the size of the usual .DDS file ([sebavan](http://www.github.com/sebavan))
 - New GUI control: the [Grid](http://doc.babylonjs.com/how_to/gui#grid) ([Deltakosh](https://github.com/deltakosh))
-- Gizmo and GizmoManager classes used to manipulate meshes in a scene. Gizmo types include: position, rotation, scale and bounding box. [Doc](http://doc.babylonjs.com/how_to/gizmo) ([TrevorDev](https://github.com/TrevorDev))
+- Gizmo and GizmoManager classes used to manipulate meshes in a scene. Gizmo types include: position, scale, rotation and bounding box. [Doc](http://doc.babylonjs.com/how_to/gizmo) ([TrevorDev](https://github.com/TrevorDev))
 - New behaviors: PointerDragBehavior, SixDofDragBehavior and MultiPointerScaleBehavior to enable smooth drag and drop/scaling with mouse or 6dof controller on a mesh. [Doc](http://doc.babylonjs.com/how_to/meshbehavior) ([TrevorDev](https://github.com/TrevorDev))
 - Particle system improvements ([Deltakosh](https://github.com/deltakosh))
   - Added a ParticleHelper class to create some pre-configured particle systems in a one-liner method style. [Doc](https://doc.babylonjs.com/How_To/ParticleHelper) ([Deltakosh](https://github.com/deltakosh)) / ([DevChris](https://github.com/yovanoc))

+ 12 - 4
src/Behaviors/Mesh/babylon.sixDofDragBehavior.ts

@@ -177,10 +177,18 @@ module BABYLON {
          *  Detaches the behavior from the mesh
          */
         public detach(): void {
-            this._scene.onPointerObservable.remove(this._pointerObserver);
-            this._ownerNode.getScene().onBeforeRenderObservable.remove(this._sceneRenderObserver);
-            this._virtualOriginMesh.dispose()
-            this._virtualDragMesh.dispose();
+            if(this._scene){
+                this._scene.onPointerObservable.remove(this._pointerObserver);
+            }
+            if(this._ownerNode){
+                this._ownerNode.getScene().onBeforeRenderObservable.remove(this._sceneRenderObserver);
+            }
+            if(this._virtualOriginMesh){
+                this._virtualOriginMesh.dispose();
+            }
+            if(this._virtualDragMesh){
+                this._virtualDragMesh.dispose();
+            }
         }
     }
 }

+ 5 - 1
src/Gizmos/babylon.gizmo.ts

@@ -74,7 +74,11 @@ module BABYLON {
                         this._rootMesh.position.copyFrom(this.attachedMesh.absolutePosition);
                     }
                     if(this._updateScale && this.gizmoLayer.utilityLayerScene.activeCamera && this.attachedMesh){
-                        this._rootMesh.position.subtractToRef(this.gizmoLayer.utilityLayerScene.activeCamera.position, tempVector);
+                        var cameraPosition = this.gizmoLayer.utilityLayerScene.activeCamera.position;
+                        if((<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition){
+                            cameraPosition = (<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition;
+                        }
+                        this._rootMesh.position.subtractToRef(cameraPosition, tempVector);
                         var dist = tempVector.length()/this._scaleFactor;
                         this._rootMesh.scaling.set(dist, dist, dist);
                     }

+ 11 - 0
src/Gizmos/babylon.gizmoManager.ts

@@ -9,6 +9,7 @@ module BABYLON {
         private _attachedMesh:Nullable<AbstractMesh> = null;
         private _boundingBoxColor = BABYLON.Color3.FromHexString("#0984e3");
         private _boundingBoxUtilLayer:Nullable<UtilityLayerRenderer> = null;
+        private _dragBehavior = new BABYLON.SixDofDragBehavior();
         /**
          * Array of meshes which will have the gizmo attached when a pointer selected them. If null, all meshes are attachable. (Default: null)
          */
@@ -66,6 +67,9 @@ module BABYLON {
          * @param mesh The mesh the gizmo's should be attached to
          */
         public attachToMesh(mesh:Nullable<AbstractMesh>){
+            if(this._attachedMesh){
+                this._attachedMesh.removeBehavior(this._dragBehavior);
+            }
             this._attachedMesh = mesh;
             for(var key in this._gizmoSet){
                 var gizmo = <Nullable<Gizmo>>((<any>this._gizmoSet)[key]);
@@ -73,6 +77,9 @@ module BABYLON {
                     gizmo.attachedMesh = mesh;
                 }
             }
+            if(this.boundingBoxGizmoEnabled && this._attachedMesh){
+                this._attachedMesh.addBehavior(this._dragBehavior);
+            }
         }
 
         /**
@@ -133,6 +140,10 @@ module BABYLON {
                 }
                 this._gizmoSet.boundingBoxGizmo = this._gizmoSet.boundingBoxGizmo || new BoundingBoxGizmo(this._boundingBoxColor, this._boundingBoxUtilLayer);
                 this._gizmoSet.boundingBoxGizmo.attachedMesh = this._attachedMesh;
+                if(this._attachedMesh){
+                    this._attachedMesh.removeBehavior(this._dragBehavior);
+                    this._attachedMesh.addBehavior(this._dragBehavior);
+                }
             }else if(this._gizmoSet.boundingBoxGizmo){
                 this._gizmoSet.boundingBoxGizmo.dispose();
                 this._gizmoSet.boundingBoxGizmo = null;