CTMLoader.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Loader for CTM encoded models generated by OpenCTM tools:
  3. * http://openctm.sourceforge.net/
  4. *
  5. * Uses js-openctm library by Juan Mellado
  6. * http://code.google.com/p/js-openctm/
  7. *
  8. * @author alteredq / http://alteredqualia.com/
  9. */
  10. THREE.CTMLoader = function ( ) {
  11. };
  12. THREE.CTMLoader.prototype = new THREE.CTMLoader();
  13. THREE.CTMLoader.prototype.constructor = THREE.CTMLoader;
  14. // Load CTMLoader compressed models
  15. // - parameters
  16. // - url (required)
  17. // - callback (required)
  18. THREE.CTMLoader.prototype.load = function( url, callback, useWorker ) {
  19. var xhr = new XMLHttpRequest(),
  20. callbackProgress = null;
  21. var length = 0;
  22. xhr.onreadystatechange = function() {
  23. if ( xhr.readyState == 4 ) {
  24. if ( xhr.status == 200 || xhr.status == 0 ) {
  25. var binaryData = xhr.responseText;
  26. var s = Date.now();
  27. if ( useWorker ) {
  28. var worker = new Worker( "js/ctm/CTMWorker.js" );
  29. worker.onmessage = function( event ) {
  30. var ctmFile = event.data;
  31. THREE.CTMLoader.prototype.createModel( ctmFile, callback );
  32. var e = Date.now();
  33. console.log( "CTM data parse time [worker]: " + (e-s) + " ms" );
  34. };
  35. worker.postMessage( binaryData );
  36. } else {
  37. var ctmFile = new CTM.File( new CTM.Stream( binaryData ) );
  38. THREE.CTMLoader.prototype.createModel( ctmFile, callback );
  39. var e = Date.now();
  40. console.log( "CTM data parse time [inline]: " + (e-s) + " ms" );
  41. }
  42. } else {
  43. console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
  44. }
  45. } else if ( xhr.readyState == 3 ) {
  46. if ( callbackProgress ) {
  47. if ( length == 0 ) {
  48. length = xhr.getResponseHeader( "Content-Length" );
  49. }
  50. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  51. }
  52. } else if ( xhr.readyState == 2 ) {
  53. length = xhr.getResponseHeader( "Content-Length" );
  54. }
  55. }
  56. xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  57. xhr.open( "GET", url, true );
  58. xhr.send( null );
  59. };
  60. THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
  61. var Model = function ( texture_path ) {
  62. var scope = this;
  63. scope.materials = [];
  64. THREE.Geometry.call( this );
  65. var normals = [],
  66. uvs = [],
  67. colors = [];
  68. init_vertices( file.body.vertices );
  69. if ( file.body.normals !== undefined )
  70. init_normals( file.body.normals );
  71. if ( file.body.uvMaps !== undefined && file.body.uvMaps.length > 0 )
  72. init_uvs( file.body.uvMaps[ 0 ].uv );
  73. if ( file.body.attrMaps !== undefined && file.body.attrMaps.length > 0 && file.body.attrMaps[ 0 ].name === "Color" )
  74. init_colors( file.body.attrMaps[ 0 ].attr );
  75. var hasNormals = normals.length > 0 ? true : false,
  76. hasUvs = uvs.length > 0 ? true : false,
  77. hasColors = colors.length > 0 ? true : false;
  78. init_faces( file.body.indices );
  79. this.computeCentroids();
  80. this.computeFaceNormals();
  81. //this.computeTangents();
  82. function init_vertices( buffer ) {
  83. var x, y, z, i, il = buffer.length;
  84. for( i = 0; i < il; i += 3 ) {
  85. x = buffer[ i ];
  86. y = buffer[ i + 1 ];
  87. z = buffer[ i + 2 ];
  88. vertex( scope, x, y, z );
  89. }
  90. };
  91. function init_normals( buffer ) {
  92. var x, y, z, i, il = buffer.length;
  93. for( i = 0; i < il; i += 3 ) {
  94. x = buffer[ i ];
  95. y = buffer[ i + 1 ];
  96. z = buffer[ i + 2 ];
  97. normals.push( x, y, z );
  98. }
  99. };
  100. function init_colors( buffer ) {
  101. var r, g, b, a, i, il = buffer.length;
  102. for( i = 0; i < il; i += 4 ) {
  103. r = buffer[ i ];
  104. g = buffer[ i + 1 ];
  105. b = buffer[ i + 2 ];
  106. a = buffer[ i + 3 ];
  107. var color = new THREE.Color();
  108. color.setRGB( r, g, b );
  109. colors.push( color );
  110. }
  111. };
  112. function init_uvs( buffer ) {
  113. var u, v, i, il = buffer.length;
  114. for( i = 0; i < il; i += 2 ) {
  115. u = buffer[ i ];
  116. v = buffer[ i + 1 ];
  117. uvs.push( u, 1 - v );
  118. }
  119. };
  120. function init_faces( buffer ) {
  121. var a, b, c,
  122. u1, v1, u2, v2, u3, v3,
  123. m, face,
  124. i, il = buffer.length;
  125. m = 0; // all faces defaulting to material 0
  126. for( i = 0; i < il; i += 3 ) {
  127. a = buffer[ i ];
  128. b = buffer[ i + 1 ];
  129. c = buffer[ i + 2 ];
  130. if ( hasNormals )
  131. face = f3n( scope, normals, a, b, c, m, a, b, c );
  132. else
  133. face = f3( scope, a, b, c, m );
  134. if ( hasColors ) {
  135. face.vertexColors[ 0 ] = colors[ a ];
  136. face.vertexColors[ 1 ] = colors[ b ];
  137. face.vertexColors[ 2 ] = colors[ c ];
  138. }
  139. if ( hasUvs ) {
  140. u1 = uvs[ a * 2 ];
  141. v1 = uvs[ a * 2 + 1 ];
  142. u2 = uvs[ b * 2 ];
  143. v2 = uvs[ b * 2 + 1 ];
  144. u3 = uvs[ c * 2 ];
  145. v3 = uvs[ c * 2 + 1 ];
  146. uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  147. }
  148. }
  149. }
  150. };
  151. function vertex ( scope, x, y, z ) {
  152. scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
  153. };
  154. function f3 ( scope, a, b, c, mi ) {
  155. var face = new THREE.Face3( a, b, c, null, null, mi );
  156. scope.faces.push( face );
  157. return face;
  158. };
  159. function f3n ( scope, normals, a, b, c, mi, na, nb, nc ) {
  160. var nax = normals[ na * 3 ],
  161. nay = normals[ na * 3 + 1 ],
  162. naz = normals[ na * 3 + 2 ],
  163. nbx = normals[ nb * 3 ],
  164. nby = normals[ nb * 3 + 1 ],
  165. nbz = normals[ nb * 3 + 2 ],
  166. ncx = normals[ nc * 3 ],
  167. ncy = normals[ nc * 3 + 1 ],
  168. ncz = normals[ nc * 3 + 2 ];
  169. var na = new THREE.Vector3( nax, nay, naz ),
  170. nb = new THREE.Vector3( nbx, nby, nbz ),
  171. nc = new THREE.Vector3( ncx, ncy, ncz );
  172. var face = new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi );
  173. scope.faces.push( face );
  174. return face;
  175. };
  176. function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
  177. var uv = [];
  178. uv.push( new THREE.UV( u1, v1 ) );
  179. uv.push( new THREE.UV( u2, v2 ) );
  180. uv.push( new THREE.UV( u3, v3 ) );
  181. where.push( uv );
  182. };
  183. Model.prototype = new THREE.Geometry();
  184. Model.prototype.constructor = Model;
  185. callback( new Model() );
  186. };