B3DMLoaderBase.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // B3DM File Format
  2. // https://github.com/CesiumGS/3d-tiles/blob/master/specification/TileFormats/Batched3DModel/README.md
  3. import { FeatureTable, BatchTable } from '../utilities/FeatureTable.js';
  4. export class B3DMLoaderBase {
  5. constructor() {
  6. this.fetchOptions = {};
  7. }
  8. load( url ) {
  9. return fetch( url, this.fetchOptions )
  10. .then( res => res.arrayBuffer() )
  11. .then( buffer => this.parse( buffer ) );
  12. }
  13. parse( buffer ) {
  14. const dataView = new DataView( buffer );
  15. // 28-byte header
  16. // 4 bytes
  17. const magic =
  18. String.fromCharCode( dataView.getUint8( 0 ) ) +
  19. String.fromCharCode( dataView.getUint8( 1 ) ) +
  20. String.fromCharCode( dataView.getUint8( 2 ) ) +
  21. String.fromCharCode( dataView.getUint8( 3 ) );
  22. console.assert( magic === 'b3dm' );
  23. // 4 bytes
  24. const version = dataView.getUint32( 4, true );
  25. console.assert( version === 1 );
  26. // 4 bytes
  27. const byteLength = dataView.getUint32( 8, true );
  28. console.assert( byteLength === buffer.byteLength );
  29. // 4 bytes
  30. const featureTableJSONByteLength = dataView.getUint32( 12, true );
  31. // 4 bytes
  32. const featureTableBinaryByteLength = dataView.getUint32( 16, true );
  33. // 4 bytes
  34. const batchTableJSONByteLength = dataView.getUint32( 20, true );
  35. // 4 bytes
  36. const batchTableBinaryByteLength = dataView.getUint32( 24, true );
  37. // Feature Table
  38. const featureTableStart = 28;
  39. const featureTable = new FeatureTable( buffer, featureTableStart, featureTableJSONByteLength, featureTableBinaryByteLength );
  40. // Batch Table
  41. const batchTableStart = featureTableStart + featureTableJSONByteLength + featureTableBinaryByteLength;
  42. const batchTable = new BatchTable( buffer, featureTable.getData( 'BATCH_LENGTH' ), batchTableStart, batchTableJSONByteLength, batchTableBinaryByteLength );
  43. const glbStart = batchTableStart + batchTableJSONByteLength + batchTableBinaryByteLength;
  44. const glbBytes = new Uint8Array( buffer, glbStart, byteLength - glbStart );
  45. return {
  46. version,
  47. featureTable,
  48. batchTable,
  49. glbBytes,
  50. };
  51. }
  52. }