]> git.r.bdr.sh - rbdr/lissajous/blob - lib/systems/camera_rotator.js
3520f716e4f5cad0451a6d11e6ff615bd1062f80
[rbdr/lissajous] / lib / systems / camera_rotator.js
1 import { mat4, vec3 } from 'gl-matrix';
2 import { System } from '@serpentity/serpentity';
3 import Cameras from '../nodes/cameras';
4
5 export default class CameraRotator extends System {
6
7 constructor() {
8
9 super();
10 }
11
12 added(engine){
13
14 this.cameras = engine.getNodes(Cameras);
15 }
16
17 removed(){
18
19 delete this.cameras;
20 }
21
22 update(dt){
23
24 for (const camera of this.cameras) {
25
26 let rotationMatrix = mat4.create();
27 mat4.rotateY(rotationMatrix, rotationMatrix, camera.angle.yaw);
28 mat4.rotateX(rotationMatrix, rotationMatrix, camera.angle.pitch);
29 mat4.rotateZ(rotationMatrix, rotationMatrix, camera.angle.roll);
30
31 let eye = vec3.fromValues(0, 0, camera.radius.radius);
32 vec3.transformMat4(eye, eye, rotationMatrix);
33
34 camera.position.x = eye[0];
35 camera.position.y = eye[1];
36 camera.position.z = eye[2];
37
38 let up = vec3.fromValues(0, 1, 0);
39 vec3.transformMat4(up, up, rotationMatrix);
40 camera.up.x = up[0];
41 camera.up.y = up[1];
42 camera.up.z = up[2];
43
44 camera.angle.pitch = (camera.angle.pitch + camera.velocity.x * dt / 100 + 2 * Math.PI) % (2 * Math.PI);
45 camera.angle.yaw = (camera.angle.yaw + camera.velocity.y * dt / 100 + 2 * Math.PI) % (2 * Math.PI);
46 camera.angle.roll = (camera.angle.roll + camera.velocity.z * dt / 100 + 2 * Math.PI) % (2 * Math.PI);
47 }
48 }
49 };