UTF8v2Loader.js 21 KB

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