Przeglądaj źródła

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

David Catuhe 8 lat temu
rodzic
commit
af59f027f3

+ 132 - 56
src/Bones/babylon.bone.ts

@@ -171,54 +171,87 @@
             return true;
         }
 
-        public translate (vec: Vector3): void {
+        public translate (vec: Vector3, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lm = this.getLocalMatrix();
 
-            lm.m[12] += vec.x;
-            lm.m[13] += vec.y;
-            lm.m[14] += vec.z;
+            if(space == Space.LOCAL){
+
+                lm.m[12] += vec.x;
+                lm.m[13] += vec.y;
+                lm.m[14] += vec.z;
+
+            }else{
+
+                this._skeleton.computeAbsoluteTransforms();
+                var tmat = Tmp.Matrix[0];
+                var tvec = Tmp.Vector3[0];
+
+                if (mesh) {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                }
+
+                tmat.m[12] = 0;
+                tmat.m[13] = 0;
+                tmat.m[14] = 0;
+
+                tmat.invert();
+                Vector3.TransformCoordinatesToRef(vec, tmat, tvec);
+
+                lm.m[12] += tvec.x;
+                lm.m[13] += tvec.y;
+                lm.m[14] += tvec.z;
+
+            }
 
             this.markAsDirty();
 	        
         }
 
-        public setPosition (position: Vector3): void {
+        public setPosition (position: Vector3, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lm = this.getLocalMatrix();
 
-            lm.m[12] = position.x;
-            lm.m[13] = position.y;
-            lm.m[14] = position.z;
+            if(space == Space.LOCAL){
 
-            this.markAsDirty();
-	        
-        }
+                lm.m[12] = position.x;
+                lm.m[13] = position.y;
+                lm.m[14] = position.z;
 
-        public setAbsolutePosition (position: Vector3, mesh: AbstractMesh = null): void {
+            }else{
 
-            this._skeleton.computeAbsoluteTransforms();
+                this._skeleton.computeAbsoluteTransforms();
 
-            var tmat = Tmp.Matrix[0];
-            var vec = Tmp.Vector3[0];
+                var tmat = Tmp.Matrix[0];
+                var vec = Tmp.Vector3[0];
 
-            if (mesh) {
-                tmat.copyFrom(this._parent.getAbsoluteTransform());
-                tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
-            }else {
-                tmat.copyFrom(this._parent.getAbsoluteTransform());
-            }
+                if (mesh) {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                }
 
-            tmat.invert();
-            Vector3.TransformCoordinatesToRef(position, tmat, vec);
+                tmat.invert();
+                Vector3.TransformCoordinatesToRef(position, tmat, vec);
 
-            var lm = this.getLocalMatrix();
-            lm.m[12] = vec.x;
-            lm.m[13] = vec.y;
-            lm.m[14] = vec.z;
+                lm.m[12] = vec.x;
+                lm.m[13] = vec.y;
+                lm.m[14] = vec.z;
+
+            }
 
             this.markAsDirty();
-			
+	        
+        }
+
+        public setAbsolutePosition(position:Vector3, mesh?: AbstractMesh){
+
+            this.setPosition(position, Space.WORLD, mesh);
+
         }
 
         public setScale (x: number, y: number, z: number, scaleChildren = false): void {
@@ -291,7 +324,7 @@
 
         }
 
-        public setYawPitchRoll (yaw: number, pitch: number, roll: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setYawPitchRoll (yaw: number, pitch: number, roll: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
 	
             var rotMat = Tmp.Matrix[0];
             Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, rotMat);
@@ -306,7 +339,7 @@
             
         }
 
-        public rotate (axis: Vector3, amount: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public rotate (axis: Vector3, amount: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
             
             var rmat = Tmp.Matrix[0];
             rmat.m[12] = 0;
@@ -319,7 +352,7 @@
             
         }
 
-        public setAxisAngle (axis: Vector3, angle: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setAxisAngle (axis: Vector3, angle: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var rotMat = Tmp.Matrix[0];
             Matrix.RotationAxisToRef(axis, angle, rotMat);
@@ -332,7 +365,7 @@
 
         }
 
-        public setRotationMatrix (rotMat: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setRotationMatrix (rotMat: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var rotMatInv = Tmp.Matrix[0];
             
@@ -347,7 +380,7 @@
 
         }
 
-        private _rotateWithMatrix (rmat: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        private _rotateWithMatrix (rmat: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lmat = this.getLocalMatrix();
             var lx = lmat.m[12];
@@ -396,7 +429,7 @@
 
         }
 
-        private _getNegativeRotationToRef(rotMatInv: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        private _getNegativeRotationToRef(rotMatInv: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             if (space == Space.WORLD) {
                 var scaleMatrix = Tmp.Matrix[2];
@@ -445,32 +478,60 @@
             
         }
 
-        public getAbsolutePosition (mesh: AbstractMesh = null): Vector3 {
+        public getPosition (space = Space.LOCAL, mesh?: AbstractMesh): Vector3 {
 
             var pos = Vector3.Zero();
 
-            this.getAbsolutePositionToRef(mesh, pos);
+            this.getPositionToRef(space, mesh, pos);
 
             return pos;
 
         }
 
-        public getAbsolutePositionToRef (mesh: AbstractMesh = null, result: Vector3): void {
+        public getPositionToRef (space = Space.LOCAL, mesh: AbstractMesh, result: Vector3): void {
 
-            this._skeleton.computeAbsoluteTransforms();
-            
-            var tmat = Tmp.Matrix[0];
+            if(space == Space.LOCAL){
+
+                var lm = this.getLocalMatrix();
+
+                result.x = lm.m[12];
+                result.y = lm.m[13];
+                result.z = lm.m[14];
 
-            if (mesh) {
-                tmat.copyFrom(this.getAbsoluteTransform());
-                tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
             }else{
-                tmat = this.getAbsoluteTransform();
+                
+                this._skeleton.computeAbsoluteTransforms();
+                
+                var tmat = Tmp.Matrix[0];
+
+                if (mesh) {
+                    tmat.copyFrom(this.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else{
+                    tmat = this.getAbsoluteTransform();
+                }
+
+                result.x = tmat.m[12];
+                result.y = tmat.m[13];
+                result.z = tmat.m[14];
+
             }
 
-            result.x = tmat.m[12];
-            result.y = tmat.m[13];
-            result.z = tmat.m[14];
+        }
+
+        public getAbsolutePosition (mesh?: AbstractMesh): Vector3 {
+
+            var pos = Vector3.Zero();
+
+            this.getPositionToRef(Space.WORLD, mesh, pos);
+
+            return pos;
+
+        }
+
+        public getAbsolutePositionToRef (mesh: AbstractMesh, result: Vector3) {
+
+            this.getPositionToRef(Space.WORLD, mesh, result);
 
         }
 
@@ -527,7 +588,7 @@
 
             var result = Vector3.Zero();
 
-            this.getDirectionToRef(localAxis, result, mesh);
+            this.getDirectionToRef(localAxis, result,  mesh);
             
             return result;
 
@@ -551,26 +612,41 @@
 
         }
 
-        public getRotation(mesh: AbstractMesh): Quaternion {
+        public getRotation(space = Space.LOCAL, mesh?: AbstractMesh): Quaternion {
 
             var result = Quaternion.Identity();
 
-            this.getRotationToRef(mesh, result);
+            this.getRotationToRef(space, mesh, result);
 
             return result;
 
         }
 
-        public getRotationToRef(mesh: AbstractMesh, result: Quaternion): void{
+        public getRotationToRef( space = Space.LOCAL, mesh: AbstractMesh, result: Quaternion): void{
+
+            if(space == Space.LOCAL){
 
-            var mat = Tmp.Matrix[0];
-            var amat = this.getAbsoluteTransform();
-            var wmat = mesh.getWorldMatrix();
+                this.getLocalMatrix().decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+            }else{
 
-            amat.multiplyToRef(wmat, mat);
+                var mat = Tmp.Matrix[0];
+                var amat = this.getAbsoluteTransform();
 
-            mat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+                if(mesh){
 
+                    var wmat = mesh.getWorldMatrix();
+                    amat.multiplyToRef(wmat, mat);
+
+                    mat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+                }else{
+
+                    amat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+                }
+
+            }
         }
 
     }

+ 26 - 1
src/Cameras/VR/babylon.webVRCamera.ts

@@ -23,9 +23,14 @@ module BABYLON {
 
         private _quaternionCache: Quaternion;
 
+        private _positionOffset: Vector3;
+
         constructor(name: string, position: Vector3, scene: Scene, compensateDistortion = false, private webVROptions: WebVROptions = {}) {
             super(name, position, scene);
 
+            //using the position provided as the current position offset
+            this._positionOffset = position;
+
             //enable VR
             this.getEngine().initWebVR();
 
@@ -81,7 +86,10 @@ module BABYLON {
                     this.rotationQuaternion.copyFromFloats(this._cacheState.orientation[0], this._cacheState.orientation[1], -this._cacheState.orientation[2], -this._cacheState.orientation[3]);
                     if (this.webVROptions.trackPosition && this._cacheState.position) {
                         this.position.copyFromFloats(this._cacheState.position[0], this._cacheState.position[1], -this._cacheState.position[2]);
-                        this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale)
+                        //scale the position accordingly
+                        this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale);
+                        //add the position offset
+                        this.position.addInPlace(this._positionOffset);
                     }
                 }
             }
@@ -122,6 +130,23 @@ module BABYLON {
             //pitch and roll won't be affected.
             this._vrDevice.resetPose();
         }
+
+        /**
+         * 
+         * Set the position offset of the VR camera
+         * The offset will be added to the WebVR pose, after scaling it (if set).
+         * 
+         * @param {Vector3} [newPosition] an optional new position. if not provided, the current camera position will be used.
+         * 
+         * @memberOf WebVRFreeCamera
+         */
+        public setPositionOffset(newPosition?: Vector3) {
+            if(newPosition) {
+                this._positionOffset = newPosition;
+            } else {
+                this._positionOffset.copyFrom(this.position);
+            }
+        }
     }
 }
 

+ 1 - 1
src/Debug/babylon.skeletonViewer.ts

@@ -91,7 +91,7 @@
                     this._debugLines[boneNum] = points;
                 }
                 childBone.getAbsolutePositionToRef(this.mesh, points[0]);
-				parentBone.getAbsolutePositionToRef(this.mesh, points[1]);
+                parentBone.getAbsolutePositionToRef(this.mesh, points[1]);
                 boneNum++;
             }
         }