From 9076b172da1e272dc475ad0a0c0027fa48254a29 Mon Sep 17 00:00:00 2001 From: Alan Millman Date: Wed, 14 Feb 2018 17:09:40 -0600 Subject: [PATCH] Editor determines GLTF version and uses appropriate loader --- editor/index.html | 1 + editor/js/Loader.js | 54 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/editor/index.html b/editor/index.html index a00a76e9e24f8b..7330a2b012fec6 100644 --- a/editor/index.html +++ b/editor/index.html @@ -24,6 +24,7 @@ + diff --git a/editor/js/Loader.js b/editor/js/Loader.js index 3be687970b023d..cb4e81cfaf89e3 100644 --- a/editor/js/Loader.js +++ b/editor/js/Loader.js @@ -176,7 +176,18 @@ var Loader = function ( editor ) { var contents = event.target.result; - var loader = new THREE.GLTFLoader(); + var loader; + + if ( isGltf1( contents ) ) { + + loader = new THREE.LegacyGLTFLoader(); + + } else { + + loader = new THREE.GLTFLoader(); + + } + loader.parse( contents, '', function ( result ) { result.scene.name = filename; @@ -572,4 +583,45 @@ var Loader = function ( editor ) { } + function isGltf1( contents ) { + + var resultContent; + + if ( typeof contents === 'string' ) { + + resultContent = contents; + + } else { + + var magic = THREE.LoaderUtils.decodeText( new Uint8Array( contents, 0, 4 ) ); + + if ( magic === 'glTF' ) { + + try { + + extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( contents ); + + } catch ( error ) { + + // dunno what it is, but it's definitely not OK + return false; + + } + + resultContent = extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content; + + } else { + + resultContent = THREE.LoaderUtils.decodeText( new Uint8Array( contents ) ); + + } + + } + + var json = JSON.parse( resultContent ); + + return ( json.asset != undefined && json.asset.version[ 0 ] < 2 ); + + } + };