CTMLoader.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 ) {
  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. THREE.CTMLoader.prototype.createModel( xhr.responseText, callback );
  26. } else {
  27. alert( "Couldn't load [" + url + "] [" + xhr.status + "]" );
  28. }
  29. } else if ( xhr.readyState == 3 ) {
  30. if ( callbackProgress ) {
  31. if ( length == 0 ) {
  32. length = xhr.getResponseHeader( "Content-Length" );
  33. }
  34. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  35. }
  36. } else if ( xhr.readyState == 2 ) {
  37. length = xhr.getResponseHeader( "Content-Length" );
  38. }
  39. }
  40. xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  41. xhr.open( "GET", url, true );
  42. xhr.send( null );
  43. };
  44. THREE.CTMLoader.prototype.createModel = function ( data, callback ) {
  45. var Model = function ( texture_path ) {
  46. var s = (new Date).getTime();
  47. var scope = this;
  48. scope.materials = [];
  49. THREE.Geometry.call( this );
  50. var file = new CTM.File( new CTM.Stream( data ) );
  51. console.log( file );
  52. var normals = [],
  53. uvs = [],
  54. colors = [];
  55. init_vertices( file.body.vertices );
  56. if ( file.body.normals !== undefined )
  57. init_normals( file.body.normals );
  58. if ( file.body.uvMaps !== undefined && file.body.uvMaps.length > 0 )
  59. init_uvs( file.body.uvMaps[ 0 ].uv );
  60. if ( file.body.attrMaps !== undefined && file.body.attrMaps.length > 0 && file.body.attrMaps[ 0 ].name === "Color" )
  61. init_colors( file.body.attrMaps[ 0 ].attr );
  62. var hasNormals = normals.length > 0 ? true : false,
  63. hasUvs = uvs.length > 0 ? true : false,
  64. hasColors = colors.length > 0 ? true : false;
  65. init_faces( file.body.indices );
  66. this.computeCentroids();
  67. this.computeFaceNormals();
  68. //this.computeTangents();
  69. var e = (new Date).getTime();
  70. console.log( "CTM data parse time: " + (e-s) + " ms" );
  71. function init_vertices( buffer ) {
  72. var x, y, z, i, il = buffer.length;
  73. for( i = 0; i < il; i += 3 ) {
  74. x = buffer[ i ];
  75. y = buffer[ i + 1 ];
  76. z = buffer[ i + 2 ];
  77. vertex( scope, x, y, z );
  78. }
  79. };
  80. function init_normals( buffer ) {
  81. var x, y, z, i, il = buffer.length;
  82. for( i = 0; i < il; i += 3 ) {
  83. x = buffer[ i ];
  84. y = buffer[ i + 1 ];
  85. z = buffer[ i + 2 ];
  86. normals.push( x, y, z );
  87. }
  88. };
  89. function init_colors( buffer ) {
  90. var r, g, b, a, i, il = buffer.length;
  91. for( i = 0; i < il; i += 4 ) {
  92. r = buffer[ i ];
  93. g = buffer[ i + 1 ];
  94. b = buffer[ i + 2 ];
  95. a = buffer[ i + 3 ];
  96. var color = new THREE.Color();
  97. color.setRGB( r, g, b );
  98. colors.push( color );
  99. }
  100. };
  101. function init_uvs( buffer ) {
  102. var u, v, i, il = buffer.length;
  103. for( i = 0; i < il; i += 2 ) {
  104. u = buffer[ i ];
  105. v = buffer[ i + 1 ];
  106. uvs.push( u, 1 - v );
  107. }
  108. };
  109. function init_faces( buffer ) {
  110. var a, b, c,
  111. u1, v1, u2, v2, u3, v3,
  112. m, face,
  113. i, il = buffer.length;
  114. m = 0; // all faces defaulting to material 0
  115. for( i = 0; i < il; i += 3 ) {
  116. a = buffer[ i ];
  117. b = buffer[ i + 1 ];
  118. c = buffer[ i + 2 ];
  119. if ( hasNormals )
  120. face = f3n( scope, normals, a, b, c, m, a, b, c );
  121. else
  122. face = f3( scope, a, b, c, m );
  123. if ( hasColors ) {
  124. face.vertexColors[ 0 ] = colors[ a ];
  125. face.vertexColors[ 1 ] = colors[ b ];
  126. face.vertexColors[ 2 ] = colors[ c ];
  127. }
  128. if ( hasUvs ) {
  129. u1 = uvs[ a * 2 ];
  130. v1 = uvs[ a * 2 + 1 ];
  131. u2 = uvs[ b * 2 ];
  132. v2 = uvs[ b * 2 + 1 ];
  133. u3 = uvs[ c * 2 ];
  134. v3 = uvs[ c * 2 + 1 ];
  135. uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  136. }
  137. }
  138. }
  139. };
  140. function vertex ( scope, x, y, z ) {
  141. scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
  142. };
  143. function f3 ( scope, a, b, c, mi ) {
  144. var face = new THREE.Face3( a, b, c, null, null, mi );
  145. scope.faces.push( face );
  146. return face;
  147. };
  148. function f3n ( scope, normals, a, b, c, mi, na, nb, nc ) {
  149. var nax = normals[ na * 3 ],
  150. nay = normals[ na * 3 + 1 ],
  151. naz = normals[ na * 3 + 2 ],
  152. nbx = normals[ nb * 3 ],
  153. nby = normals[ nb * 3 + 1 ],
  154. nbz = normals[ nb * 3 + 2 ],
  155. ncx = normals[ nc * 3 ],
  156. ncy = normals[ nc * 3 + 1 ],
  157. ncz = normals[ nc * 3 + 2 ];
  158. var na = new THREE.Vector3( nax, nay, naz ),
  159. nb = new THREE.Vector3( nbx, nby, nbz ),
  160. nc = new THREE.Vector3( ncx, ncy, ncz );
  161. var face = new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi );
  162. scope.faces.push( face );
  163. return face;
  164. };
  165. function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
  166. var uv = [];
  167. uv.push( new THREE.UV( u1, v1 ) );
  168. uv.push( new THREE.UV( u2, v2 ) );
  169. uv.push( new THREE.UV( u3, v3 ) );
  170. where.push( uv );
  171. };
  172. Model.prototype = new THREE.Geometry();
  173. Model.prototype.constructor = Model;
  174. callback( new Model() );
  175. };