-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.js
More file actions
executable file
·79 lines (57 loc) · 2.13 KB
/
Copy pathShape.js
File metadata and controls
executable file
·79 lines (57 loc) · 2.13 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
var Shape = function (name, material, translation, rotation, scaling) { return (function (self) {
// Shape base material
self.material = material || Material();
// Material selection pattern
self.pattern = null;
self.name = name;
self.translation = translation || mat4.create();
self.rotation = rotation || mat4.create();
self.scaling = scaling || mat4.create();
self.getMaterial = function (coord) {
return (self.pattern && self.pattern(coord)) || self.material;
}
self.translate = function (x, y, z) {
mat4.translate(self.translation, self.translation, vec3.fromValues(x, y, z));
}
self.rotate = function (rad, axis) {
mat4.rotate(self.rotation, self.rotation, rad, axis);
}
self.scale = function (x, y, z) {
if (x && !y && !z)
mat4.scale(self.scaling, self.scaling, vec3.fromValues(x, x, x));
else
mat4.scale(self.scaling, self.scaling, vec3.fromValues(x, y, z));
}
self.transformIncidentRay = function (inRay) {
var io = vec3.create(), ie = vec3.create(), id = vec3.create();
vec3.copy(io, inRay.origin), vec3.copy(id, inRay.direction);
var tr = Ray();
var pt = mat4.create();
// [S-1][R-1] * direction
mat4.multiply(pt, self.rotation, self.scaling);
vec3.transformMat4(tr.direction, id, mat4.invert(mat4.create(), pt));
// [T-1][S-1][R-1] * origin
mat4.multiply(pt, pt, self.translation);
vec3.transformMat4(tr.origin, io, mat4.invert(mat4.create(), pt));
// normalize direction
vec3.normalize(tr.direction, tr.direction);
return tr;
}
self.untransformCoordinate = function (c) {
var coord = vec3.create();
vec3.copy(coord, c);
// [T][R][S] c-1
vec3.transformMat4(coord, coord, self.translation);
vec3.transformMat4(coord, coord, self.rotation);
vec3.transformMat4(coord, coord, self.scaling);
return coord;
}
self.transformNormal = function (normal) {
var n = vec3.create();
vec3.copy(n, normal);
vec3.transformMat4(n, n, self.rotation);
vec3.transformMat4(n, n, mat4.invert(mat4.create(), self.scaling));
return n;
}
return self;
})(Object.create(null))}