|
@@ -1333,6 +1333,7 @@ THREE.GLTFLoader = ( function () {
|
|
|
GLTFParser.prototype.parse = function ( onLoad, onError ) {
|
|
|
|
|
|
var json = this.json;
|
|
|
+ var parser = this;
|
|
|
|
|
|
// Clear the loader cache
|
|
|
this.cache.removeAll();
|
|
@@ -1341,43 +1342,19 @@ THREE.GLTFLoader = ( function () {
|
|
|
this._withDependencies( [
|
|
|
|
|
|
'scenes',
|
|
|
- 'cameras',
|
|
|
'animations'
|
|
|
|
|
|
] ).then( function ( dependencies ) {
|
|
|
|
|
|
- var scenes = [];
|
|
|
-
|
|
|
- for ( var i = 0; i < dependencies.scenes.length; i ++ ) {
|
|
|
-
|
|
|
- scenes.push( dependencies.scenes[ i ] );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- var scene = json.scene !== undefined ? dependencies.scenes[ json.scene ] : scenes[ 0 ];
|
|
|
-
|
|
|
- var cameras = [];
|
|
|
-
|
|
|
- dependencies.cameras = dependencies.cameras || [];
|
|
|
+ var scenes = dependencies.scenes || [];
|
|
|
+ var scene = scenes[ json.scene || 0 ];
|
|
|
+ var animations = dependencies.animations || [];
|
|
|
|
|
|
- for ( var i = 0; i < dependencies.cameras.length; i ++ ) {
|
|
|
+ parser.getDependencies( 'camera' ).then( function ( cameras ) {
|
|
|
|
|
|
- var camera = dependencies.cameras[ i ];
|
|
|
- cameras.push( camera );
|
|
|
-
|
|
|
- }
|
|
|
+ onLoad( scene, scenes, cameras, animations );
|
|
|
|
|
|
- var animations = [];
|
|
|
-
|
|
|
- dependencies.animations = dependencies.animations || [];
|
|
|
-
|
|
|
- for ( var i = 0; i < dependencies.animations.length; i ++ ) {
|
|
|
-
|
|
|
- animations.push( dependencies.animations[ i ] );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- onLoad( scene, scenes, cameras, animations );
|
|
|
+ } ).catch( onError );
|
|
|
|
|
|
} ).catch( onError );
|
|
|
|
|
@@ -1406,6 +1383,24 @@ THREE.GLTFLoader = ( function () {
|
|
|
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * Requests all dependencies of the specified type asynchronously, with caching.
|
|
|
+ * @param {string} type
|
|
|
+ * @return {Promise<Array<Object>>}
|
|
|
+ */
|
|
|
+ GLTFParser.prototype.getDependencies = function ( type ) {
|
|
|
+
|
|
|
+ var parser = this;
|
|
|
+ var defs = this.json[ type + 's' ] || [];
|
|
|
+
|
|
|
+ return Promise.all( defs.map( function ( def, index ) {
|
|
|
+
|
|
|
+ return parser.getDependency( type, index );
|
|
|
+
|
|
|
+ } ) );
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
/**
|
|
|
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
|
|
|
* @param {number} bufferIndex
|
|
@@ -2028,43 +2023,39 @@ THREE.GLTFLoader = ( function () {
|
|
|
|
|
|
/**
|
|
|
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras
|
|
|
+ * @param {number} cameraIndex
|
|
|
+ * @return {Promise<THREE.Camera>}
|
|
|
*/
|
|
|
- GLTFParser.prototype.loadCameras = function () {
|
|
|
-
|
|
|
- var json = this.json;
|
|
|
+ GLTFParser.prototype.loadCamera = function ( cameraIndex ) {
|
|
|
|
|
|
- return _each( json.cameras, function ( camera ) {
|
|
|
+ var camera;
|
|
|
+ var cameraDef = this.json.cameras[ cameraIndex ];
|
|
|
+ var params = cameraDef[ cameraDef.type ];
|
|
|
|
|
|
- var _camera;
|
|
|
+ if ( ! params ) {
|
|
|
|
|
|
- var params = camera[ camera.type ];
|
|
|
+ console.warn( 'THREE.GLTFLoader: Missing camera parameters.' );
|
|
|
+ return;
|
|
|
|
|
|
- if ( ! params ) {
|
|
|
-
|
|
|
- console.warn( 'THREE.GLTFLoader: Missing camera parameters.' );
|
|
|
- return;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if ( camera.type === 'perspective' ) {
|
|
|
+ }
|
|
|
|
|
|
- var aspectRatio = params.aspectRatio || 1;
|
|
|
- var xfov = params.yfov * aspectRatio;
|
|
|
+ if ( cameraDef.type === 'perspective' ) {
|
|
|
|
|
|
- _camera = new THREE.PerspectiveCamera( THREE.Math.radToDeg( xfov ), aspectRatio, params.znear || 1, params.zfar || 2e6 );
|
|
|
+ var aspectRatio = params.aspectRatio || 1;
|
|
|
+ var xfov = params.yfov * aspectRatio;
|
|
|
|
|
|
- } else if ( camera.type === 'orthographic' ) {
|
|
|
+ camera = new THREE.PerspectiveCamera( THREE.Math.radToDeg( xfov ), aspectRatio, params.znear || 1, params.zfar || 2e6 );
|
|
|
|
|
|
- _camera = new THREE.OrthographicCamera( params.xmag / - 2, params.xmag / 2, params.ymag / 2, params.ymag / - 2, params.znear, params.zfar );
|
|
|
+ } else if ( cameraDef.type === 'orthographic' ) {
|
|
|
|
|
|
- }
|
|
|
+ camera = new THREE.OrthographicCamera( params.xmag / -2, params.xmag / 2, params.ymag / 2, params.ymag / -2, params.znear, params.zfar );
|
|
|
|
|
|
- if ( camera.name !== undefined ) _camera.name = camera.name;
|
|
|
- if ( camera.extras ) _camera.userData = camera.extras;
|
|
|
+ }
|
|
|
|
|
|
- return _camera;
|
|
|
+ if ( cameraDef.name !== undefined ) camera.name = cameraDef.name;
|
|
|
+ if ( cameraDef.extras ) camera.userData = cameraDef.extras;
|
|
|
|
|
|
- } );
|
|
|
+ return Promise.resolve( camera );
|
|
|
|
|
|
};
|
|
|
|
|
@@ -2295,7 +2286,7 @@ THREE.GLTFLoader = ( function () {
|
|
|
|
|
|
} else if ( nodeDef.camera !== undefined ) {
|
|
|
|
|
|
- return dependencies.cameras[ nodeDef.camera ];
|
|
|
+ return scope.getDependency( 'camera', nodeDef.camera );
|
|
|
|
|
|
} else if ( nodeDef.extensions
|
|
|
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS ]
|