-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.js
More file actions
731 lines (612 loc) · 24.6 KB
/
Copy pathframework.js
File metadata and controls
731 lines (612 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
How this framework works:
- A Scene contains SceneObjects
- A SceneObject contains Components and child SceneObjects if needed
- A Component is a behaviour that can be attached to a SceneObject
- A MeshRenderer is a Component that draws a Mesh
- An AnimationComponent is a Component that animates the Transform of the SceneObject it is attached to using Keyframes and interpolation
- In scene.js, create a Scene, create SceneObjects, add Components/Childs to the SceneObjects, and add the SceneObjects to Scene.SCENEOBJECTS array
- In main.js and in the render loop, call scene.render() to init, start and update all SceneObjects in the scene
*/
const DEBUG = true; //disable for some performance i guess
function tMatrix(matrix) {
gPush();
{
matrix();
}
gPop();
}
class Scene {
constructor() {
this.TIME = 0; //current time since the start of the scene
this.DELTATIME = 0.016; //time difference between current frame and previous frame
this.SCENESTARTED = false; //indicates if the scene has been started
this.SCENEOBJECTS = [];
}
render() {
if(DEBUG) {
for( var i = this.SCENEOBJECTS.length-1; i >= 0; i-- ) {
if(!(this.SCENEOBJECTS[i] instanceof SceneObject)) {
console.error("SceneObject at index " + i + " is not an instance of SceneObject. It is of type " + typeof(this.SCENEOBJECTS[i]) + ". Make sure to only add SceneObjects to the SCENEOBJECTS array.");
throw new Error("Invalid SceneObject in SCENEOBJECTS array.");
}
}
}
if(this.SCENESTARTED == false) {
console.log("Initializing and starting scene...");
for( var i = this.SCENEOBJECTS.length-1; i >= 0; i-- ) {
this.SCENEOBJECTS[i].init(this);
}
for( var i = this.SCENEOBJECTS.length-1; i >= 0; i-- ) {
this.SCENEOBJECTS[i].start();
}
this.SCENESTARTED = true;
window.requestAnimFrame(render);
}
for( var i = this.SCENEOBJECTS.length-1; i >= 0; i-- ) {
if(this.SCENEOBJECTS[i].scene == null) {
this.SCENEOBJECTS[i].init(this);
}
this.SCENEOBJECTS[i].update();
}
}
destroy(sceneObject) {
for( var i = this.SCENEOBJECTS.length-1; i >= 0; i-- ) {
if(this.SCENEOBJECTS[i] == sceneObject) {
this.SCENEOBJECTS[i].children = [];
this.SCENEOBJECTS[i].components = [];
this.SCENEOBJECTS.splice(i, 1); //remove from array
console.log("Destroyed SceneObject " + sceneObject);
break;
}
}
}
}
class Transform {
constructor(t=vec3(0.0, 0.0, 0.0), r=vec3(0.0, 0.0, 0.0), s=vec3(1.0, 1.0, 1.0)) {
this.translation = t;
this.rotation = r;
this.scale = s;
}
}
class Component {
constructor() {
this.root;
this.scene;
this.parent;
this.isActive = true;
}
init(root, scene) {
this.root = root;
this.scene = scene;
}
start() {
// to be overridden
}
update() {
// to be overridden
}
}
class SceneObject {
constructor() {
this.transform = new Transform();
this.components = [];
this.children = [];
this.isActive = true;
this.isDestroyed = false;
this.root;
this.scene;
}
transformSceneObject() {
gTranslate(this.transform.translation[0], this.transform.translation[1], this.transform.translation[2]);
gRotate(this.transform.rotation[0], 1, 0, 0);
gRotate(this.transform.rotation[1], 0, 1, 0);
gRotate(this.transform.rotation[2], 0, 0, 1);
gScale(this.transform.scale[0], this.transform.scale[1], this.transform.scale[2]);
}
update() {
tMatrix(() => {
this.transformSceneObject();
if(this.isActive && !this.isDestroyed) {
for(let component of this.components) {
if(component.isActive) component.update();
}
for(let child of this.children) {
if(child.isActive) child.update();
}
}
});
}
init(scene) {
this.scene = scene;
for(let component of this.components) {
component.init(this, this.scene);
}
}
start() {
console.log("Starting SceneObject: " );
console.log(this);
tMatrix(() => {
this.transformSceneObject();
for(let component of this.components) {
component.start();
}
for(let child of this.children) {
child.root = this;
child.init(this.scene);
child.start();
}
});
}
addComponent(component) {
if(DEBUG && !(component instanceof Component)) {
console.error("Tried to add a component that is not an instance of Component. It is of type " + typeof(component) + ".");
throw new Error("Invalid component added to SceneObject.");
}
this.components.push(component);
if(this.scene != null && this.scene.SCENESTARTED) { //if the scene has already started, init and start the component immediately
component.init(this, this.scene);
component.start();
component.parent = this;
}
}
addChild(child) {
if(DEBUG && !(child instanceof SceneObject)) {
console.error("Tried to add a child that is not an instance of SceneObject. It is of type " + typeof(child) + ".");
throw new Error("Invalid child added to SceneObject.");
}
this.children.push(child);
if(this.scene != null && this.scene.SCENESTARTED) { //if the scene has already started, init and start the child immediately
child.root = this;
child.init(this.scene);
child.start();
}
}
getComponent(componentType) { //pass the component class type as argument
for(let component of this.components) {
if(component instanceof componentType) {
return component;
}
}
return null;
}
getTransform() {
let tf = new Transform();
tf.translation = this.transform.translation;
tf.rotation = this.transform.rotation;
tf.scale = this.transform.scale;
return tf;
}
destroy() {
this.isDestroyed = true;
this.scene.destroy(this);
}
}
class Keyframe {
constructor(timestamp, targetTransform) {
this.timestamp = timestamp;
this.targetTransform = targetTransform;
}
// this is a basic interpolation function that we will use for now
matrixInterpolation(t1, t2, time, totalTime) {
let factor = time / totalTime;
let transform = new Transform();
if(t2 == null || t1 == null) return transform;
transform.translation = mix(t2.translation, t1.translation, factor);
transform.rotation = mix(t2.rotation, t1.rotation, factor);
transform.scale = mix(t2.scale, t1.scale, factor);
return transform;
}
}
class Callbackframe {
constructor(timestamp, callback) {
this.timestamp = timestamp;
this.callback = callback;
this.isActive = true;
}
}
class Mesh {
constructor(draw) {
this.draw = draw; //draw is the function used to draw the mesh
}
}
class MeshRenderer extends Component {
constructor(mesh=new Mesh()) {
super();
this.mesh = mesh;
}
update() {
tMatrix(() => {
this.mesh.draw();
});
}
}
class AnimationComponent extends Component {
constructor(keyframes=[], callbacks=[]) {
super();
this.keyframes = keyframes;
this.callbacks = callbacks;
this.currentKeyframe = new Keyframe();
this.targetKeyframe = new Keyframe();
this.currentTime = 0.0; //curent time of the animation (it is DIFFERENT from Scene.TIME)
this.offsetTime = 0.0; //make the animation start at a different time
this.offsetKeyframes = 0.0; //delay when the animation will play. makes it easier to create an animation normally and play it at a later time
this.animationLength = 0.0;
this.isLooped = false;
this.isPaused = false;
}
update() {
if(!this.isPaused) {
if(this.scene.TIME > this.offsetKeyframes) {
this.currentTime = this.scene.TIME - this.offsetKeyframes + this.offsetTime;
}
} else {
return;
}
for(let callback of this.callbacks) {
if(this.currentTime >= callback.timestamp && (this.currentTime - this.scene.DELTATIME) < callback.timestamp) {
callback.callback();
}
}
if(this.isLooped) {
this.currentTime %= this.animationLength;
} else if(this.currentTime > this.animationLength) {
this.currentTime = this.animationLength;
}
if(this.keyframes != null && this.keyframes.length >= 2) {
for(let i = 0; i < this.keyframes.length - 1; i++) {
if(this.currentTime >= this.keyframes[i].timestamp && this.currentTime < this.keyframes[i + 1].timestamp) {
this.currentKeyframe = this.keyframes[i];
this.targetKeyframe = this.keyframes[i + 1];
break;
}
}
let totalTime = this.targetKeyframe.timestamp - this.currentKeyframe.timestamp;
let time = this.currentTime - this.currentKeyframe.timestamp;
this.root.transform = this.currentKeyframe.matrixInterpolation(
this.currentKeyframe.targetTransform,
this.targetKeyframe.targetTransform,
time,
totalTime
);
}
}
start() {
this.keyframes.sort((a, b) => a.timestamp - b.timestamp);
if(this.keyframes.length > 0) {
this.animationLength = this.keyframes[this.keyframes.length - 1].timestamp;
}
if(this.keyframes[0].timestamp > 0.0) { //create initial keyframe at time 0 if there isn't one
let initialKeyframe = new Keyframe(0.0, this.root.transform);
this.keyframes.unshift(initialKeyframe);
}
}
addKeyframe(keyframe) {
this.keyframes.push(keyframe);
this.keyframes.sort((a, b) => a.timestamp - b.timestamp);
}
}
class CameraControllerComponent extends Component {
constructor() {
super();
this.enableLookObject = true;
this.lookObject = null; // the scene object the camera is looking at
this.lookSpeed = 5.0;
this.moveSpeed = 30.0; // units per second
this.keys = {};
}
update() {
let deltaTime = this.scene.DELTATIME;
let moveSpeed = this.moveSpeed;
let keys = this.keys;
let forward = normalize(subtract(at, eye));
let right = normalize(cross(forward, up));
// if (keys['w']) this.root.transform.translation = add(this.root.transform.translation, scalev(moveSpeed * deltaTime, forward));
// if (keys['s']) this.root.transform.translation = subtract(this.root.transform.translation, scalev(moveSpeed * deltaTime, forward));
// if (keys['a']) this.root.transform.translation = subtract(this.root.transform.translation, scalev(moveSpeed * deltaTime, right));
// if (keys['d']) this.root.transform.translation = add(this.root.transform.translation, scalev(moveSpeed * deltaTime, right));
// if (keys[' ']) this.root.transform.translation = add(this.root.transform.translation, scalev(moveSpeed * deltaTime, up));
// if (keys['shift']) this.root.transform.translation = subtract(this.root.transform.translation, scalev(moveSpeed * deltaTime, up));
eye = this.root.transform.translation;
if(this.lookObject == null || this.enableLookObject == false) {
at = add(eye, forward);
} else {
//at = this.lookObject.transform.translation;
let speed = this.lookSpeed * (deltaTime != null ? deltaTime : 0.016);
if(this.lookSpeed > 100)
at = this.lookObject.transform.translation;
else
at = mix( this.lookObject.transform.translation, at, speed);
}
if(this.scene.TIME % 0.5 == 0) {
//console.log("Camera position: " + eye);
//console.log("Camera at: " + at);
}
viewMatrix = lookAt(eye, at, up);
modelViewMatrix = mult(viewMatrix, modelMatrix);
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
}
start() {
window.addEventListener("keydown", (e) => { this.keys[e.key.toLowerCase()] = true; });
window.addEventListener("keyup", (e) => { this.keys[e.key.toLowerCase()] = false; });
}
setLookObject(sceneObject) {
this.previousLookObject = this.lookObject;
this.lookObject = sceneObject;
}
}
/**
* Places a number of Meshes randomly within a rectangular area.
*
* positionMultiplerCurve supports a function returning range 0-1 that is called for each spawn (allows non-linear distribution)
*/
class RandomPlacer extends Component {
constructor(
{
mesh,
positionMultiplierCurve=null,
bottomLeft = vec3(-50,-50,-50),
topRight = vec3(50,50,50),
minDistance = 0,
count = 100
}
)
{
super();
this.mesh = mesh;
this.bottomLeft = bottomLeft;
this.topRight = topRight;
this.count = count;
this.minDistance = minDistance;
this.positionMultiplierCurve = positionMultiplierCurve;
if(typeof mesh === "undefined" || !(mesh instanceof Mesh)) {
console.error("RandomPlacer: mesh is not an instance of Mesh.");
throw new Error("Invalid mesh passed to RandomPlacer.");
}
if(positionMultiplierCurve != null && typeof positionMultiplierCurve !== "function") {
console.error("RandomPlacer: positionMultiplierCurve is not a function.");
throw new Error("Invalid positionMultiplierCurve passed to RandomPlacer.");
}
this.spawns = [];
}
start() {
let positionMultiplier = 1.0;
for(let i = 0; i < this.count; i++) {
if(this.positionMultiplierCurve != null && typeof this.positionMultiplierCurve === "function") {
positionMultiplier = this.positionMultiplierCurve();
}
let x = Math.random() * (this.topRight[0] - this.bottomLeft[0]) + this.bottomLeft[0];
let y = Math.random() * (this.topRight[1] - this.bottomLeft[1]) + this.bottomLeft[1];
let z = Math.random() * (this.topRight[2] - this.bottomLeft[2]) + this.bottomLeft[2];
x *= positionMultiplier;
y *= positionMultiplier;
z *= positionMultiplier;
let pos = vec3(x, y, z);
if(length(pos) < this.minDistance){
pos = scale(pos, this.minDistance / length(pos));
}
let asteroid = new SceneObject();
asteroid.addComponent(new MeshRenderer(this.mesh));
asteroid.transform.translation = pos;
this.scene.SCENEOBJECTS.push(asteroid);
this.spawns.push(asteroid);
}
}
}
class ParticleSystem extends Component {
constructor(particleLayers=[]) {
super();
this.particleLayers = particleLayers;
this.canEmitParticles = true;
}
init(root, scene) {
super.init(root, scene);
for(let layer of this.particleLayers) {
layer.init(root, scene);
}
}
update() {
if(!this.canEmitParticles) return;
for(let layer of this.particleLayers) {
layer.update(this.scene.DELTATIME);
}
}
}
class ParticleLayer extends Component{
constructor(mesh, direction = vec3(0.0, 1.0, 0.0), spread = 0.0, speed = 10.0, lifetime = 3.0, emissionRate = 15.0, emissionTime = 2.0) {
super();
this.mesh = mesh;
this.currentTime = 0.0;
this.cooldownTimer = 0.0;
this.direction = direction;
this.spread = spread;
this.speed = speed;
this.lifetime = lifetime;
this.emissionRate = emissionRate; //particles per second
this.emissionTime = emissionTime; //how long the particlelayer will emit particles for. a value of 0 means indefinite
}
update() {
if(this.scene.DELTATIME != null) {
this.cooldownTimer += this.scene.DELTATIME;
this.currentTime += this.scene.DELTATIME;
if(this.currentTime >= this.emissionTime && this.emissionTime != 0.0) {
return;
}
}
if(this.cooldownTimer >= 1.0/this.emissionRate) {
this.cooldownTimer = 0;
let entityRoot = this.root; //the last root SceneObject
while(entityRoot.root != null) {
entityRoot = entityRoot.root;
}
let directionWithRot = normalize(rotateDir(this.direction, entityRoot.transform.rotation));
//console.log(directionWithRot);
let directionWithSpread = vec3(
directionWithRot[0] + (Math.random() - 0.5) * this.spread,
directionWithRot[1] + (Math.random() - 0.5) * this.spread,
directionWithRot[2] + (Math.random() - 0.5) * this.spread
);
directionWithSpread = normalize(directionWithSpread);
directionWithSpread[0] *= this.speed;
directionWithSpread[1] *= this.speed;
directionWithSpread[2] *= this.speed;
let particle = new SceneObject();
particle.transform = entityRoot.getTransform();
particle.transform.scale = this.root.transform.scale;
let directionWithSpreadTranslation = add(directionWithSpread, entityRoot.transform.translation);
particle.addComponent(new MeshRenderer(this.mesh));
let ac_particle = new AnimationComponent(
[
new Keyframe(this.scene.TIME, new Transform(entityRoot.transform.translation, entityRoot.transform.rotation, particle.transform.scale)),
new Keyframe(this.lifetime + this.scene.TIME, new Transform(directionWithSpreadTranslation, entityRoot.rotation, vec3(0.0, 0.0, 0.0)))
],
[
new Callbackframe(this.lifetime + this.scene.TIME, () => { particle.destroy(); })
]
);
particle.init(this.scene);
particle.addComponent(ac_particle);
this.scene.SCENEOBJECTS.push(particle);
}
}
}
/**
* Based on RandomPlacer. Places asteroids randomly within a rectangular area.
*/
class AsteroidRandomPlacer extends Component {
constructor(
{
positionMultiplierCurve=null,
bottomLeft = vec3(-50,-50,-50),
topRight = vec3(50,50,50),
count = 100,
minDistance = 0
} = {}
)
{
super();
this.bottomLeft = bottomLeft;
this.topRight = topRight;
this.count = count;
this.minDistance = minDistance;
this.positionMultiplierCurve = positionMultiplierCurve;
if(positionMultiplierCurve != null && typeof positionMultiplierCurve !== "function") {
console.error("RandomPlacer: positionMultiplierCurve is not a function.");
throw new Error("Invalid positionMultiplierCurve passed to RandomPlacer.");
}
this.spawns = [];
}
start() {
let positionMultiplier = 1.0;
//individual asteroids
for(let i = 0; i < this.count; i++) {
if(this.positionMultiplierCurve != null && typeof this.positionMultiplierCurve === "function") {
positionMultiplier = this.positionMultiplierCurve();
}
//distribute randomly
let x = Math.random() * (this.topRight[0] - this.bottomLeft[0]) + this.bottomLeft[0];
let y = Math.random() * (this.topRight[1] - this.bottomLeft[1]) + this.bottomLeft[1];
let z = Math.random() * (this.topRight[2] - this.bottomLeft[2]) + this.bottomLeft[2];
x *= positionMultiplier;
y *= positionMultiplier;
z *= positionMultiplier;
let center = vec3(x, y, z);
if(length(center) < this.minDistance){
center = scalev(this.minDistance / length(center), center);
}
let mainAsteroidPart;
//an asteroid contains multiple sub-asteroids to create lumpiness illusion
for(let i = 0; i < 5 + Math.floor(Math.random() * 6); i++) {
let pos_offset_scale = 5.4; // scale factor
let pos_offset = vec3(
(Math.random() - 0.5) * pos_offset_scale,
(Math.random() - 0.5) * pos_offset_scale,
(Math.random() - 0.5) * pos_offset_scale
);
let scale_max = 4.5;
let scale_min = 1.5;
let scale = scale_min + Math.random() * (scale_max - scale_min);
let scale_offset = vec3(
scale,
scale,
scale
);
let rotation_offset = vec3(
Math.random() * 360,
Math.random() * 360,
Math.random() * 360
);
let asteroid = new SceneObject();
let asteroidMesh = new AsteroidMesh();
let asteroidMeshRenderer = new MeshRenderer(asteroidMesh);
asteroid.addComponent(asteroidMeshRenderer);
asteroidMeshRenderer.isActive = false;
asteroid.transform.translation = add(center, pos_offset);
asteroid.transform.rotation = rotation_offset;
asteroid.transform.scale = scale_offset;
if(mainAsteroidPart == null) {
mainAsteroidPart = asteroid;
// let asteroidSpinny = new AsteroidSpinny();
// mainAsteroidPart.addComponent(asteroidSpinny);
} else {
mainAsteroidPart.addChild(asteroid);
}
this.scene.SCENEOBJECTS.push(asteroid);
this.spawns.push(asteroid);
}
}
}
}
// class AsteroidSpinny extends Component {
// constructor() {
// super();
// this.rotationSpeed;
// this.started = false;
// }
// start() {
// let minRotSpeed = 300;
// let maxRotSpeed = 3600;
// this.rotationSpeed = vec3(
// minRotSpeed + (Math.random() * (maxRotSpeed - minRotSpeed)),
// minRotSpeed + (Math.random() * (maxRotSpeed - minRotSpeed)),
// minRotSpeed + (Math.random() * (maxRotSpeed - minRotSpeed))
// );
// this.started = true;
// //console.log(this.rotationSpeed);
// }
// update() {
// if(!this.started)
// this.start();
// if(this.scene.DELTATIME != 0 && this.parent != null) {
// this.parent.transform.rotation =
// add(scalev(this.scene.DELTATIME, this.rotationSpeed),
// this.parent.transform.rotation);
// }
// }
// }
function rotateDir(d, r) {
// convert deg → rad
const k = Math.PI/180;
let [x,y,z] = d.map(v=>v*k);
let [rx,ry,rz] = r.map(v=>v*k);
// create quaternion from Euler (YZX order)
const cy = Math.cos(ry/2), sy = Math.sin(ry/2);
const cx = Math.cos(rx/2), sx = Math.sin(rx/2);
const cz = Math.cos(rz/2), sz = Math.sin(rz/2);
const qw = cy*cx*cz + sy*sx*sz;
const qx = cy*sx*cz + sy*cx*sz;
const qy = sy*cx*cz - cy*sx*sz;
const qz = cy*cx*sz - sy*sx*cz;
// rotate vector
const vx = x, vy = y, vz = z;
const ix = qw*vx + qy*vz - qz*vy;
const iy = qw*vy + qz*vx - qx*vz;
const iz = qw*vz + qx*vy - qy*vx;
const iw = -qx*vx - qy*vy - qz*vz;
const xr = ix*qw + iw*-qx + iy*-qz - iz*-qy;
const yr = iy*qw + iw*-qy + iz*-qx - ix*-qz;
const zr = iz*qw + iw*-qz + ix*-qy - iy*-qx;
// convert back to deg
const inv = 180/Math.PI;
return [xr*inv, yr*inv, zr*inv];
}