Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 41 additions & 78 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,7 @@ THREE.GLTFLoader = ( function () {

} );

parser.parse( function ( scene, scenes, cameras, animations, json ) {

var glTF = {
scene: scene,
scenes: scenes,
cameras: cameras,
animations: animations,
asset: json.asset,
parser: parser,
userData: {}
};

addUnknownExtensionsToUserData( extensions, glTF, json );

onLoad( glTF );

}, onError );
parser.parse( onLoad, onError );

}

Expand Down Expand Up @@ -1362,34 +1346,21 @@ THREE.GLTFLoader = ( function () {

if ( hasMorphPosition ) {

// TODO: Error-prone use of a callback inside a loop.
var accessor = target.POSITION !== undefined
var pendingAccessor = target.POSITION !== undefined
? parser.getDependency( 'accessor', target.POSITION )
.then( function ( accessor ) {

// Cloning not to pollute original accessor below
return cloneBufferAttribute( accessor );

} )
: geometry.attributes.position;

pendingPositionAccessors.push( accessor );
pendingPositionAccessors.push( pendingAccessor );

}

if ( hasMorphNormal ) {

// TODO: Error-prone use of a callback inside a loop.
var accessor = target.NORMAL !== undefined
var pendingAccessor = target.NORMAL !== undefined
? parser.getDependency( 'accessor', target.NORMAL )
.then( function ( accessor ) {

return cloneBufferAttribute( accessor );

} )
: geometry.attributes.normal;

pendingNormalAccessors.push( accessor );
pendingNormalAccessors.push( pendingAccessor );

}

Expand All @@ -1403,6 +1374,24 @@ THREE.GLTFLoader = ( function () {
var morphPositions = accessors[ 0 ];
var morphNormals = accessors[ 1 ];

// Clone morph target accessors before modifying them.

for ( var i = 0, il = morphPositions.length; i < il; i ++ ) {

if ( geometry.attributes.position === morphPositions[ i ] ) continue;

morphPositions[ i ] = cloneBufferAttribute( morphPositions[ i ] );

}

for ( var i = 0, il = morphNormals.length; i < il; i ++ ) {

if ( geometry.attributes.normal === morphNormals[ i ] ) continue;

morphNormals[ i ] = cloneBufferAttribute( morphNormals[ i ] );

}

for ( var i = 0, il = targets.length; i < il; i ++ ) {

var target = targets[ i ];
Expand Down Expand Up @@ -1623,29 +1612,37 @@ THREE.GLTFLoader = ( function () {

GLTFParser.prototype.parse = function ( onLoad, onError ) {

var parser = this;
var json = this.json;
var extensions = this.extensions;

// Clear the loader cache
this.cache.removeAll();

// Mark the special nodes/meshes in json for efficient parse
this.markDefs();

// Fire the callback on complete
this.getMultiDependencies( [
Promise.all( [

'scene',
'animation',
'camera'
this.getDependencies( 'scene' ),
this.getDependencies( 'animation' ),
this.getDependencies( 'camera' ),

] ).then( function ( dependencies ) {

var scenes = dependencies.scenes || [];
var scene = scenes[ json.scene || 0 ];
var animations = dependencies.animations || [];
var cameras = dependencies.cameras || [];
var result = {
scene: dependencies[ 0 ][ json.scene || 0 ],
scenes: dependencies[ 0 ],
animations: dependencies[ 1 ],
cameras: dependencies[ 2 ],
asset: json.asset,
parser: parser,
userData: {}
};

onLoad( scene, scenes, cameras, animations, json );
addUnknownExtensionsToUserData( extensions, result, json );

onLoad( result );

} ).catch( onError );

Expand Down Expand Up @@ -1818,40 +1815,6 @@ THREE.GLTFLoader = ( function () {

};

/**
* Requests all multiple dependencies of the specified types asynchronously, with caching.
* @param {Array<string>} types
* @return {Promise<Object<Array<Object>>>}
*/
GLTFParser.prototype.getMultiDependencies = function ( types ) {

var results = {};
var pending = [];

for ( var i = 0, il = types.length; i < il; i ++ ) {

var type = types[ i ];
var value = this.getDependencies( type );

// TODO: Error-prone use of a callback inside a loop.
value = value.then( function ( key, value ) {

results[ key ] = value;

}.bind( this, type + ( type === 'mesh' ? 'es' : 's' ) ) );

pending.push( value );

}

return Promise.all( pending ).then( function () {

return results;

} );

};

/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
* @param {number} bufferIndex
Expand Down