B3DMLoaderBase.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import { LoaderBase } from './LoaderBase.js';
  5. import { readMagicBytes } from '../utilities/readMagicBytes.js';
  6. export class B3DMLoaderBase extends LoaderBase {
  7. parse( buffer ) {
  8. // TODO: this should be able to take a uint8array with an offset and length
  9. const dataView = new DataView( buffer );
  10. // 28-byte header
  11. // 4 bytes
  12. const magic = readMagicBytes( dataView );
  13. console.assert( magic === 'b3dm' );
  14. // 4 bytes
  15. const version = dataView.getUint32( 4, true );
  16. console.assert( version === 1 );
  17. // 4 bytes
  18. const byteLength = dataView.getUint32( 8, true );
  19. console.assert( byteLength === buffer.byteLength );
  20. // 4 bytes
  21. const featureTableJSONByteLength = dataView.getUint32( 12, true );
  22. // 4 bytes
  23. const featureTableBinaryByteLength = dataView.getUint32( 16, true );
  24. // 4 bytes
  25. const batchTableJSONByteLength = dataView.getUint32( 20, true );
  26. // 4 bytes
  27. const batchTableBinaryByteLength = dataView.getUint32( 24, true );
  28. // Feature Table
  29. const featureTableStart = 28;
  30. const featureTableBuffer = buffer.slice(
  31. featureTableStart,
  32. featureTableStart + featureTableJSONByteLength + featureTableBinaryByteLength,
  33. );
  34. const featureTable = new FeatureTable(
  35. featureTableBuffer,
  36. 0,
  37. featureTableJSONByteLength,
  38. featureTableBinaryByteLength,
  39. );
  40. // Batch Table
  41. const batchTableStart = featureTableStart + featureTableJSONByteLength + featureTableBinaryByteLength;
  42. const batchTableBuffer = buffer.slice(
  43. batchTableStart,
  44. batchTableStart + batchTableJSONByteLength + batchTableBinaryByteLength,
  45. );
  46. const batchTable = new BatchTable(
  47. batchTableBuffer,
  48. featureTable.getData( 'BATCH_LENGTH' ),
  49. 0,
  50. batchTableJSONByteLength,
  51. batchTableBinaryByteLength,
  52. );
  53. const glbStart = batchTableStart + batchTableJSONByteLength + batchTableBinaryByteLength;
  54. const glbBytes = new Uint8Array( buffer, glbStart, byteLength - glbStart );
  55. return {
  56. version,
  57. featureTable,
  58. batchTable,
  59. glbBytes,
  60. };
  61. }
  62. }