UTF8Loader.js 18 KB

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