Преглед изворни кода

Merge pull request #15805 from merwaaan/fix-powerof2-webworkers

Fix texture resizing failing with web workers (Issue #15768)
Mr.doob пре 6 година
родитељ
комит
df0e4c0634
1 измењених фајлова са 24 додато и 9 уклоњено
  1. 24 9
      src/renderers/webgl/WebGLTextures.js

+ 24 - 9
src/renderers/webgl/WebGLTextures.js

@@ -30,25 +30,40 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
 
 			// only perform resize for certain image types
 
-			if ( image instanceof HTMLImageElement || image instanceof HTMLCanvasElement || image instanceof ImageBitmap ) {
+			if ( image instanceof ImageBitmap || image instanceof HTMLImageElement || image instanceof HTMLCanvasElement ) {
 
-				if ( _canvas === undefined ) _canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
+				// Use an offscreen canvas to resize the image if available, otherwise a canvas element will be created but this won't work in web workers
 
-				// cube textures can't reuse the same canvas
+				var useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined';
+
+				function createCanvas( width, height ) {
 
-				var canvas = needsNewCanvas ? document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ) : _canvas;
+					return useOffscreenCanvas ?
+						new OffscreenCanvas( width, height ) :
+						document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
+
+				}
 
 				var floor = needsPowerOfTwo ? _Math.floorPowerOfTwo : Math.floor;
 
-				canvas.width = floor( scale * image.width );
-				canvas.height = floor( scale * image.height );
+				var width = floor( scale * image.width );
+				var height = floor( scale * image.height );
+
+				if ( _canvas === undefined ) _canvas = createCanvas( width, height );
+
+				// cube textures can't reuse the same canvas
+
+				var canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas;
+
+				canvas.width = width;
+				canvas.height = height;
 
 				var context = canvas.getContext( '2d' );
-				context.drawImage( image, 0, 0, canvas.width, canvas.height );
+				context.drawImage( image, 0, 0, width, height );
 
-				console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + canvas.width + 'x' + canvas.height + ').' );
+				console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
 
-				return canvas;
+				return useOffscreenCanvas ? canvas.transferToImageBitmap() : canvas;
 
 			} else {