CMPTLoaderBase.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // CMPT File Format
  2. // https://github.com/CesiumGS/3d-tiles/blob/master/specification/TileFormats/Composite/README.md
  3. import { LoaderBase } from './LoaderBase.js';
  4. export class CMPTLoaderBase extends LoaderBase {
  5. parse( buffer ) {
  6. const dataView = new DataView( buffer );
  7. // 16-byte header
  8. // 4 bytes
  9. const magic =
  10. String.fromCharCode( dataView.getUint8( 0 ) ) +
  11. String.fromCharCode( dataView.getUint8( 1 ) ) +
  12. String.fromCharCode( dataView.getUint8( 2 ) ) +
  13. String.fromCharCode( dataView.getUint8( 3 ) );
  14. console.assert( magic === 'cmpt', 'CMPTLoader: The magic bytes equal "cmpt".' );
  15. // 4 bytes
  16. const version = dataView.getUint32( 4, true );
  17. console.assert( version === 1, 'CMPTLoader: The version listed in the header is "1".' );
  18. // 4 bytes
  19. const byteLength = dataView.getUint32( 8, true );
  20. console.assert( byteLength === buffer.byteLength, 'CMPTLoader: The contents buffer length listed in the header matches the file.' );
  21. // 4 bytes
  22. const tilesLength = dataView.getUint32( 12, true );
  23. const tiles = [];
  24. let offset = 16;
  25. for ( let i = 0; i < tilesLength; i ++ ) {
  26. const tileView = new DataView( buffer, offset, 12 );
  27. const tileMagic =
  28. String.fromCharCode( tileView.getUint8( 0 ) ) +
  29. String.fromCharCode( tileView.getUint8( 1 ) ) +
  30. String.fromCharCode( tileView.getUint8( 2 ) ) +
  31. String.fromCharCode( tileView.getUint8( 3 ) );
  32. const tileVersion = tileView.getUint32( 4, true );
  33. const byteLength = tileView.getUint32( 8, true );
  34. const tileBuffer = new Uint8Array( buffer, offset, byteLength );
  35. tiles.push( {
  36. type: tileMagic,
  37. buffer: tileBuffer,
  38. version: tileVersion,
  39. } );
  40. offset += byteLength;
  41. }
  42. return {
  43. version,
  44. tiles,
  45. };
  46. }
  47. }