浏览代码

WebGLTextures: Make .clampToMaxSize() more robust

Mugen87 7 年之前
父节点
当前提交
a8e70ac69e
共有 1 个文件被更改,包括 24 次插入10 次删除
  1. 24 10
      src/renderers/webgl/WebGLTextures.js

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

@@ -17,21 +17,35 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
 
 		if ( image.width > maxSize || image.height > maxSize ) {
 
-			// Warning: Scaling through the canvas will only work with images that use
-			// premultiplied alpha.
-
 			var scale = maxSize / Math.max( image.width, image.height );
 
-			var canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
-			canvas.width = Math.floor( image.width * scale );
-			canvas.height = Math.floor( image.height * scale );
+			if ( image instanceof HTMLImageElement || image instanceof HTMLCanvasElement || image instanceof ImageBitmap ) {
+
+				// Warning: Scaling through the canvas will only work with images that use
+				// premultiplied alpha.
+
+				var canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
+				canvas.width = Math.max( Math.floor( image.width * scale ), 1 );
+				canvas.height = Math.max( Math.floor( image.height * scale ), 1 );
+
+				var context = canvas.getContext( '2d' );
+				context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
+
+				console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
 
-			var context = canvas.getContext( '2d' );
-			context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
+				return canvas;
 
-			console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
+			} else {
+
+				var width = Math.max( Math.floor( image.width * scale ), 1 );
+				var height = Math.max( Math.floor( image.height * scale ), 1 );
+
+				console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + width + 'x' + height, image );
 
-			return canvas;
+				image.width = width;
+				image.height = height;
+
+			}
 
 		}