Bladeren bron

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 7 jaren geleden
bovenliggende
commit
af892c8843

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

@@ -38,7 +38,7 @@
 - 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))
 - PointerDragBehavior, SixDofDragBehavior and MultiPointerScaleBehavior to enable smooth 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. Gizmo types include position, rotation, scale, and bounding box ([TrevorDev](https://github.com/TrevorDev))
+- Gizmo and GizmoManager classes used to manipulate meshes in a scene. Gizmo types include: position, rotation, scale, and bounding box ([TrevorDev](https://github.com/TrevorDev))
 - 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))
 - Added sprite isVisible field ([TrevorDev](https://github.com/TrevorDev))
@@ -52,6 +52,7 @@
 - Added poster property on VideoTexture ([sebavan](http://www.github.com/sebavan))
 - Added ```onUserActionRequestedObservable``` to workaround and detect autoplay video policy restriction on VideoTexture ([sebavan](http://www.github.com/sebavan))
 - `Sound` now accepts `MediaStream` as source to enable easier WebAudio and WebRTC integrations ([menduz](https://github.com/menduz))
+- Vector x, y and z constructor parameters are now optional and default to 0 ([TrevorDev](https://github.com/TrevorDev))
 
 ### glTF Loader
 

+ 9 - 3
src/Behaviors/Mesh/babylon.pointerDragBehavior.ts

@@ -24,6 +24,10 @@ module BABYLON {
          * The distance towards the target drag position to move each frame. This can be useful to avoid jitter. Set this to 1 for no delay. (Default: 0.2)
          */
         public dragDeltaRatio = 0.2;
+        /**
+         * If the drag plane orientation should be updated during the dragging (Default: true)
+         */
+        public updateDragPlane = true;
         // Debug mode will display drag planes to help visualize behavior
         private _debugMode = false;
         private _moving = false;
@@ -149,8 +153,10 @@ module BABYLON {
                         var pickedPoint = this._pickWithRayOnDragPlane(pointerInfo.pickInfo.ray);
                         
                         if (pickedPoint) {
-                            this._updateDragPlanePosition(pointerInfo.pickInfo.ray, pickedPoint);
-
+                            if(this.updateDragPlane){
+                                this._updateDragPlanePosition(pointerInfo.pickInfo.ray, pickedPoint);
+                            }
+                            
                             // depending on the drag mode option drag accordingly
                             if(this.options.dragAxis){
                                 // Convert local drag axis to world
@@ -209,7 +215,7 @@ module BABYLON {
         private _lineA = new Vector3(0,0,0);
         private _lineB = new Vector3(0,0,0);
         private _localAxis = new Vector3(0,0,0);
-        private _lookAt = new Vector3(0,0,0); 
+        private _lookAt = new Vector3(0,0,0);
         // Position the drag plane based on the attached mesh position, for single axis rotate the plane along the axis to face the camera
         private _updateDragPlanePosition(ray:Ray, dragPlanePosition:Vector3){
             this._pointA.copyFrom(dragPlanePosition);

+ 34 - 6
src/Behaviors/Mesh/babylon.sixDofDragBehavior.ts

@@ -11,6 +11,8 @@ module BABYLON {
         private _virtualOriginMesh:AbstractMesh;
         private _virtualDragMesh:AbstractMesh;
         private _pointerObserver:Nullable<Observer<PointerInfo>>;
+        private _moving = false;
+        private _startingOrientation = new Quaternion();
         /**
          * How much faster the object should move when the controller is moving towards it. This is useful to bring objects that are far away from the user to them faster. Set this to 0 to avoid any speed increase. (Default: 5)
          */
@@ -83,18 +85,23 @@ module BABYLON {
                         this._virtualOriginMesh.removeChild(this._virtualDragMesh);
                         this._virtualDragMesh.position.copyFrom(pickedMesh.absolutePosition);
                         if(!pickedMesh.rotationQuaternion){
-                            pickedMesh.rotationQuaternion = new Quaternion();
+                            pickedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(pickedMesh.rotation.y,pickedMesh.rotation.x,pickedMesh.rotation.z);
                         }
+                        var oldParent=pickedMesh.parent;
+                        pickedMesh.setParent(null);
                         this._virtualDragMesh.rotationQuaternion!.copyFrom(pickedMesh.rotationQuaternion);
+                        pickedMesh.setParent(oldParent);
                         this._virtualOriginMesh.addChild(this._virtualDragMesh);
-
+                        
                         // Update state
+                        this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
                         this.dragging = true;
                         this.currentDraggingPointerID = (<PointerEvent>pointerInfo.event).pointerId;
                     }
                 }else if(pointerInfo.type == BABYLON.PointerEventTypes.POINTERUP){
                     if(this.currentDraggingPointerID == (<PointerEvent>pointerInfo.event).pointerId){
                         this.dragging = false;
+                        this._moving = false;
                         this.currentDraggingPointerID = -1;
                         pickedMesh = null;
                         this._virtualOriginMesh.removeChild(this._virtualDragMesh);
@@ -105,7 +112,7 @@ module BABYLON {
                         var originDragDifference = pointerInfo.pickInfo.ray.origin.subtract(lastSixDofOriginPosition);
                         lastSixDofOriginPosition.copyFrom(pointerInfo.pickInfo.ray.origin);
                         var localOriginDragDifference = Vector3.TransformCoordinates(originDragDifference, Matrix.Invert(this._virtualOriginMesh.getWorldMatrix().getRotationMatrix()));
-                        
+
                         this._virtualOriginMesh.addChild(this._virtualDragMesh);
                         // Determine how much the controller moved to/away towards the dragged object and use this to move the object further when its further away
                         var zDragDistance = Vector3.Dot(localOriginDragDifference, this._virtualOriginMesh.position.normalizeToNew());
@@ -113,26 +120,47 @@ module BABYLON {
                         if(this._virtualDragMesh.position.z < 0){
                             this._virtualDragMesh.position.z = 0;
                         }
-                        
+
                         // Update the controller position
                         this._virtualOriginMesh.position.copyFrom(pointerInfo.pickInfo.ray.origin);
                         this._virtualOriginMesh.lookAt(pointerInfo.pickInfo.ray.origin.subtract(pointerInfo.pickInfo.ray.direction));
                         this._virtualOriginMesh.removeChild(this._virtualDragMesh)
-                    
+                        
                         // Move the virtualObjectsPosition into the picked mesh's space if needed
                         this._targetPosition.copyFrom(this._virtualDragMesh.absolutePosition);
                         if(pickedMesh.parent){
                             Vector3.TransformCoordinatesToRef(this._targetPosition, Matrix.Invert(pickedMesh.parent.getWorldMatrix()), this._targetPosition);
                         }
+
+                        if(!this._moving){
+                            this._startingOrientation.copyFrom(this._virtualDragMesh.rotationQuaternion!)
+                        }
+                        this._moving = true;
                     }
                 }
             });
 
+            var tmpQuaternion = new Quaternion();
             // On every frame move towards target scaling to avoid jitter caused by vr controllers
             this._sceneRenderObserver = ownerNode.getScene().onBeforeRenderObservable.add(()=>{
-                if(this.dragging && pickedMesh){
+                if(this.dragging && this._moving && pickedMesh){
                     // Slowly move mesh to avoid jitter
                     pickedMesh.position.addInPlace(this._targetPosition.subtract(pickedMesh.position).scale(this.dragDeltaRatio));
+                    
+                    // Get change in rotation
+                    tmpQuaternion.copyFrom(this._startingOrientation);
+                    tmpQuaternion.x = -tmpQuaternion.x;
+                    tmpQuaternion.y = -tmpQuaternion.y;
+                    tmpQuaternion.z = -tmpQuaternion.z;
+                    this._virtualDragMesh.rotationQuaternion!.multiplyToRef(tmpQuaternion, tmpQuaternion);
+                    // Convert change in rotation to only y axis rotation
+                    Quaternion.RotationYawPitchRollToRef(tmpQuaternion.toEulerAngles("xyz").y,0,0, tmpQuaternion);
+                    tmpQuaternion.multiplyToRef(this._startingOrientation, tmpQuaternion);
+                    // Slowly move mesh to avoid jitter
+                    var oldParent=pickedMesh.parent;
+                    pickedMesh.setParent(null);
+                    Quaternion.SlerpToRef(pickedMesh.rotationQuaternion!, tmpQuaternion, this.dragDeltaRatio, pickedMesh.rotationQuaternion!);
+                    pickedMesh.setParent(oldParent);
                 }
             });
         }

+ 28 - 9
src/Gizmos/babylon.boundingBoxGizmo.ts

@@ -11,6 +11,8 @@ module BABYLON {
         private _pointerObserver:Nullable<Observer<PointerInfo>> = null;
         private _scaleDragSpeed = 0.2;
 
+        private _tmpQuaternion = new Quaternion();
+        private _tmpVector = new Vector3(0,0,0);
         /**
          * Creates an BoundingBoxGizmo
          * @param gizmoLayer The utility layer the gizmo will be added to
@@ -65,25 +67,41 @@ module BABYLON {
                 // Drag behavior
                 var _dragBehavior = new PointerDragBehavior({});
                 _dragBehavior.moveAttached = false;
+                _dragBehavior.updateDragPlane = false;
                 sphere.addBehavior(_dragBehavior);
+                let startingTurnDirection = new Vector3(1,0,0);
+                let totalTurnAmountOfDrag = 0;
+                _dragBehavior.onDragStartObservable.add((event)=>{
+                    startingTurnDirection.copyFrom(sphere.forward);
+                    totalTurnAmountOfDrag = 0;
+                })
                 _dragBehavior.onDragObservable.add((event)=>{
                     if(this.attachedMesh){
-                        var worldDragDirection = sphere.forward;
+                        var worldDragDirection = startingTurnDirection;
 
                         // Project the world right on to the drag plane
                         var toSub = event.dragPlaneNormal.scale(Vector3.Dot(event.dragPlaneNormal, worldDragDirection));
                         var dragAxis = worldDragDirection.subtract(toSub).normalizeToNew();
 
                         // project drag delta on to the resulting drag axis and rotate based on that
-                        var projectDist = Vector3.Dot(dragAxis, event.delta);
+                        var projectDist = -Vector3.Dot(dragAxis, event.delta);
 
                         // Rotate based on axis
-                        if(i>=8){
-                            this.attachedMesh.rotation.z -= projectDist;
-                        }else if(i>=4){
-                            this.attachedMesh.rotation.y -= projectDist;
-                        }else{
-                            this.attachedMesh.rotation.x -= projectDist;
+                        if(!this.attachedMesh.rotationQuaternion){
+                            this.attachedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.attachedMesh.rotation.y,this.attachedMesh.rotation.x,this.attachedMesh.rotation.z);
+                        }
+                       
+                        // Do not allow the object to turn more than a full circle
+                        totalTurnAmountOfDrag+=projectDist;
+                        if(Math.abs(totalTurnAmountOfDrag)<=2*Math.PI){
+                            if(i>=8){
+                                Quaternion.RotationYawPitchRollToRef(0,0,projectDist, this._tmpQuaternion);
+                            }else if(i>=4){
+                                Quaternion.RotationYawPitchRollToRef(projectDist,0,0, this._tmpQuaternion);
+                            }else{
+                                Quaternion.RotationYawPitchRollToRef(0,projectDist,0, this._tmpQuaternion);
+                            }
+                            this.attachedMesh.rotationQuaternion!.multiplyInPlace(this._tmpQuaternion);
                         }
                     }
                 });
@@ -129,7 +147,8 @@ module BABYLON {
                                 
                                 // Update scale and position
                                 this.attachedMesh.scaling.addInPlace(deltaScale);
-                                this.attachedMesh.position.addInPlace(worldMoveDirection);
+                                this.attachedMesh.getAbsolutePosition().addToRef(worldMoveDirection, this._tmpVector)
+                                this.attachedMesh.setAbsolutePosition(this._tmpVector);
                             }
                         })
 

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

@@ -869,9 +869,9 @@
          */
         constructor(
             /** defines the first coordinate */
-            public x: number, 
+            public x: number = 0, 
             /** defines the second coordinate */
-            public y: number) {
+            public y: number = 0) {
         }
 
         /**
@@ -1496,15 +1496,15 @@
             /**
              * Defines the first coordinates (on X axis)
              */
-            public x: number,
+            public x: number = 0,
             /**
              * Defines the second coordinates (on Y axis)
              */
-            public y: number,
+            public y: number = 0,
             /**
              * Defines the third coordinates (on Z axis)
              */
-            public z: number
+            public z: number = 0
         ) {
         }
 

+ 3 - 1
src/Mesh/babylon.transformNode.ts

@@ -536,7 +536,9 @@ module BABYLON {
          * Returns the TransformNode.
          */
         public setParent(node: Nullable<Node>): TransformNode {
-
+            if (node === null && this.parent === null) {
+                return this;
+            }
             if (node === null) {
                 var rotation = Tmp.Quaternion[0];
                 var position = Tmp.Vector3[0];

+ 1 - 1
src/babylon.node.ts

@@ -67,7 +67,7 @@
         /** @hidden */
         public _cache: any;
 
-        private _parentNode: Nullable<Node>;
+        private _parentNode: Nullable<Node> = null;
         private _children: Node[];
 
         /**