action-engine-js 1.0.0
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/LICENSE +45 -0
- package/README.md +348 -0
- package/actionengine/3rdparty/goblin/goblin.js +9609 -0
- package/actionengine/3rdparty/goblin/goblin.min.js +5 -0
- package/actionengine/camera/actioncamera.js +90 -0
- package/actionengine/camera/cameracollisionhandler.js +69 -0
- package/actionengine/character/actioncharacter.js +360 -0
- package/actionengine/character/actioncharacter3D.js +61 -0
- package/actionengine/core/app.js +430 -0
- package/actionengine/debug/basedebugpanel.js +858 -0
- package/actionengine/display/canvasmanager.js +75 -0
- package/actionengine/display/gl/programmanager.js +570 -0
- package/actionengine/display/gl/shaders/lineshader.js +118 -0
- package/actionengine/display/gl/shaders/objectshader.js +1756 -0
- package/actionengine/display/gl/shaders/particleshader.js +43 -0
- package/actionengine/display/gl/shaders/shadowshader.js +319 -0
- package/actionengine/display/gl/shaders/spriteshader.js +100 -0
- package/actionengine/display/gl/shaders/watershader.js +67 -0
- package/actionengine/display/graphics/actionmodel3D.js +191 -0
- package/actionengine/display/graphics/actionsprite3D.js +230 -0
- package/actionengine/display/graphics/lighting/actiondirectionalshadowlight.js +864 -0
- package/actionengine/display/graphics/lighting/actionlight.js +211 -0
- package/actionengine/display/graphics/lighting/actionomnidirectionalshadowlight.js +862 -0
- package/actionengine/display/graphics/lighting/lightingconstants.js +263 -0
- package/actionengine/display/graphics/lighting/lightmanager.js +789 -0
- package/actionengine/display/graphics/renderableobject.js +44 -0
- package/actionengine/display/graphics/renderers/actionrenderer2D.js +341 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/actionrenderer3D.js +655 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/canvasmanager3D.js +82 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/debugrenderer3D.js +493 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/objectrenderer3D.js +790 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/spriteRenderer3D.js +266 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/sunrenderer3D.js +140 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/waterrenderer3D.js +173 -0
- package/actionengine/display/graphics/renderers/actionrenderer3D/weatherrenderer3D.js +87 -0
- package/actionengine/display/graphics/texture/proceduraltexture.js +192 -0
- package/actionengine/display/graphics/texture/texturemanager.js +242 -0
- package/actionengine/display/graphics/texture/textureregistry.js +177 -0
- package/actionengine/input/actionscrollablearea.js +1405 -0
- package/actionengine/input/inputhandler.js +1647 -0
- package/actionengine/math/geometry/geometrybuilder.js +161 -0
- package/actionengine/math/geometry/glbexporter.js +364 -0
- package/actionengine/math/geometry/glbloader.js +722 -0
- package/actionengine/math/geometry/modelcodegenerator.js +97 -0
- package/actionengine/math/geometry/triangle.js +33 -0
- package/actionengine/math/geometry/triangleutils.js +34 -0
- package/actionengine/math/mathutils.js +25 -0
- package/actionengine/math/matrix4.js +785 -0
- package/actionengine/math/physics/actionphysics.js +108 -0
- package/actionengine/math/physics/actionphysicsobject3D.js +164 -0
- package/actionengine/math/physics/actionphysicsworld3D.js +238 -0
- package/actionengine/math/physics/actionraycast.js +129 -0
- package/actionengine/math/physics/shapes/actionphysicsbox3D.js +158 -0
- package/actionengine/math/physics/shapes/actionphysicscapsule3D.js +200 -0
- package/actionengine/math/physics/shapes/actionphysicscompoundshape3D.js +147 -0
- package/actionengine/math/physics/shapes/actionphysicscone3D.js +126 -0
- package/actionengine/math/physics/shapes/actionphysicsconvexshape3D.js +72 -0
- package/actionengine/math/physics/shapes/actionphysicscylinder3D.js +117 -0
- package/actionengine/math/physics/shapes/actionphysicsmesh3D.js +74 -0
- package/actionengine/math/physics/shapes/actionphysicsplane3D.js +100 -0
- package/actionengine/math/physics/shapes/actionphysicssphere3D.js +95 -0
- package/actionengine/math/quaternion.js +61 -0
- package/actionengine/math/vector2.js +277 -0
- package/actionengine/math/vector3.js +318 -0
- package/actionengine/math/viewfrustum.js +136 -0
- package/actionengine/network/ACTIONNETREADME.md +810 -0
- package/actionengine/network/client/ActionNetManager.js +802 -0
- package/actionengine/network/client/ActionNetManagerGUI.js +1709 -0
- package/actionengine/network/client/ActionNetManagerP2P.js +1537 -0
- package/actionengine/network/client/SyncSystem.js +422 -0
- package/actionengine/network/p2p/ActionNetPeer.js +142 -0
- package/actionengine/network/p2p/ActionNetTrackerClient.js +623 -0
- package/actionengine/network/p2p/DataConnection.js +282 -0
- package/actionengine/network/p2p/README.md +510 -0
- package/actionengine/network/p2p/example.html +502 -0
- package/actionengine/network/server/ActionNetServer.js +577 -0
- package/actionengine/network/server/ActionNetServerSSL.js +579 -0
- package/actionengine/network/server/ActionNetServerUtils.js +458 -0
- package/actionengine/network/server/SERVERREADME.md +314 -0
- package/actionengine/network/server/package-lock.json +35 -0
- package/actionengine/network/server/package.json +13 -0
- package/actionengine/network/server/start.bat +27 -0
- package/actionengine/network/server/start.sh +25 -0
- package/actionengine/network/server/startwss.bat +27 -0
- package/actionengine/sound/audiomanager.js +1589 -0
- package/actionengine/sound/soundfont/ACTIONSOUNDFONT_README.md +205 -0
- package/actionengine/sound/soundfont/actionparser.js +718 -0
- package/actionengine/sound/soundfont/actionreverb.js +252 -0
- package/actionengine/sound/soundfont/actionsoundfont.js +543 -0
- package/actionengine/sound/soundfont/sf2playerlicence.txt +29 -0
- package/actionengine/sound/soundfont/soundfont.js +2 -0
- package/dist/action-engine.min.js +328 -0
- package/package.json +35 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// actionengine/math/vector2.js
|
|
2
|
+
class Vector2 {
|
|
3
|
+
constructor(x = 0, y = 0) {
|
|
4
|
+
this.x = x;
|
|
5
|
+
this.y = y;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Static creation methods
|
|
9
|
+
static create(x = 0, y = 0) {
|
|
10
|
+
return new Vector2(x, y);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static fromAngle(angle) {
|
|
14
|
+
return new Vector2(Math.cos(angle), Math.sin(angle));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static fromArray(arr, offset = 0) {
|
|
18
|
+
return new Vector2(arr[offset], arr[offset + 1]);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static zero() {
|
|
22
|
+
return new Vector2(0, 0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static one() {
|
|
26
|
+
return new Vector2(1, 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static up() {
|
|
30
|
+
return new Vector2(0, -1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static down() {
|
|
34
|
+
return new Vector2(0, 1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static left() {
|
|
38
|
+
return new Vector2(-1, 0);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static right() {
|
|
42
|
+
return new Vector2(1, 0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Basic operations (modifying this vector)
|
|
46
|
+
set(x, y) {
|
|
47
|
+
this.x = x;
|
|
48
|
+
this.y = y;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
copy(v) {
|
|
53
|
+
this.x = v.x;
|
|
54
|
+
this.y = v.y;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
add(v) {
|
|
59
|
+
this.x += v.x;
|
|
60
|
+
this.y += v.y;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
subtract(v) {
|
|
65
|
+
this.x -= v.x;
|
|
66
|
+
this.y -= v.y;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
multiply(v) {
|
|
71
|
+
this.x *= v.x;
|
|
72
|
+
this.y *= v.y;
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
divide(v) {
|
|
77
|
+
this.x /= v.x;
|
|
78
|
+
this.y /= v.y;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
scale(scalar) {
|
|
83
|
+
this.x *= scalar;
|
|
84
|
+
this.y *= scalar;
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
negate() {
|
|
89
|
+
this.x = -this.x;
|
|
90
|
+
this.y = -this.y;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
normalize() {
|
|
95
|
+
const len = this.length();
|
|
96
|
+
if (len > 0) {
|
|
97
|
+
this.x /= len;
|
|
98
|
+
this.y /= len;
|
|
99
|
+
}
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
rotate(angle) {
|
|
104
|
+
const cos = Math.cos(angle);
|
|
105
|
+
const sin = Math.sin(angle);
|
|
106
|
+
const x = this.x * cos - this.y * sin;
|
|
107
|
+
const y = this.x * sin + this.y * cos;
|
|
108
|
+
this.x = x;
|
|
109
|
+
this.y = y;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Vector properties
|
|
114
|
+
length() {
|
|
115
|
+
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
lengthSquared() {
|
|
119
|
+
return this.x * this.x + this.y * this.y;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
angle() {
|
|
123
|
+
return Math.atan2(this.y, this.x);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
distanceTo(v) {
|
|
127
|
+
const dx = this.x - v.x;
|
|
128
|
+
const dy = this.y - v.y;
|
|
129
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
distanceToSquared(v) {
|
|
133
|
+
const dx = this.x - v.x;
|
|
134
|
+
const dy = this.y - v.y;
|
|
135
|
+
return dx * dx + dy * dy;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
dot(v) {
|
|
139
|
+
return this.x * v.x + this.y * v.y;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
cross(v) {
|
|
143
|
+
return this.x * v.y - this.y * v.x;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Utility methods
|
|
147
|
+
clone() {
|
|
148
|
+
return new Vector2(this.x, this.y);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
equals(v) {
|
|
152
|
+
return this.x === v.x && this.y === v.y;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
isZero() {
|
|
156
|
+
return this.x === 0 && this.y === 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
toString() {
|
|
160
|
+
return `Vector2(${this.x}, ${this.y})`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
toArray() {
|
|
164
|
+
return [this.x, this.y];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Static operations (returning new vectors)
|
|
168
|
+
static add(out, a, b) {
|
|
169
|
+
out.x = a.x + b.x;
|
|
170
|
+
out.y = a.y + b.y;
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static subtract(out, a, b) {
|
|
175
|
+
out.x = a.x - b.x;
|
|
176
|
+
out.y = a.y - b.y;
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static multiply(out, a, b) {
|
|
181
|
+
out.x = a.x * b.x;
|
|
182
|
+
out.y = a.y * b.y;
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
static divide(out, a, b) {
|
|
187
|
+
out.x = a.x / b.x;
|
|
188
|
+
out.y = a.y / b.y;
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static scale(out, v, scalar) {
|
|
193
|
+
out.x = v.x * scalar;
|
|
194
|
+
out.y = v.y * scalar;
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static lerp(out, a, b, t) {
|
|
199
|
+
out.x = a.x + (b.x - a.x) * t;
|
|
200
|
+
out.y = a.y + (b.y - a.y) * t;
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static min(out, a, b) {
|
|
205
|
+
out.x = Math.min(a.x, b.x);
|
|
206
|
+
out.y = Math.min(a.y, b.y);
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static max(out, a, b) {
|
|
211
|
+
out.x = Math.max(a.x, b.x);
|
|
212
|
+
out.y = Math.max(a.y, b.y);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static normalize(out, v) {
|
|
217
|
+
const len = v.length();
|
|
218
|
+
if (len > 0) {
|
|
219
|
+
out.x = v.x / len;
|
|
220
|
+
out.y = v.y / len;
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static rotate(out, v, angle) {
|
|
226
|
+
const cos = Math.cos(angle);
|
|
227
|
+
const sin = Math.sin(angle);
|
|
228
|
+
out.x = v.x * cos - v.y * sin;
|
|
229
|
+
out.y = v.x * sin + v.y * cos;
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static dot(a, b) {
|
|
234
|
+
return a.x * b.x + a.y * b.y;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static cross(a, b) {
|
|
238
|
+
return a.x * b.y - a.y * b.x;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static distance(a, b) {
|
|
242
|
+
const dx = a.x - b.x;
|
|
243
|
+
const dy = a.y - b.y;
|
|
244
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static distanceSquared(a, b) {
|
|
248
|
+
const dx = a.x - b.x;
|
|
249
|
+
const dy = a.y - b.y;
|
|
250
|
+
return dx * dx + dy * dy;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static angle(a, b) {
|
|
254
|
+
return Math.atan2(b.y - a.y, b.x - a.x);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Advanced operations
|
|
258
|
+
static reflect(out, v, normal) {
|
|
259
|
+
const dot = v.x * normal.x + v.y * normal.y;
|
|
260
|
+
out.x = v.x - 2 * dot * normal.x;
|
|
261
|
+
out.y = v.y - 2 * dot * normal.y;
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static project(out, a, b) {
|
|
266
|
+
const dot = (a.x * b.x + a.y * b.y) / (b.x * b.x + b.y * b.y);
|
|
267
|
+
out.x = b.x * dot;
|
|
268
|
+
out.y = b.y * dot;
|
|
269
|
+
return out;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static perpendicular(out, v) {
|
|
273
|
+
out.x = -v.y;
|
|
274
|
+
out.y = v.x;
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// actionengine/math/vector3.js
|
|
2
|
+
class Vector3 {
|
|
3
|
+
// Vector pool for object reuse
|
|
4
|
+
static _pool = [];
|
|
5
|
+
static _poolSize = 0;
|
|
6
|
+
static _maxPoolSize = 1000;
|
|
7
|
+
|
|
8
|
+
// Get a vector from the pool or create a new one
|
|
9
|
+
static getFromPool(x = 0, y = 0, z = 0) {
|
|
10
|
+
if (Vector3._poolSize > 0) {
|
|
11
|
+
const vec = Vector3._pool[--Vector3._poolSize];
|
|
12
|
+
vec.set(x, y, z);
|
|
13
|
+
return vec;
|
|
14
|
+
}
|
|
15
|
+
return new Vector3(x, y, z);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Return a vector to the pool when done with it
|
|
19
|
+
static returnToPool(vec) {
|
|
20
|
+
if (Vector3._poolSize < Vector3._maxPoolSize) {
|
|
21
|
+
Vector3._pool[Vector3._poolSize++] = vec;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
constructor(x = 0, y = 0, z = 0) {
|
|
25
|
+
this.x = x;
|
|
26
|
+
this.y = y;
|
|
27
|
+
this.z = z;
|
|
28
|
+
}
|
|
29
|
+
set(x, y, z) {
|
|
30
|
+
if (y === undefined && z === undefined && x.x !== undefined) {
|
|
31
|
+
// If passed another vector
|
|
32
|
+
this.x = x.x;
|
|
33
|
+
this.y = x.y;
|
|
34
|
+
this.z = x.z;
|
|
35
|
+
} else {
|
|
36
|
+
// If passed 3 numbers
|
|
37
|
+
this.x = x;
|
|
38
|
+
this.y = y;
|
|
39
|
+
this.z = z;
|
|
40
|
+
}
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Distance between two vectors
|
|
45
|
+
static distance(a, b) {
|
|
46
|
+
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// For distance calculations between points
|
|
50
|
+
distanceTo(other) {
|
|
51
|
+
const dx = this.x - other.x;
|
|
52
|
+
const dy = this.y - other.y;
|
|
53
|
+
const dz = this.z - other.z;
|
|
54
|
+
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// More efficient squared distance, avoids costly sqrt when possible
|
|
58
|
+
distanceSquared(other) {
|
|
59
|
+
const dx = this.x - other.x;
|
|
60
|
+
const dy = this.y - other.y;
|
|
61
|
+
const dz = this.z - other.z;
|
|
62
|
+
return dx * dx + dy * dy + dz * dz;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// For horizontal distance (ignoring Y) - useful for camera calculations
|
|
66
|
+
horizontalDistanceTo(other) {
|
|
67
|
+
const dx = this.x - other.x;
|
|
68
|
+
const dz = this.z - other.z;
|
|
69
|
+
return Math.sqrt(dx * dx + dz * dz);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// More efficient squared horizontal distance
|
|
73
|
+
horizontalDistanceSquared(other) {
|
|
74
|
+
const dx = this.x - other.x;
|
|
75
|
+
const dz = this.z - other.z;
|
|
76
|
+
return dx * dx + dz * dz;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// For applying movement/translation
|
|
80
|
+
translate(direction, amount) {
|
|
81
|
+
return new Vector3(this.x + direction.x * amount, this.y + direction.y * amount, this.z + direction.z * amount);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// In-place version to avoid creating a new Vector3
|
|
85
|
+
translateInPlace(direction, amount) {
|
|
86
|
+
this.x += direction.x * amount;
|
|
87
|
+
this.y += direction.y * amount;
|
|
88
|
+
this.z += direction.z * amount;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// For rotation around Y axis (useful for camera orbiting)
|
|
93
|
+
rotateY(angle) {
|
|
94
|
+
const cos = Math.cos(angle);
|
|
95
|
+
const sin = Math.sin(angle);
|
|
96
|
+
return new Vector3(this.x * cos + this.z * sin, this.y, -this.x * sin + this.z * cos);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// In-place version to avoid creating a new Vector3
|
|
100
|
+
rotateYInPlace(angle) {
|
|
101
|
+
const cos = Math.cos(angle);
|
|
102
|
+
const sin = Math.sin(angle);
|
|
103
|
+
const x = this.x;
|
|
104
|
+
const z = this.z;
|
|
105
|
+
this.x = x * cos + z * sin;
|
|
106
|
+
this.z = -x * sin + z * cos;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Gets a normalized vector representing just the horizontal component
|
|
111
|
+
horizontalNormalize() {
|
|
112
|
+
return new Vector3(this.x, 0, this.z).normalize();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static transformMat4(vec, mat) {
|
|
116
|
+
// Make sure we can access the matrix data whether it's Array or Float32Array
|
|
117
|
+
const getElement = (idx) => (mat[idx] !== undefined ? mat[idx] : mat.at(idx));
|
|
118
|
+
|
|
119
|
+
const x = vec.x;
|
|
120
|
+
const y = vec.y;
|
|
121
|
+
const z = vec.z;
|
|
122
|
+
let w = getElement(3) * x + getElement(7) * y + getElement(11) * z + getElement(15);
|
|
123
|
+
if (w === 0) w = 1;
|
|
124
|
+
|
|
125
|
+
return new Vector3(
|
|
126
|
+
(getElement(0) * x + getElement(4) * y + getElement(8) * z + getElement(12)) / w,
|
|
127
|
+
(getElement(1) * x + getElement(5) * y + getElement(9) * z + getElement(13)) / w,
|
|
128
|
+
(getElement(2) * x + getElement(6) * y + getElement(10) * z + getElement(14)) / w
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
static fromValues(x, y, z) {
|
|
132
|
+
return new Vector3(x, y, z);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static min(out, a, b) {
|
|
136
|
+
out.x = Math.min(a.x, b.x);
|
|
137
|
+
out.y = Math.min(a.y, b.y);
|
|
138
|
+
out.z = Math.min(a.z, b.z);
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static max(out, a, b) {
|
|
143
|
+
out.x = Math.max(a.x, b.x);
|
|
144
|
+
out.y = Math.max(a.y, b.y);
|
|
145
|
+
out.z = Math.max(a.z, b.z);
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
static create(x = 0, y = 0, z = 0) {
|
|
149
|
+
return new Vector3(x, y, z);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Add optimized add operation that creates less garbage
|
|
153
|
+
add(other) {
|
|
154
|
+
return new Vector3(this.x + other.x, this.y + other.y, this.z + other.z);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// In-place addition
|
|
158
|
+
addInPlace(other) {
|
|
159
|
+
this.x += other.x;
|
|
160
|
+
this.y += other.y;
|
|
161
|
+
this.z += other.z;
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Add optimized subtract operation
|
|
166
|
+
sub(other) {
|
|
167
|
+
return new Vector3(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
168
|
+
}
|
|
169
|
+
// Subtract vector b from vector a
|
|
170
|
+
static subtract(a, b) {
|
|
171
|
+
return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
|
|
172
|
+
}
|
|
173
|
+
// In-place subtraction
|
|
174
|
+
subInPlace(other) {
|
|
175
|
+
this.x -= other.x;
|
|
176
|
+
this.y -= other.y;
|
|
177
|
+
this.z -= other.z;
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Vector normalization
|
|
182
|
+
normalize() {
|
|
183
|
+
const len = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
184
|
+
if (len === 0) {
|
|
185
|
+
return new Vector3(0, 0, 0);
|
|
186
|
+
}
|
|
187
|
+
return new Vector3(this.x / len, this.y / len, this.z / len);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// In-place normalization
|
|
191
|
+
normalizeInPlace() {
|
|
192
|
+
const len = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
193
|
+
if (len !== 0) {
|
|
194
|
+
this.x /= len;
|
|
195
|
+
this.y /= len;
|
|
196
|
+
this.z /= len;
|
|
197
|
+
}
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Add dot product operation
|
|
202
|
+
dot(other) {
|
|
203
|
+
return this.x * other.x + this.y * other.y + this.z * other.z;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Add cross product operation
|
|
207
|
+
cross(other) {
|
|
208
|
+
return new Vector3(
|
|
209
|
+
this.y * other.z - this.z * other.y,
|
|
210
|
+
this.z * other.x - this.x * other.z,
|
|
211
|
+
this.x * other.y - this.y * other.x
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Array conversion
|
|
216
|
+
toArray() {
|
|
217
|
+
return [this.x, this.y, this.z];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Length calculation
|
|
221
|
+
length() {
|
|
222
|
+
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Squared length (faster, avoids sqrt)
|
|
226
|
+
lengthSquared() {
|
|
227
|
+
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
toArray() {
|
|
231
|
+
return [this.x, this.y, this.z];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
length() {
|
|
235
|
+
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
add(v) {
|
|
239
|
+
return new Vector3(this.x + v.x, this.y + v.y, this.z + v.z);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
sub(v) {
|
|
243
|
+
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
mult(n) {
|
|
247
|
+
return new Vector3(this.x * n, this.y * n, this.z * n);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
normalize() {
|
|
251
|
+
const len = this.length();
|
|
252
|
+
if (len === 0) {
|
|
253
|
+
return new Vector3();
|
|
254
|
+
}
|
|
255
|
+
return new Vector3(this.x / len, this.y / len, this.z / len);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
scale(scalar) {
|
|
259
|
+
return new Vector3(this.x * scalar, this.y * scalar, this.z * scalar);
|
|
260
|
+
}
|
|
261
|
+
// Scale a vector by a scalar
|
|
262
|
+
static scale(v, scalar) {
|
|
263
|
+
return new Vector3(v.x * scalar, v.y * scalar, v.z * scalar);
|
|
264
|
+
}
|
|
265
|
+
divideScalar(scalar) {
|
|
266
|
+
if (scalar === 0) {
|
|
267
|
+
console.warn("Vector3: Division by zero!");
|
|
268
|
+
return new Vector3(0, 0, 0);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return new Vector3(this.x / scalar, this.y / scalar, this.z / scalar);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
subtract(other) {
|
|
275
|
+
return new Vector3(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
equals(other) {
|
|
279
|
+
const epsilon = 0.000001; // Small threshold for floating point comparison
|
|
280
|
+
return (
|
|
281
|
+
Math.abs(this.x - other.x) < epsilon &&
|
|
282
|
+
Math.abs(this.y - other.y) < epsilon &&
|
|
283
|
+
Math.abs(this.z - other.z) < epsilon
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
dot(v) {
|
|
288
|
+
return this.x * v.x + this.y * v.y + this.z * v.z;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
cross(v) {
|
|
292
|
+
return new Vector3(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
clone() {
|
|
296
|
+
return new Vector3(this.x, this.y, this.z);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Copy the values from another Vector3 into this one
|
|
301
|
+
* @param {Vector3} v - Vector to copy from
|
|
302
|
+
* @returns {Vector3} this vector
|
|
303
|
+
*/
|
|
304
|
+
copy(v) {
|
|
305
|
+
this.x = v.x;
|
|
306
|
+
this.y = v.y;
|
|
307
|
+
this.z = v.z;
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
lerp(target, t) {
|
|
312
|
+
return new Vector3(
|
|
313
|
+
this.x + (target.x - this.x) * t,
|
|
314
|
+
this.y + (target.y - this.y) * t,
|
|
315
|
+
this.z + (target.z - this.z) * t
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|