UTF8Loader.js 20 KB

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