浏览代码

Merge pull request #5350 from barroij/MatrixCleanUp

Use the Matrix isIdentity cached value to speed up thing when possible
David Catuhe 6 年之前
父节点
当前提交
019cee5f73

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

@@ -35,6 +35,7 @@
   - Added an option `useClonedMeshhMap` in the `Scene` constructor options. When set to true, each `Mesh` will have and will keep up-to-date a map of cloned meshes. This is to avoid browsing all the meshes of the scene to retrieve the ones that have the current mesh as source mesh. Disabled by default
   - Added `blockfreeActiveMeshesAndRenderingGroups` property in the `Scene`, following the same model as `blockMaterialDirtyMechanism`. This is to avoid calling `Scene.freeActiveMeshes` and `Scene.freeRenderingGroups` for each disposed mesh when we dispose several meshes in a row. One have to set `blockfreeActiveMeshesAndRenderingGroups` to `true` just before disposing the meshes, and set it back to `false` just after
   - Prevented code from doing useless and possible time consuming computation when disposing the `ShaderMaterial` of a `LinesMesh`
+  - Make a better use of the `isIdentity` cached value wihtin a `Matrix`
 - Align `BoundingBox` and `BoundingSphere` API and behavior for clarity and simplicity. As a consequence, the `BoundingBox`'s method `setWorldMatrix` has been removed and the underlying world matrix cannot be modified but by calling `reConstruct` or `update`. ([barroij](https://github.com/barroij))
 
 ### glTF Loader
@@ -67,3 +68,4 @@
 - `Database.openAsync` was renamed by `Database.open` ([Deltakosh](https://github.com/deltakosh))
 - `scene.database` was renamed to `scene.offlineProvider` ([Deltakosh](https://github.com/deltakosh))
 - `BoundingBox.setWorldMatrix` was removed. `BoundingBox.getWorldMatrix` now returns a `Readonly<Matrix>` ([barroij](https://github.com/barroij))
+- `Matrix`'s accessor `m` and method `toArray` and `asArray` now returns a `Readonly<Float32Array>` as the matrix underlying array is not supposed to be modified manually from the outside of the class ([barroij](https://github.com/barroij))

+ 6 - 4
postProcessLibrary/src/digitalRain/babylon.digitalRainPostProcess.ts

@@ -247,10 +247,12 @@ module BABYLON {
 
             var alpha = 0.0;
             var cosTimeZeroOne = 0.0;
-            var matrix = new Matrix();
-            for (let i = 0; i < 16; i++) {
-                matrix.m[i] = Math.random();
-            }
+            var matrix = Matrix.FromValues(
+                Math.random(), Math.random(), Math.random(), Math.random(),
+                Math.random(), Math.random(), Math.random(), Math.random(),
+                Math.random(), Math.random(), Math.random(), Math.random(),
+                Math.random(), Math.random(), Math.random(), Math.random()
+            );
 
             this.onApply = (effect: Effect) => {
                 effect.setTexture("digitalRainFont", this._digitalRainFontTexture);

+ 21 - 37
src/Bones/babylon.bone.ts

@@ -413,9 +413,9 @@ module BABYLON {
             var lm = this.getLocalMatrix();
 
             if (space == Space.LOCAL) {
-                lm.m[12] += vec.x;
-                lm.m[13] += vec.y;
-                lm.m[14] += vec.z;
+                lm.addAtIndex(12, vec.x);
+                lm.addAtIndex(13, vec.y);
+                lm.addAtIndex(14, vec.z);
             } else {
                 var wm: Nullable<Matrix> = null;
 
@@ -437,17 +437,13 @@ module BABYLON {
                     }
                 }
 
-                tmat.m[12] = 0;
-                tmat.m[13] = 0;
-                tmat.m[14] = 0;
-
+                tmat.setTranslationFromFloats(0, 0, 0);
                 tmat.invert();
                 Vector3.TransformCoordinatesToRef(vec, tmat, tvec);
 
-                lm.m[12] += tvec.x;
-                lm.m[13] += tvec.y;
-                lm.m[14] += tvec.z;
-
+                lm.addAtIndex(12, tvec.x);
+                lm.addAtIndex(13, tvec.y);
+                lm.addAtIndex(14, tvec.z);
             }
 
             this._markAsDirtyAndDecompose();
@@ -463,9 +459,7 @@ module BABYLON {
             var lm = this.getLocalMatrix();
 
             if (space == Space.LOCAL) {
-                lm.m[12] = position.x;
-                lm.m[13] = position.y;
-                lm.m[14] = position.z;
+                lm.setTranslationFromFloats(position.x, position.y, position.z);
             } else {
                 var wm: Nullable<Matrix> = null;
 
@@ -490,11 +484,7 @@ module BABYLON {
 
                 tmat.invert();
                 Vector3.TransformCoordinatesToRef(position, tmat, vec);
-
-                lm.m[12] = vec.x;
-                lm.m[13] = vec.y;
-                lm.m[14] = vec.z;
-
+                lm.setTranslationFromFloats(vec.x, vec.y, vec.z);
             }
 
             this._markAsDirtyAndDecompose();
@@ -530,9 +520,9 @@ module BABYLON {
             for (var child of this.children) {
                 var cm = child.getLocalMatrix();
                 cm.multiplyToRef(scaleMat, cm);
-                cm.m[12] *= x;
-                cm.m[13] *= y;
-                cm.m[14] *= z;
+                cm.multiplyAtIndex(12, x);
+                cm.multiplyAtIndex(13, y);
+                cm.multiplyAtIndex(14, z);
 
                 child._markAsDirtyAndDecompose();
             }
@@ -612,12 +602,8 @@ module BABYLON {
          */
         public rotate(axis: Vector3, amount: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
             var rmat = Bone._tmpMats[0];
-            rmat.m[12] = 0;
-            rmat.m[13] = 0;
-            rmat.m[14] = 0;
-
+            rmat.setTranslationFromFloats(0, 0, 0);
             Matrix.RotationAxisToRef(axis, amount, rmat);
-
             this._rotateWithMatrix(rmat, space, mesh);
         }
 
@@ -751,9 +737,7 @@ module BABYLON {
                 }
             }
 
-            lmat.m[12] = lx;
-            lmat.m[13] = ly;
-            lmat.m[14] = lz;
+            lmat.setTranslationFromFloats(lx, ly, lz);
 
             this.computeAbsoluteTransforms();
             this._markAsDirtyAndDecompose();
@@ -775,7 +759,7 @@ module BABYLON {
                 return false;
             }
 
-            scaleMatrix.m[0] *= this._scalingDeterminant;
+            scaleMatrix.multiplyAtIndex(0, this._scalingDeterminant);
             rotMatInv.multiplyToRef(scaleMatrix, rotMatInv);
 
             return true;
@@ -986,9 +970,9 @@ module BABYLON {
                     mat.copyFrom(amat);
                 }
 
-                mat.m[0] *= this._scalingDeterminant;
-                mat.m[1] *= this._scalingDeterminant;
-                mat.m[2] *= this._scalingDeterminant;
+                mat.multiplyAtIndex(0, this._scalingDeterminant);
+                mat.multiplyAtIndex(1, this._scalingDeterminant);
+                mat.multiplyAtIndex(2, this._scalingDeterminant);
 
                 mat.decompose(undefined, result, undefined);
             }
@@ -1028,9 +1012,9 @@ module BABYLON {
                     mat.copyFrom(amat);
                 }
 
-                mat.m[0] *= this._scalingDeterminant;
-                mat.m[1] *= this._scalingDeterminant;
-                mat.m[2] *= this._scalingDeterminant;
+                mat.multiplyAtIndex(0, this._scalingDeterminant);
+                mat.multiplyAtIndex(1, this._scalingDeterminant);
+                mat.multiplyAtIndex(2, this._scalingDeterminant);
 
                 mat.getRotationMatrixToRef(result);
             }

+ 9 - 15
src/Cameras/VR/babylon.webVRCamera.ts

@@ -256,7 +256,7 @@ module BABYLON {
             });
 
             if (typeof (VRFrameData) !== "undefined") {
-                this._frameData = new VRFrameData();
+                this._frameData = new VRFrameData();
             }
 
             /**
@@ -320,11 +320,9 @@ module BABYLON {
                     this._standingMatrix = new Matrix();
                     Matrix.FromFloat32ArrayToRefScaled(result.vrDisplay.stageParameters.sittingToStandingTransform, 0, 1, this._standingMatrix);
                     if (!this.getScene().useRightHandedSystem) {
-                        [2, 6, 8, 9, 14].forEach((num) => {
-                            if (this._standingMatrix) {
-                                this._standingMatrix.m[num] *= -1;
-                            }
-                        });
+                        if (this._standingMatrix) {
+                            this._standingMatrix.toggleModelMatrixHandInPlace();
+                        }
                     }
                     callback(true);
                 }
@@ -634,9 +632,7 @@ module BABYLON {
             Matrix.FromArrayToRef(viewArray, 0, this._webvrViewMatrix);
 
             if (!this.getScene().useRightHandedSystem) {
-                [2, 6, 8, 9, 14].forEach((num) => {
-                    this._webvrViewMatrix.m[num] *= -1;
-                });
+                this._webvrViewMatrix.toggleModelMatrixHandInPlace();
             }
 
             // update the camera rotation matrix
@@ -651,9 +647,9 @@ module BABYLON {
                 this._webvrViewMatrix.invert();
                 // scale the position, if set
                 if (parentCamera.deviceScaleFactor) {
-                    this._webvrViewMatrix.m[12] *= parentCamera.deviceScaleFactor;
-                    this._webvrViewMatrix.m[13] *= parentCamera.deviceScaleFactor;
-                    this._webvrViewMatrix.m[14] *= parentCamera.deviceScaleFactor;
+                    this._webvrViewMatrix.multiplyAtIndex(12, parentCamera.deviceScaleFactor);
+                    this._webvrViewMatrix.multiplyAtIndex(13, parentCamera.deviceScaleFactor);
+                    this._webvrViewMatrix.multiplyAtIndex(14, parentCamera.deviceScaleFactor);
                 }
 
                 this._webvrViewMatrix.invert();
@@ -686,9 +682,7 @@ module BABYLON {
 
             //babylon compatible matrix
             if (!this.getScene().useRightHandedSystem) {
-                [8, 9, 10, 11].forEach((num) => {
-                    this._projectionMatrix.m[num] *= -1;
-                });
+                this._projectionMatrix.toggleProjectionMatrixHandInPlace();
             }
 
             return this._projectionMatrix;

+ 27 - 27
src/Cameras/babylon.arcRotateCamera.ts

@@ -155,7 +155,7 @@ module BABYLON {
         public get angularSensibilityX(): number {
             var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
             if (pointers) {
-                return pointers.angularSensibilityX;
+                return pointers.angularSensibilityX;
             }
 
             return 0;
@@ -174,7 +174,7 @@ module BABYLON {
         public get angularSensibilityY(): number {
             var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
             if (pointers) {
-                return pointers.angularSensibilityY;
+                return pointers.angularSensibilityY;
             }
 
             return 0;
@@ -193,7 +193,7 @@ module BABYLON {
         public get pinchPrecision(): number {
             var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
             if (pointers) {
-                return pointers.pinchPrecision;
+                return pointers.pinchPrecision;
             }
 
             return 0;
@@ -214,7 +214,7 @@ module BABYLON {
         public get pinchDeltaPercentage(): number {
             var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
             if (pointers) {
-                return pointers.pinchDeltaPercentage;
+                return pointers.pinchDeltaPercentage;
             }
 
             return 0;
@@ -233,7 +233,7 @@ module BABYLON {
         public get panningSensibility(): number {
             var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
             if (pointers) {
-                return pointers.panningSensibility;
+                return pointers.panningSensibility;
             }
 
             return 0;
@@ -252,7 +252,7 @@ module BABYLON {
         public get keysUp(): number[] {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                return keyboard.keysUp;
+                return keyboard.keysUp;
             }
 
             return [];
@@ -261,7 +261,7 @@ module BABYLON {
         public set keysUp(value: number[]) {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                keyboard.keysUp = value;
+                keyboard.keysUp = value;
             }
         }
 
@@ -271,7 +271,7 @@ module BABYLON {
         public get keysDown(): number[] {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                return keyboard.keysDown;
+                return keyboard.keysDown;
             }
 
             return [];
@@ -280,7 +280,7 @@ module BABYLON {
         public set keysDown(value: number[]) {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                keyboard.keysDown = value;
+                keyboard.keysDown = value;
             }
         }
 
@@ -290,7 +290,7 @@ module BABYLON {
         public get keysLeft(): number[] {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                return keyboard.keysLeft;
+                return keyboard.keysLeft;
             }
 
             return [];
@@ -299,7 +299,7 @@ module BABYLON {
         public set keysLeft(value: number[]) {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                keyboard.keysLeft = value;
+                keyboard.keysLeft = value;
             }
         }
 
@@ -309,7 +309,7 @@ module BABYLON {
         public get keysRight(): number[] {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                return keyboard.keysRight;
+                return keyboard.keysRight;
             }
 
             return [];
@@ -318,7 +318,7 @@ module BABYLON {
         public set keysRight(value: number[]) {
             var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
             if (keyboard) {
-                keyboard.keysRight = value;
+                keyboard.keysRight = value;
             }
         }
 
@@ -328,7 +328,7 @@ module BABYLON {
         public get wheelPrecision(): number {
             var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
             if (mousewheel) {
-                return mousewheel.wheelPrecision;
+                return mousewheel.wheelPrecision;
             }
 
             return 0;
@@ -337,7 +337,7 @@ module BABYLON {
         public set wheelPrecision(value: number) {
             var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
             if (mousewheel) {
-                mousewheel.wheelPrecision = value;
+                mousewheel.wheelPrecision = value;
             }
         }
 
@@ -349,7 +349,7 @@ module BABYLON {
         public get wheelDeltaPercentage(): number {
             var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
             if (mousewheel) {
-                return mousewheel.wheelDeltaPercentage;
+                return mousewheel.wheelDeltaPercentage;
             }
 
             return 0;
@@ -358,7 +358,7 @@ module BABYLON {
         public set wheelDeltaPercentage(value: number) {
             var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
             if (mousewheel) {
-                mousewheel.wheelDeltaPercentage = value;
+                mousewheel.wheelDeltaPercentage = value;
             }
         }
 
@@ -660,7 +660,7 @@ module BABYLON {
         /** @hidden */
         public _isSynchronizedViewMatrix(): boolean {
             if (!super._isSynchronizedViewMatrix()) {
-                return false;
+                return false;
             }
 
             return this._cache._target.equals(this._getTargetPosition())
@@ -728,13 +728,13 @@ module BABYLON {
                 this.inertialBetaOffset *= this.inertia;
                 this.inertialRadiusOffset *= this.inertia;
                 if (Math.abs(this.inertialAlphaOffset) < Epsilon) {
-                    this.inertialAlphaOffset = 0;
+                    this.inertialAlphaOffset = 0;
                 }
                 if (Math.abs(this.inertialBetaOffset) < Epsilon) {
-                    this.inertialBetaOffset = 0;
+                    this.inertialBetaOffset = 0;
                 }
                 if (Math.abs(this.inertialRadiusOffset) < this.speed * Epsilon) {
-                    this.inertialRadiusOffset = 0;
+                    this.inertialRadiusOffset = 0;
                 }
             }
 
@@ -771,10 +771,10 @@ module BABYLON {
                 this.inertialPanningY *= this.panningInertia;
 
                 if (Math.abs(this.inertialPanningX) < this.speed * Epsilon) {
-                    this.inertialPanningX = 0;
+                    this.inertialPanningX = 0;
                 }
                 if (Math.abs(this.inertialPanningY) < this.speed * Epsilon) {
-                    this.inertialPanningY = 0;
+                    this.inertialPanningY = 0;
                 }
             }
 
@@ -925,8 +925,8 @@ module BABYLON {
 
                 this._computeViewMatrix(this.position, target, up);
 
-                this._viewMatrix.m[12] += this.targetScreenOffset.x;
-                this._viewMatrix.m[13] += this.targetScreenOffset.y;
+                this._viewMatrix.addAtIndex(12, this.targetScreenOffset.x);
+                this._viewMatrix.addAtIndex(13, this.targetScreenOffset.y);
             }
             this._currentTarget = target;
             return this._viewMatrix;
@@ -970,8 +970,8 @@ module BABYLON {
             }
 
             this._computeViewMatrix(this.position, target, up);
-            this._viewMatrix.m[12] += this.targetScreenOffset.x;
-            this._viewMatrix.m[13] += this.targetScreenOffset.y;
+            this._viewMatrix.addAtIndex(12, this.targetScreenOffset.x);
+            this._viewMatrix.addAtIndex(13, this.targetScreenOffset.y);
 
             this._collisionTriggered = false;
         }

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

@@ -65,9 +65,7 @@ module BABYLON.Debug {
             if (x !== 0 || y !== 0 || z !== 0) {
                 var tmat2 = Tmp.Matrix[1];
                 BABYLON.Matrix.IdentityToRef(tmat2);
-                tmat2.m[12] = x;
-                tmat2.m[13] = y;
-                tmat2.m[14] = z;
+                tmat2.setTranslationFromFloats(x, y, z);
                 tmat2.multiplyToRef(tmat, tmat);
             }
 

+ 14 - 13
src/Materials/Textures/babylon.texture.ts

@@ -430,10 +430,13 @@ module BABYLON {
             this._t1.subtractInPlace(this._t0);
             this._t2.subtractInPlace(this._t0);
 
-            Matrix.IdentityToRef(this._cachedTextureMatrix);
-            this._cachedTextureMatrix.m[0] = this._t1.x; this._cachedTextureMatrix.m[1] = this._t1.y; this._cachedTextureMatrix.m[2] = this._t1.z;
-            this._cachedTextureMatrix.m[4] = this._t2.x; this._cachedTextureMatrix.m[5] = this._t2.y; this._cachedTextureMatrix.m[6] = this._t2.z;
-            this._cachedTextureMatrix.m[8] = this._t0.x; this._cachedTextureMatrix.m[9] = this._t0.y; this._cachedTextureMatrix.m[10] = this._t0.z;
+            Matrix.FromValuesToRef(
+                this._t1.x, this._t1.y, this._t1.z, 0.0,
+                this._t2.x, this._t2.y, this._t2.z, 0.0,
+                this._t0.x, this._t0.y, this._t0.z, 0.0,
+                       0.0,        0.0,        0.0, 1.0,
+                this._cachedTextureMatrix
+            );
 
             let scene = this.getScene();
 
@@ -497,15 +500,13 @@ module BABYLON {
                     (<any>this._cachedTextureMatrix)[13] = this.vOffset;
                     break;
                 case Texture.PROJECTION_MODE:
-                    Matrix.IdentityToRef(this._projectionModeMatrix);
-
-                    this._projectionModeMatrix.m[0] = 0.5;
-                    this._projectionModeMatrix.m[5] = -0.5;
-                    this._projectionModeMatrix.m[10] = 0.0;
-                    this._projectionModeMatrix.m[12] = 0.5;
-                    this._projectionModeMatrix.m[13] = 0.5;
-                    this._projectionModeMatrix.m[14] = 1.0;
-                    this._projectionModeMatrix.m[15] = 1.0;
+                    Matrix.FromValuesToRef(
+                        0.5,  0.0, 0.0, 0.0,
+                        0.0, -0.5, 0.0, 0.0,
+                        0.0,  0.0, 0.0, 0.0,
+                        0.5,  0.5, 1.0, 1.0,
+                        this._projectionModeMatrix
+                    );
 
                     let projectionMatrix = scene.getProjectionMatrix();
                     this._cachedProjectionMatrixId = projectionMatrix.updateFlag;

文件差异内容过多而无法显示
+ 541 - 598
src/Math/babylon.math.ts