-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.js
More file actions
55 lines (50 loc) · 1.48 KB
/
Transform.js
File metadata and controls
55 lines (50 loc) · 1.48 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
import {mat4, vec3} from "./gl-matrix-min.js"
//handles the projection change matrix as an object.
export let Projection = function(aspectRatio)
{
this.updateAspect(aspectRatio);
}
//updates the aspect
//TODO add other variables
Projection.prototype.updateAspect = function(aspectRatio) {
this.mat = mat4.create();
mat4.ortho(this.mat, -aspectRatio, aspectRatio, -1, 1, -1, 1);
let zoom = 0.15;
mat4.scale(this.mat, this.mat, vec3.fromValues(zoom, zoom, zoom)); //linear zoom.
mat4.mul(this.mat, this.mat,
mat4.fromValues(1, 0, 0, 0,
0, 1, 0, 0,
0, 1, 1, 0,
0, 0, 0, 1)); //handles the perspective shift of distance
}
Projection.prototype.get = function() {
return this.mat;
}
//the view matrix to go from model to world space
export let View = function(pos) {
this.pos = vec3.clone(pos);
this.mat = mat4.create();
this.upsideDown = false;
this.updateMatrix();
}
//recalculates the Matrix
View.prototype.updateMatrix = function() {
mat4.fromTranslation(this.mat, vec3.fromValues(-this.pos[0], -this.pos[1], 0));
if (this.upsideDown) {
let m = mat4.fromScaling(mat4.create(), vec3.fromValues(-1, -1, 1));
mat4.mul(this.mat, m, this.mat);
}
}
//flips view
View.prototype.setUpsideDown = function(upsideDown) {
this.upsideDown = upsideDown;
this.updateMatrix();
}
//moves view
View.prototype.setPos = function(newPos) {
vec3.copy(this.pos, newPos);
this.updateMatrix();
}
View.prototype.get = function() {
return this.mat;
}