ObJS.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /********************************************************************
  2. * The ObJS objectnitializes the canvas, assignin it to the HTML *
  3. * element, and set the required mouse events. *
  4. * #parameters: none *
  5. * #return: nothing *
  6. ********************************************************************/
  7. function ObJS(){
  8. /********************************************************************
  9. * Parameters for costumization. *
  10. ********************************************************************/
  11. var backColor = "rgba(200, 200, 200, 1)";
  12. var vertColor = "rgba(10, 10, 10, 1)";
  13. var edgeColor = "rgba(100, 70, 70, 1)";
  14. var faceColor = "rgba(180, 180, 255, .6)";
  15. var faceAlpha = .6;
  16. var textColor = "rgba(0, 0, 0, 1)";
  17. var vertSize = 4; //Make it even: at small scales will look better
  18. var rotationSpeed = 90; //Smaller = faster. 100 is a good speed
  19. var zoomSpeed = 0.161; //0-1. 0.2 or 0.3 are good speeds
  20. var moveSpeed = .5;
  21. /********************************************************************
  22. * Variable containing this object, to be refered from nested *
  23. * functions. *
  24. ********************************************************************/
  25. var thisObJS;
  26. /********************************************************************
  27. * Parameter used to scale the measures in the .obj file. Can be *
  28. * integer or float, greater than 0. 50 is a good default. *
  29. ********************************************************************/
  30. var scale;
  31. /********************************************************************
  32. * Variable to determine if the canvas has been initialized. *
  33. ********************************************************************/
  34. var canvasInitialized = false;
  35. /********************************************************************
  36. * Variable to determine if the mouse button is being holded down. *
  37. ********************************************************************/
  38. var mDown = false;
  39. /********************************************************************
  40. * Variables containing the canvas and its context. *
  41. ********************************************************************/
  42. var canvas;
  43. var ctx;
  44. /********************************************************************
  45. * Variables containing the number of elements. *
  46. ********************************************************************/
  47. var totalVert;
  48. var totalMaterial = 0;
  49. var totalFace
  50. /********************************************************************
  51. * Arrays containing the vertices, faces and materials. *
  52. ********************************************************************/
  53. var material;
  54. var vert;
  55. var face;
  56. /********************************************************************
  57. * Booleans to determine wich elements are to draw. *
  58. ********************************************************************/
  59. var dVerts = true;
  60. var dEdges = true;
  61. var dFaces = true;
  62. var dBackg = true;
  63. /********************************************************************
  64. * Booleans to indicate if mtl file should be used. *
  65. ********************************************************************/
  66. var useMtl = true;
  67. /********************************************************************
  68. * Global variables that will contain the position of the mouse in *
  69. * the previous frame to calculate rotation. *
  70. ********************************************************************/
  71. var pX = null;
  72. var pY = null;
  73. /********************************************************************
  74. * Function that initializes the canvas, assignin it to the HTML *
  75. * element, and set the required mouse events. *
  76. * #parameters: *
  77. * canv (canvas): canvas to use. If omited, the one named *
  78. * named "ObJSCanvas" will be used. *
  79. * #return: nothing *
  80. * #scope: public *
  81. ********************************************************************/
  82. this.initCanvas = function(canv){
  83. if (!canvasInitialized){
  84. if (canv!= null)
  85. canvas = canv;
  86. else
  87. canvas = document.getElementById("ObJSCanvas");
  88. ctx = canvas.getContext("2d");
  89. ctx.translate(canvas.width / 2, canvas.height / 2);
  90. canvas.onmousedown = function(e){
  91. pX = e.offsetX==undefined?e.layerX:e.offsetX;
  92. pY = e.offsetY==undefined?e.layerY:e.offsetY;
  93. mDown = true;
  94. };
  95. canvas.onmouseup = function(e){
  96. if(mDown)
  97. mouseClick(e);
  98. pX = null;
  99. pY = null;
  100. mDown = false;
  101. };
  102. canvas.onmousemove = function(e){
  103. if(!mDown)
  104. return;
  105. var x = e.offsetX==undefined?e.layerX:e.offsetX;
  106. var y = e.offsetY==undefined?e.layerY:e.offsetY;
  107. if (e.shiftKey){
  108. thisObJS.moveHorizontal(x - pX);
  109. thisObJS.moveVertical(y - pY);
  110. }
  111. else{
  112. thisObJS.rotateY(x - pX);
  113. thisObJS.rotateX(y - pY);
  114. }
  115. pX = x;
  116. pY = y;
  117. return false;
  118. };
  119. canvas.addEventListener('mousewheel',function(event){
  120. if (event.wheelDelta > 0)
  121. thisObJS.zoom("-");
  122. else
  123. thisObJS.zoom("+");
  124. return false;
  125. }, false);
  126. canvasInitialized = true;
  127. }
  128. };
  129. /********************************************************************
  130. * Main function, called every time a file is to be loaded. *
  131. * #parameters: *
  132. * file (string): name of the file, without path or extension. *
  133. * #return: nothing *
  134. * #scope: public *
  135. ********************************************************************/
  136. this.load = function(file){
  137. if(!canvasInitialized)
  138. initCanvas();
  139. //Get obj file
  140. var xmlhttp = new XMLHttpRequest();
  141. xmlhttp.open("GET", file, false);
  142. xmlhttp.send();
  143. var fileContent = xmlhttp.responseText;
  144. //Get mtl file
  145. xmlhttp = new XMLHttpRequest();
  146. xmlhttp.open("GET",file.substring(0, file.lastIndexOf(".")) + ".mtl", false);
  147. xmlhttp.send();
  148. var mtlContent = xmlhttp.responseText;
  149. initArrays();
  150. readFile(fileContent);
  151. readMtlFile(mtlContent);
  152. draw();
  153. };
  154. /********************************************************************
  155. * Functions switching the elements drawn (dVerts, dEdges, dFaces). *
  156. * #parameters: *
  157. * on (boolean): indicating if the element is to be drawn. *
  158. * #return: nothing *
  159. * #scope: public *
  160. ********************************************************************/
  161. this.drawVertizes = function(on){
  162. if (on == true || on == false){
  163. dVerts = on;
  164. draw();
  165. }
  166. };
  167. this.drawEdges = function(on){
  168. if (on == true || on == false){
  169. dEdges = on;
  170. draw();
  171. }
  172. };
  173. this.drawFaces = function(on){
  174. if (on == true || on == false){
  175. dFaces = on;
  176. draw();
  177. }
  178. };
  179. this.drawBackground = function(on){
  180. if (on == true || on == false){
  181. dBackg = on;
  182. draw();
  183. }
  184. };
  185. /********************************************************************
  186. * Functions switching the color of the elements. *
  187. * #parameters: *
  188. * code (string): hex color value, in #fff or #ffffff form. If *
  189. * wrong, a message will be printed to console, *
  190. * and nothing will be changed. *
  191. * #return: nothing *
  192. * #scope: public *
  193. ********************************************************************/
  194. this.setVertizesColor = function(code){
  195. if (isColor(code)){
  196. var color = hexToRgb(code);
  197. vertColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  198. draw();
  199. }
  200. else
  201. console.log("setVertizesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  202. };
  203. this.setEdgesColor = function(code){
  204. if (isColor(code)){
  205. var color = hexToRgb(code);
  206. edgeColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  207. draw();
  208. }
  209. else
  210. console.log("setEdgesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  211. };
  212. this.setFacesColor = function(code){
  213. if (isColor(code)){
  214. var color = hexToRgb(code);
  215. faceColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  216. draw();
  217. }
  218. else
  219. console.log("setFacesColor(" + code + ") ERROR: " + code + " not a valid hex color");
  220. };
  221. this.setBackgroundColor = function(code){
  222. if (isColor(code)){
  223. var color = hexToRgb(code);
  224. backColor = "rgba(" + color.r + ", " + color.g + ", " + color.b + ", 1)";
  225. draw();
  226. }
  227. else
  228. console.log("setBackgroundColor(" + code + ") ERROR: " + code + " not a valid hex color");
  229. };
  230. /********************************************************************
  231. * Function switching the use of mtl file for color. *
  232. * #parameters: *
  233. * value (boolean): indicating if the element is to be drawn. *
  234. * #return: nothing *
  235. * #scope: public *
  236. ********************************************************************/
  237. this.linkMaterial = function(value){
  238. useMtl = value;
  239. draw();
  240. }
  241. /********************************************************************
  242. * Function switching the alpha value of the faces. *
  243. * #parameters: *
  244. * percent (int): Value (0-100) of alpha. Must be processed (0-1) *
  245. * #return: nothing *
  246. * #scope: public *
  247. ********************************************************************/
  248. this.setAlpha = function(percent){
  249. if (percent >= 0 && percent <= 100){
  250. faceAlpha = percent / 100;
  251. faceColor = faceColor.substring(0, faceColor.lastIndexOf(",") + 2) + faceAlpha + ")";
  252. draw();
  253. }
  254. else
  255. console.log("setAlpha(" + percent + "); ERROR: Only values between 0 and 100 are allowed");
  256. };
  257. /********************************************************************
  258. * Function switching the rotation speed. *
  259. * #parameters: *
  260. * percent (int): Value (0-10) of speed. *
  261. * #return: nothing *
  262. * #scope: public *
  263. ********************************************************************/
  264. this.setRotationSpeed = function(percent){
  265. if (percent >= 0 && percent <= 10){
  266. rotationSpeed = Math.round((10 - percent) * 30);
  267. if (rotationSpeed == 0)
  268. rotationSpeed = 1;
  269. }
  270. else
  271. console.log("setRotationSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  272. }
  273. /********************************************************************
  274. * Functions that rotate elements in the canvas along the *
  275. * correspondent axys, calculating the new position for each vertex *
  276. * in the vert array. *
  277. * #parameters: *
  278. * des (int): distance to rotate. *
  279. * #return: nothing *
  280. * #scope: public *
  281. ********************************************************************/
  282. this.rotateY = function(des){
  283. for (var i = 0; i < vert.length; i ++) {
  284. var x = vert[i][0];
  285. var z = vert[i][2];
  286. vert[i][0] = x * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  287. vert[i][2] = z * Math.cos(des / rotationSpeed) + x * Math.sin(des / rotationSpeed);
  288. }
  289. draw();
  290. };
  291. this.rotateX = function(des){
  292. for (var i = 0; i < vert.length; i ++) {
  293. var y = vert[i][1];
  294. var z = vert[i][2];
  295. vert[i][1] = y * Math.cos(des / rotationSpeed) - z * Math.sin(des / rotationSpeed);
  296. vert[i][2] = z * Math.cos(des / rotationSpeed) + y * Math.sin(des / rotationSpeed);
  297. }
  298. draw();
  299. };
  300. /********************************************************************
  301. * Function switching the movement speed of the model. *
  302. * #parameters: *
  303. * percent (int): Value (0-10) of speed. *
  304. * #return: nothing *
  305. * #scope: public *
  306. ********************************************************************/
  307. this.setMovementSpeed = function(percent){
  308. if (percent >= 0 && percent <= 10)
  309. moveSpeed = ((percent * 0.8) / 10) + 0.01;
  310. else
  311. console.log("setMoveSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  312. }
  313. /********************************************************************
  314. * Functions that move elements in the canvas along the selected *
  315. * direction, calculating the new position for each vertex in the *
  316. * vert array. *
  317. * #parameters: *
  318. * des (int): distance to move. *
  319. * #return: nothing *
  320. * #scope: public *
  321. ********************************************************************/
  322. this.moveVertical = function(des){
  323. for (var i = 0; i < vert.length; i ++)
  324. vert[i][1] = (vert[i][1] * 1) + des * (moveSpeed / 50);
  325. draw();
  326. };
  327. this.moveHorizontal = function(des){
  328. for (var i = 0; i < vert.length; i ++)
  329. vert[i][0] = (vert[i][0] * 1) + des * (moveSpeed / 50);
  330. draw();
  331. };
  332. /********************************************************************
  333. * Function switching the zoom speede of the faces. *
  334. * #parameters: *
  335. * percent (int): Value (0-100) of speed. *
  336. * #return: nothing *
  337. * #scope: public *
  338. ********************************************************************/
  339. this.setZoomSpeed = function(percent){
  340. if (percent >= 0 && percent <= 10)
  341. zoomSpeed = ((percent * 0.8) / 10) + 0.01;
  342. else
  343. console.log("setZoomSpeed(" + percent + "); ERROR: Only values between 0 and 10 are allowed");
  344. }
  345. /********************************************************************
  346. * Function that changes the scale variable, witch results in a *
  347. * zoom in or zoom out effect. *
  348. * #parameters: none *
  349. * #return: nothing *
  350. * #scope: public *
  351. ********************************************************************/
  352. this.zoom = function(dir){
  353. if (dir == "-")
  354. scale = Math.round(scale * (1 + zoomSpeed));
  355. if (dir == "+")
  356. scale = Math.round(scale * (1 - zoomSpeed));
  357. draw();
  358. };
  359. /********************************************************************
  360. * Function that clears the arrays and the canvas. Use to unload a *
  361. * model. *
  362. * #parameters: none *
  363. * #return: nothing *
  364. * #scope: public *
  365. ********************************************************************/
  366. this.clear = function(){
  367. initArrays();
  368. clearCanvas();
  369. };
  370. /********************************************************************
  371. * Function that turns a hex color code in RGB. *
  372. * stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb *
  373. * #parameters: *
  374. * hex (string): color code, for example "#33ff99" *
  375. * #return: (Array) r, g, b, with their correspondent color. *
  376. * #scope: private *
  377. ********************************************************************/
  378. var hexToRgb = function(hex) {
  379. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  380. return result ? {
  381. r: parseInt(result[1], 16),
  382. g: parseInt(result[2], 16),
  383. b: parseInt(result[3], 16)
  384. } : null;
  385. };
  386. /********************************************************************
  387. * Function that checks if a string is a valid hexadecimal color *
  388. * code, in the format '#ffffff' or '#fff' *
  389. * #parameters: *
  390. * code (string): Code to check. *
  391. * #return: (Boolean) True if valid color code, false otherwise. *
  392. * #scope: private *
  393. ********************************************************************/
  394. var isColor = function(code){
  395. if (code[0] != '#')
  396. return false;
  397. if (code.length != 4 && code.length != 7)
  398. return false
  399. for (var i = 1; i < code.length; i ++)
  400. if (code[i] < '0' || (code[i] > 9 && code[i] < 'A') || (code[i] > 'Z' && code[i] < 'a') || code[i] > 'z')
  401. return false;
  402. return true;
  403. };
  404. /********************************************************************
  405. * Function that clears the canvas and gets it ready to draw on it. *
  406. * #parameters: none *
  407. * #return: nothing *
  408. * #scope: private *
  409. ********************************************************************/
  410. var clearCanvas = function(){
  411. ctx.clearRect(0, 0, canvas.width, canvas.height);
  412. if (dBackg)
  413. ctx.fillStyle = backColor;
  414. else
  415. ctx.fillStyle = "#ffffff";
  416. ctx.fillRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
  417. };
  418. /********************************************************************
  419. * Function initializing the arrays obj, vert and face as empty *
  420. * arrays. *
  421. * #parameters: none *
  422. * #return: nothing *
  423. * #scope: private *
  424. ********************************************************************/
  425. var initArrays = function(){
  426. material = null;
  427. vert = null;
  428. face = null;
  429. material = new Array();
  430. vert = new Array();
  431. face = new Array();
  432. };
  433. /********************************************************************
  434. * Function tat populates the vert[] array with and array of three *
  435. * elements, containig the coordinates, and the face[] array with *
  436. * the vertizes forming the face, plus one element indicating the *
  437. * codename of the material. *
  438. * #parameters: *
  439. * text (string): the content of the obj file. *
  440. * #return: nothing *
  441. * #scope: private *
  442. ********************************************************************/
  443. var readFile = function(text){
  444. var line;
  445. var v = 0;
  446. var f = 0;
  447. var m = 0;
  448. var max = 0;
  449. var j;
  450. var dist;
  451. var mat = "null";
  452. text = text + "\n";
  453. while (text.indexOf("\n") != -1){
  454. line = text.substring(0, text.indexOf("\n"));
  455. if (line.substring(0, 2) == "v "){
  456. vert[v] = new Array(3);
  457. line = line.substring(2);
  458. vert[v][0] = line.substring(0, line.indexOf(" "));
  459. line = line.substring(line.indexOf(" ") + 1);
  460. vert[v][1] = line.substring(0, line.indexOf(" "));
  461. line = line.substring(line.indexOf(" ") + 1);
  462. vert[v][2] = line;
  463. dist = Math.sqrt(vert[v][0] * vert[v][0] + vert[v][1] * vert[v][1] + vert[v][2] * vert[v][2]);
  464. if (dist > max)
  465. max = dist;
  466. v = v + 1;
  467. }
  468. else if (line.substring(0,2) == "f "){
  469. line = line.substring(2);
  470. face[f] = new Array();
  471. j = 0;
  472. while(line.indexOf(" ") != -1){
  473. face[f][j] = line.substring(0, line.indexOf(" ")) - 1;
  474. line = line.substring(line.indexOf(" ") + 1);
  475. j = j + 1;
  476. }
  477. face[f][j] = line - 1;
  478. face[f][j + 1] = mat;
  479. f = f + 1;
  480. }
  481. else if (line.substring(0, 6) == "usemtl"){
  482. mat = line.substring(7);
  483. }
  484. text = text.substring(text.indexOf("\n") + 1);
  485. }
  486. scale = calculateScale(max);
  487. totalVert = vert.length;
  488. totalFace = face.length;
  489. };
  490. /********************************************************************
  491. * Function tat populates the material[] array with an array *
  492. * containig, four values: the material name, and the three RGB *
  493. * values. *
  494. * #parameters: *
  495. * text (string): the content of the mtl file. *
  496. * #return: nothing *
  497. * #scope: private *
  498. ********************************************************************/
  499. var readMtlFile = function(text){
  500. text = text + "\n";
  501. i = -1;
  502. while (text.indexOf("\n") != -1){
  503. line = text.substring(0, text.indexOf("\n"));
  504. if (line.substring(0, 6) == "newmtl"){
  505. i = i + 1;
  506. material[i] = new Array(4);
  507. material[i][0] = line.substring(line.indexOf(" ") + 1);
  508. }
  509. //Kd, ddifuse color, usefull
  510. //Ka, ambient color, not now
  511. //Ks, specular color, not now
  512. //d or TR, transparency, not now
  513. else if (line.substring(0, 3) == "Kd "){
  514. line = line.substring(3);
  515. material[i][1] = line.substring(0, line.indexOf(" "));
  516. line = line.substring(line.indexOf(" ") + 1);
  517. material[i][2] = line.substring(0, line.indexOf(" "));
  518. line = line.substring(line.indexOf(" ") + 1);
  519. material[i][3] = line;
  520. }
  521. text = text.substring(text.indexOf("\n") + 1);
  522. }
  523. totalMaterial = material.length;
  524. };
  525. /********************************************************************
  526. * Function that automatically calculates the best scale for the *
  527. * modelermined number of elements, containing the verices that *
  528. * form a face, so the bigest dimension from the object can fit in *
  529. * the smallest dimension of the canvas. *
  530. * #parameters: *
  531. * text (int): the farthest vertex, from the center. *
  532. * #return: (int) The scale to be aplied. *
  533. * #scope: private *
  534. ********************************************************************/
  535. var calculateScale = function(max){
  536. var dim;
  537. if (canvas.width < canvas.height)
  538. dim = canvas.width;
  539. else
  540. dim = canvas.height;
  541. var sc = Math.round((0.95 * dim) / (2 * max));
  542. return sc;
  543. };
  544. /********************************************************************
  545. * Function that compares the average Z coordinate of two faces. *
  546. * Must be used when sorting the face array. *
  547. * #parameters: *
  548. * a (Array): One face. *
  549. * b (Array): The other face. *
  550. * #return: (int) -1, 1 or 0. *
  551. * #scope: private *
  552. ********************************************************************/
  553. var faceComparator = function(a,b){
  554. var aZ;
  555. var bZ;
  556. var i = 0;
  557. var sum = 0.0;
  558. while (i < a.length - 1){
  559. sum = sum + parseFloat(vert[a[i]][2]);
  560. i = i + 1;
  561. }
  562. aZ = sum / parseFloat(i);
  563. i = 0;
  564. sum = 0;
  565. while (i < b.length - 1){
  566. sum = sum + parseFloat(1 * vert[b[i]][2]);
  567. i = i + 1;
  568. }
  569. bZ = sum / parseFloat(i);
  570. if (aZ < bZ) return -1;
  571. if (aZ > bZ) return 1;
  572. return 0;
  573. };
  574. /********************************************************************
  575. * Function that writes text in the corner of the canvas. Must be *
  576. * called when everithing has been drawn. *
  577. * #parameters: none *
  578. * #return: nothing *
  579. * #scope: private *
  580. ********************************************************************/
  581. var writeCredits = function(){
  582. ctx.fillStyle = textColor;
  583. ctx.font = "12px Arial";
  584. var h = canvas.height / 2 - 14;
  585. var w = 10 - (canvas.width / 2);
  586. ctx.fillText("ObJS: " + totalVert + " vertizes " + totalFace + " faces " + totalMaterial + " materials", w, h);
  587. };
  588. /********************************************************************
  589. * Function that draws th vertizes, faces and edges in the canvas. *
  590. * #parameters: none *
  591. * #return: nothing *
  592. * #scope: private *
  593. ********************************************************************/
  594. var draw = function(){
  595. face = face.sort(faceComparator).reverse();
  596. clearCanvas();
  597. for (var i = 0; i < face.length; i++){
  598. ctx.strokeStyle = edgeColor;
  599. ctx.fillStyle = vertColor;
  600. ctx.beginPath();
  601. ctx.moveTo(scale * vert[face[i][0]][0], scale * vert[face[i][0]][1]);
  602. for (var j = 1; j < face[i].length - 1; j++) {
  603. ctx.lineTo(scale * vert[face[i][j]][0], scale * vert[face[i][j]][1]);
  604. if(dEdges)
  605. ctx.stroke();
  606. if(dVerts){
  607. var x = scale * vert[face[i][j]][0] - (vertSize / 2);
  608. var w = vertSize;
  609. var y = scale * vert[face[i][j]][1] - (vertSize / 2);
  610. var h = vertSize;
  611. ctx.fillRect(x, y, w, h);
  612. }
  613. }
  614. ctx.closePath();
  615. if (useMtl){
  616. //Get material with the same name as the one in the last element of the face
  617. j = 0;
  618. while (material[j][0] != face[i][face[i].length - 1] && j < material.length)
  619. j = j + 1;
  620. if (material[j][0] == face[i][face[i].length - 1])
  621. ctx.fillStyle = "rgba(" + Math.round(255 * material[j][1]) + ", " + Math.round(255 * material[j][2]) + ", " + Math.round(255 * material[j][3]) + ", " + faceAlpha + ")";
  622. else
  623. ctx.fillStyle = faceColor;
  624. }
  625. else
  626. ctx.fillStyle = faceColor;
  627. if(dFaces)
  628. ctx.fill();
  629. }
  630. writeCredits();
  631. };
  632. /********************************************************************
  633. * TODO: Function used when something in the canvas is clicked. *
  634. * #parameters: none *
  635. * #return: nothing *
  636. * #scope: private *
  637. ********************************************************************/
  638. var mouseClick = function(){
  639. };
  640. thisObJS = this;
  641. return this;
  642. }