Skip to content

Commit

Permalink
Added ImageBitmapLoader to src/ and tweaked *TextureLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Nov 16, 2017
1 parent cf16fd1 commit c8d42cd
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/loaders/CubeTextureLoader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* @author mrdoob / http://mrdoob.com/
*/

import { ImageLoader } from './ImageLoader.js';
import { CubeTexture } from '../textures/CubeTexture.js';
import { DefaultLoadingManager } from './LoadingManager.js';

/**
* @author mrdoob / http://mrdoob.com/
*/

function CubeTextureLoader( manager ) {

Expand Down
102 changes: 102 additions & 0 deletions src/loaders/ImageBitmapLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @author thespite / http://clicktorelease.com/
*/

import { Cache } from './Cache.js';
import { DefaultLoadingManager } from './LoadingManager.js';


function ImageBitmapLoader( manager ) {

if ( typeof createImageBitmap === 'undefined' ) {

console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );

}

this.manager = manager !== undefined ? manager : DefaultLoadingManager;
this.options = undefined;

};

ImageBitmapLoader.prototype = {

constructor: ImageBitmapLoader,

setOptions: function setOptions( options ) {

this.options = options;

return this;

},

load: function load( url, onLoad, onProgress, onError ) {

if ( url === undefined ) url = '';

if ( this.path !== undefined ) url = this.path + url;

var scope = this;

var cached = Cache.get( url );

if ( cached !== undefined ) {

scope.manager.itemStart( url );

setTimeout( function () {

if ( onLoad ) onLoad( cached );

scope.manager.itemEnd( url );

}, 0 );

return cached;

}

fetch( url ).then( function ( res ) {

return res.blob();

} ).then( function ( blob ) {

return createImageBitmap( blob, scope.options );

} ).then( function ( imageBitmap ) {

Cache.add( url, imageBitmap );

if ( onLoad ) onLoad( imageBitmap );

scope.manager.itemEnd( url );

} ).catch( function ( e ) {

if ( onError ) onError( e );

scope.manager.itemEnd( url );
scope.manager.itemError( url );

} );

},

setCrossOrigin: function ( value ) {

return this;

},

setPath: function ( value ) {

this.path = value;
return this;

}

};

export { ImageBitmapLoader };
7 changes: 5 additions & 2 deletions src/loaders/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ Object.assign( TextureLoader.prototype, {

load: function ( url, onLoad, onProgress, onError ) {

var texture = new Texture();

var loader = new ImageLoader( this.manager );
loader.setCrossOrigin( this.crossOrigin );
loader.setPath( this.path );

var texture = new Texture();
texture.image = loader.load( url, function () {
loader.load( url, function ( image ) {

texture.image = image;

// JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
var isJPEG = url.search( /\.(jpg|jpeg)$/ ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;
Expand Down

0 comments on commit c8d42cd

Please sign in to comment.