antonio-gomez / three-gltf2-loader

Node.js wrapper for the GLTF2Loader library from Three.js
https://threejs.org/examples/?q=glt#webgl_loader_gltf
MIT License
10 stars 9 forks source link

Data is already json in parse function #2

Open Strae opened 6 years ago

Strae commented 6 years ago

Hey, sorry if I dont provide a patch but im in a bit of hurry Anyway, file https://raw.githubusercontent.com/antonio-gomez/three-gltf2-loader/master/lib/main.js Used "as is" with GLTF2 models (downloaded from different sources, like sketchfab) give the error "Uncaught SyntaxError: Unexpected end of JSON input (GLTF2Loader.js:69)"

Solved changed this piece of code:

if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) {
  extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( data );
  content = extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content;
} else {
  content = convertUint8ArrayToString( new Uint8Array( data ) );
}
var json = JSON.parse( content );

into this:

if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) {
    extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( data );
    content = extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content;
    var json = JSON.parse( content );
} else {
  try {
    var json = JSON.parse( data );
  }
  catch ( e ) {
    content = convertUint8ArrayToString( new Uint8Array( data ) );
    var json = JSON.parse( content );
  }
}

(just check if it is already valid Json)

I'll submit a patch in the next days as soon as i'll find some time ;)

donmccurdy commented 6 years ago

Note that this repository is a mirror of the GLTF(2)Loader file from the threejs repository. I believe this issue has already been fixed in the official copy, you may want to try there.