From dc390fc70c2d213256de3d283a87f5238f0c742b Mon Sep 17 00:00:00 2001 From: Takahiro Date: Wed, 15 Apr 2020 16:20:48 -0700 Subject: [PATCH 1/7] Introduce GLTFLoader plugin system --- examples/js/loaders/GLTFLoader.js | 139 ++++++++++++++++++++++++----- examples/jsm/loaders/GLTFLoader.js | 139 ++++++++++++++++++++++++----- 2 files changed, 238 insertions(+), 40 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 2bb6ef82c7d3b4..a3ca8e42549b7e 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -15,6 +15,9 @@ THREE.GLTFLoader = ( function () { this.dracoLoader = null; this.ddsLoader = null; + this.plugins = []; + this.register( GLTFMaterialsClearcoatExtension ); + } GLTFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), { @@ -110,6 +113,26 @@ THREE.GLTFLoader = ( function () { }, + register: function ( plugin ) { + + if ( ! this.plugins.includes( plugin ) ) { + + this.plugins.push( plugin ); + + } + + }, + + unregister: function ( plugin ) { + + if ( this.plugins.includes( plugin ) ) { + + this.plugins.splice( this.plugins.indexOf( plugin ), 1 ); + + } + + }, + parse: function ( data, path, onLoad, onError ) { var content; @@ -168,10 +191,6 @@ THREE.GLTFLoader = ( function () { extensions[ extensionName ] = new GLTFLightsExtension( json ); break; - case EXTENSIONS.KHR_MATERIALS_CLEARCOAT: - extensions[ extensionName ] = new GLTFMaterialsClearcoatExtension(); - break; - case EXTENSIONS.KHR_MATERIALS_UNLIT: extensions[ extensionName ] = new GLTFMaterialsUnlitExtension(); break; @@ -217,7 +236,7 @@ THREE.GLTFLoader = ( function () { manager: this.manager } ); - + parser.setPlugins( this.plugins ); parser.parse( onLoad, onError ); } @@ -418,8 +437,9 @@ THREE.GLTFLoader = ( function () { * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat */ - function GLTFMaterialsClearcoatExtension() { + function GLTFMaterialsClearcoatExtension( parser ) { + this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT; } @@ -430,7 +450,16 @@ THREE.GLTFLoader = ( function () { }; - GLTFMaterialsClearcoatExtension.prototype.extendParams = function ( materialParams, materialDef, parser ) { + GLTFMaterialsClearcoatExtension.prototype.extendMaterialParams = function ( materialIndex, materialParams ) { + + var parser = this.parser; + var materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } var pending = []; @@ -1399,6 +1428,7 @@ THREE.GLTFLoader = ( function () { this.json = json || {}; this.extensions = extensions || {}; + this.plugins = {}; this.options = options || {}; // loader object cache @@ -1421,6 +1451,23 @@ THREE.GLTFLoader = ( function () { } + GLTFParser.prototype.setPlugins = function ( pluginList ) { + + for ( var i = 0; i < pluginList.length; i ++ ) { + + var plugin = new pluginList[ i ]( this ); + this.plugins[ plugin.name ] = plugin; + + // Workaround to avoid determining as unknown extension + // in addUnknownExtensionsToUserData(). + // Remove this workaround if we move all the existing + // extension handlers to plugin system + this.extensions[ plugin.name ] = true; + + } + + }; + GLTFParser.prototype.parse = function ( onLoad, onError ) { var parser = this; @@ -1524,6 +1571,38 @@ THREE.GLTFLoader = ( function () { }; + GLTFParser.prototype._invokeOne = function ( func ) { + + var extensions = Object.values( this.plugins ); + extensions.push( this ); + + for ( var i = 0; i < extensions.length; i ++ ) { + + var result = func( extensions[ i ] ); + + if ( result ) return result; + + } + + }; + + GLTFParser.prototype._invokeAll = function ( func ) { + + var extensions = Object.values( this.plugins ); + extensions.unshift( this ); + + var pending = []; + + for ( var i = 0; i < extensions.length; i ++ ) { + + pending.push( func( extensions[ i ] ) ); + + } + + return Promise.all( pending ); + + }; + /** * Requests the specified dependency asynchronously, with caching. * @param {string} type @@ -1548,7 +1627,11 @@ THREE.GLTFLoader = ( function () { break; case 'mesh': - dependency = this.loadMesh( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMesh && ext.loadMesh( index ); + + } ); break; case 'accessor': @@ -1556,7 +1639,11 @@ THREE.GLTFLoader = ( function () { break; case 'bufferView': - dependency = this.loadBufferView( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadBufferView && ext.loadBufferView( index ); + + } ); break; case 'buffer': @@ -1564,7 +1651,11 @@ THREE.GLTFLoader = ( function () { break; case 'material': - dependency = this.loadMaterial( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMaterial && ext.loadMaterial( index ); + + } ); break; case 'texture': @@ -2093,6 +2184,12 @@ THREE.GLTFLoader = ( function () { }; + GLTFParser.prototype.getMaterialType = function () { + + return THREE.MeshStandardMaterial; + + }; + /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials * @param {number} materialIndex @@ -2128,8 +2225,6 @@ THREE.GLTFLoader = ( function () { // Specification: // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material - materialType = THREE.MeshStandardMaterial; - var metallicRoughness = materialDef.pbrMetallicRoughness || {}; materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 ); @@ -2160,6 +2255,18 @@ THREE.GLTFLoader = ( function () { } + materialType = this._invokeOne( function ( ext ) { + + return ext.getMaterialType && ext.getMaterialType(); + + } ); + + pending.push( this._invokeAll( function ( ext ) { + + return ext.extendMaterialParams && ext.extendMaterialParams( materialIndex, materialParams ); + + } ) ); + } if ( materialDef.doubleSided === true ) { @@ -2227,14 +2334,6 @@ THREE.GLTFLoader = ( function () { } - if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_CLEARCOAT ] ) { - - var clearcoatExtension = extensions[ EXTENSIONS.KHR_MATERIALS_CLEARCOAT ]; - materialType = clearcoatExtension.getMaterialType(); - pending.push( clearcoatExtension.extendParams( materialParams, { extensions: materialExtensions }, parser ) ); - - } - return Promise.all( pending ).then( function () { var material; diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 73d0a7bcd56f73..703ce8f2e91a68 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -79,6 +79,9 @@ var GLTFLoader = ( function () { this.dracoLoader = null; this.ddsLoader = null; + this.plugins = []; + this.register( GLTFMaterialsClearcoatExtension ); + } GLTFLoader.prototype = Object.assign( Object.create( Loader.prototype ), { @@ -174,6 +177,26 @@ var GLTFLoader = ( function () { }, + register: function ( plugin ) { + + if ( ! this.plugins.includes( plugin ) ) { + + this.plugins.push( plugin ); + + } + + }, + + unregister: function ( plugin ) { + + if ( this.plugins.includes( plugin ) ) { + + this.plugins.splice( this.plugins.indexOf( plugin ), 1 ); + + } + + }, + parse: function ( data, path, onLoad, onError ) { var content; @@ -232,10 +255,6 @@ var GLTFLoader = ( function () { extensions[ extensionName ] = new GLTFLightsExtension( json ); break; - case EXTENSIONS.KHR_MATERIALS_CLEARCOAT: - extensions[ extensionName ] = new GLTFMaterialsClearcoatExtension(); - break; - case EXTENSIONS.KHR_MATERIALS_UNLIT: extensions[ extensionName ] = new GLTFMaterialsUnlitExtension(); break; @@ -281,7 +300,7 @@ var GLTFLoader = ( function () { manager: this.manager } ); - + parser.setPlugins( this.plugins ); parser.parse( onLoad, onError ); } @@ -482,8 +501,9 @@ var GLTFLoader = ( function () { * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat */ - function GLTFMaterialsClearcoatExtension() { + function GLTFMaterialsClearcoatExtension( parser ) { + this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT; } @@ -494,7 +514,16 @@ var GLTFLoader = ( function () { }; - GLTFMaterialsClearcoatExtension.prototype.extendParams = function ( materialParams, materialDef, parser ) { + GLTFMaterialsClearcoatExtension.prototype.extendMaterialParams = function ( materialIndex, materialParams ) { + + var parser = this.parser; + var materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } var pending = []; @@ -1463,6 +1492,7 @@ var GLTFLoader = ( function () { this.json = json || {}; this.extensions = extensions || {}; + this.plugins = {}; this.options = options || {}; // loader object cache @@ -1485,6 +1515,23 @@ var GLTFLoader = ( function () { } + GLTFParser.prototype.setPlugins = function ( pluginList ) { + + for ( var i = 0; i < pluginList.length; i ++ ) { + + var plugin = new pluginList[ i ]( this ); + this.plugins[ plugin.name ] = plugin; + + // Workaround to avoid determining as unknown extension + // in addUnknownExtensionsToUserData(). + // Remove this workaround if we move all the existing + // extension handlers to plugin system + this.extensions[ plugin.name ] = true; + + } + + }; + GLTFParser.prototype.parse = function ( onLoad, onError ) { var parser = this; @@ -1588,6 +1635,38 @@ var GLTFLoader = ( function () { }; + GLTFParser.prototype._invokeOne = function ( func ) { + + var extensions = Object.values( this.plugins ); + extensions.push( this ); + + for ( var i = 0; i < extensions.length; i ++ ) { + + var result = func( extensions[ i ] ); + + if ( result ) return result; + + } + + }; + + GLTFParser.prototype._invokeAll = function ( func ) { + + var extensions = Object.values( this.plugins ); + extensions.unshift( this ); + + var pending = []; + + for ( var i = 0; i < extensions.length; i ++ ) { + + pending.push( func( extensions[ i ] ) ); + + } + + return Promise.all( pending ); + + }; + /** * Requests the specified dependency asynchronously, with caching. * @param {string} type @@ -1612,7 +1691,11 @@ var GLTFLoader = ( function () { break; case 'mesh': - dependency = this.loadMesh( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMesh && ext.loadMesh( index ); + + } ); break; case 'accessor': @@ -1620,7 +1703,11 @@ var GLTFLoader = ( function () { break; case 'bufferView': - dependency = this.loadBufferView( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadBufferView && ext.loadBufferView( index ); + + } ); break; case 'buffer': @@ -1628,7 +1715,11 @@ var GLTFLoader = ( function () { break; case 'material': - dependency = this.loadMaterial( index ); + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMaterial && ext.loadMaterial( index ); + + } ); break; case 'texture': @@ -2157,6 +2248,12 @@ var GLTFLoader = ( function () { }; + GLTFParser.prototype.getMaterialType = function () { + + return MeshStandardMaterial; + + }; + /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials * @param {number} materialIndex @@ -2192,8 +2289,6 @@ var GLTFLoader = ( function () { // Specification: // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material - materialType = MeshStandardMaterial; - var metallicRoughness = materialDef.pbrMetallicRoughness || {}; materialParams.color = new Color( 1.0, 1.0, 1.0 ); @@ -2224,6 +2319,18 @@ var GLTFLoader = ( function () { } + materialType = this._invokeOne( function ( ext ) { + + return ext.getMaterialType && ext.getMaterialType(); + + } ); + + pending.push( this._invokeAll( function ( ext ) { + + return ext.extendMaterialParams && ext.extendMaterialParams( materialIndex, materialParams ); + + } ) ); + } if ( materialDef.doubleSided === true ) { @@ -2291,14 +2398,6 @@ var GLTFLoader = ( function () { } - if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_CLEARCOAT ] ) { - - var clearcoatExtension = extensions[ EXTENSIONS.KHR_MATERIALS_CLEARCOAT ]; - materialType = clearcoatExtension.getMaterialType(); - pending.push( clearcoatExtension.extendParams( materialParams, { extensions: materialExtensions }, parser ) ); - - } - return Promise.all( pending ).then( function () { var material; From c2fe64e18cb5017c1481727dca23ef47366b8b20 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Wed, 15 Apr 2020 17:39:11 -0700 Subject: [PATCH 2/7] GLTFLoader method chainable register/unregister --- examples/js/loaders/GLTFLoader.js | 4 ++++ examples/jsm/loaders/GLTFLoader.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index a3ca8e42549b7e..0880eadfe4bc6a 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -121,6 +121,8 @@ THREE.GLTFLoader = ( function () { } + return this; + }, unregister: function ( plugin ) { @@ -131,6 +133,8 @@ THREE.GLTFLoader = ( function () { } + return this; + }, parse: function ( data, path, onLoad, onError ) { diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 703ce8f2e91a68..d0a6dec3b5d495 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -185,6 +185,8 @@ var GLTFLoader = ( function () { } + return this; + }, unregister: function ( plugin ) { @@ -195,6 +197,8 @@ var GLTFLoader = ( function () { } + return this; + }, parse: function ( data, path, onLoad, onError ) { From 74fedc22abce56e04336c2348044fd420084fff5 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Tue, 5 May 2020 18:26:30 -0700 Subject: [PATCH 3/7] GLTFLoader: take callback in .register() --- examples/js/loaders/GLTFLoader.js | 24 ++++++++++++------------ examples/jsm/loaders/GLTFLoader.js | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 0880eadfe4bc6a..783a19adaac846 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -15,8 +15,8 @@ THREE.GLTFLoader = ( function () { this.dracoLoader = null; this.ddsLoader = null; - this.plugins = []; - this.register( GLTFMaterialsClearcoatExtension ); + this.pluginCallbacks = []; + this.register( function ( parser ) { return new GLTFMaterialsClearcoatExtension( parser ); } ); } @@ -113,11 +113,11 @@ THREE.GLTFLoader = ( function () { }, - register: function ( plugin ) { + register: function ( callback ) { - if ( ! this.plugins.includes( plugin ) ) { + if ( ! this.pluginCallbacks.includes( callback ) ) { - this.plugins.push( plugin ); + this.pluginCallbacks.push( callback ); } @@ -125,11 +125,11 @@ THREE.GLTFLoader = ( function () { }, - unregister: function ( plugin ) { + unregister: function ( callback ) { - if ( this.plugins.includes( plugin ) ) { + if ( this.pluginCallbacks.includes( callback ) ) { - this.plugins.splice( this.plugins.indexOf( plugin ), 1 ); + this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); } @@ -240,7 +240,7 @@ THREE.GLTFLoader = ( function () { manager: this.manager } ); - parser.setPlugins( this.plugins ); + parser.setPluginCallbacks( this.pluginCallbacks ); parser.parse( onLoad, onError ); } @@ -1455,11 +1455,11 @@ THREE.GLTFLoader = ( function () { } - GLTFParser.prototype.setPlugins = function ( pluginList ) { + GLTFParser.prototype.setPluginCallbacks = function ( pluginCallbacks ) { - for ( var i = 0; i < pluginList.length; i ++ ) { + for ( var i = 0; i < pluginCallbacks.length; i ++ ) { - var plugin = new pluginList[ i ]( this ); + var plugin = pluginCallbacks[ i ]( this ); this.plugins[ plugin.name ] = plugin; // Workaround to avoid determining as unknown extension diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index d0a6dec3b5d495..edb4a9fb63e901 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -79,8 +79,8 @@ var GLTFLoader = ( function () { this.dracoLoader = null; this.ddsLoader = null; - this.plugins = []; - this.register( GLTFMaterialsClearcoatExtension ); + this.pluginCallbacks = []; + this.register( function ( parser ) { return new GLTFMaterialsClearcoatExtension( parser ); } ); } @@ -177,11 +177,11 @@ var GLTFLoader = ( function () { }, - register: function ( plugin ) { + register: function ( callback ) { - if ( ! this.plugins.includes( plugin ) ) { + if ( ! this.pluginCallbacks.includes( callback ) ) { - this.plugins.push( plugin ); + this.pluginCallbacks.push( callback ); } @@ -189,11 +189,11 @@ var GLTFLoader = ( function () { }, - unregister: function ( plugin ) { + unregister: function ( callback ) { - if ( this.plugins.includes( plugin ) ) { + if ( this.pluginCallbacks.includes( callback ) ) { - this.plugins.splice( this.plugins.indexOf( plugin ), 1 ); + this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); } @@ -304,7 +304,7 @@ var GLTFLoader = ( function () { manager: this.manager } ); - parser.setPlugins( this.plugins ); + parser.setPluginCallbacks( this.pluginCallbacks ); parser.parse( onLoad, onError ); } @@ -1519,11 +1519,11 @@ var GLTFLoader = ( function () { } - GLTFParser.prototype.setPlugins = function ( pluginList ) { + GLTFParser.prototype.setPluginCallbacks = function ( pluginCallbacks ) { - for ( var i = 0; i < pluginList.length; i ++ ) { + for ( var i = 0; i < pluginCallbacks.length; i ++ ) { - var plugin = new pluginList[ i ]( this ); + var plugin = pluginCallbacks[ i ]( this ); this.plugins[ plugin.name ] = plugin; // Workaround to avoid determining as unknown extension From f96383799a6ac04b4563148e32c30472eca50685 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Tue, 5 May 2020 19:18:20 -0700 Subject: [PATCH 4/7] GLTFLoader: Suppress unknown extension warning --- examples/js/loaders/GLTFLoader.js | 53 ++++++++++++++++++------------ examples/jsm/loaders/GLTFLoader.js | 53 ++++++++++++++++++------------ 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 783a19adaac846..35fdef40d88363 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -141,6 +141,7 @@ THREE.GLTFLoader = ( function () { var content; var extensions = {}; + var plugins = {}; if ( typeof data === 'string' ) { @@ -182,6 +183,27 @@ THREE.GLTFLoader = ( function () { } + var parser = new GLTFParser( json, { + + path: path || this.resourcePath || '', + crossOrigin: this.crossOrigin, + manager: this.manager + + } ); + + for ( var i = 0; i < this.pluginCallbacks.length; i ++ ) { + + var plugin = this.pluginCallbacks[ i ]( parser ); + plugins[ plugin.name ] = plugin; + + // Workaround to avoid determining as unknown extension + // in addUnknownExtensionsToUserData(). + // Remove this workaround if we move all the existing + // extension handlers to plugin system + extensions[ plugin.name ] = true; + + } + if ( json.extensionsUsed ) { for ( var i = 0; i < json.extensionsUsed.length; ++ i ) { @@ -221,7 +243,7 @@ THREE.GLTFLoader = ( function () { default: - if ( extensionsRequired.indexOf( extensionName ) >= 0 ) { + if ( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] === undefined ) { console.warn( 'THREE.GLTFLoader: Unknown extension "' + extensionName + '".' ); @@ -233,14 +255,8 @@ THREE.GLTFLoader = ( function () { } - var parser = new GLTFParser( json, extensions, { - - path: path || this.resourcePath || '', - crossOrigin: this.crossOrigin, - manager: this.manager - - } ); - parser.setPluginCallbacks( this.pluginCallbacks ); + parser.setExtensions( extensions ); + parser.setPlugins( plugins ); parser.parse( onLoad, onError ); } @@ -1428,10 +1444,10 @@ THREE.GLTFLoader = ( function () { /* GLTF PARSER */ - function GLTFParser( json, extensions, options ) { + function GLTFParser( json, options ) { this.json = json || {}; - this.extensions = extensions || {}; + this.extensions = {}; this.plugins = {}; this.options = options || {}; @@ -1455,20 +1471,15 @@ THREE.GLTFLoader = ( function () { } - GLTFParser.prototype.setPluginCallbacks = function ( pluginCallbacks ) { + GLTFParser.prototype.setExtensions = function ( extensions ) { - for ( var i = 0; i < pluginCallbacks.length; i ++ ) { + this.extensions = extensions; - var plugin = pluginCallbacks[ i ]( this ); - this.plugins[ plugin.name ] = plugin; + }; - // Workaround to avoid determining as unknown extension - // in addUnknownExtensionsToUserData(). - // Remove this workaround if we move all the existing - // extension handlers to plugin system - this.extensions[ plugin.name ] = true; + GLTFParser.prototype.setPlugins = function ( plugins ) { - } + this.plugins = plugins; }; diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index edb4a9fb63e901..d361175888633d 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -205,6 +205,7 @@ var GLTFLoader = ( function () { var content; var extensions = {}; + var plugins = {}; if ( typeof data === 'string' ) { @@ -246,6 +247,27 @@ var GLTFLoader = ( function () { } + var parser = new GLTFParser( json, { + + path: path || this.resourcePath || '', + crossOrigin: this.crossOrigin, + manager: this.manager + + } ); + + for ( var i = 0; i < this.pluginCallbacks.length; i ++ ) { + + var plugin = this.pluginCallbacks[ i ]( parser ); + plugins[ plugin.name ] = plugin; + + // Workaround to avoid determining as unknown extension + // in addUnknownExtensionsToUserData(). + // Remove this workaround if we move all the existing + // extension handlers to plugin system + extensions[ plugin.name ] = true; + + } + if ( json.extensionsUsed ) { for ( var i = 0; i < json.extensionsUsed.length; ++ i ) { @@ -285,7 +307,7 @@ var GLTFLoader = ( function () { default: - if ( extensionsRequired.indexOf( extensionName ) >= 0 ) { + if ( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] === undefined ) { console.warn( 'THREE.GLTFLoader: Unknown extension "' + extensionName + '".' ); @@ -297,14 +319,8 @@ var GLTFLoader = ( function () { } - var parser = new GLTFParser( json, extensions, { - - path: path || this.resourcePath || '', - crossOrigin: this.crossOrigin, - manager: this.manager - - } ); - parser.setPluginCallbacks( this.pluginCallbacks ); + parser.setExtensions( extensions ); + parser.setPlugins( plugins ); parser.parse( onLoad, onError ); } @@ -1492,10 +1508,10 @@ var GLTFLoader = ( function () { /* GLTF PARSER */ - function GLTFParser( json, extensions, options ) { + function GLTFParser( json, options ) { this.json = json || {}; - this.extensions = extensions || {}; + this.extensions = {}; this.plugins = {}; this.options = options || {}; @@ -1519,20 +1535,15 @@ var GLTFLoader = ( function () { } - GLTFParser.prototype.setPluginCallbacks = function ( pluginCallbacks ) { + GLTFParser.prototype.setExtensions = function ( extensions ) { - for ( var i = 0; i < pluginCallbacks.length; i ++ ) { + this.extensions = extensions; - var plugin = pluginCallbacks[ i ]( this ); - this.plugins[ plugin.name ] = plugin; + }; - // Workaround to avoid determining as unknown extension - // in addUnknownExtensionsToUserData(). - // Remove this workaround if we move all the existing - // extension handlers to plugin system - this.extensions[ plugin.name ] = true; + GLTFParser.prototype.setPlugins = function ( plugins ) { - } + this.plugins = plugins; }; From 879f28f93c01b332bf7059ec1d69a9da02ec3d12 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Mon, 11 May 2020 11:16:13 -0700 Subject: [PATCH 5/7] GLTFLoader: Pass materialIndex argument to getMaterialType() --- examples/js/loaders/GLTFLoader.js | 6 +++--- examples/jsm/loaders/GLTFLoader.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 35fdef40d88363..ed2dc6f7012a31 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -464,7 +464,7 @@ THREE.GLTFLoader = ( function () { } - GLTFMaterialsClearcoatExtension.prototype.getMaterialType = function () { + GLTFMaterialsClearcoatExtension.prototype.getMaterialType = function ( materialIndex ) { return THREE.MeshPhysicalMaterial; @@ -2199,7 +2199,7 @@ THREE.GLTFLoader = ( function () { }; - GLTFParser.prototype.getMaterialType = function () { + GLTFParser.prototype.getMaterialType = function ( materialIndex ) { return THREE.MeshStandardMaterial; @@ -2272,7 +2272,7 @@ THREE.GLTFLoader = ( function () { materialType = this._invokeOne( function ( ext ) { - return ext.getMaterialType && ext.getMaterialType(); + return ext.getMaterialType && ext.getMaterialType( materialIndex ); } ); diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index d361175888633d..9e83661a7808cc 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -528,7 +528,7 @@ var GLTFLoader = ( function () { } - GLTFMaterialsClearcoatExtension.prototype.getMaterialType = function () { + GLTFMaterialsClearcoatExtension.prototype.getMaterialType = function ( materialIndex ) { return MeshPhysicalMaterial; @@ -2263,7 +2263,7 @@ var GLTFLoader = ( function () { }; - GLTFParser.prototype.getMaterialType = function () { + GLTFParser.prototype.getMaterialType = function ( materialIndex ) { return MeshStandardMaterial; @@ -2336,7 +2336,7 @@ var GLTFLoader = ( function () { materialType = this._invokeOne( function ( ext ) { - return ext.getMaterialType && ext.getMaterialType(); + return ext.getMaterialType && ext.getMaterialType( materialIndex ); } ); From 91d296e359d787c0fd914e1f078b72cf36ef32a5 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Mon, 11 May 2020 11:32:06 -0700 Subject: [PATCH 6/7] GLTFLoader: Replace Array.includes() with Array.indexOf() --- examples/js/loaders/GLTFLoader.js | 4 ++-- examples/jsm/loaders/GLTFLoader.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index ed2dc6f7012a31..5f5c80932db862 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -115,7 +115,7 @@ THREE.GLTFLoader = ( function () { register: function ( callback ) { - if ( ! this.pluginCallbacks.includes( callback ) ) { + if ( this.pluginCallbacks.indexOf( callback ) === -1 ) { this.pluginCallbacks.push( callback ); @@ -127,7 +127,7 @@ THREE.GLTFLoader = ( function () { unregister: function ( callback ) { - if ( this.pluginCallbacks.includes( callback ) ) { + if ( this.pluginCallbacks.indexOf( callback ) !== -1 ) { this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 9e83661a7808cc..c647566d172e64 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -179,7 +179,7 @@ var GLTFLoader = ( function () { register: function ( callback ) { - if ( ! this.pluginCallbacks.includes( callback ) ) { + if ( this.pluginCallbacks.indexOf( callback ) === -1 ) { this.pluginCallbacks.push( callback ); @@ -191,7 +191,7 @@ var GLTFLoader = ( function () { unregister: function ( callback ) { - if ( this.pluginCallbacks.includes( callback ) ) { + if ( this.pluginCallbacks.indexOf( callback ) !== -1 ) { this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); From 7208eb816b10b291540ae7905555697a4a6a1431 Mon Sep 17 00:00:00 2001 From: Takahiro Date: Wed, 10 Jun 2020 21:34:41 -0700 Subject: [PATCH 7/7] Replace soft tags with hard tabs --- examples/jsm/loaders/GLTFLoader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 1ac371b258aa56..0809d719d31371 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -258,7 +258,7 @@ var GLTFLoader = ( function () { parser.fileLoader.setRequestHeader( this.requestHeader ); - for ( var i = 0; i < this.pluginCallbacks.length; i ++ ) { + for ( var i = 0; i < this.pluginCallbacks.length; i ++ ) { var plugin = this.pluginCallbacks[ i ]( parser ); plugins[ plugin.name ] = plugin;