Browse Source

Merge pull request #19518 from toji/gltf-imagebitmap

Allow GLTFLoader to use ImageBitmapLoader
Mr.doob 5 years ago
parent
commit
ff71bd022e
2 changed files with 48 additions and 4 deletions
  1. 23 2
      examples/js/loaders/GLTFLoader.js
  2. 25 2
      examples/jsm/loaders/GLTFLoader.js

+ 23 - 2
examples/js/loaders/GLTFLoader.js

@@ -1413,7 +1413,15 @@ THREE.GLTFLoader = ( function () {
 		// BufferGeometry caching
 		// BufferGeometry caching
 		this.primitiveCache = {};
 		this.primitiveCache = {};
 
 
-		this.textureLoader = new THREE.TextureLoader( this.options.manager );
+		this.useImageBitmap = typeof createImageBitmap !== 'undefined';
+
+		// Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the
+		// expensive work of uploading a texture to the GPU off the main thread.
+		if ( this.useImageBitmap ) {
+			this.textureLoader = new THREE.ImageBitmapLoader( this.options.manager );
+		} else {
+			this.textureLoader = new THREE.TextureLoader( this.options.manager );
+		}
 		this.textureLoader.setCrossOrigin( this.options.crossOrigin );
 		this.textureLoader.setCrossOrigin( this.options.crossOrigin );
 
 
 		this.fileLoader = new THREE.FileLoader( this.options.manager );
 		this.fileLoader = new THREE.FileLoader( this.options.manager );
@@ -1832,6 +1840,7 @@ THREE.GLTFLoader = ( function () {
 		var parser = this;
 		var parser = this;
 		var json = this.json;
 		var json = this.json;
 		var options = this.options;
 		var options = this.options;
+		var useImageBitmap = this.useImageBitmap;
 		var textureLoader = this.textureLoader;
 		var textureLoader = this.textureLoader;
 
 
 		var URL = self.URL || self.webkitURL;
 		var URL = self.URL || self.webkitURL;
@@ -1886,7 +1895,19 @@ THREE.GLTFLoader = ( function () {
 
 
 			return new Promise( function ( resolve, reject ) {
 			return new Promise( function ( resolve, reject ) {
 
 
-				loader.load( resolveURL( sourceURI, options.path ), resolve, undefined, reject );
+				var onLoad = resolve;
+
+				if ( useImageBitmap ) {
+
+					onLoad = function ( imageBitmap ) {
+
+						resolve( new THREE.CanvasTexture( imageBitmap ) );
+
+					};
+
+				}
+
+				loader.load( resolveURL( sourceURI, options.path ), onLoad, undefined, reject );
 
 
 			} );
 			} );
 
 

+ 25 - 2
examples/jsm/loaders/GLTFLoader.js

@@ -12,6 +12,7 @@ import {
 	Box3,
 	Box3,
 	BufferAttribute,
 	BufferAttribute,
 	BufferGeometry,
 	BufferGeometry,
+	CanvasTexture,
 	ClampToEdgeWrapping,
 	ClampToEdgeWrapping,
 	Color,
 	Color,
 	DirectionalLight,
 	DirectionalLight,
@@ -19,6 +20,7 @@ import {
 	FileLoader,
 	FileLoader,
 	FrontSide,
 	FrontSide,
 	Group,
 	Group,
+	ImageBitmapLoader,
 	InterleavedBuffer,
 	InterleavedBuffer,
 	InterleavedBufferAttribute,
 	InterleavedBufferAttribute,
 	Interpolant,
 	Interpolant,
@@ -1476,7 +1478,15 @@ var GLTFLoader = ( function () {
 		// BufferGeometry caching
 		// BufferGeometry caching
 		this.primitiveCache = {};
 		this.primitiveCache = {};
 
 
-		this.textureLoader = new TextureLoader( this.options.manager );
+		this.useImageBitmap = typeof createImageBitmap !== 'undefined';
+
+		// Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the
+		// expensive work of uploading a texture to the GPU off the main thread.
+		if ( this.useImageBitmap ) {
+			this.textureLoader = new ImageBitmapLoader( this.options.manager );
+		} else {
+			this.textureLoader = new TextureLoader( this.options.manager );
+		}
 		this.textureLoader.setCrossOrigin( this.options.crossOrigin );
 		this.textureLoader.setCrossOrigin( this.options.crossOrigin );
 
 
 		this.fileLoader = new FileLoader( this.options.manager );
 		this.fileLoader = new FileLoader( this.options.manager );
@@ -1895,6 +1905,7 @@ var GLTFLoader = ( function () {
 		var parser = this;
 		var parser = this;
 		var json = this.json;
 		var json = this.json;
 		var options = this.options;
 		var options = this.options;
+		var useImageBitmap = this.useImageBitmap;
 		var textureLoader = this.textureLoader;
 		var textureLoader = this.textureLoader;
 
 
 		var URL = self.URL || self.webkitURL;
 		var URL = self.URL || self.webkitURL;
@@ -1949,7 +1960,19 @@ var GLTFLoader = ( function () {
 
 
 			return new Promise( function ( resolve, reject ) {
 			return new Promise( function ( resolve, reject ) {
 
 
-				loader.load( resolveURL( sourceURI, options.path ), resolve, undefined, reject );
+				var onLoad = resolve;
+
+				if ( useImageBitmap ) {
+
+					onLoad = function ( imageBitmap ) {
+
+						resolve( new CanvasTexture( imageBitmap ) );
+
+					};
+
+				}
+
+				loader.load( resolveURL( sourceURI, options.path ), onLoad, undefined, reject );
 
 
 			} );
 			} );