UTF8Loader.js 20 KB

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