瀏覽代碼

Add read magic bytes function

Garrett Johnson 3 年之前
父節點
當前提交
da60fd8593
共有 5 個文件被更改,包括 38 次插入25 次删除
  1. 2 5
      src/base/B3DMLoaderBase.js
  2. 3 10
      src/base/CMPTLoaderBase.js
  3. 2 5
      src/base/I3DMLoaderBase.js
  4. 2 5
      src/base/PNTSLoaderBase.js
  5. 29 0
      src/utilities/readMagicBytes.js

+ 2 - 5
src/base/B3DMLoaderBase.js

@@ -3,6 +3,7 @@
 
 import { FeatureTable, BatchTable } from '../utilities/FeatureTable.js';
 import { LoaderBase } from './LoaderBase.js';
+import { readMagicBytes } from '../utilities/readMagicBytes.js';
 
 export class B3DMLoaderBase extends LoaderBase {
 
@@ -14,11 +15,7 @@ export class B3DMLoaderBase extends LoaderBase {
 		// 28-byte header
 
 		// 4 bytes
-		const magic =
-			String.fromCharCode( dataView.getUint8( 0 ) ) +
-			String.fromCharCode( dataView.getUint8( 1 ) ) +
-			String.fromCharCode( dataView.getUint8( 2 ) ) +
-			String.fromCharCode( dataView.getUint8( 3 ) );
+		const magic = readMagicBytes( dataView );
 
 		console.assert( magic === 'b3dm' );
 

+ 3 - 10
src/base/CMPTLoaderBase.js

@@ -1,6 +1,7 @@
 // CMPT File Format
 // https://github.com/CesiumGS/3d-tiles/blob/master/specification/TileFormats/Composite/README.md
 import { LoaderBase } from './LoaderBase.js';
+import { readMagicBytes } from '../utilities/readMagicBytes.js';
 
 export class CMPTLoaderBase extends LoaderBase {
 
@@ -11,11 +12,7 @@ export class CMPTLoaderBase extends LoaderBase {
 		// 16-byte header
 
 		// 4 bytes
-		const magic =
-			String.fromCharCode( dataView.getUint8( 0 ) ) +
-			String.fromCharCode( dataView.getUint8( 1 ) ) +
-			String.fromCharCode( dataView.getUint8( 2 ) ) +
-			String.fromCharCode( dataView.getUint8( 3 ) );
+		const magic = readMagicBytes( dataView );
 
 		console.assert( magic === 'cmpt', 'CMPTLoader: The magic bytes equal "cmpt".' );
 
@@ -37,11 +34,7 @@ export class CMPTLoaderBase extends LoaderBase {
 		for ( let i = 0; i < tilesLength; i ++ ) {
 
 			const tileView = new DataView( buffer, offset, 12 );
-			const tileMagic =
-				String.fromCharCode( tileView.getUint8( 0 ) ) +
-				String.fromCharCode( tileView.getUint8( 1 ) ) +
-				String.fromCharCode( tileView.getUint8( 2 ) ) +
-				String.fromCharCode( tileView.getUint8( 3 ) );
+			const tileMagic = readMagicBytes( tileView );
 			const tileVersion = tileView.getUint32( 4, true );
 			const byteLength = tileView.getUint32( 8, true );
 

+ 2 - 5
src/base/I3DMLoaderBase.js

@@ -4,6 +4,7 @@
 import { FeatureTable, BatchTable } from '../utilities/FeatureTable.js';
 import { arrayToString } from '../utilities/arrayToString.js';
 import { LoaderBase } from './LoaderBase.js';
+import { readMagicBytes } from '../utilities/readMagicBytes.js';
 
 export class I3DMLoaderBase extends LoaderBase {
 
@@ -14,11 +15,7 @@ export class I3DMLoaderBase extends LoaderBase {
 		// 32-byte header
 
 		// 4 bytes
-		const magic =
-			String.fromCharCode( dataView.getUint8( 0 ) ) +
-			String.fromCharCode( dataView.getUint8( 1 ) ) +
-			String.fromCharCode( dataView.getUint8( 2 ) ) +
-			String.fromCharCode( dataView.getUint8( 3 ) );
+		const magic = readMagicBytes( dataView );
 
 		console.assert( magic === 'i3dm' );
 

+ 2 - 5
src/base/PNTSLoaderBase.js

@@ -2,6 +2,7 @@
 // https://github.com/CesiumGS/3d-tiles/blob/master/specification/TileFormats/PointCloud/README.md
 
 import { FeatureTable, BatchTable } from '../utilities/FeatureTable.js';
+import { readMagicBytes } from '../utilities/readMagicBytes.js';
 import { LoaderBase } from './LoaderBase.js';
 
 export class PNTSLoaderBase extends LoaderBase {
@@ -13,11 +14,7 @@ export class PNTSLoaderBase extends LoaderBase {
 		// 28-byte header
 
 		// 4 bytes
-		const magic =
-			String.fromCharCode( dataView.getUint8( 0 ) ) +
-			String.fromCharCode( dataView.getUint8( 1 ) ) +
-			String.fromCharCode( dataView.getUint8( 2 ) ) +
-			String.fromCharCode( dataView.getUint8( 3 ) );
+		const magic = readMagicBytes( dataView );
 
 		console.assert( magic === 'pnts' );
 

+ 29 - 0
src/utilities/readMagicBytes.js

@@ -0,0 +1,29 @@
+export function readMagicBytes( bufferOrDataView ) {
+
+	let view;
+	if ( bufferOrDataView instanceof DataView ) {
+
+		view = bufferOrDataView;
+
+	} else {
+
+		view = new DataView( bufferOrDataView );
+
+	}
+
+	if ( String.fromCharCode( view.getUint8( 0 ) ) === '{' ) {
+
+		return null;
+
+	}
+
+	let magicBytes = '';
+	for ( let i = 0; i < 4; i ++ ) {
+
+	  res += String.fromCharCode( view.getUint8( i ) );
+
+	}
+
+	return magicBytes;
+
+}