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