UTF8Loader.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /**
  2. * Loader for UTF8 version2 (after r51) encoded models generated by:
  3. * http://code.google.com/p/webgl-loader/
  4. *
  5. * Code to load/decompress mesh is taken from r100 of this webgl-loader
  6. */
  7. THREE.UTF8Loader = function () {};
  8. /**
  9. * Load UTF8 encoded model
  10. * @param jsonUrl - URL from which to load json containing information about model
  11. * @param callback - Callback(THREE.Object3D) on successful loading of model
  12. * @param options - options on how to load model (see THREE.MTLLoader.MaterialCreator for basic options)
  13. * Additional options include
  14. * geometryBase: Base url from which to load referenced geometries
  15. * materialBase: Base url from which to load referenced textures
  16. */
  17. THREE.UTF8Loader.prototype.load = function ( jsonUrl, callback, options ) {
  18. this.downloadModelJson( jsonUrl, options, callback );
  19. };
  20. // BufferGeometryCreator
  21. THREE.UTF8Loader.BufferGeometryCreator = function () {
  22. };
  23. THREE.UTF8Loader.BufferGeometryCreator.prototype.create = function ( attribArray, indexArray ) {
  24. var ntris = indexArray.length / 3;
  25. var geometry = new THREE.BufferGeometry();
  26. var positionArray = new Float32Array( 3 * 3 * ntris );
  27. var normalArray = new Float32Array( 3 * 3 * ntris );
  28. var uvArray = new Float32Array( 2 * 3 * ntris );
  29. var i, j, offset;
  30. var x, y, z;
  31. var u, v;
  32. var end = attribArray.length;
  33. var stride = 8;
  34. // extract positions
  35. j = 0;
  36. offset = 0;
  37. for( i = offset; i < end; i += stride ) {
  38. x = attribArray[ i ];
  39. y = attribArray[ i + 1 ];
  40. z = attribArray[ i + 2 ];
  41. positionArray[ j++ ] = x;
  42. positionArray[ j++ ] = y;
  43. positionArray[ j++ ] = z;
  44. }
  45. // extract uvs
  46. j = 0;
  47. offset = 3;
  48. for( i = offset; i < end; i += stride ) {
  49. u = attribArray[ i ];
  50. v = attribArray[ i + 1 ];
  51. uvArray[ j++ ] = u;
  52. uvArray[ j++ ] = v;
  53. }
  54. // extract normals
  55. j = 0;
  56. offset = 5;
  57. for( i = offset; i < end; i += stride ) {
  58. x = attribArray[ i ];
  59. y = attribArray[ i + 1 ];
  60. z = attribArray[ i + 2 ];
  61. normalArray[ j++ ] = x;
  62. normalArray[ j++ ] = y;
  63. normalArray[ j++ ] = z;
  64. }
  65. // create attributes
  66. var attributes = geometry.attributes;
  67. attributes[ "index" ] = { itemSize: 1, array: indexArray, numItems: indexArray.length };
  68. attributes[ "position" ] = { itemSize: 3, array: positionArray, numItems: positionArray.length };
  69. attributes[ "normal" ] = { itemSize: 3, array: normalArray, numItems: normalArray.length };
  70. attributes[ "uv" ] = { itemSize: 2, array: uvArray, numItems: uvArray.length };
  71. // create offsets
  72. // (all triangles should fit in a single chunk)
  73. geometry.offsets = [ { start: 0, count: indexArray.length, index: 0 } ];
  74. geometry.computeBoundingSphere();
  75. return geometry;
  76. };
  77. // GeometryCreator
  78. THREE.UTF8Loader.GeometryCreator = function () {
  79. };
  80. THREE.UTF8Loader.GeometryCreator.prototype = {
  81. create: function ( attribArray, indexArray ) {
  82. var geometry = new THREE.Geometry();
  83. this.init_vertices( geometry, attribArray, 8, 0 );
  84. var uvs = this.init_uvs( attribArray, 8, 3 );
  85. var normals = this.init_normals( attribArray, 8, 5 );
  86. this.init_faces( geometry, normals, uvs, indexArray );
  87. geometry.computeCentroids();
  88. geometry.computeFaceNormals();
  89. return geometry;
  90. },
  91. init_vertices: function ( scope, data, stride, offset ) {
  92. var i, x, y, z;
  93. var end = data.length;
  94. for( i = offset; i < end; i += stride ) {
  95. x = data[ i ];
  96. y = data[ i + 1 ];
  97. z = data[ i + 2 ];
  98. this.addVertex( scope, x, y, z );
  99. }
  100. },
  101. init_normals: function( data, stride, offset ) {
  102. var normals = [];
  103. var i, x, y, z;
  104. var end = data.length;
  105. for( i = offset; i < end; i += stride ) {
  106. // Assumes already normalized to <-1,1> (unlike previous version of UTF8Loader)
  107. x = data[ i ];
  108. y = data[ i + 1 ];
  109. z = data[ i + 2 ];
  110. normals.push( x, y, z );
  111. }
  112. return normals;
  113. },
  114. init_uvs: function( data, stride, offset ) {
  115. var uvs = [];
  116. var i, u, v;
  117. var end = data.length;
  118. for( i = offset; i < end; i += stride ) {
  119. // Assumes uvs are already normalized (unlike previous version of UTF8Loader)
  120. // uvs can be negative, need to set wrap for texture map later on ...
  121. u = data[ i ];
  122. v = data[ i + 1 ];
  123. uvs.push( u, v );
  124. }
  125. return uvs;
  126. },
  127. init_faces: function( scope, normals, uvs, indices ) {
  128. var i,
  129. a, b, c,
  130. u1, v1, u2, v2, u3, v3;
  131. var end = indices.length;
  132. var m = 0; // all faces defaulting to material 0
  133. for( i = 0; i < end; i += 3 ) {
  134. a = indices[ i ];
  135. b = indices[ i + 1 ];
  136. c = indices[ i + 2 ];
  137. this.f3n( scope, normals, a, b, c, m, a, b, c );
  138. u1 = uvs[ a * 2 ];
  139. v1 = uvs[ a * 2 + 1 ];
  140. u2 = uvs[ b * 2 ];
  141. v2 = uvs[ b * 2 + 1 ];
  142. u3 = uvs[ c * 2 ];
  143. v3 = uvs[ c * 2 + 1 ];
  144. this.uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  145. }
  146. },
  147. addVertex: function ( scope, x, y, z ) {
  148. scope.vertices.push( new THREE.Vector3( x, y, z ) );
  149. },
  150. f3n: function( scope, normals, a, b, c, mi, nai, nbi, nci ) {
  151. var nax = normals[ nai * 3 ],
  152. nay = normals[ nai * 3 + 1 ],
  153. naz = normals[ nai * 3 + 2 ],
  154. nbx = normals[ nbi * 3 ],
  155. nby = normals[ nbi * 3 + 1 ],
  156. nbz = normals[ nbi * 3 + 2 ],
  157. ncx = normals[ nci * 3 ],
  158. ncy = normals[ nci * 3 + 1 ],
  159. ncz = normals[ nci * 3 + 2 ];
  160. var na = new THREE.Vector3( nax, nay, naz ),
  161. nb = new THREE.Vector3( nbx, nby, nbz ),
  162. nc = new THREE.Vector3( ncx, ncy, ncz );
  163. scope.faces.push( new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi ) );
  164. },
  165. uv3: function ( 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. };
  173. // UTF-8 decoder from webgl-loader (r100)
  174. // http://code.google.com/p/webgl-loader/
  175. // Model manifest description. Contains objects like:
  176. // name: {
  177. // materials: { 'material_name': { ... } ... },
  178. // decodeParams: {
  179. // decodeOffsets: [ ... ],
  180. // decodeScales: [ ... ],
  181. // },
  182. // urls: {
  183. // 'url': [
  184. // { material: 'material_name',
  185. // attribRange: [#, #],
  186. // indexRange: [#, #],
  187. // names: [ 'object names' ... ],
  188. // lengths: [#, #, # ... ]
  189. // }
  190. // ],
  191. // ...
  192. // }
  193. // }
  194. var DEFAULT_DECODE_PARAMS = {
  195. decodeOffsets: [-4095, -4095, -4095, 0, 0, -511, -511, -511],
  196. decodeScales: [1/8191, 1/8191, 1/8191, 1/1023, 1/1023, 1/1023, 1/1023, 1/1023]
  197. // TODO: normal decoding? (see walt.js)
  198. // needs to know: input, output (from vertex format!)
  199. //
  200. // Should split attrib/index.
  201. // 1) Decode position and non-normal attributes.
  202. // 2) Decode indices, computing normals
  203. // 3) Maybe normalize normals? Only necessary for refinement, or fixed?
  204. // 4) Maybe refine normals? Should this be part of regular refinement?
  205. // 5) Morphing
  206. };
  207. // Triangle strips!
  208. // TODO: will it be an optimization to specialize this method at
  209. // runtime for different combinations of stride, decodeOffset and
  210. // decodeScale?
  211. THREE.UTF8Loader.prototype.decompressAttribsInner_ = function ( str, inputStart, inputEnd,
  212. output, outputStart, stride,
  213. decodeOffset, decodeScale ) {
  214. var prev = 0;
  215. for ( var j = inputStart; j < inputEnd; j ++ ) {
  216. var code = str.charCodeAt( j );
  217. prev += ( code >> 1 ) ^ ( -( code & 1 ) );
  218. output[ outputStart ] = decodeScale * ( prev + decodeOffset );
  219. outputStart += stride;
  220. }
  221. };
  222. THREE.UTF8Loader.prototype.decompressIndices_ = function( str, inputStart, numIndices,
  223. output, outputStart ) {
  224. var highest = 0;
  225. for ( var i = 0; i < numIndices; i ++ ) {
  226. var code = str.charCodeAt( inputStart ++ );
  227. output[ outputStart ++ ] = highest - code;
  228. if ( code === 0 ) {
  229. highest ++;
  230. }
  231. }
  232. };
  233. THREE.UTF8Loader.prototype.decompressAABBs_ = function ( str, inputStart, numBBoxen,
  234. decodeOffsets, decodeScales ) {
  235. var numFloats = 6 * numBBoxen;
  236. var inputEnd = inputStart + numFloats;
  237. var outputStart = 0;
  238. var bboxen = new Float32Array( numFloats );
  239. for ( var i = inputStart; i < inputEnd; i += 6 ) {
  240. var minX = str.charCodeAt(i + 0) + decodeOffsets[0];
  241. var minY = str.charCodeAt(i + 1) + decodeOffsets[1];
  242. var minZ = str.charCodeAt(i + 2) + decodeOffsets[2];
  243. var radiusX = (str.charCodeAt(i + 3) + 1) >> 1;
  244. var radiusY = (str.charCodeAt(i + 4) + 1) >> 1;
  245. var radiusZ = (str.charCodeAt(i + 5) + 1) >> 1;
  246. bboxen[ outputStart++ ] = decodeScales[0] * (minX + radiusX);
  247. bboxen[ outputStart++ ] = decodeScales[1] * (minY + radiusY);
  248. bboxen[ outputStart++ ] = decodeScales[2] * (minZ + radiusZ);
  249. bboxen[ outputStart++ ] = decodeScales[0] * radiusX;
  250. bboxen[ outputStart++ ] = decodeScales[1] * radiusY;
  251. bboxen[ outputStart++ ] = decodeScales[2] * radiusZ;
  252. }
  253. return bboxen;
  254. };
  255. THREE.UTF8Loader.prototype.decompressMesh = function ( str, meshParams, decodeParams, name, idx, callback ) {
  256. // Extract conversion parameters from attribArrays.
  257. var stride = decodeParams.decodeScales.length;
  258. var decodeOffsets = decodeParams.decodeOffsets;
  259. var decodeScales = decodeParams.decodeScales;
  260. var attribStart = meshParams.attribRange[0];
  261. var numVerts = meshParams.attribRange[1];
  262. // Decode attributes.
  263. var inputOffset = attribStart;
  264. var attribsOut = new Float32Array( stride * numVerts );
  265. for (var j = 0; j < stride; j ++ ) {
  266. var end = inputOffset + numVerts;
  267. var decodeScale = decodeScales[j];
  268. if ( decodeScale ) {
  269. // Assume if decodeScale is never set, simply ignore the
  270. // attribute.
  271. this.decompressAttribsInner_( str, inputOffset, end,
  272. attribsOut, j, stride,
  273. decodeOffsets[j], decodeScale );
  274. }
  275. inputOffset = end;
  276. }
  277. var indexStart = meshParams.indexRange[ 0 ];
  278. var numIndices = 3 * meshParams.indexRange[ 1 ];
  279. var indicesOut = new Uint16Array( numIndices );
  280. this.decompressIndices_( str, inputOffset, numIndices, indicesOut, 0 );
  281. // Decode bboxen.
  282. var bboxen = undefined;
  283. var bboxOffset = meshParams.bboxes;
  284. if ( bboxOffset ) {
  285. bboxen = this.decompressAABBs_( str, bboxOffset, meshParams.names.length, decodeOffsets, decodeScales );
  286. }
  287. callback( name, idx, attribsOut, indicesOut, bboxen, meshParams );
  288. };
  289. THREE.UTF8Loader.prototype.copyAttrib = function ( stride, attribsOutFixed, lastAttrib, index ) {
  290. for ( var j = 0; j < stride; j ++ ) {
  291. lastAttrib[ j ] = attribsOutFixed[ stride * index + j ];
  292. }
  293. };
  294. THREE.UTF8Loader.prototype.decodeAttrib2 = function ( str, stride, decodeOffsets, decodeScales, deltaStart,
  295. numVerts, attribsOut, attribsOutFixed, lastAttrib,
  296. index ) {
  297. for ( var j = 0; j < 5; j ++ ) {
  298. var code = str.charCodeAt( deltaStart + numVerts*j + index );
  299. var delta = ( code >> 1) ^ (-(code & 1));
  300. lastAttrib[ j ] += delta;
  301. attribsOutFixed[ stride * index + j ] = lastAttrib[ j ];
  302. attribsOut[ stride * index + j ] = decodeScales[ j ] * ( lastAttrib[ j ] + decodeOffsets[ j ] );
  303. }
  304. };
  305. THREE.UTF8Loader.prototype.accumulateNormal = function ( i0, i1, i2, attribsOutFixed, crosses ) {
  306. var p0x = attribsOutFixed[ 8*i0 ];
  307. var p0y = attribsOutFixed[ 8*i0 + 1 ];
  308. var p0z = attribsOutFixed[ 8*i0 + 2 ];
  309. var p1x = attribsOutFixed[ 8*i1 ];
  310. var p1y = attribsOutFixed[ 8*i1 + 1 ];
  311. var p1z = attribsOutFixed[ 8*i1 + 2 ];
  312. var p2x = attribsOutFixed[ 8*i2 ];
  313. var p2y = attribsOutFixed[ 8*i2 + 1 ];
  314. var p2z = attribsOutFixed[ 8*i2 + 2 ];
  315. p1x -= p0x;
  316. p1y -= p0y;
  317. p1z -= p0z;
  318. p2x -= p0x;
  319. p2y -= p0y;
  320. p2z -= p0z;
  321. p0x = p1y*p2z - p1z*p2y;
  322. p0y = p1z*p2x - p1x*p2z;
  323. p0z = p1x*p2y - p1y*p2x;
  324. crosses[ 3*i0 ] += p0x;
  325. crosses[ 3*i0 + 1 ] += p0y;
  326. crosses[ 3*i0 + 2 ] += p0z;
  327. crosses[ 3*i1 ] += p0x;
  328. crosses[ 3*i1 + 1 ] += p0y;
  329. crosses[ 3*i1 + 2 ] += p0z;
  330. crosses[ 3*i2 ] += p0x;
  331. crosses[ 3*i2 + 1 ] += p0y;
  332. crosses[ 3*i2 + 2 ] += p0z;
  333. };
  334. THREE.UTF8Loader.prototype.decompressMesh2 = function( str, meshParams, decodeParams, name, idx, callback ) {
  335. var MAX_BACKREF = 96;
  336. // Extract conversion parameters from attribArrays.
  337. var stride = decodeParams.decodeScales.length;
  338. var decodeOffsets = decodeParams.decodeOffsets;
  339. var decodeScales = decodeParams.decodeScales;
  340. var deltaStart = meshParams.attribRange[ 0 ];
  341. var numVerts = meshParams.attribRange[ 1 ];
  342. var codeStart = meshParams.codeRange[ 0 ];
  343. var codeLength = meshParams.codeRange[ 1 ];
  344. var numIndices = 3 * meshParams.codeRange[ 2 ];
  345. var indicesOut = new Uint16Array( numIndices );
  346. var crosses = new Int32Array( 3 * numVerts );
  347. var lastAttrib = new Uint16Array( stride );
  348. var attribsOutFixed = new Uint16Array( stride * numVerts );
  349. var attribsOut = new Float32Array( stride * numVerts );
  350. var highest = 0;
  351. var outputStart = 0;
  352. for ( var i = 0; i < numIndices; i += 3 ) {
  353. var code = str.charCodeAt( codeStart ++ );
  354. var max_backref = Math.min( i, MAX_BACKREF );
  355. if ( code < max_backref ) {
  356. // Parallelogram
  357. var winding = code % 3;
  358. var backref = i - ( code - winding );
  359. var i0, i1, i2;
  360. switch ( winding ) {
  361. case 0:
  362. i0 = indicesOut[ backref + 2 ];
  363. i1 = indicesOut[ backref + 1 ];
  364. i2 = indicesOut[ backref + 0 ];
  365. break;
  366. case 1:
  367. i0 = indicesOut[ backref + 0 ];
  368. i1 = indicesOut[ backref + 2 ];
  369. i2 = indicesOut[ backref + 1 ];
  370. break;
  371. case 2:
  372. i0 = indicesOut[ backref + 1 ];
  373. i1 = indicesOut[ backref + 0 ];
  374. i2 = indicesOut[ backref + 2 ];
  375. break;
  376. }
  377. indicesOut[ outputStart ++ ] = i0;
  378. indicesOut[ outputStart ++ ] = i1;
  379. code = str.charCodeAt( codeStart ++ );
  380. var index = highest - code;
  381. indicesOut[ outputStart ++ ] = index;
  382. if ( code === 0 ) {
  383. for (var j = 0; j < 5; j ++ ) {
  384. var deltaCode = str.charCodeAt( deltaStart + numVerts * j + highest );
  385. var prediction = ((deltaCode >> 1) ^ (-(deltaCode & 1))) +
  386. attribsOutFixed[stride*i0 + j] +
  387. attribsOutFixed[stride*i1 + j] -
  388. attribsOutFixed[stride*i2 + j];
  389. lastAttrib[j] = prediction;
  390. attribsOutFixed[ stride * highest + j ] = prediction;
  391. attribsOut[ stride * highest + j ] = decodeScales[ j ] * ( prediction + decodeOffsets[ j ] );
  392. }
  393. highest ++;
  394. } else {
  395. this.copyAttrib( stride, attribsOutFixed, lastAttrib, index );
  396. }
  397. this.accumulateNormal( i0, i1, index, attribsOutFixed, crosses );
  398. } else {
  399. // Simple
  400. var index0 = highest - ( code - max_backref );
  401. indicesOut[ outputStart ++ ] = index0;
  402. if ( code === max_backref ) {
  403. this.decodeAttrib2( str, stride, decodeOffsets, decodeScales, deltaStart,
  404. numVerts, attribsOut, attribsOutFixed, lastAttrib,
  405. highest ++ );
  406. } else {
  407. this.copyAttrib(stride, attribsOutFixed, lastAttrib, index0);
  408. }
  409. code = str.charCodeAt( codeStart ++ );
  410. var index1 = highest - code;
  411. indicesOut[ outputStart ++ ] = index1;
  412. if ( code === 0 ) {
  413. this.decodeAttrib2( str, stride, decodeOffsets, decodeScales, deltaStart,
  414. numVerts, attribsOut, attribsOutFixed, lastAttrib,
  415. highest ++ );
  416. } else {
  417. this.copyAttrib( stride, attribsOutFixed, lastAttrib, index1 );
  418. }
  419. code = str.charCodeAt( codeStart ++ );
  420. var index2 = highest - code;
  421. indicesOut[ outputStart ++ ] = index2;
  422. if ( code === 0 ) {
  423. for ( var j = 0; j < 5; j ++ ) {
  424. lastAttrib[ j ] = ( attribsOutFixed[ stride * index0 + j ] + attribsOutFixed[ stride * index1 + j ] ) / 2;
  425. }
  426. this.decodeAttrib2( str, stride, decodeOffsets, decodeScales, deltaStart,
  427. numVerts, attribsOut, attribsOutFixed, lastAttrib,
  428. highest ++ );
  429. } else {
  430. this.copyAttrib( stride, attribsOutFixed, lastAttrib, index2 );
  431. }
  432. this.accumulateNormal( index0, index1, index2, attribsOutFixed, crosses );
  433. }
  434. }
  435. for ( var i = 0; i < numVerts; i ++ ) {
  436. var nx = crosses[ 3*i ];
  437. var ny = crosses[ 3*i + 1 ];
  438. var nz = crosses[ 3*i + 2 ];
  439. var norm = 511.0 / Math.sqrt( nx*nx + ny*ny + nz*nz );
  440. var cx = str.charCodeAt( deltaStart + 5*numVerts + i );
  441. var cy = str.charCodeAt( deltaStart + 6*numVerts + i );
  442. var cz = str.charCodeAt( deltaStart + 7*numVerts + i );
  443. attribsOut[ stride*i + 5 ] = norm*nx + ((cx >> 1) ^ (-(cx & 1)));
  444. attribsOut[ stride*i + 6 ] = norm*ny + ((cy >> 1) ^ (-(cy & 1)));
  445. attribsOut[ stride*i + 7 ] = norm*nz + ((cz >> 1) ^ (-(cz & 1)));
  446. }
  447. callback( name, idx, attribsOut, indicesOut, undefined, meshParams );
  448. };
  449. THREE.UTF8Loader.prototype.downloadMesh = function ( path, name, meshEntry, decodeParams, callback ) {
  450. var loader = this;
  451. var idx = 0;
  452. function onprogress( req, e ) {
  453. while ( idx < meshEntry.length ) {
  454. var meshParams = meshEntry[ idx ];
  455. var indexRange = meshParams.indexRange;
  456. if ( indexRange ) {
  457. var meshEnd = indexRange[ 0 ] + 3 * indexRange[ 1 ];
  458. if ( req.responseText.length < meshEnd ) break;
  459. loader.decompressMesh( req.responseText, meshParams, decodeParams, name, idx, callback );
  460. } else {
  461. var codeRange = meshParams.codeRange;
  462. var meshEnd = codeRange[ 0 ] + codeRange[ 1 ];
  463. if ( req.responseText.length < meshEnd ) break;
  464. loader.decompressMesh2( req.responseText, meshParams, decodeParams, name, idx, callback );
  465. }
  466. ++idx;
  467. }
  468. };
  469. getHttpRequest( path, function( req, e ) {
  470. if ( req.status === 200 || req.status === 0 ) {
  471. onprogress( req, e );
  472. }
  473. // TODO: handle errors.
  474. }, onprogress );
  475. };
  476. THREE.UTF8Loader.prototype.downloadMeshes = function ( path, meshUrlMap, decodeParams, callback ) {
  477. for ( var url in meshUrlMap ) {
  478. var meshEntry = meshUrlMap[url];
  479. this.downloadMesh( path + url, url, meshEntry, decodeParams, callback );
  480. }
  481. };
  482. THREE.UTF8Loader.prototype.createMeshCallback = function( materialBaseUrl, loadModelInfo, allDoneCallback ) {
  483. var nCompletedUrls = 0;
  484. var nExpectedUrls = 0;
  485. var expectedMeshesPerUrl = {};
  486. var decodedMeshesPerUrl = {};
  487. var modelParts = {};
  488. var meshUrlMap = loadModelInfo.urls;
  489. for ( var url in meshUrlMap ) {
  490. expectedMeshesPerUrl[ url ] = meshUrlMap[ url ].length;
  491. decodedMeshesPerUrl[ url ] = 0;
  492. nExpectedUrls ++;
  493. modelParts[ url ] = new THREE.Object3D();
  494. }
  495. var model = new THREE.Object3D();
  496. // Prepare materials first...
  497. var materialCreator = new THREE.MTLLoader.MaterialCreator( materialBaseUrl, loadModelInfo.options );
  498. materialCreator.setMaterials( loadModelInfo.materials );
  499. materialCreator.preload();
  500. // Create callback for creating mesh parts
  501. var geometryCreator = new THREE.UTF8Loader.GeometryCreator();
  502. var bufferGeometryCreator = new THREE.UTF8Loader.BufferGeometryCreator();
  503. var meshCallback = function( name, idx, attribArray, indexArray, bboxen, meshParams ) {
  504. // Got ourselves a new mesh
  505. // name identifies this part of the model (url)
  506. // idx is the mesh index of this mesh of the part
  507. // attribArray defines the vertices
  508. // indexArray defines the faces
  509. // bboxen defines the bounding box
  510. // meshParams contains the material info
  511. var useBuffers = loadModelInfo.options.useBuffers !== undefined ? loadModelInfo.options.useBuffers : true;
  512. if ( useBuffers ) {
  513. var geometry = bufferGeometryCreator.create( attribArray, indexArray );
  514. } else {
  515. var geometry = geometryCreator.create( attribArray, indexArray );
  516. }
  517. var material = materialCreator.create( meshParams.material );
  518. var mesh = new THREE.Mesh( geometry, material );
  519. modelParts[ name ].add( mesh );
  520. //model.add(new THREE.Mesh(geometry, material));
  521. decodedMeshesPerUrl[ name ] ++;
  522. if ( decodedMeshesPerUrl[ name ] === expectedMeshesPerUrl[ name ] ) {
  523. nCompletedUrls ++;
  524. model.add( modelParts[ name ] );
  525. if ( nCompletedUrls === nExpectedUrls ) {
  526. // ALL DONE!!!
  527. allDoneCallback( model );
  528. }
  529. }
  530. };
  531. return meshCallback;
  532. };
  533. THREE.UTF8Loader.prototype.downloadModel = function ( geometryBase, materialBase, model, callback ) {
  534. var meshCallback = this.createMeshCallback( materialBase, model, callback );
  535. this.downloadMeshes( geometryBase, model.urls, model.decodeParams, meshCallback );
  536. };
  537. THREE.UTF8Loader.prototype.downloadModelJson = function ( jsonUrl, options, callback ) {
  538. getJsonRequest( jsonUrl, function( loaded ) {
  539. if ( ! loaded.decodeParams ) {
  540. if ( options && options.decodeParams ) {
  541. loaded.decodeParams = options.decodeParams;
  542. } else {
  543. loaded.decodeParams = DEFAULT_DECODE_PARAMS;
  544. }
  545. }
  546. loaded.options = options;
  547. var geometryBase = jsonUrl.substr( 0, jsonUrl.lastIndexOf( "/" ) + 1 );
  548. var materialBase = geometryBase;
  549. if ( options && options.geometryBase ) {
  550. geometryBase = options.geometryBase;
  551. if ( geometryBase.charAt( geometryBase.length - 1 ) !== "/" ) {
  552. geometryBase = geometryBase + "/";
  553. }
  554. }
  555. if ( options && options.materialBase ) {
  556. materialBase = options.materialBase;
  557. if ( materialBase.charAt( materialBase.length - 1 ) !== "/" ) {
  558. materialBase = materialBase + "/";
  559. }
  560. }
  561. this.downloadModel( geometryBase, materialBase, loaded, callback );
  562. }.bind( this ) );
  563. };
  564. // XMLHttpRequest stuff
  565. function getHttpRequest( url, onload, opt_onprogress ) {
  566. var LISTENERS = {
  567. load: function( e ) { onload( req, e ); },
  568. progress: function( e ) { opt_onprogress( req, e ); }
  569. };
  570. var req = new XMLHttpRequest();
  571. addListeners( req, LISTENERS );
  572. req.open( 'GET', url, true );
  573. req.send( null );
  574. }
  575. function getJsonRequest( url, onjson ) {
  576. getHttpRequest( url,
  577. function( e ) { onjson( JSON.parse( e.responseText ) ); },
  578. function() {} );
  579. }
  580. function addListeners( dom, listeners ) {
  581. // TODO: handle event capture, object binding.
  582. for ( var key in listeners ) {
  583. dom.addEventListener( key, listeners[ key ] );
  584. }
  585. }