Browse Source

Optimize transform.position

customisations
alemart 10 months ago
parent
commit
921f782cad
1 changed files with 24 additions and 2 deletions
  1. 24
    2
      src/geometry/transform.ts

+ 24
- 2
src/geometry/transform.ts View File

55
     /** whether or not this transformation has been decomposed */
55
     /** whether or not this transformation has been decomposed */
56
     private _isDecomposed: boolean;
56
     private _isDecomposed: boolean;
57
 
57
 
58
+    /** whether or not we have extracted the position from the matrix */
59
+    private _isPositionComputed: boolean;
60
+
58
 
61
 
59
 
62
 
60
     /**
63
     /**
73
         this._orientation = Quaternion.Identity();
76
         this._orientation = Quaternion.Identity();
74
         this._scale = new Vector3(1, 1, 1);
77
         this._scale = new Vector3(1, 1, 1);
75
         this._isDecomposed = false;
78
         this._isDecomposed = false;
79
+        this._isPositionComputed = false;
76
     }
80
     }
77
 
81
 
78
     /**
82
     /**
99
      */
103
      */
100
     get position(): Vector3
104
     get position(): Vector3
101
     {
105
     {
102
-        if(!this._isDecomposed)
103
-            this._decompose();
106
+        if(!this._isPositionComputed)
107
+            this._computePosition();
104
 
108
 
105
         return this._position;
109
         return this._position;
106
     }
110
     }
195
             this._scale._set(sx, sy, sz);
199
             this._scale._set(sx, sy, sz);
196
             this._orientation._copyFrom(Quaternion.Identity());
200
             this._orientation._copyFrom(Quaternion.Identity());
197
             this._isDecomposed = true;
201
             this._isDecomposed = true;
202
+            this._isPositionComputed = true;
198
             return;
203
             return;
199
         }
204
         }
200
 
205
 
225
 
230
 
226
         // done!
231
         // done!
227
         this._isDecomposed = true;
232
         this._isDecomposed = true;
233
+        this._isPositionComputed = true;
234
+    }
235
+
236
+    /**
237
+     * A simpler decomposition routine.
238
+     * Sometimes we just need the position.
239
+     */
240
+    private _computePosition(): void
241
+    {
242
+        const m = this._matrix.read();
243
+        const h = Math.abs(m[15]) < EPSILON ? Number.NaN : 1 / m[15]; // usually h = 1
244
+
245
+        // find t
246
+        this._position._set(m[12] * h, m[13] * h, m[14] * h);
247
+
248
+        // done!
249
+        this._isPositionComputed = true;
228
     }
250
     }
229
 
251
 
230
     /**
252
     /**

Loading…
Cancel
Save