UTF8v2Loader.js 22 KB

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