@vyr/engine 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +19 -0
- package/src/ArrayUtils.ts +65 -0
- package/src/AsyncTask.ts +72 -0
- package/src/Category.ts +119 -0
- package/src/Color.ts +111 -0
- package/src/Engine.ts +101 -0
- package/src/Generate.ts +40 -0
- package/src/InputSystem.ts +108 -0
- package/src/Listener.ts +59 -0
- package/src/ObjectPool.ts +84 -0
- package/src/ObjectUtils.ts +49 -0
- package/src/Scriptable.ts +27 -0
- package/src/Serialization.ts +49 -0
- package/src/Traverser.ts +39 -0
- package/src/actor/Actor.ts +28 -0
- package/src/actor/AnimationUnitActor.ts +289 -0
- package/src/actor/DivActor.ts +70 -0
- package/src/actor/FragmentActor.ts +56 -0
- package/src/actor/HTMActor.ts +166 -0
- package/src/actor/HTMServiceActor.ts +57 -0
- package/src/actor/HTMTransformControllerActor.ts +404 -0
- package/src/actor/StyleActor.ts +96 -0
- package/src/actor/index.ts +8 -0
- package/src/asset/Asset.ts +271 -0
- package/src/asset/AssetGraph.ts +246 -0
- package/src/asset/index.ts +2 -0
- package/src/descriptor/AnimationUnitDescriptor.ts +65 -0
- package/src/descriptor/CameraDescriptor.ts +12 -0
- package/src/descriptor/ControllerDescriptor.ts +16 -0
- package/src/descriptor/DatasetDescriptor.ts +92 -0
- package/src/descriptor/Descriptor.ts +415 -0
- package/src/descriptor/DivDescriptor.ts +18 -0
- package/src/descriptor/DynamicDescriptor.ts +27 -0
- package/src/descriptor/HTMLDescriptor.ts +87 -0
- package/src/descriptor/HTMLServiceDescriptor.ts +19 -0
- package/src/descriptor/HTMLTransformControllerDescriptor.ts +34 -0
- package/src/descriptor/NodeDescriptor.ts +32 -0
- package/src/descriptor/PrefabDescriptor.ts +53 -0
- package/src/descriptor/PrefabInstanceDescriptor.ts +32 -0
- package/src/descriptor/RoutineDescriptor.ts +54 -0
- package/src/descriptor/ServiceDescriptor.ts +32 -0
- package/src/descriptor/ServiceSchedulerDescriptor.ts +32 -0
- package/src/descriptor/StyleDescriptor.ts +213 -0
- package/src/descriptor/index.ts +17 -0
- package/src/graphics/Collection.ts +25 -0
- package/src/graphics/Compilation.ts +82 -0
- package/src/graphics/Graphics.ts +475 -0
- package/src/graphics/Observer.ts +36 -0
- package/src/graphics/Unit.ts +83 -0
- package/src/graphics/VariableProxy.ts +92 -0
- package/src/graphics/index.ts +5 -0
- package/src/index.ts +26 -0
- package/src/interpreter/AnimationUnitInterpreter.ts +53 -0
- package/src/interpreter/DatasetInterpreter.ts +11 -0
- package/src/interpreter/DivInterpreter.ts +44 -0
- package/src/interpreter/DynamicInterpreter.ts +207 -0
- package/src/interpreter/FragmentInterpreter.ts +34 -0
- package/src/interpreter/HTMLServiceInterpreter.ts +47 -0
- package/src/interpreter/HTMLTransformControllerInterpreter.ts +40 -0
- package/src/interpreter/Interpreter.ts +69 -0
- package/src/interpreter/PrefaInterpreter.ts +11 -0
- package/src/interpreter/PrefabInstanceInterpreter.ts +12 -0
- package/src/interpreter/RoutineInterpreter.ts +88 -0
- package/src/interpreter/ServiceInterpreter.ts +24 -0
- package/src/interpreter/ServiceSchedulerInterpreter.ts +42 -0
- package/src/interpreter/StyleInterpreter.ts +66 -0
- package/src/interpreter/index.ts +14 -0
- package/src/locale/Language.ts +10 -0
- package/src/locale/LanguageProvider.ts +48 -0
- package/src/locale/index.ts +2 -0
- package/src/math/Euler.ts +303 -0
- package/src/math/Matrix4.ts +1123 -0
- package/src/math/Quaternion.ts +737 -0
- package/src/math/Vector2.ts +680 -0
- package/src/math/Vector3.ts +1062 -0
- package/src/math/index.ts +5 -0
- package/src/math/utils.ts +17 -0
- package/src/preset/execute/dataset/index.ts +1 -0
- package/src/preset/execute/dataset/update.ts +52 -0
- package/src/preset/execute/graphics/index.ts +1 -0
- package/src/preset/execute/graphics/invoke.ts +49 -0
- package/src/preset/execute/index.ts +4 -0
- package/src/preset/execute/net/index.ts +1 -0
- package/src/preset/execute/net/request.ts +103 -0
- package/src/preset/execute/scheduler/index.ts +1 -0
- package/src/preset/execute/scheduler/switch.ts +46 -0
- package/src/preset/index.ts +7 -0
- package/src/preset/routine/graphics/index.ts +1 -0
- package/src/preset/routine/graphics/invoke.ts +27 -0
- package/src/preset/routine/index.ts +2 -0
- package/src/preset/routine/scheduler/index.ts +1 -0
- package/src/preset/routine/scheduler/switch.ts +27 -0
- package/src/setup/index.ts +17 -0
- package/src/utils/AssetProvider.ts +72 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
import { DeserializationObject } from "../Serialization"
|
|
2
|
+
import { clamp } from './utils';
|
|
3
|
+
import { Euler } from './Euler';
|
|
4
|
+
import { Quaternion } from './Quaternion';
|
|
5
|
+
import { Matrix4 } from './Matrix4';
|
|
6
|
+
import { Color } from "../Color";
|
|
7
|
+
|
|
8
|
+
interface Spherical {
|
|
9
|
+
radius: number;
|
|
10
|
+
phi: number;
|
|
11
|
+
theta: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Cylindrical {
|
|
15
|
+
radius: number;
|
|
16
|
+
theta: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Class representing a 3D vector. A 3D vector is an ordered triplet of numbers
|
|
22
|
+
* (labeled x, y and z), which can be used to represent a number of things, such as:
|
|
23
|
+
*
|
|
24
|
+
* - A point in 3D space.
|
|
25
|
+
* - A direction and length in 3D space. In three.js the length will
|
|
26
|
+
* always be the Euclidean distance(straight-line distance) from `(0, 0, 0)` to `(x, y, z)`
|
|
27
|
+
* and the direction is also measured from `(0, 0, 0)` towards `(x, y, z)`.
|
|
28
|
+
* - Any arbitrary ordered triplet of numbers.
|
|
29
|
+
*
|
|
30
|
+
* There are other things a 3D vector can be used to represent, such as
|
|
31
|
+
* momentum vectors and so on, however these are the most
|
|
32
|
+
* common uses in three.js.
|
|
33
|
+
*
|
|
34
|
+
* Iterating through a vector instance will yield its components `(x, y, z)` in
|
|
35
|
+
* the corresponding order.
|
|
36
|
+
* ```js
|
|
37
|
+
* const a = new Vector3( 0, 1, 0 );
|
|
38
|
+
*
|
|
39
|
+
* //no arguments; will be initialised to (0, 0, 0)
|
|
40
|
+
* const b = new Vector3( );
|
|
41
|
+
*
|
|
42
|
+
* const d = a.distanceTo( b );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
class Vector3 {
|
|
46
|
+
static create(v3?: number | DeserializationObject<Vector3>, y?: number, z?: number) {
|
|
47
|
+
if (v3 === undefined) return new Vector3()
|
|
48
|
+
|
|
49
|
+
if (typeof v3 === 'number') {
|
|
50
|
+
return new Vector3(v3 as number, y, z)
|
|
51
|
+
} else {
|
|
52
|
+
return new Vector3(v3.x, v3.y, v3.z)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The x value of this vector.
|
|
58
|
+
*/
|
|
59
|
+
x: number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The y value of this vector.
|
|
63
|
+
*/
|
|
64
|
+
y: number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The z value of this vector.
|
|
68
|
+
*/
|
|
69
|
+
z: number;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Constructs a new 3D vector.
|
|
73
|
+
*
|
|
74
|
+
* @param x - The x value of this vector.
|
|
75
|
+
* @param y - The y value of this vector.
|
|
76
|
+
* @param z - The z value of this vector.
|
|
77
|
+
*/
|
|
78
|
+
constructor(x: number = 0, y: number = 0, z: number = 0) {
|
|
79
|
+
this.x = x;
|
|
80
|
+
this.y = y;
|
|
81
|
+
this.z = z;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets the vector components.
|
|
86
|
+
*
|
|
87
|
+
* @param x - The value of the x component.
|
|
88
|
+
* @param y - The value of the y component.
|
|
89
|
+
* @param z - The value of the z component.
|
|
90
|
+
* @return A reference to this vector.
|
|
91
|
+
*/
|
|
92
|
+
set(x: number, y: number, z?: number): Vector3 {
|
|
93
|
+
if (z === undefined) z = this.z; // sprite.scale.set(x,y)
|
|
94
|
+
|
|
95
|
+
this.x = x;
|
|
96
|
+
this.y = y;
|
|
97
|
+
this.z = z!;
|
|
98
|
+
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Sets the vector components to the same value.
|
|
104
|
+
*
|
|
105
|
+
* @param scalar - The value to set for all vector components.
|
|
106
|
+
* @return A reference to this vector.
|
|
107
|
+
*/
|
|
108
|
+
setScalar(scalar: number): Vector3 {
|
|
109
|
+
this.x = scalar;
|
|
110
|
+
this.y = scalar;
|
|
111
|
+
this.z = scalar;
|
|
112
|
+
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Sets the vector's x component to the given value
|
|
118
|
+
*
|
|
119
|
+
* @param x - The value to set.
|
|
120
|
+
* @return A reference to this vector.
|
|
121
|
+
*/
|
|
122
|
+
setX(x: number): Vector3 {
|
|
123
|
+
this.x = x;
|
|
124
|
+
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Sets the vector's y component to the given value
|
|
130
|
+
*
|
|
131
|
+
* @param y - The value to set.
|
|
132
|
+
* @return A reference to this vector.
|
|
133
|
+
*/
|
|
134
|
+
setY(y: number): Vector3 {
|
|
135
|
+
this.y = y;
|
|
136
|
+
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Sets the vector's z component to the given value
|
|
142
|
+
*
|
|
143
|
+
* @param z - The value to set.
|
|
144
|
+
* @return A reference to this vector.
|
|
145
|
+
*/
|
|
146
|
+
setZ(z: number): Vector3 {
|
|
147
|
+
this.z = z;
|
|
148
|
+
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Allows to set a vector component with an index.
|
|
154
|
+
*
|
|
155
|
+
* @param index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
|
|
156
|
+
* @param value - The value to set.
|
|
157
|
+
* @return A reference to this vector.
|
|
158
|
+
*/
|
|
159
|
+
setComponent(index: number, value: number): Vector3 {
|
|
160
|
+
switch (index) {
|
|
161
|
+
case 0: this.x = value; break;
|
|
162
|
+
case 1: this.y = value; break;
|
|
163
|
+
case 2: this.z = value; break;
|
|
164
|
+
default: throw new Error('index is out of range: ' + index);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns the value of the vector component which matches the given index.
|
|
172
|
+
*
|
|
173
|
+
* @param index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
|
|
174
|
+
* @return A vector component value.
|
|
175
|
+
*/
|
|
176
|
+
getComponent(index: number): number {
|
|
177
|
+
switch (index) {
|
|
178
|
+
case 0: return this.x;
|
|
179
|
+
case 1: return this.y;
|
|
180
|
+
case 2: return this.z;
|
|
181
|
+
default: throw new Error('index is out of range: ' + index);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns a new vector with copied values from this instance.
|
|
187
|
+
*
|
|
188
|
+
* @return A clone of this instance.
|
|
189
|
+
*/
|
|
190
|
+
clone(): Vector3 {
|
|
191
|
+
return new (this.constructor as typeof Vector3)(this.x, this.y, this.z);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Copies the values of the given vector to this instance.
|
|
196
|
+
*
|
|
197
|
+
* @param v - The vector to copy.
|
|
198
|
+
* @return A reference to this vector.
|
|
199
|
+
*/
|
|
200
|
+
copy(v: Vector3): Vector3 {
|
|
201
|
+
this.x = v.x;
|
|
202
|
+
this.y = v.y;
|
|
203
|
+
this.z = v.z;
|
|
204
|
+
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Adds the given vector to this instance.
|
|
210
|
+
*
|
|
211
|
+
* @param v - The vector to add.
|
|
212
|
+
* @return A reference to this vector.
|
|
213
|
+
*/
|
|
214
|
+
add(v: Vector3): Vector3 {
|
|
215
|
+
this.x += v.x;
|
|
216
|
+
this.y += v.y;
|
|
217
|
+
this.z += v.z;
|
|
218
|
+
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Adds the given scalar value to all components of this instance.
|
|
224
|
+
*
|
|
225
|
+
* @param s - The scalar to add.
|
|
226
|
+
* @return A reference to this vector.
|
|
227
|
+
*/
|
|
228
|
+
addScalar(s: number): Vector3 {
|
|
229
|
+
this.x += s;
|
|
230
|
+
this.y += s;
|
|
231
|
+
this.z += s;
|
|
232
|
+
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Adds the given vectors and stores the result in this instance.
|
|
238
|
+
*
|
|
239
|
+
* @param a - The first vector.
|
|
240
|
+
* @param b - The second vector.
|
|
241
|
+
* @return A reference to this vector.
|
|
242
|
+
*/
|
|
243
|
+
addVectors(a: Vector3, b: Vector3): Vector3 {
|
|
244
|
+
this.x = a.x + b.x;
|
|
245
|
+
this.y = a.y + b.y;
|
|
246
|
+
this.z = a.z + b.z;
|
|
247
|
+
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Adds the given vector scaled by the given factor to this instance.
|
|
253
|
+
*
|
|
254
|
+
* @param v - The vector.
|
|
255
|
+
* @param s - The factor that scales `v`.
|
|
256
|
+
* @return A reference to this vector.
|
|
257
|
+
*/
|
|
258
|
+
addScaledVector(v: Vector3, s: number): Vector3 {
|
|
259
|
+
this.x += v.x * s;
|
|
260
|
+
this.y += v.y * s;
|
|
261
|
+
this.z += v.z * s;
|
|
262
|
+
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Subtracts the given vector from this instance.
|
|
268
|
+
*
|
|
269
|
+
* @param v - The vector to subtract.
|
|
270
|
+
* @return A reference to this vector.
|
|
271
|
+
*/
|
|
272
|
+
sub(v: Vector3): Vector3 {
|
|
273
|
+
this.x -= v.x;
|
|
274
|
+
this.y -= v.y;
|
|
275
|
+
this.z -= v.z;
|
|
276
|
+
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Subtracts the given scalar value from all components of this instance.
|
|
282
|
+
*
|
|
283
|
+
* @param s - The scalar to subtract.
|
|
284
|
+
* @return A reference to this vector.
|
|
285
|
+
*/
|
|
286
|
+
subScalar(s: number): Vector3 {
|
|
287
|
+
this.x -= s;
|
|
288
|
+
this.y -= s;
|
|
289
|
+
this.z -= s;
|
|
290
|
+
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Subtracts the given vectors and stores the result in this instance.
|
|
296
|
+
*
|
|
297
|
+
* @param a - The first vector.
|
|
298
|
+
* @param b - The second vector.
|
|
299
|
+
* @return A reference to this vector.
|
|
300
|
+
*/
|
|
301
|
+
subVectors(a: Vector3, b: Vector3): Vector3 {
|
|
302
|
+
this.x = a.x - b.x;
|
|
303
|
+
this.y = a.y - b.y;
|
|
304
|
+
this.z = a.z - b.z;
|
|
305
|
+
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Multiplies the given vector with this instance.
|
|
311
|
+
*
|
|
312
|
+
* @param v - The vector to multiply.
|
|
313
|
+
* @return A reference to this vector.
|
|
314
|
+
*/
|
|
315
|
+
multiply(v: Vector3): Vector3 {
|
|
316
|
+
this.x *= v.x;
|
|
317
|
+
this.y *= v.y;
|
|
318
|
+
this.z *= v.z;
|
|
319
|
+
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Multiplies the given scalar value with all components of this instance.
|
|
325
|
+
*
|
|
326
|
+
* @param scalar - The scalar to multiply.
|
|
327
|
+
* @return A reference to this vector.
|
|
328
|
+
*/
|
|
329
|
+
multiplyScalar(scalar: number): Vector3 {
|
|
330
|
+
this.x *= scalar;
|
|
331
|
+
this.y *= scalar;
|
|
332
|
+
this.z *= scalar;
|
|
333
|
+
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Multiplies the given vectors and stores the result in this instance.
|
|
339
|
+
*
|
|
340
|
+
* @param a - The first vector.
|
|
341
|
+
* @param b - The second vector.
|
|
342
|
+
* @return A reference to this vector.
|
|
343
|
+
*/
|
|
344
|
+
multiplyVectors(a: Vector3, b: Vector3): Vector3 {
|
|
345
|
+
this.x = a.x * b.x;
|
|
346
|
+
this.y = a.y * b.y;
|
|
347
|
+
this.z = a.z * b.z;
|
|
348
|
+
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Applies the given Euler rotation to this vector.
|
|
354
|
+
*
|
|
355
|
+
* @param euler - The Euler angles.
|
|
356
|
+
* @return A reference to this vector.
|
|
357
|
+
*/
|
|
358
|
+
applyEuler(euler: Euler): Vector3 {
|
|
359
|
+
return this.applyQuaternion(_quaternion.setFromEuler(euler));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Applies a rotation specified by an axis and an angle to this vector.
|
|
364
|
+
*
|
|
365
|
+
* @param axis - A normalized vector representing the rotation axis.
|
|
366
|
+
* @param angle - The angle in radians.
|
|
367
|
+
* @return A reference to this vector.
|
|
368
|
+
*/
|
|
369
|
+
applyAxisAngle(axis: Vector3, angle: number): Vector3 {
|
|
370
|
+
return this.applyQuaternion(_quaternion.setFromAxisAngle(axis, angle));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and
|
|
375
|
+
* divides by perspective.
|
|
376
|
+
*
|
|
377
|
+
* @param m - The matrix to apply.
|
|
378
|
+
* @return A reference to this vector.
|
|
379
|
+
*/
|
|
380
|
+
applyMatrix4(m: Matrix4): Vector3 {
|
|
381
|
+
const x = this.x, y = this.y, z = this.z;
|
|
382
|
+
const e = m.elements;
|
|
383
|
+
|
|
384
|
+
const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
|
|
385
|
+
|
|
386
|
+
this.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
|
|
387
|
+
this.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
|
|
388
|
+
this.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
|
|
389
|
+
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Applies the given Quaternion to this vector.
|
|
395
|
+
*
|
|
396
|
+
* @param q - The Quaternion.
|
|
397
|
+
* @return A reference to this vector.
|
|
398
|
+
*/
|
|
399
|
+
applyQuaternion(q: Quaternion): Vector3 {
|
|
400
|
+
// quaternion q is assumed to have unit length
|
|
401
|
+
const vx = this.x, vy = this.y, vz = this.z;
|
|
402
|
+
const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
|
|
403
|
+
|
|
404
|
+
// t = 2 * cross( q.xyz, v );
|
|
405
|
+
const tx = 2 * (qy * vz - qz * vy);
|
|
406
|
+
const ty = 2 * (qz * vx - qx * vz);
|
|
407
|
+
const tz = 2 * (qx * vy - qy * vx);
|
|
408
|
+
|
|
409
|
+
// v + q.w * t + cross( q.xyz, t );
|
|
410
|
+
this.x = vx + qw * tx + qy * tz - qz * ty;
|
|
411
|
+
this.y = vy + qw * ty + qz * tx - qx * tz;
|
|
412
|
+
this.z = vz + qw * tz + qx * ty - qy * tx;
|
|
413
|
+
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Projects this vector from world space into the camera's normalized
|
|
419
|
+
* device coordinate (NDC) space.
|
|
420
|
+
*
|
|
421
|
+
* @param matrix - The camera matrixWorldInverse
|
|
422
|
+
* @param projectionMatrix - The camera projectionMatrix
|
|
423
|
+
* @return A reference to this vector.
|
|
424
|
+
*/
|
|
425
|
+
project(matrix: Matrix4, projectionMatrix: Matrix4): Vector3 {
|
|
426
|
+
return this.applyMatrix4(matrix).applyMatrix4(projectionMatrix);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Unprojects this vector from the camera's normalized device coordinate (NDC)
|
|
431
|
+
* space into world space.
|
|
432
|
+
*
|
|
433
|
+
* @param matrix - The camera projectionMatrixInverse
|
|
434
|
+
* @param matrixWorld - The camera matrixWorld
|
|
435
|
+
* @return A reference to this vector.
|
|
436
|
+
*/
|
|
437
|
+
unproject(matrix: Matrix4, matrixWorld: Matrix4): Vector3 {
|
|
438
|
+
return this.applyMatrix4(matrix).applyMatrix4(matrixWorld);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Transforms the direction of this vector by a matrix (the upper left 3 x 3
|
|
443
|
+
* subset of the given 4x4 matrix and then normalizes the result.
|
|
444
|
+
*
|
|
445
|
+
* @param m - The matrix.
|
|
446
|
+
* @return A reference to this vector.
|
|
447
|
+
*/
|
|
448
|
+
transformDirection(m: Matrix4): Vector3 {
|
|
449
|
+
// input: Matrix4 affine matrix
|
|
450
|
+
// vector interpreted as a direction
|
|
451
|
+
const x = this.x, y = this.y, z = this.z;
|
|
452
|
+
const e = m.elements;
|
|
453
|
+
|
|
454
|
+
this.x = e[0] * x + e[4] * y + e[8] * z;
|
|
455
|
+
this.y = e[1] * x + e[5] * y + e[9] * z;
|
|
456
|
+
this.z = e[2] * x + e[6] * y + e[10] * z;
|
|
457
|
+
|
|
458
|
+
return this.normalize();
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Divides this instance by the given vector.
|
|
463
|
+
*
|
|
464
|
+
* @param v - The vector to divide.
|
|
465
|
+
* @return A reference to this vector.
|
|
466
|
+
*/
|
|
467
|
+
divide(v: Vector3): Vector3 {
|
|
468
|
+
this.x /= v.x;
|
|
469
|
+
this.y /= v.y;
|
|
470
|
+
this.z /= v.z;
|
|
471
|
+
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Divides this vector by the given scalar.
|
|
477
|
+
*
|
|
478
|
+
* @param scalar - The scalar to divide.
|
|
479
|
+
* @return A reference to this vector.
|
|
480
|
+
*/
|
|
481
|
+
divideScalar(scalar: number): Vector3 {
|
|
482
|
+
return this.multiplyScalar(1 / scalar);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* If this vector's x, y or z value is greater than the given vector's x, y or z
|
|
487
|
+
* value, replace that value with the corresponding min value.
|
|
488
|
+
*
|
|
489
|
+
* @param v - The vector.
|
|
490
|
+
* @return A reference to this vector.
|
|
491
|
+
*/
|
|
492
|
+
min(v: Vector3): Vector3 {
|
|
493
|
+
this.x = Math.min(this.x, v.x);
|
|
494
|
+
this.y = Math.min(this.y, v.y);
|
|
495
|
+
this.z = Math.min(this.z, v.z);
|
|
496
|
+
|
|
497
|
+
return this;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* If this vector's x, y or z value is less than the given vector's x, y or z
|
|
502
|
+
* value, replace that value with the corresponding max value.
|
|
503
|
+
*
|
|
504
|
+
* @param v - The vector.
|
|
505
|
+
* @return A reference to this vector.
|
|
506
|
+
*/
|
|
507
|
+
max(v: Vector3): Vector3 {
|
|
508
|
+
this.x = Math.max(this.x, v.x);
|
|
509
|
+
this.y = Math.max(this.y, v.y);
|
|
510
|
+
this.z = Math.max(this.z, v.z);
|
|
511
|
+
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* If this vector's x, y or z value is greater than the max vector's x, y or z
|
|
517
|
+
* value, it is replaced by the corresponding value.
|
|
518
|
+
* If this vector's x, y or z value is less than the min vector's x, y or z value,
|
|
519
|
+
* it is replaced by the corresponding value.
|
|
520
|
+
*
|
|
521
|
+
* @param min - The minimum x, y and z values.
|
|
522
|
+
* @param max - The maximum x, y and z values in the desired range.
|
|
523
|
+
* @return A reference to this vector.
|
|
524
|
+
*/
|
|
525
|
+
clamp(min: Vector3, max: Vector3): Vector3 {
|
|
526
|
+
// assumes min < max, componentwise
|
|
527
|
+
this.x = clamp(this.x, min.x, max.x);
|
|
528
|
+
this.y = clamp(this.y, min.y, max.y);
|
|
529
|
+
this.z = clamp(this.z, min.z, max.z);
|
|
530
|
+
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* If this vector's x, y or z values are greater than the max value, they are
|
|
536
|
+
* replaced by the max value.
|
|
537
|
+
* If this vector's x, y or z values are less than the min value, they are
|
|
538
|
+
* replaced by the min value.
|
|
539
|
+
*
|
|
540
|
+
* @param minVal - The minimum value the components will be clamped to.
|
|
541
|
+
* @param maxVal - The maximum value the components will be clamped to.
|
|
542
|
+
* @return A reference to this vector.
|
|
543
|
+
*/
|
|
544
|
+
clampScalar(minVal: number, maxVal: number): Vector3 {
|
|
545
|
+
this.x = clamp(this.x, minVal, maxVal);
|
|
546
|
+
this.y = clamp(this.y, minVal, maxVal);
|
|
547
|
+
this.z = clamp(this.z, minVal, maxVal);
|
|
548
|
+
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* If this vector's length is greater than the max value, it is replaced by
|
|
554
|
+
* the max value.
|
|
555
|
+
* If this vector's length is less than the min value, it is replaced by the
|
|
556
|
+
* min value.
|
|
557
|
+
*
|
|
558
|
+
* @param min - The minimum value the vector length will be clamped to.
|
|
559
|
+
* @param max - The maximum value the vector length will be clamped to.
|
|
560
|
+
* @return A reference to this vector.
|
|
561
|
+
*/
|
|
562
|
+
clampLength(min: number, max: number): Vector3 {
|
|
563
|
+
const length = this.length();
|
|
564
|
+
return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The components of this vector are rounded down to the nearest integer value.
|
|
569
|
+
*
|
|
570
|
+
* @return A reference to this vector.
|
|
571
|
+
*/
|
|
572
|
+
floor(): Vector3 {
|
|
573
|
+
this.x = Math.floor(this.x);
|
|
574
|
+
this.y = Math.floor(this.y);
|
|
575
|
+
this.z = Math.floor(this.z);
|
|
576
|
+
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* The components of this vector are rounded up to the nearest integer value.
|
|
582
|
+
*
|
|
583
|
+
* @return A reference to this vector.
|
|
584
|
+
*/
|
|
585
|
+
ceil(): Vector3 {
|
|
586
|
+
this.x = Math.ceil(this.x);
|
|
587
|
+
this.y = Math.ceil(this.y);
|
|
588
|
+
this.z = Math.ceil(this.z);
|
|
589
|
+
|
|
590
|
+
return this;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* The components of this vector are rounded to the nearest integer value
|
|
595
|
+
*
|
|
596
|
+
* @return A reference to this vector.
|
|
597
|
+
*/
|
|
598
|
+
round(): Vector3 {
|
|
599
|
+
this.x = Math.round(this.x);
|
|
600
|
+
this.y = Math.round(this.y);
|
|
601
|
+
this.z = Math.round(this.z);
|
|
602
|
+
|
|
603
|
+
return this;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* The components of this vector are rounded towards zero (up if negative,
|
|
608
|
+
* down if positive) to an integer value.
|
|
609
|
+
*
|
|
610
|
+
* @return A reference to this vector.
|
|
611
|
+
*/
|
|
612
|
+
roundToZero(): Vector3 {
|
|
613
|
+
this.x = Math.trunc(this.x);
|
|
614
|
+
this.y = Math.trunc(this.y);
|
|
615
|
+
this.z = Math.trunc(this.z);
|
|
616
|
+
|
|
617
|
+
return this;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Inverts this vector - i.e. sets x = -x, y = -y and z = -z.
|
|
622
|
+
*
|
|
623
|
+
* @return A reference to this vector.
|
|
624
|
+
*/
|
|
625
|
+
negate(): Vector3 {
|
|
626
|
+
this.x = -this.x;
|
|
627
|
+
this.y = -this.y;
|
|
628
|
+
this.z = -this.z;
|
|
629
|
+
|
|
630
|
+
return this;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Calculates the dot product of the given vector with this instance.
|
|
635
|
+
*
|
|
636
|
+
* @param v - The vector to compute the dot product with.
|
|
637
|
+
* @return The result of the dot product.
|
|
638
|
+
*/
|
|
639
|
+
dot(v: Vector3): number {
|
|
640
|
+
return this.x * v.x + this.y * v.y + this.z * v.z;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Computes the square of the Euclidean length (straight-line length) from
|
|
645
|
+
* (0, 0, 0) to (x, y, z). If you are comparing the lengths of vectors, you should
|
|
646
|
+
* compare the length squared instead as it is slightly more efficient to calculate.
|
|
647
|
+
*
|
|
648
|
+
* @return The square length of this vector.
|
|
649
|
+
*/
|
|
650
|
+
lengthSq(): number {
|
|
651
|
+
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Computes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
|
|
656
|
+
*
|
|
657
|
+
* @return The length of this vector.
|
|
658
|
+
*/
|
|
659
|
+
length(): number {
|
|
660
|
+
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Computes the Manhattan length of this vector.
|
|
665
|
+
*
|
|
666
|
+
* @return The length of this vector.
|
|
667
|
+
*/
|
|
668
|
+
manhattanLength(): number {
|
|
669
|
+
return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Converts this vector to a unit vector - that is, sets it equal to a vector
|
|
674
|
+
* with the same direction as this one, but with a vector length of `1`.
|
|
675
|
+
*
|
|
676
|
+
* @return A reference to this vector.
|
|
677
|
+
*/
|
|
678
|
+
normalize(): Vector3 {
|
|
679
|
+
return this.divideScalar(this.length() || 1);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Sets this vector to a vector with the same direction as this one, but
|
|
684
|
+
* with the specified length.
|
|
685
|
+
*
|
|
686
|
+
* @param length - The new length of this vector.
|
|
687
|
+
* @return A reference to this vector.
|
|
688
|
+
*/
|
|
689
|
+
setLength(length: number): Vector3 {
|
|
690
|
+
return this.normalize().multiplyScalar(length);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Linearly interpolates between the given vector and this instance, where
|
|
695
|
+
* alpha is the percent distance along the line - alpha = 0 will be this
|
|
696
|
+
* vector, and alpha = 1 will be the given one.
|
|
697
|
+
*
|
|
698
|
+
* @param v - The vector to interpolate towards.
|
|
699
|
+
* @param alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
|
|
700
|
+
* @return A reference to this vector.
|
|
701
|
+
*/
|
|
702
|
+
lerp(v: Vector3, alpha: number): Vector3 {
|
|
703
|
+
this.x += (v.x - this.x) * alpha;
|
|
704
|
+
this.y += (v.y - this.y) * alpha;
|
|
705
|
+
this.z += (v.z - this.z) * alpha;
|
|
706
|
+
|
|
707
|
+
return this;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Linearly interpolates between the given vectors, where alpha is the percent
|
|
712
|
+
* distance along the line - alpha = 0 will be first vector, and alpha = 1 will
|
|
713
|
+
* be the second one. The result is stored in this instance.
|
|
714
|
+
*
|
|
715
|
+
* @param v1 - The first vector.
|
|
716
|
+
* @param v2 - The second vector.
|
|
717
|
+
* @param alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
|
|
718
|
+
* @return A reference to this vector.
|
|
719
|
+
*/
|
|
720
|
+
lerpVectors(v1: Vector3, v2: Vector3, alpha: number): Vector3 {
|
|
721
|
+
this.x = v1.x + (v2.x - v1.x) * alpha;
|
|
722
|
+
this.y = v1.y + (v2.y - v1.y) * alpha;
|
|
723
|
+
this.z = v1.z + (v2.z - v1.z) * alpha;
|
|
724
|
+
|
|
725
|
+
return this;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Calculates the cross product of the given vector with this instance.
|
|
730
|
+
*
|
|
731
|
+
* @param v - The vector to compute the cross product with.
|
|
732
|
+
* @return The result of the cross product.
|
|
733
|
+
*/
|
|
734
|
+
cross(v: Vector3): Vector3 {
|
|
735
|
+
return this.crossVectors(this, v);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Calculates the cross product of the given vectors and stores the result
|
|
740
|
+
* in this instance.
|
|
741
|
+
*
|
|
742
|
+
* @param a - The first vector.
|
|
743
|
+
* @param b - The second vector.
|
|
744
|
+
* @return A reference to this vector.
|
|
745
|
+
*/
|
|
746
|
+
crossVectors(a: Vector3, b: Vector3): Vector3 {
|
|
747
|
+
const ax = a.x, ay = a.y, az = a.z;
|
|
748
|
+
const bx = b.x, by = b.y, bz = b.z;
|
|
749
|
+
|
|
750
|
+
this.x = ay * bz - az * by;
|
|
751
|
+
this.y = az * bx - ax * bz;
|
|
752
|
+
this.z = ax * by - ay * bx;
|
|
753
|
+
|
|
754
|
+
return this;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Projects this vector onto the given one.
|
|
759
|
+
*
|
|
760
|
+
* @param v - The vector to project to.
|
|
761
|
+
* @return A reference to this vector.
|
|
762
|
+
*/
|
|
763
|
+
projectOnVector(v: Vector3): Vector3 {
|
|
764
|
+
const denominator = v.lengthSq();
|
|
765
|
+
|
|
766
|
+
if (denominator === 0) return this.set(0, 0, 0);
|
|
767
|
+
|
|
768
|
+
const scalar = v.dot(this) / denominator;
|
|
769
|
+
|
|
770
|
+
return this.copy(v).multiplyScalar(scalar);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Projects this vector onto a plane by subtracting this
|
|
775
|
+
* vector projected onto the plane's normal from this vector.
|
|
776
|
+
*
|
|
777
|
+
* @param planeNormal - The plane normal.
|
|
778
|
+
* @return A reference to this vector.
|
|
779
|
+
*/
|
|
780
|
+
projectOnPlane(planeNormal: Vector3): Vector3 {
|
|
781
|
+
_vector.copy(this).projectOnVector(planeNormal);
|
|
782
|
+
return this.sub(_vector);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Reflects this vector off a plane orthogonal to the given normal vector.
|
|
787
|
+
*
|
|
788
|
+
* @param normal - The (normalized) normal vector.
|
|
789
|
+
* @return A reference to this vector.
|
|
790
|
+
*/
|
|
791
|
+
reflect(normal: Vector3): Vector3 {
|
|
792
|
+
return this.sub(_vector.copy(normal).multiplyScalar(2 * this.dot(normal)));
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Returns the angle between the given vector and this instance in radians.
|
|
797
|
+
*
|
|
798
|
+
* @param v - The vector to compute the angle with.
|
|
799
|
+
* @return The angle in radians.
|
|
800
|
+
*/
|
|
801
|
+
angleTo(v: Vector3): number {
|
|
802
|
+
const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
|
|
803
|
+
|
|
804
|
+
if (denominator === 0) return Math.PI / 2;
|
|
805
|
+
|
|
806
|
+
const theta = this.dot(v) / denominator;
|
|
807
|
+
|
|
808
|
+
// clamp, to handle numerical problems
|
|
809
|
+
return Math.acos(clamp(theta, -1, 1));
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* Computes the distance from the given vector to this instance.
|
|
814
|
+
*
|
|
815
|
+
* @param v - The vector to compute the distance to.
|
|
816
|
+
* @return The distance.
|
|
817
|
+
*/
|
|
818
|
+
distanceTo(v: Vector3): number {
|
|
819
|
+
return Math.sqrt(this.distanceToSquared(v));
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Computes the squared distance from the given vector to this instance.
|
|
824
|
+
* If you are just comparing the distance with another distance, you should compare
|
|
825
|
+
* the distance squared instead as it is slightly more efficient to calculate.
|
|
826
|
+
*
|
|
827
|
+
* @param v - The vector to compute the squared distance to.
|
|
828
|
+
* @return The squared distance.
|
|
829
|
+
*/
|
|
830
|
+
distanceToSquared(v: Vector3): number {
|
|
831
|
+
const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
|
|
832
|
+
return dx * dx + dy * dy + dz * dz;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Computes the Manhattan distance from the given vector to this instance.
|
|
837
|
+
*
|
|
838
|
+
* @param v - The vector to compute the Manhattan distance to.
|
|
839
|
+
* @return The Manhattan distance.
|
|
840
|
+
*/
|
|
841
|
+
manhattanDistanceTo(v: Vector3): number {
|
|
842
|
+
return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Sets the vector components from the given spherical coordinates.
|
|
847
|
+
*
|
|
848
|
+
* @param s - The spherical coordinates.
|
|
849
|
+
* @return A reference to this vector.
|
|
850
|
+
*/
|
|
851
|
+
setFromSpherical(s: Spherical): Vector3 {
|
|
852
|
+
return this.setFromSphericalCoords(s.radius, s.phi, s.theta);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* Sets the vector components from the given spherical coordinates.
|
|
857
|
+
*
|
|
858
|
+
* @param radius - The radius.
|
|
859
|
+
* @param phi - The phi angle in radians.
|
|
860
|
+
* @param theta - The theta angle in radians.
|
|
861
|
+
* @return A reference to this vector.
|
|
862
|
+
*/
|
|
863
|
+
setFromSphericalCoords(radius: number, phi: number, theta: number): Vector3 {
|
|
864
|
+
const sinPhiRadius = Math.sin(phi) * radius;
|
|
865
|
+
|
|
866
|
+
this.x = sinPhiRadius * Math.sin(theta);
|
|
867
|
+
this.y = Math.cos(phi) * radius;
|
|
868
|
+
this.z = sinPhiRadius * Math.cos(theta);
|
|
869
|
+
|
|
870
|
+
return this;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Sets the vector components from the given cylindrical coordinates.
|
|
875
|
+
*
|
|
876
|
+
* @param c - The cylindrical coordinates.
|
|
877
|
+
* @return A reference to this vector.
|
|
878
|
+
*/
|
|
879
|
+
setFromCylindrical(c: Cylindrical): Vector3 {
|
|
880
|
+
return this.setFromCylindricalCoords(c.radius, c.theta, c.y);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* Sets the vector components from the given cylindrical coordinates.
|
|
885
|
+
*
|
|
886
|
+
* @param radius - The radius.
|
|
887
|
+
* @param theta - The theta angle in radians.
|
|
888
|
+
* @param y - The y value.
|
|
889
|
+
* @return A reference to this vector.
|
|
890
|
+
*/
|
|
891
|
+
setFromCylindricalCoords(radius: number, theta: number, y: number): Vector3 {
|
|
892
|
+
this.x = radius * Math.sin(theta);
|
|
893
|
+
this.y = y;
|
|
894
|
+
this.z = radius * Math.cos(theta);
|
|
895
|
+
|
|
896
|
+
return this;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Sets the vector components to the position elements of the
|
|
901
|
+
* given transformation matrix.
|
|
902
|
+
*
|
|
903
|
+
* @param m - The 4x4 matrix.
|
|
904
|
+
* @return A reference to this vector.
|
|
905
|
+
*/
|
|
906
|
+
setFromMatrixPosition(m: Matrix4): Vector3 {
|
|
907
|
+
const e = m.elements;
|
|
908
|
+
|
|
909
|
+
this.x = e[12];
|
|
910
|
+
this.y = e[13];
|
|
911
|
+
this.z = e[14];
|
|
912
|
+
|
|
913
|
+
return this;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Sets the vector components to the scale elements of the
|
|
918
|
+
* given transformation matrix.
|
|
919
|
+
*
|
|
920
|
+
* @param m - The 4x4 matrix.
|
|
921
|
+
* @return A reference to this vector.
|
|
922
|
+
*/
|
|
923
|
+
setFromMatrixScale(m: Matrix4): Vector3 {
|
|
924
|
+
const sx = this.setFromMatrixColumn(m, 0).length();
|
|
925
|
+
const sy = this.setFromMatrixColumn(m, 1).length();
|
|
926
|
+
const sz = this.setFromMatrixColumn(m, 2).length();
|
|
927
|
+
|
|
928
|
+
this.x = sx;
|
|
929
|
+
this.y = sy;
|
|
930
|
+
this.z = sz;
|
|
931
|
+
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Sets the vector components from the specified matrix column.
|
|
937
|
+
*
|
|
938
|
+
* @param m - The 4x4 matrix.
|
|
939
|
+
* @param index - The column index.
|
|
940
|
+
* @return A reference to this vector.
|
|
941
|
+
*/
|
|
942
|
+
setFromMatrixColumn(m: Matrix4, index: number): Vector3 {
|
|
943
|
+
return this.fromArray(m.elements, index * 4);
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Sets the vector components from the given Euler angles.
|
|
948
|
+
*
|
|
949
|
+
* @param e - The Euler angles to set.
|
|
950
|
+
* @return A reference to this vector.
|
|
951
|
+
*/
|
|
952
|
+
setFromEuler(e: Euler): Vector3 {
|
|
953
|
+
this.x = e.x;
|
|
954
|
+
this.y = e.y;
|
|
955
|
+
this.z = e.z;
|
|
956
|
+
|
|
957
|
+
return this;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Sets the vector components from the RGB components of the
|
|
962
|
+
* given color.
|
|
963
|
+
*
|
|
964
|
+
* @param c - The color to set.
|
|
965
|
+
* @return A reference to this vector.
|
|
966
|
+
*/
|
|
967
|
+
setFromColor(c: Color): Vector3 {
|
|
968
|
+
const rgb = c.toRgb()
|
|
969
|
+
|
|
970
|
+
this.x = rgb.r;
|
|
971
|
+
this.y = rgb.g;
|
|
972
|
+
this.z = rgb.b;
|
|
973
|
+
|
|
974
|
+
return this;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Returns `true` if this vector is equal with the given one.
|
|
979
|
+
*
|
|
980
|
+
* @param v - The vector to test for equality.
|
|
981
|
+
* @return Whether this vector is equal with the given one.
|
|
982
|
+
*/
|
|
983
|
+
equals(v: Vector3): boolean {
|
|
984
|
+
return (v.x === this.x) && (v.y === this.y) && (v.z === this.z);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* Sets this vector's x value to be `array[ offset ]`, y value to be `array[ offset + 1 ]`
|
|
989
|
+
* and z value to be `array[ offset + 2 ]`.
|
|
990
|
+
*
|
|
991
|
+
* @param array - An array holding the vector component values.
|
|
992
|
+
* @param offset - The offset into the array.
|
|
993
|
+
* @return A reference to this vector.
|
|
994
|
+
*/
|
|
995
|
+
fromArray(array: number[], offset: number = 0): Vector3 {
|
|
996
|
+
this.x = array[offset];
|
|
997
|
+
this.y = array[offset + 1];
|
|
998
|
+
this.z = array[offset + 2];
|
|
999
|
+
|
|
1000
|
+
return this;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
* Writes the components of this vector to the given array. If no array is provided,
|
|
1005
|
+
* the method returns a new instance.
|
|
1006
|
+
*
|
|
1007
|
+
* @param array - The target array holding the vector components.
|
|
1008
|
+
* @param offset - Index of the first element in the array.
|
|
1009
|
+
* @return The vector components.
|
|
1010
|
+
*/
|
|
1011
|
+
toArray(array: number[] = [], offset: number = 0): number[] {
|
|
1012
|
+
array[offset] = this.x;
|
|
1013
|
+
array[offset + 1] = this.y;
|
|
1014
|
+
array[offset + 2] = this.z;
|
|
1015
|
+
|
|
1016
|
+
return array;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Sets each component of this vector to a pseudo-random value between `0` and
|
|
1021
|
+
* `1`, excluding `1`.
|
|
1022
|
+
*
|
|
1023
|
+
* @return A reference to this vector.
|
|
1024
|
+
*/
|
|
1025
|
+
random(): Vector3 {
|
|
1026
|
+
this.x = Math.random();
|
|
1027
|
+
this.y = Math.random();
|
|
1028
|
+
this.z = Math.random();
|
|
1029
|
+
|
|
1030
|
+
return this;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Sets this vector to a uniformly random point on a unit sphere.
|
|
1035
|
+
*
|
|
1036
|
+
* @return A reference to this vector.
|
|
1037
|
+
*/
|
|
1038
|
+
randomDirection(): Vector3 {
|
|
1039
|
+
// https://mathworld.wolfram.com/SpherePointPicking.html
|
|
1040
|
+
const theta = Math.random() * Math.PI * 2;
|
|
1041
|
+
const u = Math.random() * 2 - 1;
|
|
1042
|
+
const c = Math.sqrt(1 - u * u);
|
|
1043
|
+
|
|
1044
|
+
this.x = c * Math.cos(theta);
|
|
1045
|
+
this.y = u;
|
|
1046
|
+
this.z = c * Math.sin(theta);
|
|
1047
|
+
|
|
1048
|
+
return this;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
*[Symbol.iterator](): Generator<number> {
|
|
1052
|
+
yield this.x;
|
|
1053
|
+
yield this.y;
|
|
1054
|
+
yield this.z;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
// 全局辅助变量
|
|
1059
|
+
const _vector = /*@__PURE__*/ new Vector3();
|
|
1060
|
+
const _quaternion = /*@__PURE__*/ new Quaternion();
|
|
1061
|
+
|
|
1062
|
+
export { Vector3 };
|