浏览代码

Optimize transform.position

customisations
alemart 10 个月前
父节点
当前提交
921f782cad
共有 1 个文件被更改,包括 24 次插入2 次删除
  1. 24
    2
      src/geometry/transform.ts

+ 24
- 2
src/geometry/transform.ts 查看文件

@@ -55,6 +55,9 @@ export class Transform
55 55
     /** whether or not this transformation has been decomposed */
56 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,6 +76,7 @@ export class Transform
73 76
         this._orientation = Quaternion.Identity();
74 77
         this._scale = new Vector3(1, 1, 1);
75 78
         this._isDecomposed = false;
79
+        this._isPositionComputed = false;
76 80
     }
77 81
 
78 82
     /**
@@ -99,8 +103,8 @@ export class Transform
99 103
      */
100 104
     get position(): Vector3
101 105
     {
102
-        if(!this._isDecomposed)
103
-            this._decompose();
106
+        if(!this._isPositionComputed)
107
+            this._computePosition();
104 108
 
105 109
         return this._position;
106 110
     }
@@ -195,6 +199,7 @@ export class Transform
195 199
             this._scale._set(sx, sy, sz);
196 200
             this._orientation._copyFrom(Quaternion.Identity());
197 201
             this._isDecomposed = true;
202
+            this._isPositionComputed = true;
198 203
             return;
199 204
         }
200 205
 
@@ -225,6 +230,23 @@ export class Transform
225 230
 
226 231
         // done!
227 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
     /**

正在加载...
取消
保存