CTMLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 ( showStatus ) {
  11. THREE.Loader.call( this, showStatus );
  12. };
  13. THREE.CTMLoader.prototype = Object.create( THREE.Loader.prototype );
  14. // Load multiple CTM parts defined in JSON
  15. THREE.CTMLoader.prototype.loadParts = function( url, callback, useWorker, useBuffers, basePath ) {
  16. var scope = this;
  17. var xhr = new XMLHttpRequest();
  18. basePath = basePath ? basePath : this.extractUrlBase( url );
  19. xhr.onreadystatechange = function() {
  20. if ( xhr.readyState === 4 ) {
  21. if ( xhr.status === 200 || xhr.status === 0 ) {
  22. var jsonObject = JSON.parse( xhr.responseText );
  23. var materials = [], geometries = [], counter = 0;
  24. function callbackFinal( geometry ) {
  25. counter += 1;
  26. geometries.push( geometry );
  27. if ( counter === jsonObject.offsets.length ) {
  28. callback( geometries, materials );
  29. }
  30. }
  31. // init materials
  32. for ( var i = 0; i < jsonObject.materials.length; i ++ ) {
  33. materials[ i ] = THREE.Loader.prototype.createMaterial( jsonObject.materials[ i ], basePath );
  34. }
  35. // load joined CTM file
  36. var partUrl = basePath + jsonObject.data;
  37. scope.load( partUrl, callbackFinal, useWorker, useBuffers, jsonObject.offsets );
  38. }
  39. }
  40. }
  41. xhr.open( "GET", url, true );
  42. if ( xhr.overrideMimeType ) xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  43. xhr.setRequestHeader( "Content-Type", "text/plain" );
  44. xhr.send( null );
  45. };
  46. // Load CTMLoader compressed models
  47. // - parameters
  48. // - url (required)
  49. // - callback (required)
  50. THREE.CTMLoader.prototype.load = function( url, callback, useWorker, useBuffers, offsets ) {
  51. var scope = this;
  52. offsets = offsets !== undefined ? offsets : [ 0 ];
  53. var xhr = new XMLHttpRequest(),
  54. callbackProgress = null;
  55. var length = 0;
  56. xhr.onreadystatechange = function() {
  57. if ( xhr.readyState === 4 ) {
  58. if ( xhr.status === 200 || xhr.status === 0 ) {
  59. var binaryData = xhr.responseText;
  60. //var s = Date.now();
  61. if ( useWorker ) {
  62. var worker = new Worker( "js/loaders/ctm/CTMWorker.js" );
  63. worker.onmessage = function( event ) {
  64. var files = event.data;
  65. for ( var i = 0; i < files.length; i ++ ) {
  66. var ctmFile = files[ i ];
  67. if ( useBuffers ) {
  68. scope.createModelBuffers( ctmFile, callback );
  69. } else {
  70. scope.createModelClassic( ctmFile, callback );
  71. }
  72. }
  73. //var e = Date.now();
  74. //console.log( "CTM data parse time [worker]: " + (e-s) + " ms" );
  75. };
  76. worker.postMessage( { "data": binaryData, "offsets": offsets } );
  77. } else {
  78. for ( var i = 0; i < offsets.length; i ++ ) {
  79. var stream = new CTM.Stream( binaryData );
  80. stream.offset = offsets[ i ];
  81. var ctmFile = new CTM.File( stream );
  82. if ( useBuffers ) {
  83. scope.createModelBuffers( ctmFile, callback );
  84. } else {
  85. scope.createModelClassic( ctmFile, callback );
  86. }
  87. }
  88. //var e = Date.now();
  89. //console.log( "CTM data parse time [inline]: " + (e-s) + " ms" );
  90. }
  91. } else {
  92. console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
  93. }
  94. } else if ( xhr.readyState === 3 ) {
  95. if ( callbackProgress ) {
  96. if ( length === 0 ) {
  97. length = xhr.getResponseHeader( "Content-Length" );
  98. }
  99. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  100. }
  101. } else if ( xhr.readyState === 2 ) {
  102. length = xhr.getResponseHeader( "Content-Length" );
  103. }
  104. }
  105. xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  106. xhr.open( "GET", url, true );
  107. xhr.send( null );
  108. };
  109. THREE.CTMLoader.prototype.createModelBuffers = function ( file, callback ) {
  110. var Model = function ( ) {
  111. var scope = this;
  112. var reorderVertices = true;
  113. scope.materials = [];
  114. THREE.BufferGeometry.call( this );
  115. // init GL buffers
  116. var vertexIndexArray = file.body.indices,
  117. vertexPositionArray = file.body.vertices,
  118. vertexNormalArray = file.body.normals;
  119. var vertexUvArray, vertexColorArray;
  120. if ( file.body.uvMaps !== undefined && file.body.uvMaps.length > 0 ) {
  121. vertexUvArray = file.body.uvMaps[ 0 ].uv;
  122. }
  123. if ( file.body.attrMaps !== undefined && file.body.attrMaps.length > 0 && file.body.attrMaps[ 0 ].name === "Color" ) {
  124. vertexColorArray = file.body.attrMaps[ 0 ].attr;
  125. }
  126. // reorder vertices
  127. // (needed for buffer splitting, to keep together face vertices)
  128. if ( reorderVertices ) {
  129. var newFaces = new Uint32Array( vertexIndexArray.length ),
  130. newVertices = new Float32Array( vertexPositionArray.length );
  131. var newNormals, newUvs, newColors;
  132. if ( vertexNormalArray ) newNormals = new Float32Array( vertexNormalArray.length );
  133. if ( vertexUvArray ) newUvs = new Float32Array( vertexUvArray.length );
  134. if ( vertexColorArray ) newColors = new Float32Array( vertexColorArray.length );
  135. var indexMap = {}, vertexCounter = 0;
  136. function handleVertex( v ) {
  137. if ( indexMap[ v ] === undefined ) {
  138. indexMap[ v ] = vertexCounter;
  139. var sx = v * 3,
  140. sy = v * 3 + 1,
  141. sz = v * 3 + 2,
  142. dx = vertexCounter * 3,
  143. dy = vertexCounter * 3 + 1,
  144. dz = vertexCounter * 3 + 2;
  145. newVertices[ dx ] = vertexPositionArray[ sx ];
  146. newVertices[ dy ] = vertexPositionArray[ sy ];
  147. newVertices[ dz ] = vertexPositionArray[ sz ];
  148. if ( vertexNormalArray ) {
  149. newNormals[ dx ] = vertexNormalArray[ sx ];
  150. newNormals[ dy ] = vertexNormalArray[ sy ];
  151. newNormals[ dz ] = vertexNormalArray[ sz ];
  152. }
  153. if ( vertexUvArray ) {
  154. newUvs[ vertexCounter * 2 ] = vertexUvArray[ v * 2 ];
  155. newUvs[ vertexCounter * 2 + 1 ] = vertexUvArray[ v * 2 + 1 ];
  156. }
  157. if ( vertexColorArray ) {
  158. newColors[ vertexCounter * 4 ] = vertexColorArray[ v * 4 ];
  159. newColors[ vertexCounter * 4 + 1 ] = vertexColorArray[ v * 4 + 1 ];
  160. newColors[ vertexCounter * 4 + 2 ] = vertexColorArray[ v * 4 + 2 ];
  161. newColors[ vertexCounter * 4 + 3 ] = vertexColorArray[ v * 4 + 3 ];
  162. }
  163. vertexCounter += 1;
  164. }
  165. }
  166. var a, b, c;
  167. for ( var i = 0; i < vertexIndexArray.length; i += 3 ) {
  168. a = vertexIndexArray[ i ];
  169. b = vertexIndexArray[ i + 1 ];
  170. c = vertexIndexArray[ i + 2 ];
  171. handleVertex( a );
  172. handleVertex( b );
  173. handleVertex( c );
  174. newFaces[ i ] = indexMap[ a ];
  175. newFaces[ i + 1 ] = indexMap[ b ];
  176. newFaces[ i + 2 ] = indexMap[ c ];
  177. }
  178. vertexIndexArray = newFaces;
  179. vertexPositionArray = newVertices;
  180. if ( vertexNormalArray ) vertexNormalArray = newNormals;
  181. if ( vertexUvArray ) vertexUvArray = newUvs;
  182. if ( vertexColorArray ) vertexColorArray = newColors;
  183. }
  184. // compute offsets
  185. scope.offsets = [];
  186. var indices = vertexIndexArray;
  187. var start = 0,
  188. min = vertexPositionArray.length,
  189. max = 0,
  190. minPrev = min;
  191. for ( var i = 0; i < indices.length; ) {
  192. for ( var j = 0; j < 3; ++ j ) {
  193. var idx = indices[ i ++ ];
  194. if ( idx < min ) min = idx;
  195. if ( idx > max ) max = idx;
  196. }
  197. if ( max - min > 65535 ) {
  198. i -= 3;
  199. for ( var k = start; k < i; ++ k ) {
  200. indices[ k ] -= minPrev;
  201. }
  202. scope.offsets.push( { start: start, count: i - start, index: minPrev } );
  203. start = i;
  204. min = vertexPositionArray.length;
  205. max = 0;
  206. }
  207. minPrev = min;
  208. }
  209. for ( var k = start; k < i; ++ k ) {
  210. indices[ k ] -= minPrev;
  211. }
  212. scope.offsets.push( { start: start, count: i - start, index: minPrev } );
  213. // recast CTM 32-bit indices as 16-bit WebGL indices
  214. var vertexIndexArray16 = new Uint16Array( vertexIndexArray );
  215. // attributes
  216. var attributes = scope.attributes;
  217. attributes[ "index" ] = { itemSize: 1, array: vertexIndexArray16, numItems: vertexIndexArray16.length };
  218. attributes[ "position" ] = { itemSize: 3, array: vertexPositionArray, numItems: vertexPositionArray.length };
  219. if ( vertexNormalArray !== undefined ) {
  220. attributes[ "normal" ] = { itemSize: 3, array: vertexNormalArray, numItems: vertexNormalArray.length };
  221. }
  222. if ( vertexUvArray !== undefined ) {
  223. attributes[ "uv" ] = { itemSize: 2, array: vertexUvArray, numItems: vertexUvArray.length };
  224. }
  225. if ( vertexColorArray !== undefined ) {
  226. attributes[ "color" ] = { itemSize: 4, array: vertexColorArray, numItems: vertexColorArray.length };
  227. }
  228. }
  229. Model.prototype = Object.create( THREE.BufferGeometry.prototype );
  230. var geometry = new Model();
  231. // compute vertex normals if not present in the CTM model
  232. if ( geometry.attributes[ "normal" ] === undefined ) {
  233. geometry.computeVertexNormals();
  234. }
  235. callback( geometry );
  236. };
  237. THREE.CTMLoader.prototype.createModelClassic = function ( file, callback ) {
  238. var Model = function ( ) {
  239. var scope = this;
  240. scope.materials = [];
  241. THREE.Geometry.call( this );
  242. var normals = [],
  243. uvs = [],
  244. colors = [];
  245. init_vertices( file.body.vertices );
  246. if ( file.body.normals !== undefined )
  247. init_normals( file.body.normals );
  248. if ( file.body.uvMaps !== undefined && file.body.uvMaps.length > 0 )
  249. init_uvs( file.body.uvMaps[ 0 ].uv );
  250. if ( file.body.attrMaps !== undefined && file.body.attrMaps.length > 0 && file.body.attrMaps[ 0 ].name === "Color" )
  251. init_colors( file.body.attrMaps[ 0 ].attr );
  252. var hasNormals = normals.length > 0 ? true : false,
  253. hasUvs = uvs.length > 0 ? true : false,
  254. hasColors = colors.length > 0 ? true : false;
  255. init_faces( file.body.indices );
  256. this.computeCentroids();
  257. this.computeFaceNormals();
  258. //this.computeTangents();
  259. function init_vertices( buffer ) {
  260. var x, y, z, i, il = buffer.length;
  261. for( i = 0; i < il; i += 3 ) {
  262. x = buffer[ i ];
  263. y = buffer[ i + 1 ];
  264. z = buffer[ i + 2 ];
  265. vertex( scope, x, y, z );
  266. }
  267. };
  268. function init_normals( buffer ) {
  269. var x, y, z, i, il = buffer.length;
  270. for( i = 0; i < il; i += 3 ) {
  271. x = buffer[ i ];
  272. y = buffer[ i + 1 ];
  273. z = buffer[ i + 2 ];
  274. normals.push( x, y, z );
  275. }
  276. };
  277. function init_colors( buffer ) {
  278. var r, g, b, a, i, il = buffer.length;
  279. for( i = 0; i < il; i += 4 ) {
  280. r = buffer[ i ];
  281. g = buffer[ i + 1 ];
  282. b = buffer[ i + 2 ];
  283. a = buffer[ i + 3 ];
  284. var color = new THREE.Color();
  285. color.setRGB( r, g, b );
  286. colors.push( color );
  287. }
  288. };
  289. function init_uvs( buffer ) {
  290. var u, v, i, il = buffer.length;
  291. for( i = 0; i < il; i += 2 ) {
  292. u = buffer[ i ];
  293. v = buffer[ i + 1 ];
  294. uvs.push( u, v );
  295. }
  296. };
  297. function init_faces( buffer ) {
  298. var a, b, c,
  299. u1, v1, u2, v2, u3, v3,
  300. m, face,
  301. i, il = buffer.length;
  302. m = 0; // all faces defaulting to material 0
  303. for( i = 0; i < il; i += 3 ) {
  304. a = buffer[ i ];
  305. b = buffer[ i + 1 ];
  306. c = buffer[ i + 2 ];
  307. if ( hasNormals ){
  308. face = f3n( scope, normals, a, b, c, m, a, b, c );
  309. } else {
  310. face = f3( scope, a, b, c, m );
  311. }
  312. if ( hasColors ) {
  313. face.vertexColors[ 0 ] = colors[ a ];
  314. face.vertexColors[ 1 ] = colors[ b ];
  315. face.vertexColors[ 2 ] = colors[ c ];
  316. }
  317. if ( hasUvs ) {
  318. u1 = uvs[ a * 2 ];
  319. v1 = uvs[ a * 2 + 1 ];
  320. u2 = uvs[ b * 2 ];
  321. v2 = uvs[ b * 2 + 1 ];
  322. u3 = uvs[ c * 2 ];
  323. v3 = uvs[ c * 2 + 1 ];
  324. uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  325. }
  326. }
  327. }
  328. };
  329. function vertex ( scope, x, y, z ) {
  330. scope.vertices.push( new THREE.Vector3( x, y, z ) );
  331. };
  332. function f3 ( scope, a, b, c, mi ) {
  333. var face = new THREE.Face3( a, b, c, null, null, mi );
  334. scope.faces.push( face );
  335. return face;
  336. };
  337. function f3n ( scope, normals, a, b, c, mi, na, nb, nc ) {
  338. var nax = normals[ na * 3 ],
  339. nay = normals[ na * 3 + 1 ],
  340. naz = normals[ na * 3 + 2 ],
  341. nbx = normals[ nb * 3 ],
  342. nby = normals[ nb * 3 + 1 ],
  343. nbz = normals[ nb * 3 + 2 ],
  344. ncx = normals[ nc * 3 ],
  345. ncy = normals[ nc * 3 + 1 ],
  346. ncz = normals[ nc * 3 + 2 ];
  347. var na = new THREE.Vector3( nax, nay, naz ),
  348. nb = new THREE.Vector3( nbx, nby, nbz ),
  349. nc = new THREE.Vector3( ncx, ncy, ncz );
  350. var face = new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi );
  351. scope.faces.push( face );
  352. return face;
  353. };
  354. function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
  355. var uv = [];
  356. uv.push( new THREE.UV( u1, v1 ) );
  357. uv.push( new THREE.UV( u2, v2 ) );
  358. uv.push( new THREE.UV( u3, v3 ) );
  359. where.push( uv );
  360. };
  361. Model.prototype = Object.create( THREE.Geometry.prototype );
  362. callback( new Model() );
  363. };