-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
executable file
·663 lines (514 loc) · 16.3 KB
/
utils.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//Utils ver. 0.4
//Includes minimal mat3 support
//Includes texture operations
//Includes initInteraction() function
var utils = {
createAndCompileShaders: function (gl, shaderText) {
var vertexShader = utils.createShader(gl, gl.VERTEX_SHADER, shaderText[0]);
var fragmentShader = utils.createShader(gl, gl.FRAGMENT_SHADER, shaderText[1]);
var program = utils.createProgram(gl, vertexShader, fragmentShader);
return program;
},
createShader: function (gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
} else {
console.log(gl.getShaderInfoLog(shader)); // eslint-disable-line
if (type == gl.VERTEX_SHADER) {
alert("ERROR IN VERTEX SHADER : " + gl.getShaderInfoLog(vertexShader));
}
if (type == gl.FRAGMENT_SHADER) {
alert("ERROR IN FRAGMENT SHADER : " + gl.getShaderInfoLog(vertexShader));
}
gl.deleteShader(shader);
throw "could not compile shader:" + gl.getShaderInfoLog(shader);
}
},
createProgram: function (gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
} else {
throw ("program filed to link:" + gl.getProgramInfoLog(program));
console.log(gl.getProgramInfoLog(program)); // eslint-disable-line
gl.deleteProgram(program);
return undefined;
}
},
resizeCanvasToDisplaySize: function (canvas) {
const expandFullScreen = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
expandFullScreen();
// Resize screen when the browser has triggered the resize event
window.addEventListener('resize', expandFullScreen);
},
//**** MODEL UTILS
// Function to load a 3D model in JSON format
get_json: async function (url, func) {
var response = await fetch(url);
if (!response.ok) {
alert('Network response was not ok');
return;
}
var json = await response.json();
func(json);
},
get_objstr: async function (url) {
var response = await fetch(url);
if (!response.ok) {
alert('Network response was not ok');
return;
}
var text = await response.text();
return text;
},
//function to convert decimal value of colors
decimalToHex: function (d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
},
//*** SHADERS UTILS
/*Function to load a shader's code, compile it and return the handle to it
Requires:
path to the shader's text (url)
*/
/*fetch('http://foo.com/static/bar.glsl') .then(response => {
if (!response.ok) {
alert('Network response was not ok');
}
return response.text();
}).then(data => console.log(data));*/
loadFile: async function (url, data, callback, errorCallBack) {
var response = await fetch(url);
if (!response.ok) {
alert('Network response was not ok');
return;
}
var text = await response.text();
callback(text, data);
},
/*loadFile: function (url, data, callback, errorCallback) {
// Set up a synchronous request! Important!
var request = new XMLHttpRequest();
//The third parameter set to false makes the request synchronous
request.open('GET', url, false);
// Hook the event that gets called as the request progresses
request.onreadystatechange = function () {
// If the request is "DONE" (completed or failed) and if we got HTTP status 200 (OK)
if (request.readyState == 4 && request.status == 200) {
callback(request.responseText, data)
//} else { // Failed
// errorCallback(url);
}
};
request.send(null);
},*/
loadFiles: async function (urls, callback, errorCallback) {
var numUrls = urls.length;
var numComplete = 0;
var result = [];
// Callback for a single file
function partialCallback(text, urlIndex) {
result[urlIndex] = text;
numComplete++;
// When all files have downloaded
if (numComplete == numUrls) {
callback(result);
}
}
for (var i = 0; i < numUrls; i++) {
await this.loadFile(urls[i], i, partialCallback, errorCallback);
}
},
// loadFiles: function (urls, gl, callback, errorCallback) {
// var numUrls = urls.length;
// var numComplete = 0;
// var result = [];
// // Callback for a single file
// function partialCallback(text, urlIndex) {
// result[urlIndex] = text;
// numComplete++;
// // When all files have downloaded
// if (numComplete == numUrls) {
// callback(gl,result);
// }
// }
// for (var i = 0; i < numUrls; i++) {
// this.loadFile(urls[i], i, partialCallback, errorCallback);
// }
// },
// *** TEXTURE UTILS (to solve problems with non power of 2 textures in webGL
getTexture: function (context, image_URL) {
var image = new Image();
image.webglTexture = false;
image.isLoaded = false;
image.onload = function (e) {
var texture = context.createTexture();
context.bindTexture(context.TEXTURE_2D, texture);
context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, image);
//context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, 1);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.LINEAR);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST_MIPMAP_LINEAR);
context.generateMipmap(context.TEXTURE_2D);
context.bindTexture(context.TEXTURE_2D, null);
image.webglTexture = texture;
image.isLoaded = true;
};
image.src = image_URL;
return image;
},
isPowerOfTwo: function (x) {
return (x & (x - 1)) == 0;
},
nextHighestPowerOfTwo: function (x) {
--x;
for (var i = 1; i < 32; i <<= 1) {
x = x | x >> i;
}
return x + 1;
},
//*** Interaction UTILS
initInteraction: function () {
var keyFunction = function (e) {
if (e.keyCode == 37) { // Left arrow
cx -= delta;
}
if (e.keyCode == 39) { // Right arrow
cx += delta;
}
if (e.keyCode == 38) { // Up arrow
cz -= delta;
}
if (e.keyCode == 40) { // Down arrow
cz += delta;
}
if (e.keyCode == 107) { // Add
cy += delta;
}
if (e.keyCode == 109) { // Subtract
cy -= delta;
}
if (e.keyCode == 65) { // a
angle -= delta * 10.0;
}
if (e.keyCode == 68) { // d
angle += delta * 10.0;
}
if (e.keyCode == 87) { // w
elevation += delta * 10.0;
}
if (e.keyCode == 83) { // s
elevation -= delta * 10.0;
}
}
//'window' is a JavaScript object (if "canvas", it will not work)
window.addEventListener("keyup", keyFunction, false);
},
//*** MATH LIBRARY
degToRad: function (angle) {
return (angle * Math.PI / 180);
},
radToDeg: function (radians) {
return radians * 180 / Math.PI;
},
identityMatrix: function () {
return [1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
},
identityMatrix3: function () {
return [1, 0, 0,
0, 1, 0,
0, 0, 1
];
},
// returns the 3x3 submatrix from a Matrix4x4
sub3x3from4x4: function (m) {
out = [];
out[0] = m[0];
out[1] = m[1];
out[2] = m[2];
out[3] = m[4];
out[4] = m[5];
out[5] = m[6];
out[6] = m[8];
out[7] = m[9];
out[8] = m[10];
return out;
},
// Multiply the mat3 with a vec3.
multiplyMatrix3Vector3: function (m, a) {
out = [];
var x = a[0],
y = a[1],
z = a[2];
out[0] = x * m[0] + y * m[1] + z * m[2];
out[1] = x * m[3] + y * m[4] + z * m[5];
out[2] = x * m[6] + y * m[7] + z * m[8];
return out;
},
//Transpose the values of a mat3
transposeMatrix3: function (a) {
out = [];
out[0] = a[0];
out[1] = a[3];
out[2] = a[6];
out[3] = a[1];
out[4] = a[4];
out[5] = a[7];
out[6] = a[2];
out[7] = a[5];
out[8] = a[8];
return out;
},
invertMatrix3: function (m) {
out = [];
var a00 = m[0],
a01 = m[1],
a02 = m[2],
a10 = m[3],
a11 = m[4],
a12 = m[5],
a20 = m[6],
a21 = m[7],
a22 = m[8],
b01 = a22 * a11 - a12 * a21,
b11 = -a22 * a10 + a12 * a20,
b21 = a21 * a10 - a11 * a20,
// Calculate the determinant
det = a00 * b01 + a01 * b11 + a02 * b21;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = b01 * det;
out[1] = (-a22 * a01 + a02 * a21) * det;
out[2] = (a12 * a01 - a02 * a11) * det;
out[3] = b11 * det;
out[4] = (a22 * a00 - a02 * a20) * det;
out[5] = (-a12 * a00 + a02 * a10) * det;
out[6] = b21 * det;
out[7] = (-a21 * a00 + a01 * a20) * det;
out[8] = (a11 * a00 - a01 * a10) * det;
return out;
},
//requires as a parameter a 4x4 matrix (array of 16 values)
invertMatrix: function (m) {
var out = [];
var inv = [];
var det, i;
inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return out = this.identityMatrix();
det = 1.0 / det;
for (i = 0; i < 16; i++) {
out[i] = inv[i] * det;
}
return out;
},
transposeMatrix: function (m) {
var out = [];
var row, column, row_offset;
row_offset = 0;
for (row = 0; row < 4; ++row) {
row_offset = row * 4;
for (column = 0; column < 4; ++column) {
out[row_offset + column] = m[row + column * 4];
}
}
return out;
},
multiplyMatrices: function (m1, m2) {
// Perform matrix product { out = m1 * m2;}
var out = [];
var row, column, row_offset;
row_offset = 0;
for (row = 0; row < 4; ++row) {
row_offset = row * 4;
for (column = 0; column < 4; ++column) {
out[row_offset + column] =
(m1[row_offset + 0] * m2[column + 0]) +
(m1[row_offset + 1] * m2[column + 4]) +
(m1[row_offset + 2] * m2[column + 8]) +
(m1[row_offset + 3] * m2[column + 12]);
}
}
return out;
},
multiplyMatrixVector: function (m, v) {
/* Mutiplies a matrix [m] by a vector [v] */
var out = [];
var row, row_offset;
row_offset = 0;
for (row = 0; row < 4; ++row) {
row_offset = row * 4;
out[row] =
(m[row_offset + 0] * v[0]) +
(m[row_offset + 1] * v[1]) +
(m[row_offset + 2] * v[2]) +
(m[row_offset + 3] * v[3]);
}
return out;
},
crossVector: function(u, v){
/* cross product of vectors [u] and [v] */
var out = [u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0]];
return out;
},
normalizeVector3: function(v){
/* normalize vector [v] */
var len = Math.sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
var out = [v[0]/len, v[1]/len, v[2]/len];
return out;
},
subtractVector3: function(u, v){
/* subtract vector [v] from vector [u] */
let result = [];
for (let i = 0; i < 3; i++) {
result.push(u[i] - v[i]);
}
return result;
},
//*** MODEL MATRIX OPERATIONS
MakeTranslateMatrix: function (dx, dy, dz) {
// Create a transform matrix for a translation of ({dx}, {dy}, {dz}).
var out = this.identityMatrix();
out[3] = dx;
out[7] = dy;
out[11] = dz;
return out;
},
MakeRotateXMatrix: function (a) {
// Create a transform matrix for a rotation of {a} along the X axis.
var out = this.identityMatrix();
var adeg = this.degToRad(a);
var c = Math.cos(adeg);
var s = Math.sin(adeg);
out[5] = out[10] = c;
out[6] = -s;
out[9] = s;
return out;
},
MakeRotateYMatrix: function (a) {
// Create a transform matrix for a rotation of {a} along the Y axis.
var out = this.identityMatrix();
var adeg = this.degToRad(a);
var c = Math.cos(adeg);
var s = Math.sin(adeg);
out[0] = out[10] = c;
out[2] = -s;
out[8] = s;
return out;
},
MakeRotateZMatrix: function (a) {
// Create a transform matrix for a rotation of {a} along the Z axis.
var out = this.identityMatrix();
var adeg = this.degToRad(a);
var c = Math.cos(adeg);
var s = Math.sin(adeg);
out[0] = out[5] = c;
out[4] = -s;
out[1] = s;
return out;
},
MakeScaleMatrix: function (s) {
// Create a transform matrix for proportional scale
var out = this.identityMatrix();
out[0] = out[5] = out[10] = s;
return out;
},
//***Projection Matrix operations
MakeWorld: function (tx, ty, tz, rx, ry, rz, s) {
//Creates a world matrix for an object.
var Rx = this.MakeRotateXMatrix(ry);
var Ry = this.MakeRotateYMatrix(rx);
var Rz = this.MakeRotateZMatrix(rz);
var S = this.MakeScaleMatrix(s);
var T = this.MakeTranslateMatrix(tx, ty, tz);
out = this.multiplyMatrices(Rz, S);
out = this.multiplyMatrices(Ry, out);
out = this.multiplyMatrices(Rx, out);
out = this.multiplyMatrices(T, out);
return out;
},
MakeView: function (cx, cy, cz, elev, ang) {
// Creates in {out} a view matrix. The camera is centerd in ({cx}, {cy}, {cz}).
// It looks {ang} degrees on y axis, and {elev} degrees on the x axis.
var T = [];
var Rx = [];
var Ry = [];
var tmp = [];
var out = [];
T = this.MakeTranslateMatrix(-cx, -cy, -cz);
Rx = this.MakeRotateXMatrix(-elev);
Ry = this.MakeRotateYMatrix(-ang);
tmp = this.multiplyMatrices(Ry, T);
out = this.multiplyMatrices(Rx, tmp);
return out;
},
MakePerspective: function (fovy, a, n, f) {
// Creates the perspective projection matrix. The matrix is returned.
// {fovy} contains the vertical field-of-view in degrees. {a} is the aspect ratio.
// {n} is the distance of the near plane, and {f} is the far plane.
var perspective = this.identityMatrix();
var halfFovyRad = this.degToRad(fovy / 2); // stores {fovy/2} in radiants
var ct = 1.0 / Math.tan(halfFovyRad); // cotangent of {fov/2}
perspective[0] = ct / a;
perspective[5] = ct;
perspective[10] = (f + n) / (n - f);
perspective[11] = 2.0 * f * n / (n - f);
perspective[14] = -1.0;
perspective[15] = 0.0;
return perspective;
}
}