فهرست منبع

Rotate drag plane by attached mesh's orienation and move world space position of attached mesh

Trevor Baron 7 سال پیش
والد
کامیت
14960249d6
3فایلهای تغییر یافته به همراه12 افزوده شده و 6 حذف شده
  1. 1 1
      dist/preview release/what's new.md
  2. 10 4
      src/Behaviors/Mesh/babylon.pointerDragBehavior.ts
  3. 1 1
      src/Gizmos/babylon.planeRotationGizmo.ts

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

@@ -27,7 +27,7 @@
 - Get a root mesh from an asset container, load a mesh from a file with a single string url ([TrevorDev](https://github.com/TrevorDev))
 - Get a root mesh from an asset container, load a mesh from a file with a single string url ([TrevorDev](https://github.com/TrevorDev))
 - UtilityLayer class to render another scene as a layer on top of an existing scene ([TrevorDev](https://github.com/TrevorDev))
 - UtilityLayer class to render another scene as a layer on top of an existing scene ([TrevorDev](https://github.com/TrevorDev))
 - AnimationGroup has now onAnimationGroupEnd observable ([RaananW](https://github.com/RaananW))
 - AnimationGroup has now onAnimationGroupEnd observable ([RaananW](https://github.com/RaananW))
-- PointerDragBehavior, SixDofDragBehavior, MultiPointerScaleBehavior to enable drag and drop/scaling with mouse or 6dof controller on a mesh ([TrevorDev](https://github.com/TrevorDev))
+- PointerDragBehavior, SixDofDragBehavior and MultiPointerScaleBehavior to enable drag and drop/scaling with mouse or 6dof controller on a mesh ([TrevorDev](https://github.com/TrevorDev))
 - Gizmo and GizmoManager classes used to manipulate meshes in a scene. Position, rotation, scale, and bounding box gizmos ([TrevorDev](https://github.com/TrevorDev))
 - Gizmo and GizmoManager classes used to manipulate meshes in a scene. Position, rotation, scale, and bounding box gizmos ([TrevorDev](https://github.com/TrevorDev))
 - Added a new `mesh.ignoreNonUniformScaling` to turn off non uniform scaling compensation ([Deltakosh](https://github.com/deltakosh))
 - Added a new `mesh.ignoreNonUniformScaling` to turn off non uniform scaling compensation ([Deltakosh](https://github.com/deltakosh))
 - AssetsManager tasks will only run when their state is INIT. It is now possible to remove a task from the assets manager ([RaananW](https://github.com/RaananW))
 - AssetsManager tasks will only run when their state is INIT. It is now possible to remove a task from the assets manager ([RaananW](https://github.com/RaananW))

+ 10 - 4
src/Behaviors/Mesh/babylon.pointerDragBehavior.ts

@@ -50,11 +50,16 @@ module BABYLON {
         public _dragPlaneParent:Nullable<Mesh>=null;
         public _dragPlaneParent:Nullable<Mesh>=null;
 
 
         /**
         /**
-         *  If the drag behavior will react to drag events
+         *  If the drag behavior will react to drag events (Default: true)
          */
          */
         public enabled = true;
         public enabled = true;
         
         
         /**
         /**
+         * If set, the drag plane/axis will be rotated based on the attached mesh's world rotation (Default: true)
+         */
+        public useObjectOrienationForDragging = true;
+
+        /**
          * Creates a pointer drag behavior that can be attached to a mesh
          * Creates a pointer drag behavior that can be attached to a mesh
          * @param options The drag axis or normal of the plane that will be dragged across. If no options are specified the drag plane will always face the ray's origin (eg. camera)
          * @param options The drag axis or normal of the plane that will be dragged across. If no options are specified the drag plane will always face the ray's origin (eg. camera)
          */
          */
@@ -159,7 +164,7 @@ module BABYLON {
                                 pickedPoint.subtractToRef(this.lastDragPosition, delta);
                                 pickedPoint.subtractToRef(this.lastDragPosition, delta);
                             }
                             }
                             if(this.moveAttached){
                             if(this.moveAttached){
-                                (<Mesh>this._attachedNode).position.addInPlace(delta);
+                                (<Mesh>this._attachedNode).setAbsolutePosition((<Mesh>this._attachedNode).absolutePosition.add(delta));
                             }
                             }
                             this.onDragObservable.notifyObservers({dragDistance: dragLength, delta: delta, dragPlanePoint: pickedPoint, dragPlaneNormal: this._dragPlane.forward, pointerId: this.currentDraggingPointerID});
                             this.onDragObservable.notifyObservers({dragDistance: dragLength, delta: delta, dragPlanePoint: pickedPoint, dragPlaneNormal: this._dragPlane.forward, pointerId: this.currentDraggingPointerID});
                             this.lastDragPosition.copyFrom(pickedPoint);
                             this.lastDragPosition.copyFrom(pickedPoint);
@@ -191,7 +196,7 @@ module BABYLON {
         private _updateDragPlanePosition(ray:Ray){
         private _updateDragPlanePosition(ray:Ray){
             var pointA = this._dragPlaneParent ? this._dragPlaneParent.absolutePosition : (<Mesh>this._attachedNode).absolutePosition;
             var pointA = this._dragPlaneParent ? this._dragPlaneParent.absolutePosition : (<Mesh>this._attachedNode).absolutePosition;
             if(this.options.dragAxis){
             if(this.options.dragAxis){
-                var localAxis = Vector3.TransformCoordinates(this.options.dragAxis, this._attachedNode.getWorldMatrix().getRotationMatrix());
+                var localAxis = this.useObjectOrienationForDragging ? Vector3.TransformCoordinates(this.options.dragAxis, this._attachedNode.getWorldMatrix().getRotationMatrix()) : this.options.dragAxis;
 
 
                 // Calculate plane normal in direction of camera but perpendicular to drag axis
                 // Calculate plane normal in direction of camera but perpendicular to drag axis
                 var pointB = pointA.add(localAxis); // towards drag axis
                 var pointB = pointA.add(localAxis); // towards drag axis
@@ -206,8 +211,9 @@ module BABYLON {
                 this._dragPlane.position.copyFrom(pointA);
                 this._dragPlane.position.copyFrom(pointA);
                 this._dragPlane.lookAt(pointA.subtract(norm));
                 this._dragPlane.lookAt(pointA.subtract(norm));
             }else if(this.options.dragPlaneNormal){
             }else if(this.options.dragPlaneNormal){
+                var localAxis = this.useObjectOrienationForDragging ? Vector3.TransformCoordinates(this.options.dragPlaneNormal, this._attachedNode.getWorldMatrix().getRotationMatrix()) : this.options.dragPlaneNormal;
                 this._dragPlane.position.copyFrom(pointA);
                 this._dragPlane.position.copyFrom(pointA);
-                this._dragPlane.lookAt(pointA.subtract(this.options.dragPlaneNormal));
+                this._dragPlane.lookAt(pointA.subtract(localAxis));
             }else{
             }else{
                 this._dragPlane.position.copyFrom(pointA);
                 this._dragPlane.position.copyFrom(pointA);
                 this._dragPlane.lookAt(ray.origin);
                 this._dragPlane.lookAt(ray.origin);

+ 1 - 1
src/Gizmos/babylon.planeRotationGizmo.ts

@@ -29,7 +29,7 @@ module BABYLON {
             this._rootMesh.lookAt(this._rootMesh.position.subtract(planeNormal));
             this._rootMesh.lookAt(this._rootMesh.position.subtract(planeNormal));
 
 
             // Add drag behavior to handle events when the gizmo is dragged
             // Add drag behavior to handle events when the gizmo is dragged
-            this._dragBehavior = new PointerDragBehavior({dragPlaneNormal: planeNormal});
+            this._dragBehavior = new PointerDragBehavior({dragPlaneNormal: new Vector3(0,0,1)});
             this._dragBehavior.moveAttached = false;
             this._dragBehavior.moveAttached = false;
             this._rootMesh.addBehavior(this._dragBehavior);
             this._rootMesh.addBehavior(this._dragBehavior);