浏览代码

Introduce Transform._scaleAndRotate()

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

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

152
      */
152
      */
153
     get right(): Vector3
153
     get right(): Vector3
154
     {
154
     {
155
-        if(this._right === Vector3.ZERO) {
156
-            this._right = new Vector3(1, 0, 0)
157
-                          ._applyRotationQuaternion(this.orientation);
158
-        }
155
+        if(this._right === Vector3.ZERO)
156
+            this._right = this._scaleAndRotate(new Vector3(1, 0, 0))._normalize();
159
 
157
 
160
         return this._right;
158
         return this._right;
161
     }
159
     }
165
      */
163
      */
166
     get up(): Vector3
164
     get up(): Vector3
167
     {
165
     {
168
-        if(this._up === Vector3.ZERO) {
169
-            this._up = new Vector3(0, 1, 0)
170
-                       ._applyRotationQuaternion(this.orientation);
171
-        }
166
+        if(this._up === Vector3.ZERO)
167
+            this._up = this._scaleAndRotate(new Vector3(0, 1, 0))._normalize();
172
 
168
 
173
         return this._up;
169
         return this._up;
174
     }
170
     }
181
         if(this._forward === Vector3.ZERO) {
177
         if(this._forward === Vector3.ZERO) {
182
             // in a right-handed system, the unit forward vector is (0, 0, -1)
178
             // in a right-handed system, the unit forward vector is (0, 0, -1)
183
             // in a left-handed system, it is (0, 0, 1)
179
             // in a left-handed system, it is (0, 0, 1)
184
-            this._forward = new Vector3(0, 0, -1)
185
-                            ._applyRotationQuaternion(this.orientation);
180
+            this._forward = this._scaleAndRotate(new Vector3(0, 0, -1))._normalize();
186
         }
181
         }
187
 
182
 
188
         return this._forward;
183
         return this._forward;
189
     }
184
     }
190
 
185
 
191
     /**
186
     /**
187
+     * Use this transform to scale and rotate a vector
188
+     * The translation component of the transform is ignored
189
+     * @param v a vector
190
+     * @returns input vector v
191
+     */
192
+    private _scaleAndRotate(v: Vector3): Vector3
193
+    {
194
+        const m = this._matrix.read();
195
+        const h = Math.abs(m[15]) < EPSILON ? Number.NaN : 1 / m[15]; // usually h = 1
196
+        const vx = v.x, vy = v.y, vz = v.z;
197
+
198
+        const x = m[0] * vx + m[4] * vy + m[8] * vz;
199
+        const y = m[1] * vx + m[5] * vy + m[9] * vz;
200
+        const z = m[2] * vx + m[6] * vy + m[10] * vz;
201
+
202
+        return v._set(x * h, y * h, z * h);
203
+    }
204
+
205
+    /**
192
      * Decompose this transform
206
      * Decompose this transform
193
      */
207
      */
194
     private _decompose(): void
208
     private _decompose(): void

正在加载...
取消
保存