Explorar o código

Make the precision an engine option

Popov72 %!s(int64=5) %!d(string=hai) anos
pai
achega
9799e468b3
Modificáronse 2 ficheiros con 30 adicións e 44 borrados
  1. 7 2
      src/Engines/thinEngine.ts
  2. 23 42
      src/Maths/math.vector.ts

+ 7 - 2
src/Engines/thinEngine.ts

@@ -111,6 +111,11 @@ export interface EngineOptions extends WebGLContextAttributes {
      * Make the canvas XR Compatible for XR sessions
      */
     xrCompatible?: boolean;
+
+    /**
+     * Make the matrix computations to be performed in 64 bits instead of 32 bits. False by default
+     */
+    useHighPrecisionMatrix?: boolean;
 }
 
 /**
@@ -477,8 +482,6 @@ export class ThinEngine {
      */
     constructor(canvasOrContext: Nullable<HTMLCanvasElement | WebGLRenderingContext | WebGL2RenderingContext>, antialias?: boolean, options?: EngineOptions, adaptToDeviceRatio: boolean = false) {
 
-        Matrix._StopTrackingInstances();
-
         let canvas: Nullable<HTMLCanvasElement> = null;
 
         if (!canvasOrContext) {
@@ -487,6 +490,8 @@ export class ThinEngine {
 
         options = options || {};
 
+        Matrix.SetPrecision(!!options.useHighPrecisionMatrix);
+
         if ((canvasOrContext as any).getContext) {
             canvas = <HTMLCanvasElement>canvasOrContext;
             this._renderingCanvas = canvas;

+ 23 - 42
src/Maths/math.vector.ts

@@ -3552,10 +3552,11 @@ export class Quaternion {
  * Class used to store matrix data (4x4)
  */
 export class Matrix {
-    private static _Use64Bits = true;
+    private static _Use64Bits = false;
 
     private static _TrackPrecisionChange = true;
-    private static _TrackedMatrices: Array<Matrix> = [];
+    private static _TrackedMatrices: Array<Matrix> | null = [];
+    private static _CurrentType: any = Float32Array;
 
     /**
      * Gets the precision of matrix computations
@@ -3564,50 +3565,28 @@ export class Matrix {
         return Matrix._Use64Bits;
     }
 
-    /**
-     * Switch the matrix computations to 64 bits mode
-     */
-    public static SwitchTo64Bits() {
-        if (Matrix._Use64Bits) {
-            if (Matrix._TrackPrecisionChange) { // note: will be removed when _Use64Bits is defaulted to false when the PR is accepted
-                Matrix._StopTrackingInstances();
-            }
-            return;
-        }
-
-        Matrix._StopTrackingInstances();
-        Matrix._Use64Bits = true;
+    /** @hidden */
+    public static SetPrecision(use64bits: boolean) {
+        Matrix._TrackPrecisionChange = false;
 
-        for (let m = 0; m < Matrix._TrackedMatrices.length; ++m) {
-            const matrix = Matrix._TrackedMatrices[m];
-            const values = matrix._m;
+        if (use64bits && !Matrix._Use64Bits) {
+            Matrix._Use64Bits = true;
+            if (Matrix._TrackedMatrices) {
+                for (let m = 0; m < Matrix._TrackedMatrices.length; ++m) {
+                    const matrix = Matrix._TrackedMatrices[m];
+                    const values = matrix._m;
 
-            (matrix._m as any) = new Array(16);
+                    (matrix._m as any) = new Array(16);
 
-            for (let i = 0; i < 16; ++i) {
-                matrix._m[i] = values[i];
+                    for (let i = 0; i < 16; ++i) {
+                        matrix._m[i] = values[i];
+                    }
+                }
             }
         }
 
-        (Matrix._TrackedMatrices as any) = null; // reclaim some memory, as we don't need this anymore
-    }
-
-    /** @hidden */
-    public static _StopTrackingInstances() {
-        Matrix._TrackPrecisionChange = false;
-
-        // replace the constructor with the right one depending on the precision, making sure we don't loose performance
-        if (Matrix._Use64Bits) {
-            Matrix.prototype.constructor = function() {
-                (this._m as any) = new Array(16);
-                this._updateIdentityStatus(false);
-            };
-        } else {
-            Matrix.prototype.constructor = function() {
-                (this._m as any) = new Float32Array(16);
-                this._updateIdentityStatus(false);
-            };
-        }
+        Matrix._CurrentType = Matrix._Use64Bits ? Array : Float32Array;
+        Matrix._TrackedMatrices = null; // reclaim some memory, as we don't need _TrackedMatrices anymore
     }
 
     private static _updateFlagSeed = 0;
@@ -3653,9 +3632,11 @@ export class Matrix {
      * Creates an empty matrix (filled with zeros)
      */
     public constructor() {
-        Matrix._TrackedMatrices.push(this);
+        if (Matrix._TrackPrecisionChange) {
+            Matrix._TrackedMatrices!.push(this);
+        }
 
-        this._m = Matrix._Use64Bits ? new Array(16) : new Float32Array(16);
+        this._m = new Matrix._CurrentType(16);
         this._updateIdentityStatus(false);
     }