-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
210 lines (198 loc) · 7.17 KB
/
model.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
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
'use strict';
var Model = {};
/** Sort elements so i1 <= i2 <= i3
* @param {number} i1
* @param {number} i2
* @param {number} i3
* @param {Vector} v1
* @param {Vector} v2
* @param {Vector} v3
* @return {number, number, number, Vector, Vector, Vector}
*/
Model.sort3 = function(i1, i2, i3, v1, v2, v3) {
//bubble sort on 3 elements
var ci, cv;
if (i1 > i2) {
ci = i1;
i1 = i2;
i2 = ci;
cv = v1;
v1 = v2;
v2 = cv;
}
if (i2 > i3) {
ci = i2;
i2 = i3;
i3 = ci;
cv = v2;
v2 = v3;
v3 = cv;
}
if (i1 > i2) {
ci = i1;
i1 = i2;
i2 = ci;
cv = v1;
v1 = v2;
v2 = cv;
}
return [i1, i2, i3, v1, v2, v3];
};
/** Returns list of normals for each face
* @param {Vector[]} vertexes - list of rotated vectors
* @param {int[][3]} faces - list of vertex indexes
* @return {Vector[]}
*/
Model.get_face_normals = function(vertexes, faces) {
var normals = [];
for (var i = 0; i < faces.length; i++) {
var face = faces[i];
var v1 = vertexes[face[0]];
var v2 = vertexes[face[1]];
var v3 = vertexes[face[2]];
normals[i] = v2.subtract(v1).cross(v3.subtract(v2)).toUnitVector();
}
return normals;
};
/** Returns list of normals for each vertex
* @param {Vector[]} vertexes - list of rotated vectors
* @param {int[][3]} faces - list of vertex indexes
* @return {Vector[]}
*/
Model.get_vertex_normals = function(vertexes, faces) {
var face_normals = Model.get_face_normals(vertexes, faces);
var vertex_normals = [];
for (var i = 0; i < vertexes.length; i++) {
vertex_normals[i] = $V([0, 0, 0]);
}
for (var i = 0; i < faces.length; i++) {
for (var j = 0; j < 3; j++) {
var v = faces[i][j];
vertex_normals[v] = vertex_normals[v].add(face_normals[i]);
}
}
for (var i = 0; i < vertexes.length; i++) {
vertex_normals[i] = vertex_normals[i].toUnitVector();
}
return vertex_normals;
};
/** Convert intensity to html5 color
* @param {Number} intensity
* @param {Number} max_light - maximum level of intesity (0..255)
* @param {Number} ambient_light - minimum level of intesity (0..255)
* @return {string} html5 color
*/
Model.intensity_to_rgb = function(intensity, max_light, ambient_light) {
var c = Math.max(Math.floor(intensity * max_light), 0) + ambient_light;
return 'rgb(' + c + ',' + c + ',' + c + ')';
};
/** Return rotated vertexes
* @param {Vector[]} vertexes - e.g. [[0.32, 0.43, 0.3], ...]
* @param {Number[4]} rotations - 4 angles in radians
* [rot_z, rot_y, rot_z, rot_x]
* rotations are applied in specified order
* @return {Vector[]} rotated vertexes
*/
Model.rotate_vertexes = function(vertexes, rotations) {
//[rot_z, rot_y, rot_z, rot_x]
var model_m = (Matrix.RotationX(rotations[3])
.multiply(Matrix.RotationZ(rotations[2]))
.multiply(Matrix.RotationY(rotations[1]))
.multiply(Matrix.RotationZ(rotations[0])));
var new_vertexes = [];
for (var i = 0; i < vertexes.length; i++) {
new_vertexes[i] = model_m.multiply($V(vertexes[i]));
}
return new_vertexes;
};
/** Draw asteroid model into canvas
* @param {element} canvas - element to which the object should be drawn
* @param {Vector[]} vertexes - e.g. [[0.32, 0.43, 0.3], ...]
* @param {int[][3]} faces - array of triples of indexes to vertexes
* vertex_index is from 0 to vertexes.lenght-1
* e.g. [[1,2,3], [2,3,5], ...]
* @param {Number} ambient_light - level of ambient lightning (0..1.0)
* @param {Vector} light_direction
* @param {Vector} observer_direction
* @param {Vector[]} face_normals - if vertex_normals not present use flat shading
* @param {Vector[]} vertex_normals - if present shade object with goraud shading
*/
Model.draw = function(canvas, vertexes, faces, ambient_light,
light_direction, observer_direction, face_normals, vertex_normals) {
var ctx = canvas.getContext('2d');
var r = Math.min(canvas.width, canvas.height) / 2 - 5;
// we suppose that asteroid is in (0, 0, 0)
// and direction up is in Z axis
var camera_up = $V([0, 0, 1]);
var camera_to = $V(observer_direction).multiply(-1).toUnitVector();
var camera_right = camera_to.cross(camera_up);
var camera_up = camera_right.cross(camera_to);
var projection_m = $M([
camera_up.multiply(r).elements,
camera_right.multiply(r).elements
]);
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(canvas.width / 2, canvas.height / 2);
// direction to sun
var light = $V(light_direction).toUnitVector();
ambient_light = Math.floor(ambient_light * 255);
var max_light = 255 - ambient_light;
var projected = vertexes.map((v) => projection_m.multiply(v));
for (var i = 0; i < faces.length; i++) {
var face = faces[i];
var v1 = projected[face[0]];
var v2 = projected[face[1]];
var v3 = projected[face[2]];
var dv1 = v2.subtract(v1).elements;
var dv2 = v3.subtract(v2).elements;
var norm = dv1[0] * dv2[1] - dv1[1] * dv2[0];
var color;
// cull back side
// skip collapsed triangles
if (norm <= 1) continue;
if (vertex_normals) {
var i1 = vertex_normals[face[0]].dot(light);
var i2 = vertex_normals[face[1]].dot(light);
var i3 = vertex_normals[face[2]].dot(light);
[i1, i2, i3, v1, v2, v3] = Model.sort3(i1, i2, i3, v1, v2, v3);
if (i3 - i1 < 0.01) {
color = Model.intensity_to_rgb(i1, max_light, ambient_light);
} else {
var c1 = Model.intensity_to_rgb(i1, max_light, ambient_light);
var c3 = Model.intensity_to_rgb(i3, max_light, ambient_light);
// draw a polygon and transform it with matrix
// to fill into coordinates
var k = v3.subtract(v1);
var s = k.multiply((i2 - i1) / (i3 - i1)).add(v1);
var dir = v2.subtract(s).toUnitVector();
var n = $V([-dir.e(2), dir.e(1)]);
if (n.dot(k) > 0) {
n.multiply(-1);
}
var endpos = n.multiply(k.dot(n)).add(v1);
color = ctx.createLinearGradient(
v1.e(1), v1.e(2), endpos.e(1), endpos.e(2));
color.addColorStop(0, c1);
color.addColorStop(1, c3);
}
} else if (face_normals) {
var intensity = face_normals[i].dot(light);
color = Model.intensity_to_rgb(
intensity, max_light, ambient_light);
} else {
color = Model.intensity_to_rgb(0, max_light, ambient_light);
}
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.lineWidth = 0.67;
ctx.beginPath();
ctx.moveTo(v1.e(1), v1.e(2));
ctx.lineTo(v2.e(1), v2.e(2));
ctx.lineTo(v3.e(1), v3.e(2));
ctx.closePath();
ctx.fill();
ctx.stroke();
}
ctx.restore();
};