Răsfoiți Sursa

Added ImageBitmapLoader to src/ and tweaked *TextureLoader.

Mr.doob 7 ani în urmă
părinte
comite
c8d42cd17d

+ 4 - 3
src/loaders/CubeTextureLoader.js

@@ -1,10 +1,11 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
 import { ImageLoader } from './ImageLoader.js';
 import { ImageLoader } from './ImageLoader.js';
 import { CubeTexture } from '../textures/CubeTexture.js';
 import { CubeTexture } from '../textures/CubeTexture.js';
 import { DefaultLoadingManager } from './LoadingManager.js';
 import { DefaultLoadingManager } from './LoadingManager.js';
 
 
-/**
- * @author mrdoob / http://mrdoob.com/
- */
 
 
 function CubeTextureLoader( manager ) {
 function CubeTextureLoader( manager ) {
 
 

+ 102 - 0
src/loaders/ImageBitmapLoader.js

@@ -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 };

+ 5 - 2
src/loaders/TextureLoader.js

@@ -20,12 +20,15 @@ Object.assign( TextureLoader.prototype, {
 
 
 	load: function ( url, onLoad, onProgress, onError ) {
 	load: function ( url, onLoad, onProgress, onError ) {
 
 
+		var texture = new Texture();
+
 		var loader = new ImageLoader( this.manager );
 		var loader = new ImageLoader( this.manager );
 		loader.setCrossOrigin( this.crossOrigin );
 		loader.setCrossOrigin( this.crossOrigin );
 		loader.setPath( this.path );
 		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.
 			// 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;
 			var isJPEG = url.search( /\.(jpg|jpeg)$/ ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;