Переглянути джерело

Merge pull request #8314 from BabylonJS/accessors

Accessors
David Catuhe 5 роки тому
батько
коміт
dac80e5c9a

+ 1 - 1
src/Culling/ray.ts

@@ -491,7 +491,7 @@ export class Ray {
     * @param world a matrix to transform the ray to. Default is the identity matrix.
     * @returns the new ray
     */
-    public static CreateNewFromTo(origin: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly): Ray {
+    public static CreateNewFromTo(origin: Vector3, end: Vector3, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly): Ray {
         var direction = end.subtract(origin);
         var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
         direction.normalize();

+ 1 - 1
src/Maths/math.plane.ts

@@ -186,7 +186,7 @@ export class Plane {
      * @returns a new Plane the normal vector to this plane at the given origin point.
      * Note : the vector "normal" is updated because normalized.
      */
-    static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): Plane {
+    static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: Vector3): Plane {
         var result = new Plane(0.0, 0.0, 0.0, 0.0);
         normal.normalize();
         result.normal = normal;

Різницю між файлами не показано, бо вона завелика
+ 303 - 214
src/Maths/math.vector.ts


+ 12 - 19
src/Meshes/transformNode.ts

@@ -46,6 +46,8 @@ export class TransformNode extends Node {
     private _up = new Vector3(0, 1, 0);
     private _right = new Vector3(1, 0, 0);
     private _rightInverted = new Vector3(-1, 0, 0);
+    private _tmpRotation = Quaternion.Zero();
+    private _tmpScaling = Vector3.Zero();
 
     // Properties
     @serializeAsVector3("position")
@@ -308,19 +310,15 @@ export class TransformNode extends Node {
             return false;
         }
 
-        if (!cache.position.equals(this._position)) {
+        if (this.position._isDirty) {
             return false;
         }
 
-        if (this._rotationQuaternion) {
-            if (!cache.rotationQuaternion.equals(this._rotationQuaternion)) {
-                return false;
-            }
-        } else if (!cache.rotation.equals(this._rotation)) {
+        if (this.scaling._isDirty) {
             return false;
         }
 
-        if (!cache.scaling.equals(this._scaling)) {
+        if (this._rotationQuaternion && this._rotationQuaternion._isDirty || this.rotation._isDirty) {
             return false;
         }
 
@@ -333,10 +331,6 @@ export class TransformNode extends Node {
 
         let cache = this._cache;
         cache.localMatrixUpdated = false;
-        cache.position = Vector3.Zero();
-        cache.scaling = Vector3.Zero();
-        cache.rotation = Vector3.Zero();
-        cache.rotationQuaternion = new Quaternion(0, 0, 0, 0);
         cache.billboardMode = -1;
         cache.infiniteDistance = false;
     }
@@ -1009,11 +1003,14 @@ export class TransformNode extends Node {
         this._currentRenderId = currentRenderId;
         this._childUpdateId++;
         this._isDirty = false;
+        this._position._isDirty = false;
+        this._rotation._isDirty = false;
+        this._scaling._isDirty = false;
         let parent = this._getEffectiveParent();
 
         // Scaling
-        let scaling: Vector3 = cache.scaling;
-        let translation: Vector3 = cache.position;
+        let scaling: Vector3 = this._tmpScaling;
+        let translation: Vector3 = this._position;
 
         // Translation
         if (this._infiniteDistance) {
@@ -1022,19 +1019,16 @@ export class TransformNode extends Node {
                 var cameraGlobalPosition = new Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
 
                 translation.copyFromFloats(this._position.x + cameraGlobalPosition.x, this._position.y + cameraGlobalPosition.y, this._position.z + cameraGlobalPosition.z);
-            } else {
-                translation.copyFrom(this._position);
             }
-        } else {
-            translation.copyFrom(this._position);
         }
 
         // Scaling
         scaling.copyFromFloats(this._scaling.x * this.scalingDeterminant, this._scaling.y * this.scalingDeterminant, this._scaling.z * this.scalingDeterminant);
 
         // Rotation
-        let rotation: Quaternion = cache.rotationQuaternion;
+        let rotation: Quaternion = this._tmpRotation;
         if (this._rotationQuaternion) {
+            this._rotationQuaternion._isDirty = false;
             if (this.reIntegrateRotationIntoRotationQuaternion) {
                 var len = this.rotation.lengthSquared();
                 if (len) {
@@ -1045,7 +1039,6 @@ export class TransformNode extends Node {
             rotation.copyFrom(this._rotationQuaternion);
         } else {
             Quaternion.RotationYawPitchRollToRef(this._rotation.y, this._rotation.x, this._rotation.z, rotation);
-            cache.rotation.copyFrom(this._rotation);
         }
 
         // Compose

+ 4 - 2
src/XR/features/WebXRAnchorSystem.ts

@@ -152,7 +152,8 @@ export class WebXRAnchorSystem extends WebXRAbstractFeature {
         // convert to XR space (right handed) if needed
         this._populateTmpTransformation(position, rotationQuaternion);
         // the matrix that we'll use
-        const m = new XRRigidTransform({...this._tmpVector}, {...this._tmpQuaternion});
+        const m = new XRRigidTransform({x: this._tmpVector.x, y: this._tmpVector.y, z: this._tmpVector.z},
+                                       {x: this._tmpQuaternion.x, y: this._tmpQuaternion.y, z: this._tmpQuaternion.z, w: this._tmpQuaternion.w});
         if (!hitTestResult.xrHitResult.createAnchor) {
             throw new Error('Anchors not enabled in this browsed. Add "anchors" to optional features');
         } else {
@@ -181,7 +182,8 @@ export class WebXRAnchorSystem extends WebXRAbstractFeature {
         // convert to XR space (right handed) if needed
         this._populateTmpTransformation(position, rotationQuaternion);
         // the matrix that we'll use
-        const xrTransformation = new XRRigidTransform({...this._tmpVector}, {...this._tmpQuaternion});
+        const xrTransformation = new XRRigidTransform({x: this._tmpVector.x, y: this._tmpVector.y, z: this._tmpVector.z},
+                                                      {x: this._tmpQuaternion.x, y: this._tmpQuaternion.y, z: this._tmpQuaternion.z, w: this._tmpQuaternion.w});
         if (forceCreateInCurrentFrame && this.attached && this._xrSessionManager.currentFrame) {
             return this._createAnchorAtTransformation(xrTransformation, this._xrSessionManager.currentFrame);
         } else {

+ 3 - 3
src/XR/webXRCamera.ts

@@ -275,8 +275,8 @@ export class WebXRCamera extends FreeCamera {
             this._xrInvPositionCache.y = 0;
         }
         const transform = new XRRigidTransform(
-            { ...this._xrInvPositionCache },
-            { ...this._xrInvQuaternionCache });
+            { x: this._xrInvPositionCache.x, y: this._xrInvPositionCache.y, z: this._xrInvPositionCache.z },
+            { x: this._xrInvQuaternionCache.x, y: this._xrInvQuaternionCache.y, z: this._xrInvQuaternionCache.z, w: this._xrInvQuaternionCache.w });
         // Update offset reference to use a new originOffset with the teleported
         // player position and orientation.
         // This new offset needs to be applied to the base ref space.
@@ -297,7 +297,7 @@ export class WebXRCamera extends FreeCamera {
             pos.negateInPlace();
 
             const transform2 = new XRRigidTransform(
-                { ...pos });
+                {  x: pos.x, y: pos.y, z: pos.z  });
             // Update offset reference to use a new originOffset with the teleported
             // player position and orientation.
             // This new offset needs to be applied to the base ref space.

+ 23 - 24
tests/unit/babylon/src/Physics/babylon.physicsComponents.tests.ts

@@ -149,7 +149,7 @@ describe('Babylon physicsComponents', () => {
     // now test 'applyImpulse' which is an injected property of AbstractMesh
     describe('applyImpulse should move a mesh', function() {
 
-        it('should move if an impulse is applied', function() {
+        it('if an impulse is applied', function() {
 
             scene.enablePhysics(noGravityVector, physicsPlugin);   // NO gravity
             let getGravity = scene.getPhysicsEngine().gravity;
@@ -158,28 +158,28 @@ describe('Babylon physicsComponents', () => {
             let sphere = BABYLON.MeshBuilder.CreateSphere("mySphere", { diameter: 1 }, scene);  // use MeshBuilder instead of Mesh
             sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1, restitution: 0.9 }, scene);
 
-            let getPosition = sphere.position;
-            expect(getPosition).to.deep.equal(BABYLON.Vector3.Zero());
+            let getPosition = sphere.position.asArray();
+            expect(getPosition).to.deep.equal([0, 0, 0]);
 
-            let linearVelocity = sphere.physicsImpostor.getLinearVelocity();
-            expect(linearVelocity).to.deep.equal(BABYLON.Vector3.Zero());
+            let linearVelocity = sphere.physicsImpostor.getLinearVelocity().asArray();
+            expect(linearVelocity).to.deep.equal([0, 0, 0]);
 
-            let angularVelocity = sphere.physicsImpostor.getAngularVelocity();
-            expect(linearVelocity).to.deep.equal(BABYLON.Vector3.Zero());
+            let angularVelocity = sphere.physicsImpostor.getAngularVelocity().asArray();
+            expect(linearVelocity).to.deep.equal([0, 0, 0]);
 
             //////////////////////////////`/////////////////////
             // so far, so good.  let's run the physics engine.  nothing should change.
 
             scene.animate();
 
-            getPosition = sphere.position;
-            expect(getPosition).to.deep.equal(BABYLON.Vector3.Zero());
+            getPosition = sphere.position.asArray();
+            expect(getPosition).to.deep.equal([0, 0, 0]);
 
-            linearVelocity = sphere.physicsImpostor.getLinearVelocity();
-            expect(linearVelocity).to.deep.equal(BABYLON.Vector3.Zero());
+            linearVelocity = sphere.physicsImpostor.getLinearVelocity().asArray();
+            expect(linearVelocity).to.deep.equal([0, 0, 0]);
 
-            angularVelocity = sphere.physicsImpostor.getAngularVelocity();
-            expect(linearVelocity).to.deep.equal(BABYLON.Vector3.Zero());
+            angularVelocity = sphere.physicsImpostor.getAngularVelocity().asArray();
+            expect(linearVelocity).to.deep.equal([0, 0, 0]);
 
             ///////////////////////////////////////////////////
             // now give an impulse, and run the physics engine again.  the sphere should start moving.
@@ -189,20 +189,19 @@ describe('Babylon physicsComponents', () => {
             sphere.applyImpulse(force, contact);  // give the sphere its kick
             scene.animate();   // and run the physics engine
 
-            getPosition = sphere.position;
-            expect(getPosition.x).to.be.greaterThan(0);   // moved about 0.01, I'm clueless how that was calculated
-            expect(getPosition.y).to.be.equal(0);
-            expect(getPosition.z).to.be.equal(0);
+            var getPosition2 = sphere.position;
+            expect(getPosition2.x).to.be.greaterThan(0);   // moved about 0.01, I'm clueless how that was calculated
+            expect(getPosition2.y).to.be.equal(0);
+            expect(getPosition2.z).to.be.equal(0);
 
-            linearVelocity = sphere.physicsImpostor.getLinearVelocity();
-            expect(linearVelocity.x).to.be.closeTo(10, 0.001);      // mass of 1, whack of 10, sounds right
-            expect(linearVelocity.y).to.be.equal(0);
-            expect(linearVelocity.z).to.be.equal(0);
+            var linearVelocity2 = sphere.physicsImpostor.getLinearVelocity();
+            expect(linearVelocity2.x).to.be.closeTo(10, 0.001);      // mass of 1, whack of 10, sounds right
+            expect(linearVelocity2.y).to.be.equal(0);
+            expect(linearVelocity2.z).to.be.equal(0);
 
             // we whacked it right along the axis, so don't expect any angular velocity
-            angularVelocity = sphere.physicsImpostor.getAngularVelocity()
-            console.log(angularVelocity);
-            expect(angularVelocity).to.deep.equal(BABYLON.Vector3.Zero());
+            var angularVelocity2 = sphere.physicsImpostor.getAngularVelocity()
+            expect(angularVelocity2.asArray()).to.deep.equal([0, 0, 0]);
 
         });
     });