-
Notifications
You must be signed in to change notification settings - Fork 0
/
trs.js
73 lines (65 loc) · 2.33 KB
/
trs.js
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
/**
* @class TRS
* @classdesc Helper class for affine transformations
* @property {Array} translation - The translation vector
* @property {Array} rotation - The rotation vector
* @property {Array} scale - The scale vector
* @method getTransformationMatrix - Returns the combined transformation matrix
*
*/
class TRS {
constructor() {
this.translation = [0,0,0];
this.rotation = [0,0,0];
this.scale = [1,1,1];
}
setTranslation(transX, transY, transZ) {
this.translation = [transX, transY, transZ];
}
setRotation(rotX, rotY, rotZ) {
this.rotation = [rotX, rotY, rotZ];
}
setScale(scaleX, scaleY, scaleZ) {
this.scale = [scaleX, scaleY, scaleZ];
}
getTranslationMatrix() {
return [1,0,0,0,
0,1,0,0,
0,0,1,0,
this.translation[0],this.translation[1],this.translation[2],1];
}
getRotationMatrix() {
var rotX = this.rotation[0];
var rotY = this.rotation[1];
var rotZ = this.rotation[2];
var rotXMatrix = [1,0,0,0,
0,Math.cos(rotX),Math.sin(rotX),0,
0,-Math.sin(rotX),Math.cos(rotX),0,
0,0,0,1];
var rotYMatrix = [Math.cos(rotY),0,-Math.sin(rotY),0,
0,1,0,0,
Math.sin(rotY),0,Math.cos(rotY),0,
0,0,0,1];
var rotZMatrix = [Math.cos(rotZ),Math.sin(rotZ),0,0,
-Math.sin(rotZ),Math.cos(rotZ),0,0,
0,0,1,0,
0,0,0,1];
var rotationMatrix = MatrixMult(rotXMatrix, rotYMatrix);
rotationMatrix = MatrixMult(rotationMatrix, rotZMatrix);
return rotationMatrix;
}
getScaleMatrix() {
return [this.scale[0],0,0,0,
0,this.scale[1],0,0,
0,0,this.scale[2],0,
0,0,0,1];
}
getTransformationMatrix() {
var translationMatrix = this.getTranslationMatrix();
var rotationMatrix = this.getRotationMatrix();
var scaleMatrix = this.getScaleMatrix();
var transformationMatrix = MatrixMult(translationMatrix, rotationMatrix);
transformationMatrix = MatrixMult(transformationMatrix, scaleMatrix);
return transformationMatrix;
}
}