CTMLoader.js 13 KB

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