瀏覽代碼

Add Transform.right, Transform.up and Transform.forward

customisations
alemart 10 月之前
父節點
當前提交
9494621fb2
共有 2 個檔案被更改,包括 89 行新增0 行删除
  1. 24
    0
      docs/api/transform.md
  2. 65
    0
      src/geometry/transform.ts

+ 24
- 0
docs/api/transform.md 查看文件

@@ -39,3 +39,27 @@ A unit [quaternion](quaternion.md) describing the rotational component of the tr
39 39
 The scale encoded by the transform.
40 40
 
41 41
 *Since:* 0.4.0
42
+
43
+## right
44
+
45
+`transform.right: Vector3, read-only`
46
+
47
+The unit right vector of the local space.
48
+
49
+*Since:* 0.4.0
50
+
51
+## up
52
+
53
+`transform.up: Vector3, read-only`
54
+
55
+The unit up vector of the local space.
56
+
57
+*Since:* 0.4.0
58
+
59
+## forward
60
+
61
+`transform.forward: Vector3, read-only`
62
+
63
+The unit forward vector of the local space.
64
+
65
+*Since:* 0.4.0

+ 65
- 0
src/geometry/transform.ts 查看文件

@@ -58,6 +58,15 @@ export class Transform
58 58
     /** whether or not we have extracted the position from the matrix */
59 59
     private _isPositionComputed: boolean;
60 60
 
61
+    /** unit right vector of the local space, computed lazily */
62
+    private _right: Vector3;
63
+
64
+    /** unit up vector of the local space, computed lazily */
65
+    private _up: Vector3;
66
+
67
+    /** unit forward vector of the local space, computed lazily */
68
+    private _forward: Vector3;
69
+
61 70
 
62 71
 
63 72
     /**
@@ -75,12 +84,19 @@ export class Transform
75 84
         this._position = Vector3.Zero();
76 85
         this._orientation = Quaternion.Identity();
77 86
         this._scale = new Vector3(1, 1, 1);
87
+
78 88
         this._isDecomposed = false;
79 89
         this._isPositionComputed = false;
90
+
91
+        this._right = Vector3.ZERO;
92
+        this._up = Vector3.ZERO;
93
+        this._forward = Vector3.ZERO;
80 94
     }
81 95
 
82 96
     /**
83 97
      * The 4x4 transformation matrix
98
+     * This matrix is not meant to be changed. Changing it will not update the
99
+     * previously computed components of the transform!
84 100
      */
85 101
     get matrix(): SpeedyMatrix
86 102
     {
@@ -132,6 +148,55 @@ export class Transform
132 148
     }
133 149
 
134 150
     /**
151
+     * Unit right vector of the local space
152
+     */
153
+    get right(): Vector3
154
+    {
155
+        if(this._right === Vector3.ZERO) {
156
+            const rotationMatrix = this.orientation._toRotationMatrix(); // R
157
+            const u = rotationMatrix.block(0, 2, 0, 0).read(); // R * [1 0 0]'
158
+            this._right = new Vector3(u[0], u[1], u[2]);
159
+        }
160
+
161
+        return this._right;
162
+    }
163
+
164
+    /**
165
+     * Unit up vector of the local space
166
+     */
167
+    get up(): Vector3
168
+    {
169
+        if(this._up === Vector3.ZERO) {
170
+            const rotationMatrix = this.orientation._toRotationMatrix(); // R
171
+            const v = rotationMatrix.block(0, 2, 1, 1).read(); // R * [0 1 0]'
172
+            this._up = new Vector3(v[0], v[1], v[2]);
173
+        }
174
+
175
+        return this._up;
176
+    }
177
+
178
+    /**
179
+     * Unit forward vector of the local space
180
+     */
181
+    get forward(): Vector3
182
+    {
183
+        if(this._forward === Vector3.ZERO) {
184
+            const rotationMatrix = this.orientation._toRotationMatrix(); // R
185
+            const w = rotationMatrix.block(0, 2, 2, 2).read(); // R * [0 0 1]'
186
+            this._forward = new Vector3(-w[0], -w[1], -w[2]); // (*)
187
+
188
+            /*
189
+
190
+            (*) in a right-handed system, the unit forward vector is (0,0,-1)
191
+                in a left-handed system, it is (0,0,1)
192
+
193
+            */
194
+        }
195
+
196
+        return this._forward;
197
+    }
198
+
199
+    /**
135 200
      * Decompose this transform
136 201
      */
137 202
     private _decompose(): void

Loading…
取消
儲存