@openglobus/og 0.12.4 → 0.13.2
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/README.md +0 -2
- package/dist/@openglobus/og.esm.js +2 -2
- package/dist/@openglobus/og.esm.js.map +1 -1
- package/dist/@openglobus/og.umd.js +2 -2
- package/dist/@openglobus/og.umd.js.map +1 -1
- package/package.json +7 -7
- package/src/og/Extent.js +3 -3
- package/src/og/Globe.js +277 -275
- package/src/og/ImageCanvas.js +3 -3
- package/src/og/LonLat.js +9 -9
- package/src/og/QueueArray.js +2 -2
- package/src/og/Rectangle.js +5 -5
- package/src/og/bv/Box.js +1 -1
- package/src/og/bv/Sphere.js +2 -2
- package/src/og/camera/Camera.js +1 -1
- package/src/og/camera/PlanetCamera.js +649 -646
- package/src/og/control/LayerSwitcher.js +1 -1
- package/src/og/control/MouseNavigation.js +22 -13
- package/src/og/control/MouseWheelZoomControl.js +2 -2
- package/src/og/control/ScaleControl.js +1 -1
- package/src/og/control/TouchNavigation.js +319 -319
- package/src/og/control/ZoomControl.js +132 -132
- package/src/og/control/visibleExtent/Segment.js +12 -12
- package/src/og/ellipsoid/Ellipsoid.js +577 -567
- package/src/og/entity/BillboardHandler.js +878 -1051
- package/src/og/entity/Entity.js +1 -1
- package/src/og/entity/EntityCollection.js +847 -857
- package/src/og/entity/GeoObjectHandler.js +2 -2
- package/src/og/entity/LabelHandler.js +887 -1040
- package/src/og/entity/LabelWorker.js +0 -6
- package/src/og/entity/PointCloud.js +495 -495
- package/src/og/entity/Polyline.js +2218 -2222
- package/src/og/entity/RayHandler.js +1 -1
- package/src/og/layer/CanvasTiles.js +6 -3
- package/src/og/layer/GeoImage.js +1 -1
- package/src/og/layer/KML.js +5 -5
- package/src/og/layer/Layer.js +800 -752
- package/src/og/layer/Vector.js +997 -1010
- package/src/og/layer/XYZ.js +359 -357
- package/src/og/math/Line2.js +4 -4
- package/src/og/math/Quat.js +2 -2
- package/src/og/math/Ray.js +2 -2
- package/src/og/math/Vec2.js +524 -524
- package/src/og/math/Vec3.js +900 -900
- package/src/og/math/Vec4.js +261 -261
- package/src/og/math.js +397 -398
- package/src/og/quadTree/EntityCollectionNode.js +389 -387
- package/src/og/renderer/Renderer.js +14 -20
- package/src/og/renderer/RendererEvents.js +1034 -1045
- package/src/og/scene/Planet.js +12 -9
- package/src/og/scene/RenderNode.js +347 -354
- package/src/og/segment/Segment.js +3 -1
- package/src/og/segment/SegmentLonLat.js +1 -4
- package/src/og/shaders/billboard.js +220 -204
- package/src/og/shaders/drawnode.js +607 -603
- package/src/og/shaders/label.js +576 -456
- package/src/og/shaders/polyline.js +365 -361
- package/src/og/terrain/MapboxTerrain.js +1 -1
- package/src/og/utils/FontAtlas.js +209 -200
- package/src/og/utils/GeoImageCreator.js +4 -5
- package/src/og/utils/TextureAtlas.js +4 -4
- package/src/og/utils/VectorTileCreator.js +414 -414
- package/src/og/utils/shared.js +1 -1
- package/src/og/webgl/Handler.js +28 -60
- package/src/og/webgl/Multisample.js +260 -260
- package/src/og/webgl/Program.js +411 -396
- package/types/bv/Box.d.ts +1 -1
- package/types/bv/Sphere.d.ts +1 -1
- package/types/camera/PlanetCamera.d.ts +3 -3
- package/types/control/LayerSwitcher.d.ts +1 -1
- package/types/ellipsoid/Ellipsoid.d.ts +9 -2
- package/types/entity/BillboardHandler.d.ts +0 -4
- package/types/entity/EntityCollection.d.ts +0 -8
- package/types/entity/LabelHandler.d.ts +0 -2
- package/types/entity/Polyline.d.ts +1 -1
- package/types/layer/KML.d.ts +11 -11
- package/types/layer/Layer.d.ts +4 -0
- package/types/layer/Vector.d.ts +1 -8
- package/types/math/Line2.d.ts +4 -4
- package/types/math/Ray.d.ts +2 -2
- package/types/math/Vec2.d.ts +1 -1
- package/types/math/Vec3.d.ts +1 -1
- package/types/math/Vec4.d.ts +2 -2
- package/types/math.d.ts +1 -1
- package/types/renderer/Renderer.d.ts +1 -1
- package/types/renderer/RendererEvents.d.ts +1 -1
- package/types/shaders/label.d.ts +1 -1
- package/types/utils/FontAtlas.d.ts +3 -2
- package/types/utils/TextureAtlas.d.ts +3 -3
- package/types/utils/VectorTileCreator.d.ts +4 -4
- package/types/utils/shared.d.ts +1 -1
- package/types/webgl/Handler.d.ts +6 -31
- package/types/webgl/Program.d.ts +2 -0
package/src/og/math/Vec3.js
CHANGED
|
@@ -1,911 +1,911 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module og/math/Vec3
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
"use strict";
|
|
6
|
-
|
|
7
|
-
import { Vec4 } from "./Vec4.js";
|
|
8
|
-
import { Quat } from "./Quat.js";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Class represents a 3d vector.
|
|
12
|
-
* @class
|
|
13
|
-
* @param {number} [x] - First value.
|
|
14
|
-
* @param {number} [y] - Second value.
|
|
15
|
-
* @param {number} [z] - Third value.
|
|
16
|
-
*/
|
|
17
|
-
export class Vec3 {
|
|
18
|
-
constructor(x, y, z) {
|
|
19
|
-
/**
|
|
20
|
-
* @public
|
|
21
|
-
* @type {number}
|
|
22
|
-
*/
|
|
23
|
-
this.x = x || 0.0;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @public
|
|
27
|
-
* @type {number}
|
|
28
|
-
*/
|
|
29
|
-
this.y = y || 0.0;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @public
|
|
33
|
-
* @type {number}
|
|
34
|
-
*/
|
|
35
|
-
this.z = z || 0.0;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** @const */
|
|
39
|
-
static get UP() {
|
|
40
|
-
return new Vec3(0, 1, 0);
|
|
41
|
-
}
|
|
42
|
-
/** @const */
|
|
43
|
-
static get DOWN() {
|
|
44
|
-
return new Vec3(0, -1, 0);
|
|
45
|
-
}
|
|
46
|
-
/** @const */
|
|
47
|
-
static get RIGHT() {
|
|
48
|
-
return new Vec3(1, 0, 0);
|
|
49
|
-
}
|
|
50
|
-
/** @const */
|
|
51
|
-
static get LEFT() {
|
|
52
|
-
return new Vec3(-1, 0, 0);
|
|
53
|
-
}
|
|
54
|
-
/** @const */
|
|
55
|
-
static get FORWARD() {
|
|
56
|
-
return new Vec3(0, 0, -1);
|
|
57
|
-
}
|
|
58
|
-
/** @const */
|
|
59
|
-
static get BACKWARD() {
|
|
60
|
-
return new Vec3(0, 0, 1);
|
|
61
|
-
}
|
|
62
|
-
/** @const */
|
|
63
|
-
static get ZERO() {
|
|
64
|
-
return new Vec3();
|
|
65
|
-
}
|
|
66
|
-
/** @const */
|
|
67
|
-
static get UNIT_X() {
|
|
68
|
-
return new Vec3(1, 0, 0);
|
|
69
|
-
}
|
|
70
|
-
/** @const */
|
|
71
|
-
static get UNIT_Y() {
|
|
72
|
-
return new Vec3(0, 1, 0);
|
|
73
|
-
}
|
|
74
|
-
/** @const */
|
|
75
|
-
static get UNIT_Z() {
|
|
76
|
-
return new Vec3(0, 0, 1);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Separate 63 bit Vec3 to two Vec3 32 bit float values.
|
|
81
|
-
* @function
|
|
82
|
-
* @param {number} value - Double type value.
|
|
83
|
-
* @param {Vec3} high - Out vector high values.
|
|
84
|
-
* @param {Vec3} low - Out vector low values.
|
|
85
|
-
* @returns {Array.<number>} Encoded array. (exactly 2 entries)
|
|
86
|
-
*/
|
|
87
|
-
static doubleToTwoFloats(v, high, low) {
|
|
88
|
-
let x = v.x,
|
|
89
|
-
y = v.y,
|
|
90
|
-
z = v.z;
|
|
91
|
-
|
|
92
|
-
if (x >= 0.0) {
|
|
93
|
-
let doubleHigh = Math.floor(x / 65536.0) * 65536.0;
|
|
94
|
-
high.x = Math.fround(doubleHigh);
|
|
95
|
-
low.x = Math.fround(x - doubleHigh);
|
|
96
|
-
} else {
|
|
97
|
-
let doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
|
|
98
|
-
high.x = Math.fround(-doubleHigh);
|
|
99
|
-
low.x = Math.fround(x + doubleHigh);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (y >= 0.0) {
|
|
103
|
-
let doubleHigh = Math.floor(y / 65536.0) * 65536.0;
|
|
104
|
-
high.y = Math.fround(doubleHigh);
|
|
105
|
-
low.y = Math.fround(y - doubleHigh);
|
|
106
|
-
} else {
|
|
107
|
-
let doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
|
|
108
|
-
high.y = Math.fround(-doubleHigh);
|
|
109
|
-
low.y = Math.fround(y + doubleHigh);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (z >= 0.0) {
|
|
113
|
-
let doubleHigh = Math.floor(z / 65536.0) * 65536.0;
|
|
114
|
-
high.z = Math.fround(doubleHigh);
|
|
115
|
-
low.z = Math.fround(z - doubleHigh);
|
|
116
|
-
} else {
|
|
117
|
-
let doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
|
|
118
|
-
high.z = Math.fround(-doubleHigh);
|
|
119
|
-
low.z = Math.fround(z + doubleHigh);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Separate 63 bit Vec3 to two Vec3 32 bit float values.
|
|
125
|
-
* @function
|
|
126
|
-
* @param {number} value - Double type value.
|
|
127
|
-
* @param {Float32Array} high - Out vector high values.
|
|
128
|
-
* @param {Float32Array} low - Out vector low values.
|
|
129
|
-
* @returns {Array.<number>} Encoded array. (exactly 2 entries)
|
|
130
|
-
*/
|
|
131
|
-
static doubleToTwoFloat32Array(v, high, low) {
|
|
132
|
-
let x = v.x,
|
|
133
|
-
y = v.y,
|
|
134
|
-
z = v.z;
|
|
135
|
-
|
|
136
|
-
if (x >= 0.0) {
|
|
137
|
-
let doubleHigh = Math.floor(x / 65536.0) * 65536.0;
|
|
138
|
-
high[0] = Math.fround(doubleHigh);
|
|
139
|
-
low[0] = Math.fround(x - doubleHigh);
|
|
140
|
-
} else {
|
|
141
|
-
let doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
|
|
142
|
-
high[0] = Math.fround(-doubleHigh);
|
|
143
|
-
low[0] = Math.fround(x + doubleHigh);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (y >= 0.0) {
|
|
147
|
-
let doubleHigh = Math.floor(y / 65536.0) * 65536.0;
|
|
148
|
-
high[1] = Math.fround(doubleHigh);
|
|
149
|
-
low[1] = Math.fround(y - doubleHigh);
|
|
150
|
-
} else {
|
|
151
|
-
let doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
|
|
152
|
-
high[1] = Math.fround(-doubleHigh);
|
|
153
|
-
low[1] = Math.fround(y + doubleHigh);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (z >= 0.0) {
|
|
157
|
-
let doubleHigh = Math.floor(z / 65536.0) * 65536.0;
|
|
158
|
-
high[2] = Math.fround(doubleHigh);
|
|
159
|
-
low[2] = Math.fround(z - doubleHigh);
|
|
160
|
-
} else {
|
|
161
|
-
let doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
|
|
162
|
-
high[2] = Math.fround(-doubleHigh);
|
|
163
|
-
low[2] = Math.fround(z + doubleHigh);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Creates 3d vector from array.
|
|
169
|
-
* @function
|
|
170
|
-
* @param {Array.<number>} arr - Input array (exactly 3 entries)
|
|
171
|
-
* @returns {Vec3} -
|
|
172
|
-
*/
|
|
173
|
-
static fromVec(arr) {
|
|
174
|
-
return new Vec3(arr[0], arr[1], arr[2]);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Gets angle between two vectors.
|
|
179
|
-
* @static
|
|
180
|
-
* @param {Vec3} a - First vector.
|
|
181
|
-
* @param {Vec3} b - Second vector.
|
|
182
|
-
* @returns {number} -
|
|
183
|
-
*/
|
|
184
|
-
static angle(a, b) {
|
|
185
|
-
return Math.acos(a.dot(b) / Math.sqrt(a.length2() * b.length2()));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Returns two vectors linear interpolation.
|
|
190
|
-
* @static
|
|
191
|
-
* @param {Vec3} v1 - Start vector.
|
|
192
|
-
* @param {Vec3} v2 - End vector.
|
|
193
|
-
* @param {number} l - Interpolate value.
|
|
194
|
-
* @returns {Vec3} -
|
|
195
|
-
*/
|
|
196
|
-
static lerp(v1, v2, l) {
|
|
197
|
-
return Vec3(v1.x + (v2.x - v1.x) * l, v1.y + (v2.y - v1.y) * l, v1.z + (v2.z - v1.z) * l);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Returns summary vector.
|
|
202
|
-
* @static
|
|
203
|
-
* @param {Vec3} a - First vector.
|
|
204
|
-
* @param {Vec3} b - Second vector.
|
|
205
|
-
* @returns {Vec3} - Summary vector.
|
|
206
|
-
*/
|
|
207
|
-
static add(a, b) {
|
|
208
|
-
var res = new Vec3(a.x, a.y, a.z);
|
|
209
|
-
res.addA(b);
|
|
210
|
-
return res;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Returns two vectors subtraction.
|
|
215
|
-
* @static
|
|
216
|
-
* @param {Vec3} a - First vector.
|
|
217
|
-
* @param {Vec3} b - Second vector.
|
|
218
|
-
* @returns {Vec3} - Vectors subtraction.
|
|
219
|
-
*/
|
|
220
|
-
static sub(a, b) {
|
|
221
|
-
var res = new Vec3(a.x, a.y, a.z);
|
|
222
|
-
res.subA(b);
|
|
223
|
-
return res;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Returns scaled vector.
|
|
228
|
-
* @static
|
|
229
|
-
* @param {Vec3} a - Input vector.
|
|
230
|
-
* @param {number} scale - Scale value.
|
|
231
|
-
* @returns {Vec3} -
|
|
232
|
-
*/
|
|
233
|
-
static scale(a, scale) {
|
|
234
|
-
var res = new Vec3(a.x, a.y, a.z);
|
|
235
|
-
res.scale(scale);
|
|
236
|
-
return res;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Returns two vectors production.
|
|
241
|
-
* @static
|
|
242
|
-
* @param {Vec3} a - First vector.
|
|
243
|
-
* @param {Vec3} b - Second vector.
|
|
244
|
-
* @returns {Vec3} -
|
|
245
|
-
*/
|
|
246
|
-
static mul(a, b) {
|
|
247
|
-
var res = new Vec3(a.x, a.y, a.z);
|
|
248
|
-
res.mulA(b);
|
|
249
|
-
return res;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Returns true if two vectors are non collinear.
|
|
254
|
-
* @public
|
|
255
|
-
* @param {Vec3} a - First vector.
|
|
256
|
-
* @param {Vec3} b - Second vector.
|
|
257
|
-
* @returns {Vec3} -
|
|
258
|
-
*/
|
|
259
|
-
static noncollinear(a, b) {
|
|
260
|
-
return a.y * b.z - a.z * b.y || a.z * b.x - a.x * b.z || a.x * b.y - a.y * b.z;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Get projection of the vector to plane where n - normal to the plane.
|
|
265
|
-
* @static
|
|
266
|
-
* @param {Vec3} b - Vector to project.
|
|
267
|
-
* @param {Vec3} n - Plane normal.
|
|
268
|
-
* @param {Vec3} [def] - Default value for non existed result.
|
|
269
|
-
* @returns {Vec3} -
|
|
270
|
-
*/
|
|
271
|
-
static proj_b_to_plane(b, n, def) {
|
|
272
|
-
var res = b.sub(n.scaleTo(n.dot(b) / n.dot(n)));
|
|
273
|
-
if (def && res.isZero()) {
|
|
274
|
-
return new Vec3(def.x, def.y, def.z);
|
|
275
|
-
}
|
|
276
|
-
return res;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Get projection of the first vector to the second.
|
|
281
|
-
* @static
|
|
282
|
-
* @param {Vec3} b - First vector.
|
|
283
|
-
* @param {Vec3} a - Second vector.
|
|
284
|
-
* @returns {Vec3} -
|
|
285
|
-
*/
|
|
286
|
-
static proj_b_to_a(b, a) {
|
|
287
|
-
return a.scaleTo(a.dot(b) / a.dot(a));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Makes vectors normalized and orthogonal to each other.
|
|
292
|
-
* Normalizes normal. Normalizes tangent and makes sure it is orthogonal to normal (that is, angle between them is 90 degrees).
|
|
293
|
-
* @static
|
|
294
|
-
* @param {Vec3} normal - Normal vector.
|
|
295
|
-
* @param {Vec3} tangent - Tangent vector.
|
|
296
|
-
* @returns {Vec3} -
|
|
297
|
-
*/
|
|
298
|
-
static orthoNormalize(normal, tangent) {
|
|
299
|
-
normal = normal.normal();
|
|
300
|
-
normal.scale(tangent.dot(normal));
|
|
301
|
-
return tangent.subA(normal).normalize();
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Returns vector components division product one to another.
|
|
306
|
-
* @static
|
|
307
|
-
* @param {Vec3} a - First vector.
|
|
308
|
-
* @param {Vec3} b - Second vector.
|
|
309
|
-
* @returns {Vec3} -
|
|
310
|
-
*/
|
|
311
|
-
static div(a, b) {
|
|
312
|
-
var res = new Vec3(a.x, a.y, a.z);
|
|
313
|
-
res.divA(b);
|
|
314
|
-
return res;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Converts to 4d vector, Fourth value is 1.0.
|
|
319
|
-
* @public
|
|
320
|
-
* @returns {Vec4} -
|
|
321
|
-
*/
|
|
322
|
-
toVec4() {
|
|
323
|
-
return new Vec4(this.x, this.y, this.z, 1.0);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Returns clone vector.
|
|
328
|
-
* @public
|
|
329
|
-
* @returns {Vec3} -
|
|
330
|
-
*/
|
|
331
|
-
clone() {
|
|
332
|
-
return new Vec3(this.x, this.y, this.z);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Converts vector to text string.
|
|
337
|
-
* @public
|
|
338
|
-
* @returns {string} -
|
|
339
|
-
*/
|
|
340
|
-
toString() {
|
|
341
|
-
return "(" + this.x + "," + this.y + "," + this.z + ")";
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Returns true if vector's values are zero.
|
|
346
|
-
* @public
|
|
347
|
-
* @returns {boolean} -
|
|
348
|
-
*/
|
|
349
|
-
isZero() {
|
|
350
|
-
return !(this.x || this.y || this.z);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Get projection of the first vector to the second.
|
|
355
|
-
* @static
|
|
356
|
-
* @param {Vec3} a - Project vector.
|
|
357
|
-
* @returns {Vec3} -
|
|
358
|
-
*/
|
|
359
|
-
projToVec(a) {
|
|
360
|
-
return a.scaleTo(a.dot(this) / a.dot(a));
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Compares with vector. Returns true if it equals another.
|
|
365
|
-
* @public
|
|
366
|
-
* @param {Vec3} p - Vector to compare.
|
|
367
|
-
* @returns {boolean} -
|
|
368
|
-
*/
|
|
369
|
-
equal(p) {
|
|
370
|
-
return this.x === p.x && this.y === p.y && this.z === p.z;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Copy input vector's values.
|
|
375
|
-
* @param {Vec3} point3 - Vector to copy.
|
|
376
|
-
* @returns {Vec3} -
|
|
377
|
-
*/
|
|
378
|
-
copy(point3) {
|
|
379
|
-
this.x = point3.x;
|
|
380
|
-
this.y = point3.y;
|
|
381
|
-
this.z = point3.z;
|
|
382
|
-
return this;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* Gets vector's length.
|
|
387
|
-
* @public
|
|
388
|
-
* @returns {number} -
|
|
389
|
-
*/
|
|
390
|
-
length() {
|
|
391
|
-
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Returns squared vector's length.
|
|
396
|
-
* @public
|
|
397
|
-
* @returns {number} -
|
|
398
|
-
*/
|
|
399
|
-
length2() {
|
|
400
|
-
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Converts vector's values to a quaternion object.
|
|
405
|
-
* @public
|
|
406
|
-
* @returns {Quat} -
|
|
407
|
-
*/
|
|
408
|
-
getQuat() {
|
|
409
|
-
return new Quat(this.x, this.y, this.z);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Adds vector to the current.
|
|
414
|
-
* @public
|
|
415
|
-
* @param {Vec3} point3 - Point to add.
|
|
416
|
-
* @returns {Vec3} -
|
|
417
|
-
*/
|
|
418
|
-
addA(point3) {
|
|
419
|
-
this.x += point3.x;
|
|
420
|
-
this.y += point3.y;
|
|
421
|
-
this.z += point3.z;
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Gets two vectors summarization.
|
|
427
|
-
* @public
|
|
428
|
-
* @param {Vec3} point3 - Vector to add.
|
|
429
|
-
* @returns {Vec3} Returns a sum vector.
|
|
430
|
-
*/
|
|
431
|
-
add(point3) {
|
|
432
|
-
return new Vec3(this.x + point3.x, this.y + point3.y, this.z + point3.z);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
/**
|
|
436
|
-
* Subtract vector from the current.
|
|
437
|
-
* @public
|
|
438
|
-
* @param {Vec3} point3 - Subtract vector.
|
|
439
|
-
* @returns {Vec3} -
|
|
440
|
-
*/
|
|
441
|
-
subA(point3) {
|
|
442
|
-
this.x -= point3.x;
|
|
443
|
-
this.y -= point3.y;
|
|
444
|
-
this.z -= point3.z;
|
|
445
|
-
return this;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* Gets vector subtraction.
|
|
450
|
-
* @public
|
|
451
|
-
* @param {Vec3} point3 - Subtract vector.
|
|
452
|
-
* @return {Vec3} Returns new instance of a subtraction
|
|
453
|
-
*/
|
|
454
|
-
sub(point3) {
|
|
455
|
-
return new Vec3(this.x - point3.x, this.y - point3.y, this.z - point3.z);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Scale current vector.
|
|
460
|
-
* @public
|
|
461
|
-
* @param {number} scale - Scale value.
|
|
462
|
-
* @returns {Vec3} -
|
|
463
|
-
*/
|
|
464
|
-
scale(scale) {
|
|
465
|
-
this.x *= scale;
|
|
466
|
-
this.y *= scale;
|
|
467
|
-
this.z *= scale;
|
|
468
|
-
return this;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Scale current vector to another instance.
|
|
473
|
-
* @public
|
|
474
|
-
* @param {number} scale - Scale value.
|
|
475
|
-
* @returns {Vec3} -
|
|
476
|
-
*/
|
|
477
|
-
scaleTo(scale) {
|
|
478
|
-
return new Vec3(this.x * scale, this.y * scale, this.z * scale);
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* Multiply current vector object to another and store result in the current instance.
|
|
483
|
-
* @public
|
|
484
|
-
* @param {Vec3} vec - Multiply vector.
|
|
485
|
-
* @returns {Vec3} -
|
|
486
|
-
*/
|
|
487
|
-
mulA(vec) {
|
|
488
|
-
this.x *= vec.x;
|
|
489
|
-
this.y *= vec.y;
|
|
490
|
-
this.z *= vec.z;
|
|
491
|
-
return this;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
/**
|
|
495
|
-
* Multiply current vector object to another and returns new vector instance.
|
|
496
|
-
* @public
|
|
497
|
-
* @param {Vec3} vec - Multiply vector.
|
|
498
|
-
* @returns {Vec3} -
|
|
499
|
-
*/
|
|
500
|
-
mul(vec) {
|
|
501
|
-
return new Vec3(this.x * vec.x, this.y * vec.y, this.z * vec.z);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* Divide current vector's components to another. Results stores in the current vector object.
|
|
506
|
-
* @public
|
|
507
|
-
* @param {Vec3} vec - Div vector.
|
|
508
|
-
* @returns {Vec3} -
|
|
509
|
-
*/
|
|
510
|
-
divA(vec) {
|
|
511
|
-
this.x /= vec.x;
|
|
512
|
-
this.y /= vec.y;
|
|
513
|
-
this.z /= vec.z;
|
|
514
|
-
return this;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Divide current vector's components to another and returns new vector instance.
|
|
519
|
-
* @public
|
|
520
|
-
* @param {Vec3} vec - Div vector.
|
|
521
|
-
* @returns {Vec3} -
|
|
522
|
-
*/
|
|
523
|
-
div(vec) {
|
|
524
|
-
return new Vec3(this.x / vec.x, this.y / vec.y, this.z / vec.z);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* Gets vectors dot production.
|
|
529
|
-
* @public
|
|
530
|
-
* @param {Vec3} point3 - Another vector.
|
|
531
|
-
* @returns {number} -
|
|
532
|
-
*/
|
|
533
|
-
dot(point3) {
|
|
534
|
-
return point3.x * this.x + point3.y * this.y + point3.z * this.z;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
/**
|
|
538
|
-
* Gets vectors dot production.
|
|
539
|
-
* @public
|
|
540
|
-
* @param {Array.<number>} arr - Array vector. (exactly 3 entries)
|
|
541
|
-
* @returns {number} -
|
|
542
|
-
*/
|
|
543
|
-
dotArr(arr) {
|
|
544
|
-
return arr[0] * this.x + arr[1] * this.y + arr[2] * this.z;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
/**
|
|
548
|
-
* Gets vectors cross production.
|
|
549
|
-
* @public
|
|
550
|
-
* @param {Vec3} point3 - Another vector.
|
|
551
|
-
* @returns {Vec3} -
|
|
552
|
-
*/
|
|
553
|
-
cross(point3) {
|
|
554
|
-
return new Vec3(
|
|
555
|
-
this.y * point3.z - this.z * point3.y,
|
|
556
|
-
this.z * point3.x - this.x * point3.z,
|
|
557
|
-
this.x * point3.y - this.y * point3.x
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* Sets vector to zero.
|
|
563
|
-
* @public
|
|
564
|
-
* @returns {Vec3} -
|
|
565
|
-
*/
|
|
566
|
-
clear() {
|
|
567
|
-
this.x = this.y = this.z = 0;
|
|
568
|
-
return this;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Returns normalized vector.
|
|
573
|
-
* @public
|
|
574
|
-
* @returns {Vec3} -
|
|
575
|
-
*/
|
|
576
|
-
getNormal() {
|
|
577
|
-
var res = new Vec3();
|
|
578
|
-
res.copy(this);
|
|
579
|
-
|
|
580
|
-
var length = 1.0 / res.length();
|
|
581
|
-
|
|
582
|
-
res.x *= length;
|
|
583
|
-
res.y *= length;
|
|
584
|
-
res.z *= length;
|
|
585
|
-
|
|
586
|
-
return res;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Returns normalized vector.
|
|
591
|
-
* @deprecated
|
|
592
|
-
* @public
|
|
593
|
-
* @returns {Vec3} -
|
|
594
|
-
*/
|
|
595
|
-
normal() {
|
|
596
|
-
var res = new Vec3();
|
|
597
|
-
res.copy(this);
|
|
598
|
-
|
|
599
|
-
var length = 1.0 / res.length();
|
|
600
|
-
|
|
601
|
-
res.x *= length;
|
|
602
|
-
res.y *= length;
|
|
603
|
-
res.z *= length;
|
|
604
|
-
|
|
605
|
-
return res;
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
* Returns normalized negate vector.
|
|
610
|
-
* @public
|
|
611
|
-
* @returns {Vec3} -
|
|
612
|
-
*/
|
|
613
|
-
normalNegate() {
|
|
614
|
-
var res = new Vec3();
|
|
615
|
-
res.copy(this);
|
|
616
|
-
|
|
617
|
-
var length = -1.0 / res.length();
|
|
618
|
-
|
|
619
|
-
res.x *= length;
|
|
620
|
-
res.y *= length;
|
|
621
|
-
res.z *= length;
|
|
622
|
-
|
|
623
|
-
return res;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/**
|
|
627
|
-
* Returns normalized negate scale vector.
|
|
628
|
-
* @public
|
|
629
|
-
* @returns {Vec3} -
|
|
630
|
-
*/
|
|
631
|
-
normalNegateScale(scale) {
|
|
632
|
-
var res = new Vec3();
|
|
633
|
-
res.copy(this);
|
|
634
|
-
|
|
635
|
-
var length = -scale / res.length();
|
|
636
|
-
|
|
637
|
-
res.x *= length;
|
|
638
|
-
res.y *= length;
|
|
639
|
-
res.z *= length;
|
|
640
|
-
|
|
641
|
-
return res;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
/**
|
|
645
|
-
* Returns normalized scale vector.
|
|
646
|
-
* @public
|
|
647
|
-
* @returns {Vec3} -
|
|
648
|
-
*/
|
|
649
|
-
normalScale(scale) {
|
|
650
|
-
var res = new Vec3();
|
|
651
|
-
res.copy(this);
|
|
652
|
-
|
|
653
|
-
var length = scale / res.length();
|
|
654
|
-
|
|
655
|
-
res.x *= length;
|
|
656
|
-
res.y *= length;
|
|
657
|
-
res.z *= length;
|
|
658
|
-
|
|
659
|
-
return res;
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
/**
|
|
663
|
-
* Normalize current vector.
|
|
664
|
-
* @public
|
|
665
|
-
* @returns {Vec3} -
|
|
666
|
-
*/
|
|
667
|
-
normalize() {
|
|
668
|
-
var length = 1.0 / this.length();
|
|
669
|
-
|
|
670
|
-
this.x *= length;
|
|
671
|
-
this.y *= length;
|
|
672
|
-
this.z *= length;
|
|
673
|
-
|
|
674
|
-
return this;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
/**
|
|
678
|
-
* Converts vector to a number array.
|
|
679
|
-
* @public
|
|
680
|
-
* @returns {Array.<number>} - (exactly 3 entries)
|
|
681
|
-
* @deprecated
|
|
682
|
-
*/
|
|
683
|
-
toVec() {
|
|
684
|
-
return [this.x, this.y, this.z];
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Converts vector to a number array.
|
|
689
|
-
* @public
|
|
690
|
-
* @returns {Array.<number>} - (exactly 3 entries)
|
|
691
|
-
*/
|
|
692
|
-
toArray() {
|
|
693
|
-
return [this.x, this.y, this.z];
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
/**
|
|
697
|
-
* Gets distance to point.
|
|
698
|
-
* @public
|
|
699
|
-
* @param {Vec3} point3 - Distant point.
|
|
700
|
-
* @returns {number} -
|
|
701
|
-
*/
|
|
1
|
+
/**
|
|
2
|
+
* @module og/math/Vec3
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
import { Vec4 } from "./Vec4.js";
|
|
8
|
+
import { Quat } from "./Quat.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Class represents a 3d vector.
|
|
12
|
+
* @class
|
|
13
|
+
* @param {number} [x] - First value.
|
|
14
|
+
* @param {number} [y] - Second value.
|
|
15
|
+
* @param {number} [z] - Third value.
|
|
16
|
+
*/
|
|
17
|
+
export class Vec3 {
|
|
18
|
+
constructor(x = 0.0, y = 0.0, z = 0.0) {
|
|
19
|
+
/**
|
|
20
|
+
* @public
|
|
21
|
+
* @type {number}
|
|
22
|
+
*/
|
|
23
|
+
this.x = x || 0.0;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
* @type {number}
|
|
28
|
+
*/
|
|
29
|
+
this.y = y || 0.0;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @public
|
|
33
|
+
* @type {number}
|
|
34
|
+
*/
|
|
35
|
+
this.z = z || 0.0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @const */
|
|
39
|
+
static get UP() {
|
|
40
|
+
return new Vec3(0, 1, 0);
|
|
41
|
+
}
|
|
42
|
+
/** @const */
|
|
43
|
+
static get DOWN() {
|
|
44
|
+
return new Vec3(0, -1, 0);
|
|
45
|
+
}
|
|
46
|
+
/** @const */
|
|
47
|
+
static get RIGHT() {
|
|
48
|
+
return new Vec3(1, 0, 0);
|
|
49
|
+
}
|
|
50
|
+
/** @const */
|
|
51
|
+
static get LEFT() {
|
|
52
|
+
return new Vec3(-1, 0, 0);
|
|
53
|
+
}
|
|
54
|
+
/** @const */
|
|
55
|
+
static get FORWARD() {
|
|
56
|
+
return new Vec3(0, 0, -1);
|
|
57
|
+
}
|
|
58
|
+
/** @const */
|
|
59
|
+
static get BACKWARD() {
|
|
60
|
+
return new Vec3(0, 0, 1);
|
|
61
|
+
}
|
|
62
|
+
/** @const */
|
|
63
|
+
static get ZERO() {
|
|
64
|
+
return new Vec3();
|
|
65
|
+
}
|
|
66
|
+
/** @const */
|
|
67
|
+
static get UNIT_X() {
|
|
68
|
+
return new Vec3(1, 0, 0);
|
|
69
|
+
}
|
|
70
|
+
/** @const */
|
|
71
|
+
static get UNIT_Y() {
|
|
72
|
+
return new Vec3(0, 1, 0);
|
|
73
|
+
}
|
|
74
|
+
/** @const */
|
|
75
|
+
static get UNIT_Z() {
|
|
76
|
+
return new Vec3(0, 0, 1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Separate 63 bit Vec3 to two Vec3 32 bit float values.
|
|
81
|
+
* @function
|
|
82
|
+
* @param {number} value - Double type value.
|
|
83
|
+
* @param {Vec3} high - Out vector high values.
|
|
84
|
+
* @param {Vec3} low - Out vector low values.
|
|
85
|
+
* @returns {Array.<number>} Encoded array. (exactly 2 entries)
|
|
86
|
+
*/
|
|
87
|
+
static doubleToTwoFloats(v, high, low) {
|
|
88
|
+
let x = v.x,
|
|
89
|
+
y = v.y,
|
|
90
|
+
z = v.z;
|
|
91
|
+
|
|
92
|
+
if (x >= 0.0) {
|
|
93
|
+
let doubleHigh = Math.floor(x / 65536.0) * 65536.0;
|
|
94
|
+
high.x = Math.fround(doubleHigh);
|
|
95
|
+
low.x = Math.fround(x - doubleHigh);
|
|
96
|
+
} else {
|
|
97
|
+
let doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
|
|
98
|
+
high.x = Math.fround(-doubleHigh);
|
|
99
|
+
low.x = Math.fround(x + doubleHigh);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (y >= 0.0) {
|
|
103
|
+
let doubleHigh = Math.floor(y / 65536.0) * 65536.0;
|
|
104
|
+
high.y = Math.fround(doubleHigh);
|
|
105
|
+
low.y = Math.fround(y - doubleHigh);
|
|
106
|
+
} else {
|
|
107
|
+
let doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
|
|
108
|
+
high.y = Math.fround(-doubleHigh);
|
|
109
|
+
low.y = Math.fround(y + doubleHigh);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (z >= 0.0) {
|
|
113
|
+
let doubleHigh = Math.floor(z / 65536.0) * 65536.0;
|
|
114
|
+
high.z = Math.fround(doubleHigh);
|
|
115
|
+
low.z = Math.fround(z - doubleHigh);
|
|
116
|
+
} else {
|
|
117
|
+
let doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
|
|
118
|
+
high.z = Math.fround(-doubleHigh);
|
|
119
|
+
low.z = Math.fround(z + doubleHigh);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Separate 63 bit Vec3 to two Vec3 32 bit float values.
|
|
125
|
+
* @function
|
|
126
|
+
* @param {number} value - Double type value.
|
|
127
|
+
* @param {Float32Array} high - Out vector high values.
|
|
128
|
+
* @param {Float32Array} low - Out vector low values.
|
|
129
|
+
* @returns {Array.<number>} Encoded array. (exactly 2 entries)
|
|
130
|
+
*/
|
|
131
|
+
static doubleToTwoFloat32Array(v, high, low) {
|
|
132
|
+
let x = v.x,
|
|
133
|
+
y = v.y,
|
|
134
|
+
z = v.z;
|
|
135
|
+
|
|
136
|
+
if (x >= 0.0) {
|
|
137
|
+
let doubleHigh = Math.floor(x / 65536.0) * 65536.0;
|
|
138
|
+
high[0] = Math.fround(doubleHigh);
|
|
139
|
+
low[0] = Math.fround(x - doubleHigh);
|
|
140
|
+
} else {
|
|
141
|
+
let doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
|
|
142
|
+
high[0] = Math.fround(-doubleHigh);
|
|
143
|
+
low[0] = Math.fround(x + doubleHigh);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (y >= 0.0) {
|
|
147
|
+
let doubleHigh = Math.floor(y / 65536.0) * 65536.0;
|
|
148
|
+
high[1] = Math.fround(doubleHigh);
|
|
149
|
+
low[1] = Math.fround(y - doubleHigh);
|
|
150
|
+
} else {
|
|
151
|
+
let doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
|
|
152
|
+
high[1] = Math.fround(-doubleHigh);
|
|
153
|
+
low[1] = Math.fround(y + doubleHigh);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (z >= 0.0) {
|
|
157
|
+
let doubleHigh = Math.floor(z / 65536.0) * 65536.0;
|
|
158
|
+
high[2] = Math.fround(doubleHigh);
|
|
159
|
+
low[2] = Math.fround(z - doubleHigh);
|
|
160
|
+
} else {
|
|
161
|
+
let doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
|
|
162
|
+
high[2] = Math.fround(-doubleHigh);
|
|
163
|
+
low[2] = Math.fround(z + doubleHigh);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates 3d vector from array.
|
|
169
|
+
* @function
|
|
170
|
+
* @param {Array.<number>} arr - Input array (exactly 3 entries)
|
|
171
|
+
* @returns {Vec3} -
|
|
172
|
+
*/
|
|
173
|
+
static fromVec(arr) {
|
|
174
|
+
return new Vec3(arr[0], arr[1], arr[2]);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Gets angle between two vectors.
|
|
179
|
+
* @static
|
|
180
|
+
* @param {Vec3} a - First vector.
|
|
181
|
+
* @param {Vec3} b - Second vector.
|
|
182
|
+
* @returns {number} -
|
|
183
|
+
*/
|
|
184
|
+
static angle(a, b) {
|
|
185
|
+
return Math.acos(a.dot(b) / Math.sqrt(a.length2() * b.length2()));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Returns two vectors linear interpolation.
|
|
190
|
+
* @static
|
|
191
|
+
* @param {Vec3} v1 - Start vector.
|
|
192
|
+
* @param {Vec3} v2 - End vector.
|
|
193
|
+
* @param {number} l - Interpolate value.
|
|
194
|
+
* @returns {Vec3} -
|
|
195
|
+
*/
|
|
196
|
+
static lerp(v1, v2, l) {
|
|
197
|
+
return Vec3(v1.x + (v2.x - v1.x) * l, v1.y + (v2.y - v1.y) * l, v1.z + (v2.z - v1.z) * l);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Returns summary vector.
|
|
202
|
+
* @static
|
|
203
|
+
* @param {Vec3} a - First vector.
|
|
204
|
+
* @param {Vec3} b - Second vector.
|
|
205
|
+
* @returns {Vec3} - Summary vector.
|
|
206
|
+
*/
|
|
207
|
+
static add(a, b) {
|
|
208
|
+
var res = new Vec3(a.x, a.y, a.z);
|
|
209
|
+
res.addA(b);
|
|
210
|
+
return res;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Returns two vectors subtraction.
|
|
215
|
+
* @static
|
|
216
|
+
* @param {Vec3} a - First vector.
|
|
217
|
+
* @param {Vec3} b - Second vector.
|
|
218
|
+
* @returns {Vec3} - Vectors subtraction.
|
|
219
|
+
*/
|
|
220
|
+
static sub(a, b) {
|
|
221
|
+
var res = new Vec3(a.x, a.y, a.z);
|
|
222
|
+
res.subA(b);
|
|
223
|
+
return res;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Returns scaled vector.
|
|
228
|
+
* @static
|
|
229
|
+
* @param {Vec3} a - Input vector.
|
|
230
|
+
* @param {number} scale - Scale value.
|
|
231
|
+
* @returns {Vec3} -
|
|
232
|
+
*/
|
|
233
|
+
static scale(a, scale) {
|
|
234
|
+
var res = new Vec3(a.x, a.y, a.z);
|
|
235
|
+
res.scale(scale);
|
|
236
|
+
return res;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Returns two vectors production.
|
|
241
|
+
* @static
|
|
242
|
+
* @param {Vec3} a - First vector.
|
|
243
|
+
* @param {Vec3} b - Second vector.
|
|
244
|
+
* @returns {Vec3} -
|
|
245
|
+
*/
|
|
246
|
+
static mul(a, b) {
|
|
247
|
+
var res = new Vec3(a.x, a.y, a.z);
|
|
248
|
+
res.mulA(b);
|
|
249
|
+
return res;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Returns true if two vectors are non collinear.
|
|
254
|
+
* @public
|
|
255
|
+
* @param {Vec3} a - First vector.
|
|
256
|
+
* @param {Vec3} b - Second vector.
|
|
257
|
+
* @returns {Vec3} -
|
|
258
|
+
*/
|
|
259
|
+
static noncollinear(a, b) {
|
|
260
|
+
return a.y * b.z - a.z * b.y || a.z * b.x - a.x * b.z || a.x * b.y - a.y * b.z;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get projection of the vector to plane where n - normal to the plane.
|
|
265
|
+
* @static
|
|
266
|
+
* @param {Vec3} b - Vector to project.
|
|
267
|
+
* @param {Vec3} n - Plane normal.
|
|
268
|
+
* @param {Vec3} [def] - Default value for non existed result.
|
|
269
|
+
* @returns {Vec3} -
|
|
270
|
+
*/
|
|
271
|
+
static proj_b_to_plane(b, n, def) {
|
|
272
|
+
var res = b.sub(n.scaleTo(n.dot(b) / n.dot(n)));
|
|
273
|
+
if (def && res.isZero()) {
|
|
274
|
+
return new Vec3(def.x, def.y, def.z);
|
|
275
|
+
}
|
|
276
|
+
return res;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Get projection of the first vector to the second.
|
|
281
|
+
* @static
|
|
282
|
+
* @param {Vec3} b - First vector.
|
|
283
|
+
* @param {Vec3} a - Second vector.
|
|
284
|
+
* @returns {Vec3} -
|
|
285
|
+
*/
|
|
286
|
+
static proj_b_to_a(b, a) {
|
|
287
|
+
return a.scaleTo(a.dot(b) / a.dot(a));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Makes vectors normalized and orthogonal to each other.
|
|
292
|
+
* Normalizes normal. Normalizes tangent and makes sure it is orthogonal to normal (that is, angle between them is 90 degrees).
|
|
293
|
+
* @static
|
|
294
|
+
* @param {Vec3} normal - Normal vector.
|
|
295
|
+
* @param {Vec3} tangent - Tangent vector.
|
|
296
|
+
* @returns {Vec3} -
|
|
297
|
+
*/
|
|
298
|
+
static orthoNormalize(normal, tangent) {
|
|
299
|
+
normal = normal.normal();
|
|
300
|
+
normal.scale(tangent.dot(normal));
|
|
301
|
+
return tangent.subA(normal).normalize();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Returns vector components division product one to another.
|
|
306
|
+
* @static
|
|
307
|
+
* @param {Vec3} a - First vector.
|
|
308
|
+
* @param {Vec3} b - Second vector.
|
|
309
|
+
* @returns {Vec3} -
|
|
310
|
+
*/
|
|
311
|
+
static div(a, b) {
|
|
312
|
+
var res = new Vec3(a.x, a.y, a.z);
|
|
313
|
+
res.divA(b);
|
|
314
|
+
return res;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Converts to 4d vector, Fourth value is 1.0.
|
|
319
|
+
* @public
|
|
320
|
+
* @returns {Vec4} -
|
|
321
|
+
*/
|
|
322
|
+
toVec4() {
|
|
323
|
+
return new Vec4(this.x, this.y, this.z, 1.0);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Returns clone vector.
|
|
328
|
+
* @public
|
|
329
|
+
* @returns {Vec3} -
|
|
330
|
+
*/
|
|
331
|
+
clone() {
|
|
332
|
+
return new Vec3(this.x, this.y, this.z);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Converts vector to text string.
|
|
337
|
+
* @public
|
|
338
|
+
* @returns {string} -
|
|
339
|
+
*/
|
|
340
|
+
toString() {
|
|
341
|
+
return "(" + this.x + "," + this.y + "," + this.z + ")";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Returns true if vector's values are zero.
|
|
346
|
+
* @public
|
|
347
|
+
* @returns {boolean} -
|
|
348
|
+
*/
|
|
349
|
+
isZero() {
|
|
350
|
+
return !(this.x || this.y || this.z);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Get projection of the first vector to the second.
|
|
355
|
+
* @static
|
|
356
|
+
* @param {Vec3} a - Project vector.
|
|
357
|
+
* @returns {Vec3} -
|
|
358
|
+
*/
|
|
359
|
+
projToVec(a) {
|
|
360
|
+
return a.scaleTo(a.dot(this) / a.dot(a));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Compares with vector. Returns true if it equals another.
|
|
365
|
+
* @public
|
|
366
|
+
* @param {Vec3} p - Vector to compare.
|
|
367
|
+
* @returns {boolean} -
|
|
368
|
+
*/
|
|
369
|
+
equal(p) {
|
|
370
|
+
return this.x === p.x && this.y === p.y && this.z === p.z;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Copy input vector's values.
|
|
375
|
+
* @param {Vec3} point3 - Vector to copy.
|
|
376
|
+
* @returns {Vec3} -
|
|
377
|
+
*/
|
|
378
|
+
copy(point3) {
|
|
379
|
+
this.x = point3.x;
|
|
380
|
+
this.y = point3.y;
|
|
381
|
+
this.z = point3.z;
|
|
382
|
+
return this;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Gets vector's length.
|
|
387
|
+
* @public
|
|
388
|
+
* @returns {number} -
|
|
389
|
+
*/
|
|
390
|
+
length() {
|
|
391
|
+
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Returns squared vector's length.
|
|
396
|
+
* @public
|
|
397
|
+
* @returns {number} -
|
|
398
|
+
*/
|
|
399
|
+
length2() {
|
|
400
|
+
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Converts vector's values to a quaternion object.
|
|
405
|
+
* @public
|
|
406
|
+
* @returns {Quat} -
|
|
407
|
+
*/
|
|
408
|
+
getQuat() {
|
|
409
|
+
return new Quat(this.x, this.y, this.z);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Adds vector to the current.
|
|
414
|
+
* @public
|
|
415
|
+
* @param {Vec3} point3 - Point to add.
|
|
416
|
+
* @returns {Vec3} -
|
|
417
|
+
*/
|
|
418
|
+
addA(point3) {
|
|
419
|
+
this.x += point3.x;
|
|
420
|
+
this.y += point3.y;
|
|
421
|
+
this.z += point3.z;
|
|
422
|
+
return this;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Gets two vectors summarization.
|
|
427
|
+
* @public
|
|
428
|
+
* @param {Vec3} point3 - Vector to add.
|
|
429
|
+
* @returns {Vec3} Returns a sum vector.
|
|
430
|
+
*/
|
|
431
|
+
add(point3) {
|
|
432
|
+
return new Vec3(this.x + point3.x, this.y + point3.y, this.z + point3.z);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Subtract vector from the current.
|
|
437
|
+
* @public
|
|
438
|
+
* @param {Vec3} point3 - Subtract vector.
|
|
439
|
+
* @returns {Vec3} -
|
|
440
|
+
*/
|
|
441
|
+
subA(point3) {
|
|
442
|
+
this.x -= point3.x;
|
|
443
|
+
this.y -= point3.y;
|
|
444
|
+
this.z -= point3.z;
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Gets vector subtraction.
|
|
450
|
+
* @public
|
|
451
|
+
* @param {Vec3} point3 - Subtract vector.
|
|
452
|
+
* @return {Vec3} Returns new instance of a subtraction
|
|
453
|
+
*/
|
|
454
|
+
sub(point3) {
|
|
455
|
+
return new Vec3(this.x - point3.x, this.y - point3.y, this.z - point3.z);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Scale current vector.
|
|
460
|
+
* @public
|
|
461
|
+
* @param {number} scale - Scale value.
|
|
462
|
+
* @returns {Vec3} -
|
|
463
|
+
*/
|
|
464
|
+
scale(scale) {
|
|
465
|
+
this.x *= scale;
|
|
466
|
+
this.y *= scale;
|
|
467
|
+
this.z *= scale;
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Scale current vector to another instance.
|
|
473
|
+
* @public
|
|
474
|
+
* @param {number} scale - Scale value.
|
|
475
|
+
* @returns {Vec3} -
|
|
476
|
+
*/
|
|
477
|
+
scaleTo(scale) {
|
|
478
|
+
return new Vec3(this.x * scale, this.y * scale, this.z * scale);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Multiply current vector object to another and store result in the current instance.
|
|
483
|
+
* @public
|
|
484
|
+
* @param {Vec3} vec - Multiply vector.
|
|
485
|
+
* @returns {Vec3} -
|
|
486
|
+
*/
|
|
487
|
+
mulA(vec) {
|
|
488
|
+
this.x *= vec.x;
|
|
489
|
+
this.y *= vec.y;
|
|
490
|
+
this.z *= vec.z;
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Multiply current vector object to another and returns new vector instance.
|
|
496
|
+
* @public
|
|
497
|
+
* @param {Vec3} vec - Multiply vector.
|
|
498
|
+
* @returns {Vec3} -
|
|
499
|
+
*/
|
|
500
|
+
mul(vec) {
|
|
501
|
+
return new Vec3(this.x * vec.x, this.y * vec.y, this.z * vec.z);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Divide current vector's components to another. Results stores in the current vector object.
|
|
506
|
+
* @public
|
|
507
|
+
* @param {Vec3} vec - Div vector.
|
|
508
|
+
* @returns {Vec3} -
|
|
509
|
+
*/
|
|
510
|
+
divA(vec) {
|
|
511
|
+
this.x /= vec.x;
|
|
512
|
+
this.y /= vec.y;
|
|
513
|
+
this.z /= vec.z;
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Divide current vector's components to another and returns new vector instance.
|
|
519
|
+
* @public
|
|
520
|
+
* @param {Vec3} vec - Div vector.
|
|
521
|
+
* @returns {Vec3} -
|
|
522
|
+
*/
|
|
523
|
+
div(vec) {
|
|
524
|
+
return new Vec3(this.x / vec.x, this.y / vec.y, this.z / vec.z);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Gets vectors dot production.
|
|
529
|
+
* @public
|
|
530
|
+
* @param {Vec3} point3 - Another vector.
|
|
531
|
+
* @returns {number} -
|
|
532
|
+
*/
|
|
533
|
+
dot(point3) {
|
|
534
|
+
return point3.x * this.x + point3.y * this.y + point3.z * this.z;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Gets vectors dot production.
|
|
539
|
+
* @public
|
|
540
|
+
* @param {Array.<number>} arr - Array vector. (exactly 3 entries)
|
|
541
|
+
* @returns {number} -
|
|
542
|
+
*/
|
|
543
|
+
dotArr(arr) {
|
|
544
|
+
return arr[0] * this.x + arr[1] * this.y + arr[2] * this.z;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Gets vectors cross production.
|
|
549
|
+
* @public
|
|
550
|
+
* @param {Vec3} point3 - Another vector.
|
|
551
|
+
* @returns {Vec3} -
|
|
552
|
+
*/
|
|
553
|
+
cross(point3) {
|
|
554
|
+
return new Vec3(
|
|
555
|
+
this.y * point3.z - this.z * point3.y,
|
|
556
|
+
this.z * point3.x - this.x * point3.z,
|
|
557
|
+
this.x * point3.y - this.y * point3.x
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Sets vector to zero.
|
|
563
|
+
* @public
|
|
564
|
+
* @returns {Vec3} -
|
|
565
|
+
*/
|
|
566
|
+
clear() {
|
|
567
|
+
this.x = this.y = this.z = 0;
|
|
568
|
+
return this;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Returns normalized vector.
|
|
573
|
+
* @public
|
|
574
|
+
* @returns {Vec3} -
|
|
575
|
+
*/
|
|
576
|
+
getNormal() {
|
|
577
|
+
var res = new Vec3();
|
|
578
|
+
res.copy(this);
|
|
579
|
+
|
|
580
|
+
var length = 1.0 / res.length();
|
|
581
|
+
|
|
582
|
+
res.x *= length;
|
|
583
|
+
res.y *= length;
|
|
584
|
+
res.z *= length;
|
|
585
|
+
|
|
586
|
+
return res;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Returns normalized vector.
|
|
591
|
+
* @deprecated
|
|
592
|
+
* @public
|
|
593
|
+
* @returns {Vec3} -
|
|
594
|
+
*/
|
|
595
|
+
normal() {
|
|
596
|
+
var res = new Vec3();
|
|
597
|
+
res.copy(this);
|
|
598
|
+
|
|
599
|
+
var length = 1.0 / res.length();
|
|
600
|
+
|
|
601
|
+
res.x *= length;
|
|
602
|
+
res.y *= length;
|
|
603
|
+
res.z *= length;
|
|
604
|
+
|
|
605
|
+
return res;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Returns normalized negate vector.
|
|
610
|
+
* @public
|
|
611
|
+
* @returns {Vec3} -
|
|
612
|
+
*/
|
|
613
|
+
normalNegate() {
|
|
614
|
+
var res = new Vec3();
|
|
615
|
+
res.copy(this);
|
|
616
|
+
|
|
617
|
+
var length = -1.0 / res.length();
|
|
618
|
+
|
|
619
|
+
res.x *= length;
|
|
620
|
+
res.y *= length;
|
|
621
|
+
res.z *= length;
|
|
622
|
+
|
|
623
|
+
return res;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Returns normalized negate scale vector.
|
|
628
|
+
* @public
|
|
629
|
+
* @returns {Vec3} -
|
|
630
|
+
*/
|
|
631
|
+
normalNegateScale(scale) {
|
|
632
|
+
var res = new Vec3();
|
|
633
|
+
res.copy(this);
|
|
634
|
+
|
|
635
|
+
var length = -scale / res.length();
|
|
636
|
+
|
|
637
|
+
res.x *= length;
|
|
638
|
+
res.y *= length;
|
|
639
|
+
res.z *= length;
|
|
640
|
+
|
|
641
|
+
return res;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Returns normalized scale vector.
|
|
646
|
+
* @public
|
|
647
|
+
* @returns {Vec3} -
|
|
648
|
+
*/
|
|
649
|
+
normalScale(scale) {
|
|
650
|
+
var res = new Vec3();
|
|
651
|
+
res.copy(this);
|
|
652
|
+
|
|
653
|
+
var length = scale / res.length();
|
|
654
|
+
|
|
655
|
+
res.x *= length;
|
|
656
|
+
res.y *= length;
|
|
657
|
+
res.z *= length;
|
|
658
|
+
|
|
659
|
+
return res;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Normalize current vector.
|
|
664
|
+
* @public
|
|
665
|
+
* @returns {Vec3} -
|
|
666
|
+
*/
|
|
667
|
+
normalize() {
|
|
668
|
+
var length = 1.0 / this.length();
|
|
669
|
+
|
|
670
|
+
this.x *= length;
|
|
671
|
+
this.y *= length;
|
|
672
|
+
this.z *= length;
|
|
673
|
+
|
|
674
|
+
return this;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Converts vector to a number array.
|
|
679
|
+
* @public
|
|
680
|
+
* @returns {Array.<number>} - (exactly 3 entries)
|
|
681
|
+
* @deprecated
|
|
682
|
+
*/
|
|
683
|
+
toVec() {
|
|
684
|
+
return [this.x, this.y, this.z];
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Converts vector to a number array.
|
|
689
|
+
* @public
|
|
690
|
+
* @returns {Array.<number>} - (exactly 3 entries)
|
|
691
|
+
*/
|
|
692
|
+
toArray() {
|
|
693
|
+
return [this.x, this.y, this.z];
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Gets distance to point.
|
|
698
|
+
* @public
|
|
699
|
+
* @param {Vec3} point3 - Distant point.
|
|
700
|
+
* @returns {number} -
|
|
701
|
+
*/
|
|
702
702
|
distance(point3) {
|
|
703
703
|
let dx = this.x - point3.x,
|
|
704
704
|
dy = this.y - point3.y,
|
|
705
705
|
dz = this.z - point3.z;
|
|
706
|
-
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
706
|
+
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
707
707
|
}
|
|
708
708
|
|
|
709
|
-
/**
|
|
710
|
-
* Gets square distance to point.
|
|
711
|
-
* @public
|
|
712
|
-
* @param {Vec3} point3 - Distant point.
|
|
713
|
-
* @returns {number} -
|
|
714
|
-
*/
|
|
709
|
+
/**
|
|
710
|
+
* Gets square distance to point.
|
|
711
|
+
* @public
|
|
712
|
+
* @param {Vec3} point3 - Distant point.
|
|
713
|
+
* @returns {number} -
|
|
714
|
+
*/
|
|
715
715
|
distance2(point3) {
|
|
716
716
|
let dx = this.x - point3.x,
|
|
717
717
|
dy = this.y - point3.y,
|
|
718
718
|
dz = this.z - point3.z;
|
|
719
719
|
return dx * dx + dy * dy + dz * dz;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
/**
|
|
723
|
-
* Sets vector's values.
|
|
724
|
-
* @public
|
|
725
|
-
* @param {number} x - Value X.
|
|
726
|
-
* @param {number} y - Value Y.
|
|
727
|
-
* @param {number} z - Value Z.
|
|
728
|
-
* @returns {Vec3} -
|
|
729
|
-
*/
|
|
730
|
-
set(x, y, z) {
|
|
731
|
-
this.x = x;
|
|
732
|
-
this.y = y;
|
|
733
|
-
this.z = z;
|
|
734
|
-
return this;
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
/**
|
|
738
|
-
* Negate current vector.
|
|
739
|
-
* @public
|
|
740
|
-
* @returns {Vec3} -
|
|
741
|
-
*/
|
|
742
|
-
negate() {
|
|
743
|
-
this.x = -this.x;
|
|
744
|
-
this.y = -this.y;
|
|
745
|
-
this.z = -this.z;
|
|
746
|
-
return this;
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
* Negate current vector to another instance.
|
|
751
|
-
* @public
|
|
752
|
-
* @returns {Vec3} -
|
|
753
|
-
*/
|
|
754
|
-
negateTo() {
|
|
755
|
-
return new Vec3(-this.x, -this.y, -this.z);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
/**
|
|
759
|
-
* Gets projected point coordinates of the current vector on the ray.
|
|
760
|
-
* @public
|
|
761
|
-
* @param {Vec3} pos - Ray position.
|
|
762
|
-
* @param {Vec3} direction - Ray direction.
|
|
763
|
-
* @returns {Vec3} -
|
|
764
|
-
*/
|
|
765
|
-
projToRay(pos, direction) {
|
|
766
|
-
var v = Vec3.proj_b_to_a(Vec3.sub(this, pos), direction);
|
|
767
|
-
v.addA(pos);
|
|
768
|
-
return v;
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
/**
|
|
772
|
-
* Gets angle between two vectors.
|
|
773
|
-
* @public
|
|
774
|
-
* @param {Vec3} a - Another vector.
|
|
775
|
-
* @returns {number} -
|
|
776
|
-
*/
|
|
777
|
-
angle(a) {
|
|
778
|
-
return Vec3.angle(this, a);
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
* Returns two vectors linear interpolation.
|
|
783
|
-
* @public
|
|
784
|
-
* @param {Vec3} v2 - End vector.
|
|
785
|
-
* @param {number} l - Interpolate value.
|
|
786
|
-
* @returns {Vec3} -
|
|
787
|
-
*/
|
|
788
|
-
lerp(v2, l) {
|
|
789
|
-
return new Vec3(
|
|
790
|
-
this.x + (v2.x - this.x) * l,
|
|
791
|
-
this.y + (v2.y - this.y) * l,
|
|
792
|
-
this.z + (v2.z - this.z) * l
|
|
793
|
-
);
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
/**
|
|
797
|
-
* Returns vector interpolation by v(t) = v1 * t + v2 * (1 - t)
|
|
798
|
-
* @public
|
|
799
|
-
* @param {Vec3} v2 - End vector.
|
|
800
|
-
* @param {number} t - Interpolate value.
|
|
801
|
-
* @returns {Vec3} -
|
|
802
|
-
*/
|
|
803
|
-
smerp(v2, t) {
|
|
804
|
-
var one_d = 1 - t;
|
|
805
|
-
return new Vec3(
|
|
806
|
-
this.x * t + v2.x * one_d,
|
|
807
|
-
this.y * t + v2.y * one_d,
|
|
808
|
-
this.z * t + v2.z * one_d
|
|
809
|
-
);
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
static get LERP_DELTA() {
|
|
813
|
-
return 1e-6;
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
/**
|
|
817
|
-
* Spherically interpolates between two vectors.
|
|
818
|
-
* Interpolates between current and v2 vector by amount t. The difference between this and linear interpolation (aka, "lerp") is that
|
|
819
|
-
* the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated
|
|
820
|
-
* by the angle and its magnitude is interpolated between the magnitudes of from and to.
|
|
821
|
-
* @public
|
|
822
|
-
* @param {Vec3} v2 -
|
|
823
|
-
* @param {number} t - The parameter t is clamped to the range [0, 1].
|
|
824
|
-
* @returns {Vec3} -
|
|
825
|
-
*/
|
|
826
|
-
slerp(v2, t) {
|
|
827
|
-
var res = new Vec3();
|
|
828
|
-
|
|
829
|
-
if (t <= 0.0) {
|
|
830
|
-
res.copy(this);
|
|
831
|
-
return;
|
|
832
|
-
} else if (t >= 1.0) {
|
|
833
|
-
res.copy(v2);
|
|
834
|
-
return;
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
var omega, sinom, scale0, scale1;
|
|
838
|
-
var cosom = this.dot(v2);
|
|
839
|
-
|
|
840
|
-
if (1.0 - cosom > Vec3.LERP_DELTA) {
|
|
841
|
-
omega = Math.acos(cosom);
|
|
842
|
-
sinom = Math.sin(omega);
|
|
843
|
-
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
844
|
-
scale1 = Math.sin(t * omega) / sinom;
|
|
845
|
-
} else {
|
|
846
|
-
scale0 = 1.0 - t;
|
|
847
|
-
scale1 = t;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
return Vec3.add(this.scaleTo(scale0), v2.scale(scale1));
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
/**
|
|
854
|
-
* Gets the shortest arc quaternion to rotate this vector to the destination vector.
|
|
855
|
-
* @param {Vec3} dest -
|
|
856
|
-
* @param {Vec3} fallbackAxis -
|
|
857
|
-
* @returns {Quat} -
|
|
858
|
-
* @todo: TEST IT!
|
|
859
|
-
*/
|
|
860
|
-
getRotationTo(dest, fallbackAxis) {
|
|
861
|
-
// Based on Stan Melax's article in Game Programming Gems
|
|
862
|
-
// Copy, since cannot modify local
|
|
863
|
-
let v0 = this.clone();
|
|
864
|
-
let v1 = dest.clone();
|
|
865
|
-
v0.normalize();
|
|
866
|
-
v1.normalize();
|
|
867
|
-
|
|
868
|
-
let d = v0.dot(v1);
|
|
869
|
-
// If dot == 1, vectors are the same
|
|
870
|
-
if (d >= 1.0) {
|
|
871
|
-
return Quat.IDENTITY.clone();
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
if (d < 1e-6 - 1.0) {
|
|
875
|
-
if (!fallbackAxis.isEqual(Vec3.ZERO)) {
|
|
876
|
-
// rotate 180 degrees about the fallback axis
|
|
877
|
-
return Quat.axisAngleToQuat(Math.PI, fallbackAxis);
|
|
878
|
-
} else {
|
|
879
|
-
// Generate an axis
|
|
880
|
-
let axis = Vec3.UNIT_X.cross(v0);
|
|
881
|
-
if (axis.isZero()) {
|
|
882
|
-
// pick another if colinear
|
|
883
|
-
axis = Vec3.UNIT_Y.cross(v0);
|
|
884
|
-
}
|
|
885
|
-
axis.normalize();
|
|
886
|
-
return Quat.axisAngleToQuat(Math.PI, axis);
|
|
887
|
-
}
|
|
888
|
-
} else {
|
|
889
|
-
let s = Math.sqrt((1 + d) * 2);
|
|
890
|
-
let invs = 1.0 / s;
|
|
891
|
-
|
|
892
|
-
let c = v0.cross(v1);
|
|
893
|
-
|
|
894
|
-
let q = new Quat(c.x * invs, c.y * invs, c.z * invs, s * 0.5);
|
|
895
|
-
q.normalise();
|
|
896
|
-
return q;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
/**
|
|
902
|
-
* Vector 3d object creator.
|
|
903
|
-
* @function
|
|
904
|
-
* @param {number} [x] - First cvalue.
|
|
905
|
-
* @param {number} [y] - Second value.
|
|
906
|
-
* @param {number} [z] - Third value.
|
|
907
|
-
* @returns {Vec3} -
|
|
908
|
-
*/
|
|
909
|
-
export function vec3(x, y, z) {
|
|
910
|
-
return new Vec3(x, y, z);
|
|
911
|
-
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Sets vector's values.
|
|
724
|
+
* @public
|
|
725
|
+
* @param {number} x - Value X.
|
|
726
|
+
* @param {number} y - Value Y.
|
|
727
|
+
* @param {number} z - Value Z.
|
|
728
|
+
* @returns {Vec3} -
|
|
729
|
+
*/
|
|
730
|
+
set(x, y, z) {
|
|
731
|
+
this.x = x;
|
|
732
|
+
this.y = y;
|
|
733
|
+
this.z = z;
|
|
734
|
+
return this;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Negate current vector.
|
|
739
|
+
* @public
|
|
740
|
+
* @returns {Vec3} -
|
|
741
|
+
*/
|
|
742
|
+
negate() {
|
|
743
|
+
this.x = -this.x;
|
|
744
|
+
this.y = -this.y;
|
|
745
|
+
this.z = -this.z;
|
|
746
|
+
return this;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Negate current vector to another instance.
|
|
751
|
+
* @public
|
|
752
|
+
* @returns {Vec3} -
|
|
753
|
+
*/
|
|
754
|
+
negateTo() {
|
|
755
|
+
return new Vec3(-this.x, -this.y, -this.z);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Gets projected point coordinates of the current vector on the ray.
|
|
760
|
+
* @public
|
|
761
|
+
* @param {Vec3} pos - Ray position.
|
|
762
|
+
* @param {Vec3} direction - Ray direction.
|
|
763
|
+
* @returns {Vec3} -
|
|
764
|
+
*/
|
|
765
|
+
projToRay(pos, direction) {
|
|
766
|
+
var v = Vec3.proj_b_to_a(Vec3.sub(this, pos), direction);
|
|
767
|
+
v.addA(pos);
|
|
768
|
+
return v;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Gets angle between two vectors.
|
|
773
|
+
* @public
|
|
774
|
+
* @param {Vec3} a - Another vector.
|
|
775
|
+
* @returns {number} -
|
|
776
|
+
*/
|
|
777
|
+
angle(a) {
|
|
778
|
+
return Vec3.angle(this, a);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Returns two vectors linear interpolation.
|
|
783
|
+
* @public
|
|
784
|
+
* @param {Vec3} v2 - End vector.
|
|
785
|
+
* @param {number} l - Interpolate value.
|
|
786
|
+
* @returns {Vec3} -
|
|
787
|
+
*/
|
|
788
|
+
lerp(v2, l) {
|
|
789
|
+
return new Vec3(
|
|
790
|
+
this.x + (v2.x - this.x) * l,
|
|
791
|
+
this.y + (v2.y - this.y) * l,
|
|
792
|
+
this.z + (v2.z - this.z) * l
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* Returns vector interpolation by v(t) = v1 * t + v2 * (1 - t)
|
|
798
|
+
* @public
|
|
799
|
+
* @param {Vec3} v2 - End vector.
|
|
800
|
+
* @param {number} t - Interpolate value.
|
|
801
|
+
* @returns {Vec3} -
|
|
802
|
+
*/
|
|
803
|
+
smerp(v2, t) {
|
|
804
|
+
var one_d = 1 - t;
|
|
805
|
+
return new Vec3(
|
|
806
|
+
this.x * t + v2.x * one_d,
|
|
807
|
+
this.y * t + v2.y * one_d,
|
|
808
|
+
this.z * t + v2.z * one_d
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
static get LERP_DELTA() {
|
|
813
|
+
return 1e-6;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Spherically interpolates between two vectors.
|
|
818
|
+
* Interpolates between current and v2 vector by amount t. The difference between this and linear interpolation (aka, "lerp") is that
|
|
819
|
+
* the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated
|
|
820
|
+
* by the angle and its magnitude is interpolated between the magnitudes of from and to.
|
|
821
|
+
* @public
|
|
822
|
+
* @param {Vec3} v2 -
|
|
823
|
+
* @param {number} t - The parameter t is clamped to the range [0, 1].
|
|
824
|
+
* @returns {Vec3} -
|
|
825
|
+
*/
|
|
826
|
+
slerp(v2, t) {
|
|
827
|
+
var res = new Vec3();
|
|
828
|
+
|
|
829
|
+
if (t <= 0.0) {
|
|
830
|
+
res.copy(this);
|
|
831
|
+
return;
|
|
832
|
+
} else if (t >= 1.0) {
|
|
833
|
+
res.copy(v2);
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
var omega, sinom, scale0, scale1;
|
|
838
|
+
var cosom = this.dot(v2);
|
|
839
|
+
|
|
840
|
+
if (1.0 - cosom > Vec3.LERP_DELTA) {
|
|
841
|
+
omega = Math.acos(cosom);
|
|
842
|
+
sinom = Math.sin(omega);
|
|
843
|
+
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
844
|
+
scale1 = Math.sin(t * omega) / sinom;
|
|
845
|
+
} else {
|
|
846
|
+
scale0 = 1.0 - t;
|
|
847
|
+
scale1 = t;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
return Vec3.add(this.scaleTo(scale0), v2.scale(scale1));
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* Gets the shortest arc quaternion to rotate this vector to the destination vector.
|
|
855
|
+
* @param {Vec3} dest -
|
|
856
|
+
* @param {Vec3} fallbackAxis -
|
|
857
|
+
* @returns {Quat} -
|
|
858
|
+
* @todo: TEST IT!
|
|
859
|
+
*/
|
|
860
|
+
getRotationTo(dest, fallbackAxis) {
|
|
861
|
+
// Based on Stan Melax's article in Game Programming Gems
|
|
862
|
+
// Copy, since cannot modify local
|
|
863
|
+
let v0 = this.clone();
|
|
864
|
+
let v1 = dest.clone();
|
|
865
|
+
v0.normalize();
|
|
866
|
+
v1.normalize();
|
|
867
|
+
|
|
868
|
+
let d = v0.dot(v1);
|
|
869
|
+
// If dot == 1, vectors are the same
|
|
870
|
+
if (d >= 1.0) {
|
|
871
|
+
return Quat.IDENTITY.clone();
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
if (d < 1e-6 - 1.0) {
|
|
875
|
+
if (!fallbackAxis.isEqual(Vec3.ZERO)) {
|
|
876
|
+
// rotate 180 degrees about the fallback axis
|
|
877
|
+
return Quat.axisAngleToQuat(Math.PI, fallbackAxis);
|
|
878
|
+
} else {
|
|
879
|
+
// Generate an axis
|
|
880
|
+
let axis = Vec3.UNIT_X.cross(v0);
|
|
881
|
+
if (axis.isZero()) {
|
|
882
|
+
// pick another if colinear
|
|
883
|
+
axis = Vec3.UNIT_Y.cross(v0);
|
|
884
|
+
}
|
|
885
|
+
axis.normalize();
|
|
886
|
+
return Quat.axisAngleToQuat(Math.PI, axis);
|
|
887
|
+
}
|
|
888
|
+
} else {
|
|
889
|
+
let s = Math.sqrt((1 + d) * 2);
|
|
890
|
+
let invs = 1.0 / s;
|
|
891
|
+
|
|
892
|
+
let c = v0.cross(v1);
|
|
893
|
+
|
|
894
|
+
let q = new Quat(c.x * invs, c.y * invs, c.z * invs, s * 0.5);
|
|
895
|
+
q.normalise();
|
|
896
|
+
return q;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* Vector 3d object creator.
|
|
903
|
+
* @function
|
|
904
|
+
* @param {number} [x] - First cvalue.
|
|
905
|
+
* @param {number} [y] - Second value.
|
|
906
|
+
* @param {number} [z] - Third value.
|
|
907
|
+
* @returns {Vec3} -
|
|
908
|
+
*/
|
|
909
|
+
export function vec3(x, y, z) {
|
|
910
|
+
return new Vec3(x, y, z);
|
|
911
|
+
}
|