UTF8Loader.js 18 KB

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