diff --git a/src/loaders/CompressedTextureLoader.js b/src/loaders/CompressedTextureLoader.js index e0795fecd3e138c97a9096e7b9593c3641037e09..02ef233412d3146bb5e3881783c39ab9a21cebc3 100644 --- a/src/loaders/CompressedTextureLoader.js +++ b/src/loaders/CompressedTextureLoader.js @@ -9,17 +9,15 @@ import { Loader } from './Loader.js'; * Sub classes have to implement the parse() method which will be used in load(). */ -function CompressedTextureLoader( manager ) { +class CompressedTextureLoader extends Loader { - Loader.call( this, manager ); + constructor( manager ) { -} - -CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { + super( manager ); - constructor: CompressedTextureLoader, + } - load: function ( url, onLoad, onProgress, onError ) { + load( url, onLoad, onProgress, onError ) { const scope = this; @@ -130,7 +128,7 @@ CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototy } -} ); +} export { CompressedTextureLoader }; diff --git a/src/loaders/DataTextureLoader.js b/src/loaders/DataTextureLoader.js index 6c0d69cccb18d418bb147172c635096e5159519d..e584dbb9412ac8668b742cd03cf198de52abc9b1 100644 --- a/src/loaders/DataTextureLoader.js +++ b/src/loaders/DataTextureLoader.js @@ -9,17 +9,15 @@ import { Loader } from './Loader.js'; * Sub classes have to implement the parse() method which will be used in load(). */ -function DataTextureLoader( manager ) { +class DataTextureLoader extends Loader { - Loader.call( this, manager ); + constructor( manager ) { -} - -DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { + super( manager ); - constructor: DataTextureLoader, + } - load: function ( url, onLoad, onProgress, onError ) { + load( url, onLoad, onProgress, onError ) { const scope = this; @@ -110,7 +108,7 @@ DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), } -} ); +} export { DataTextureLoader }; diff --git a/src/loaders/FileLoader.js b/src/loaders/FileLoader.js index 53d3a26ea9bb5f1e51b9c8ba3748cdd6843a9025..962cedc9e52a98af6190a8ea9debfb8bf5a13ac1 100644 --- a/src/loaders/FileLoader.js +++ b/src/loaders/FileLoader.js @@ -3,17 +3,15 @@ import { Loader } from './Loader.js'; const loading = {}; -function FileLoader( manager ) { +class FileLoader extends Loader { - Loader.call( this, manager ); + constructor( manager ) { -} - -FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), { + super( manager ); - constructor: FileLoader, + } - load: function ( url, onLoad, onProgress, onError ) { + load( url, onLoad, onProgress, onError ) { if ( url === undefined ) url = ''; @@ -277,23 +275,23 @@ FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), { return request; - }, + } - setResponseType: function ( value ) { + setResponseType( value ) { this.responseType = value; return this; - }, + } - setMimeType: function ( value ) { + setMimeType( value ) { this.mimeType = value; return this; } -} ); +} export { FileLoader }; diff --git a/src/loaders/ImageBitmapLoader.js b/src/loaders/ImageBitmapLoader.js index 83a564e0b21d3b3d211ed4c254d44e8c6db97fc2..2a9d15a37428408baa0f4785af5d838eea60d8da 100644 --- a/src/loaders/ImageBitmapLoader.js +++ b/src/loaders/ImageBitmapLoader.js @@ -1,41 +1,37 @@ import { Cache } from './Cache.js'; import { Loader } from './Loader.js'; -function ImageBitmapLoader( manager ) { +class ImageBitmapLoader extends Loader { - if ( typeof createImageBitmap === 'undefined' ) { + constructor( manager ) { - console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' ); + super( manager ); - } - - if ( typeof fetch === 'undefined' ) { - - console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' ); + if ( typeof createImageBitmap === 'undefined' ) { - } + console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' ); - Loader.call( this, manager ); + } - this.options = { premultiplyAlpha: 'none' }; + if ( typeof fetch === 'undefined' ) { -} + console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' ); -ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), { + } - constructor: ImageBitmapLoader, + this.options = { premultiplyAlpha: 'none' }; - isImageBitmapLoader: true, + } - setOptions: function setOptions( options ) { + setOptions( options ) { this.options = options; return this; - }, + } - load: function ( url, onLoad, onProgress, onError ) { + load( url, onLoad, onProgress, onError ) { if ( url === undefined ) url = ''; @@ -96,6 +92,8 @@ ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), } -} ); +} + +ImageBitmapLoader.prototype.isImageBitmapLoader = true; export { ImageBitmapLoader }; diff --git a/src/loaders/Loader.js b/src/loaders/Loader.js index 5cab81fba75c50e0aae724f6243cfcd781f96568..3d161b5d6f62b5952108421e3dd6ff4dfbaa7809 100644 --- a/src/loaders/Loader.js +++ b/src/loaders/Loader.js @@ -1,22 +1,22 @@ import { DefaultLoadingManager } from './LoadingManager.js'; -function Loader( manager ) { +class Loader { - this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager; + constructor( manager ) { - this.crossOrigin = 'anonymous'; - this.withCredentials = false; - this.path = ''; - this.resourcePath = ''; - this.requestHeader = {}; + this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager; -} + this.crossOrigin = 'anonymous'; + this.withCredentials = false; + this.path = ''; + this.resourcePath = ''; + this.requestHeader = {}; -Object.assign( Loader.prototype, { + } - load: function ( /* url, onLoad, onProgress, onError */ ) {}, + load( /* url, onLoad, onProgress, onError */ ) {} - loadAsync: function ( url, onProgress ) { + loadAsync( url, onProgress ) { const scope = this; @@ -26,45 +26,45 @@ Object.assign( Loader.prototype, { } ); - }, + } - parse: function ( /* data */ ) {}, + parse( /* data */ ) {} - setCrossOrigin: function ( crossOrigin ) { + setCrossOrigin( crossOrigin ) { this.crossOrigin = crossOrigin; return this; - }, + } - setWithCredentials: function ( value ) { + setWithCredentials( value ) { this.withCredentials = value; return this; - }, + } - setPath: function ( path ) { + setPath( path ) { this.path = path; return this; - }, + } - setResourcePath: function ( resourcePath ) { + setResourcePath( resourcePath ) { this.resourcePath = resourcePath; return this; - }, + } - setRequestHeader: function ( requestHeader ) { + setRequestHeader( requestHeader ) { this.requestHeader = requestHeader; return this; } -} ); +} export { Loader }; diff --git a/src/loaders/LoaderUtils.js b/src/loaders/LoaderUtils.js index 9bda4b40a1c47c0624f04a0fb04937c712a89d3d..de58b44ec2e1950805b8bbc47fd34328df23ee94 100644 --- a/src/loaders/LoaderUtils.js +++ b/src/loaders/LoaderUtils.js @@ -1,6 +1,6 @@ -const LoaderUtils = { +class LoaderUtils { - decodeText: function ( array ) { + static decodeText( array ) { if ( typeof TextDecoder !== 'undefined' ) { @@ -32,9 +32,9 @@ const LoaderUtils = { } - }, + } - extractUrlBase: function ( url ) { + static extractUrlBase( url ) { const index = url.lastIndexOf( '/' ); @@ -44,6 +44,6 @@ const LoaderUtils = { } -}; +} export { LoaderUtils }; diff --git a/src/loaders/LoadingManager.js b/src/loaders/LoadingManager.js index 7160698ffdb3f16e117d69ab9b2780893d5e72d3..f3643c5a8a5986d448572ea5d86eb1f5a5d37fbd 100644 --- a/src/loaders/LoadingManager.js +++ b/src/loaders/LoadingManager.js @@ -1,139 +1,142 @@ -function LoadingManager( onLoad, onProgress, onError ) { +class LoadingManager { - const scope = this; + constructor( onLoad, onProgress, onError ) { - let isLoading = false; - let itemsLoaded = 0; - let itemsTotal = 0; - let urlModifier = undefined; - const handlers = []; + const scope = this; - // Refer to #5689 for the reason why we don't set .onStart - // in the constructor + let isLoading = false; + let itemsLoaded = 0; + let itemsTotal = 0; + let urlModifier = undefined; + const handlers = []; - this.onStart = undefined; - this.onLoad = onLoad; - this.onProgress = onProgress; - this.onError = onError; + // Refer to #5689 for the reason why we don't set .onStart + // in the constructor - this.itemStart = function ( url ) { + this.onStart = undefined; + this.onLoad = onLoad; + this.onProgress = onProgress; + this.onError = onError; - itemsTotal ++; + this.itemStart = function ( url ) { - if ( isLoading === false ) { + itemsTotal ++; - if ( scope.onStart !== undefined ) { + if ( isLoading === false ) { - scope.onStart( url, itemsLoaded, itemsTotal ); + if ( scope.onStart !== undefined ) { + + scope.onStart( url, itemsLoaded, itemsTotal ); + + } } - } + isLoading = true; - isLoading = true; + }; - }; + this.itemEnd = function ( url ) { - this.itemEnd = function ( url ) { + itemsLoaded ++; - itemsLoaded ++; + if ( scope.onProgress !== undefined ) { - if ( scope.onProgress !== undefined ) { + scope.onProgress( url, itemsLoaded, itemsTotal ); - scope.onProgress( url, itemsLoaded, itemsTotal ); + } - } + if ( itemsLoaded === itemsTotal ) { - if ( itemsLoaded === itemsTotal ) { + isLoading = false; - isLoading = false; + if ( scope.onLoad !== undefined ) { - if ( scope.onLoad !== undefined ) { + scope.onLoad(); - scope.onLoad(); + } } - } + }; - }; + this.itemError = function ( url ) { - this.itemError = function ( url ) { + if ( scope.onError !== undefined ) { - if ( scope.onError !== undefined ) { + scope.onError( url ); - scope.onError( url ); + } - } + }; - }; + this.resolveURL = function ( url ) { - this.resolveURL = function ( url ) { + if ( urlModifier ) { - if ( urlModifier ) { + return urlModifier( url ); - return urlModifier( url ); + } - } + return url; - return url; + }; - }; + this.setURLModifier = function ( transform ) { - this.setURLModifier = function ( transform ) { + urlModifier = transform; - urlModifier = transform; + return this; - return this; + }; - }; + this.addHandler = function ( regex, loader ) { - this.addHandler = function ( regex, loader ) { + handlers.push( regex, loader ); - handlers.push( regex, loader ); + return this; - return this; + }; - }; + this.removeHandler = function ( regex ) { - this.removeHandler = function ( regex ) { + const index = handlers.indexOf( regex ); - const index = handlers.indexOf( regex ); + if ( index !== - 1 ) { - if ( index !== - 1 ) { + handlers.splice( index, 2 ); - handlers.splice( index, 2 ); + } - } + return this; - return this; + }; - }; + this.getHandler = function ( file ) { - this.getHandler = function ( file ) { + for ( let i = 0, l = handlers.length; i < l; i += 2 ) { - for ( let i = 0, l = handlers.length; i < l; i += 2 ) { + const regex = handlers[ i ]; + const loader = handlers[ i + 1 ]; - const regex = handlers[ i ]; - const loader = handlers[ i + 1 ]; + if ( regex.global ) regex.lastIndex = 0; // see #17920 - if ( regex.global ) regex.lastIndex = 0; // see #17920 + if ( regex.test( file ) ) { - if ( regex.test( file ) ) { + return loader; - return loader; + } } - } + return null; - return null; + }; - }; + } } const DefaultLoadingManager = new LoadingManager(); - export { DefaultLoadingManager, LoadingManager }; diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 6263439008372b66a4d34b3b94d92dd0aaef2ca4..c66af88bfac6438882b56ef16336c522fcdb86cc 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -3,17 +3,15 @@ import { ImageLoader } from './ImageLoader.js'; import { Texture } from '../textures/Texture.js'; import { Loader } from './Loader.js'; -function TextureLoader( manager ) { +class TextureLoader extends Loader { - Loader.call( this, manager ); + constructor( manager ) { -} - -TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { + super( manager ); - constructor: TextureLoader, + } - load: function ( url, onLoad, onProgress, onError ) { + load( url, onLoad, onProgress, onError ) { const texture = new Texture(); @@ -43,7 +41,7 @@ TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { } -} ); +} export { TextureLoader };