WebGLTextures.js 36.1 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

W
WestLangley 已提交
5
import { LinearFilter, NearestFilter, RGBFormat, RGBAFormat, DepthFormat, DepthStencilFormat, UnsignedShortType, UnsignedIntType, UnsignedInt248Type, FloatType, HalfFloatType, ClampToEdgeWrapping, NearestMipmapLinearFilter, NearestMipmapNearestFilter } from '../../constants.js';
B
bentok 已提交
6
import { _Math } from '../../math/Math.js';
R
Rich Harris 已提交
7

M
Mugen87 已提交
8
function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) {
9

J
Joel 已提交
10
	var _videoTextures = new WeakMap();
11
	var _canvas;
12

13 14
	// cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
	// also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
15

16
	var useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
V
Volker Buzek 已提交
17
		&& ( new OffscreenCanvas( 1, 1 ).getContext( "2d" ) ) !== null;
18 19 20 21 22 23 24 25 26 27 28

	function createCanvas( width, height ) {

		// Use OffscreenCanvas when available. Specially needed in web workers

		return useOffscreenCanvas ?
			new OffscreenCanvas( width, height ) :
			document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );

	}

M
Mugen87 已提交
29
	function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) {
30

M
Mugen87 已提交
31
		var scale = 1;
32

M
Mugen87 已提交
33
		// handle case if texture exceeds max size
34

M
Mugen87 已提交
35
		if ( image.width > maxSize || image.height > maxSize ) {
36

M
Mugen87 已提交
37
			scale = maxSize / Math.max( image.width, image.height );
38

M
Mugen87 已提交
39
		}
40

M
Mugen87 已提交
41
		// only perform resize if necessary
42

M
Mugen87 已提交
43
		if ( scale < 1 || needsPowerOfTwo === true ) {
44

M
Mugen87 已提交
45
			// only perform resize for certain image types
46

47 48 49
			if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
				( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
				( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
50

M
Mugen87 已提交
51
				var floor = needsPowerOfTwo ? _Math.floorPowerOfTwo : Math.floor;
52

53 54 55 56 57 58 59 60 61 62 63
				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;
64

M
Mugen87 已提交
65
				var context = canvas.getContext( '2d' );
66
				context.drawImage( image, 0, 0, width, height );
67

68
				console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
69

70
				return canvas;
71

M
Mugen87 已提交
72
			} else {
73

M
Mugen87 已提交
74
				if ( 'data' in image ) {
75

M
Mugen87 已提交
76
					console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' );
77

M
Mugen87 已提交
78
				}
79

M
Mugen87 已提交
80
				return image;
81

M
Mugen87 已提交
82
			}
83 84 85 86 87 88 89

		}

		return image;

	}

M
Mugen87 已提交
90 91 92 93 94 95
	function isPowerOfTwo( image ) {

		return _Math.isPowerOfTwo( image.width ) && _Math.isPowerOfTwo( image.height );

	}

96 97
	function textureNeedsPowerOfTwo( texture ) {

T
Takahiro 已提交
98
		if ( capabilities.isWebGL2 ) return false;
T
Takahiro 已提交
99

M
Mr.doob 已提交
100 101 102
		return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) ||
			( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter );

103 104
	}

105
	function textureNeedsGenerateMipmaps( texture, supportsMips ) {
106

107
		return texture.generateMipmaps && supportsMips &&
108
			texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
109 110 111

	}

112
	function generateMipmap( target, texture, width, height ) {
113 114 115

		_gl.generateMipmap( target );

116
		var textureProperties = properties.get( texture );
A
aardgoose 已提交
117 118

		// Note: Math.log( x ) * Math.LOG2E used instead of Math.log2( x ) which is not supported by IE11
119
		textureProperties.__maxMipLevel = Math.log( Math.max( width, height ) ) * Math.LOG2E;
120 121 122

	}

123 124
	function getInternalFormat( glFormat, glType ) {

T
Takahiro 已提交
125
		if ( ! capabilities.isWebGL2 ) return glFormat;
T
Takahiro 已提交
126

127 128
		var internalFormat = glFormat;

A
artur.trzesiok 已提交
129
		if ( glFormat === _gl.RED ) {
A
artur.trzesiok 已提交
130

131 132 133
			if ( glType === _gl.FLOAT ) internalFormat = _gl.R32F;
			if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.R16F;
			if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.R8;
A
artur.trzesiok 已提交
134

A
artur.trzesiok 已提交
135
		}
A
artur.trzesiok 已提交
136

137 138
		if ( glFormat === _gl.RGB ) {

139 140 141
			if ( glType === _gl.FLOAT ) internalFormat = _gl.RGB32F;
			if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGB16F;
			if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RGB8;
142 143 144 145

		}

		if ( glFormat === _gl.RGBA ) {
T
Takahiro 已提交
146

147 148 149 150 151 152
			if ( glType === _gl.FLOAT ) internalFormat = _gl.RGBA32F;
			if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGBA16F;
			if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RGBA8;

		}

V
vasco 已提交
153
		if ( internalFormat === _gl.R16F || internalFormat === _gl.R32F ||
154 155 156 157 158 159 160
			internalFormat === _gl.RGBA16F || internalFormat === _gl.RGBA32F ) {

			extensions.get( 'EXT_color_buffer_float' );

		} else if ( internalFormat === _gl.RGB16F || internalFormat === _gl.RGB32F ) {

			console.warn( 'THREE.WebGLRenderer: Floating point textures with RGB format not supported. Please use RGBA instead.' );
T
Takahiro 已提交
161 162

		}
163

164
		return internalFormat;
165 166 167

	}

168 169
	// Fallback filters for non-power-of-2 textures

M
Mr.doob 已提交
170
	function filterFallback( f ) {
171

W
WestLangley 已提交
172
		if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

			return _gl.NEAREST;

		}

		return _gl.LINEAR;

	}

	//

	function onTextureDispose( event ) {

		var texture = event.target;

		texture.removeEventListener( 'dispose', onTextureDispose );

		deallocateTexture( texture );

M
Mugen87 已提交
192 193
		if ( texture.isVideoTexture ) {

J
Joel 已提交
194
			_videoTextures.delete( texture );
M
Mugen87 已提交
195 196

		}
197

M
Mugen87 已提交
198
		info.memory.textures --;
199 200 201 202 203 204 205 206 207 208 209

	}

	function onRenderTargetDispose( event ) {

		var renderTarget = event.target;

		renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );

		deallocateRenderTarget( renderTarget );

M
Mugen87 已提交
210
		info.memory.textures --;
211 212 213 214 215 216 217 218 219

	}

	//

	function deallocateTexture( texture ) {

		var textureProperties = properties.get( texture );

M
Mugen87 已提交
220
		if ( textureProperties.__webglInit === undefined ) return;
221

M
Mugen87 已提交
222
		_gl.deleteTexture( textureProperties.__webglTexture );
223

224
		properties.remove( texture );
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

	}

	function deallocateRenderTarget( renderTarget ) {

		var renderTargetProperties = properties.get( renderTarget );
		var textureProperties = properties.get( renderTarget.texture );

		if ( ! renderTarget ) return;

		if ( textureProperties.__webglTexture !== undefined ) {

			_gl.deleteTexture( textureProperties.__webglTexture );

		}

		if ( renderTarget.depthTexture ) {

			renderTarget.depthTexture.dispose();

		}

247
		if ( renderTarget.isWebGLRenderTargetCube ) {
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

			for ( var i = 0; i < 6; i ++ ) {

				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
				if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );

			}

		} else {

			_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
			if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );

		}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		if ( renderTarget.isWebGLMultiviewRenderTarget ) {

			_gl.deleteTexture( renderTargetProperties.__webglColorTexture );
			_gl.deleteTexture( renderTargetProperties.__webglDepthStencilTexture );

			info.memory.textures -= 2;

			for ( var i = 0, il = renderTargetProperties.__webglViewFramebuffers.length; i < il; i ++ ) {

				_gl.deleteFramebuffer( renderTargetProperties.__webglViewFramebuffers[ i ] );

			}

		}

278 279
		properties.remove( renderTarget.texture );
		properties.remove( renderTarget );
280 281 282 283 284

	}

	//

285
	var textureUnits = 0;
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	function resetTextureUnits() {

		textureUnits = 0;

	}

	function allocateTextureUnit() {

		var textureUnit = textureUnits;

		if ( textureUnit >= capabilities.maxTextures ) {

			console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );

		}

		textureUnits += 1;

		return textureUnit;

	}

	//
310 311 312 313 314

	function setTexture2D( texture, slot ) {

		var textureProperties = properties.get( texture );

315 316
		if ( texture.isVideoTexture ) updateVideoTexture( texture );

317 318 319 320 321 322
		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {

			var image = texture.image;

			if ( image === undefined ) {

323
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined' );
324

325
			} else if ( image.complete === false ) {
326

327
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' );
328

329
			} else {
330

331 332
				uploadTexture( textureProperties, texture, slot );
				return;
333

334
			}
335 336 337 338 339 340 341 342

		}

		state.activeTexture( _gl.TEXTURE0 + slot );
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );

	}

T
Takahiro 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	function setTexture2DArray( texture, slot ) {

		var textureProperties = properties.get( texture );

		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {

			uploadTexture( textureProperties, texture, slot );
			return;

		}

		state.activeTexture( _gl.TEXTURE0 + slot );
		state.bindTexture( _gl.TEXTURE_2D_ARRAY, textureProperties.__webglTexture );

	}

A
artur.trzesiok 已提交
359
	function setTexture3D( texture, slot ) {
A
artur.trzesiok 已提交
360

A
artur.trzesiok 已提交
361 362 363
		var textureProperties = properties.get( texture );

		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
A
artur.trzesiok 已提交
364 365 366 367

			uploadTexture( textureProperties, texture, slot );
			return;

A
artur.trzesiok 已提交
368 369 370 371 372 373
		}

		state.activeTexture( _gl.TEXTURE0 + slot );
		state.bindTexture( _gl.TEXTURE_3D, textureProperties.__webglTexture );

	}
A
artur.trzesiok 已提交
374

M
Mr.doob 已提交
375
	function setTextureCube( texture, slot ) {
376

377 378
		if ( texture.image.length !== 6 ) return;

379 380
		var textureProperties = properties.get( texture );

381
		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
382

383
			initTexture( textureProperties, texture );
384

385 386
			state.activeTexture( _gl.TEXTURE0 + slot );
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
387

388
			_gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
389

390 391
			var isCompressed = ( texture && texture.isCompressedTexture );
			var isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture );
392

393
			var cubeImage = [];
394

395
			for ( var i = 0; i < 6; i ++ ) {
396

397
				if ( ! isCompressed && ! isDataTexture ) {
398

399
					cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, capabilities.maxCubemapSize );
400

401
				} else {
402

403
					cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
404

405
				}
406

407
			}
408

409 410 411 412 413
			var image = cubeImage[ 0 ],
				supportsMips = isPowerOfTwo( image ) || capabilities.isWebGL2,
				glFormat = utils.convert( texture.format ),
				glType = utils.convert( texture.type ),
				glInternalFormat = getInternalFormat( glFormat, glType );
414

415
			setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, supportsMips );
416

417
			var mipmaps;
418

419
			if ( isCompressed ) {
420

421 422
				for ( var i = 0; i < 6; i ++ ) {

423
					mipmaps = cubeImage[ i ].mipmaps;
424

425
					for ( var j = 0; j < mipmaps.length; j ++ ) {
426

427
						var mipmap = mipmaps[ j ];
428

429
						if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
A
angus 已提交
430

431
							if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {
A
angus 已提交
432

433 434 435 436 437
								state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );

							} else {

								console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' );
A
angus 已提交
438 439 440

							}

441 442
						} else {

443
							state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
444

445
						}
446

447
					}
448

449
				}
450

451
				textureProperties.__maxMipLevel = mipmaps.length - 1;
452

453
			} else {
454

455
				mipmaps = texture.mipmaps;
456

457
				for ( var i = 0; i < 6; i ++ ) {
458

459
					if ( isDataTexture ) {
460

461
						state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
462

463
						for ( var j = 0; j < mipmaps.length; j ++ ) {
464

465 466
							var mipmap = mipmaps[ j ];
							var mipmapImage = mipmap.image[ i ].image;
467

468
							state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data );
469

470
						}
471

472
					} else {
473

474
						state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] );
475

476
						for ( var j = 0; j < mipmaps.length; j ++ ) {
477

478
							var mipmap = mipmaps[ j ];
479

480
							state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] );
481 482 483 484 485 486 487

						}

					}

				}

488
				textureProperties.__maxMipLevel = mipmaps.length;
489

490
			}
491

492
			if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
493

494 495
				// We assume images for cube map have the same size.
				generateMipmap( _gl.TEXTURE_CUBE_MAP, texture, image.width, image.height );
496

497
			}
498

499
			textureProperties.__version = texture.version;
500

501
			if ( texture.onUpdate ) texture.onUpdate( texture );
502

503
		} else {
504

505 506
			state.activeTexture( _gl.TEXTURE0 + slot );
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
507 508 509 510 511

		}

	}

M
Mr.doob 已提交
512
	function setTextureCubeDynamic( texture, slot ) {
513 514 515 516 517 518

		state.activeTexture( _gl.TEXTURE0 + slot );
		state.bindTexture( _gl.TEXTURE_CUBE_MAP, properties.get( texture ).__webglTexture );

	}

519
	function setTextureParameters( textureType, texture, supportsMips ) {
520 521 522

		var extension;

523
		if ( supportsMips ) {
524

525 526
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, utils.convert( texture.wrapS ) );
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, utils.convert( texture.wrapT ) );
527

T
Takahiro 已提交
528
			if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
529 530 531 532 533

				_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, utils.convert( texture.wrapR ) );

			}

534 535
			_gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, utils.convert( texture.magFilter ) );
			_gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, utils.convert( texture.minFilter ) );
536 537 538 539 540 541

		} else {

			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );

T
Takahiro 已提交
542
			if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
543 544 545 546 547

				_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, _gl.CLAMP_TO_EDGE );

			}

R
Rich Harris 已提交
548
			if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) {
549

550
				console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' );
551 552 553 554 555 556

			}

			_gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
			_gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );

R
Rich Harris 已提交
557
			if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) {
558

559
				console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' );
560 561 562 563 564 565 566 567 568

			}

		}

		extension = extensions.get( 'EXT_texture_filter_anisotropic' );

		if ( extension ) {

R
Rich Harris 已提交
569
			if ( texture.type === FloatType && extensions.get( 'OES_texture_float_linear' ) === null ) return;
T
Takahiro 已提交
570
			if ( texture.type === HalfFloatType && ( capabilities.isWebGL2 || extensions.get( 'OES_texture_half_float_linear' ) ) === null ) return;
571 572 573 574 575 576 577 578 579 580 581 582

			if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {

				_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
				properties.get( texture ).__currentAnisotropy = texture.anisotropy;

			}

		}

	}

M
Mugen87 已提交
583
	function initTexture( textureProperties, texture ) {
584 585 586 587 588 589 590 591 592

		if ( textureProperties.__webglInit === undefined ) {

			textureProperties.__webglInit = true;

			texture.addEventListener( 'dispose', onTextureDispose );

			textureProperties.__webglTexture = _gl.createTexture();

M
Mugen87 已提交
593
			info.memory.textures ++;
594 595

		}
A
artur.trzesiok 已提交
596

M
Mugen87 已提交
597
	}
A
artur.trzesiok 已提交
598

M
Mugen87 已提交
599 600
	function uploadTexture( textureProperties, texture, slot ) {

T
Takahiro 已提交
601 602 603 604
		var textureType = _gl.TEXTURE_2D;

		if ( texture.isDataTexture2DArray ) textureType = _gl.TEXTURE_2D_ARRAY;
		if ( texture.isDataTexture3D ) textureType = _gl.TEXTURE_3D;
A
artur.trzesiok 已提交
605

M
Mugen87 已提交
606
		initTexture( textureProperties, texture );
A
artur.trzesiok 已提交
607

M
Mugen87 已提交
608 609
		state.activeTexture( _gl.TEXTURE0 + slot );
		state.bindTexture( textureType, textureProperties.__webglTexture );
610 611 612 613 614

		_gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
		_gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
		_gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );

M
Mugen87 已提交
615 616
		var needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false;
		var image = resizeImage( texture.image, needsPowerOfTwo, false, capabilities.maxTextureSize );
617

618
		var supportsMips = isPowerOfTwo( image ) || capabilities.isWebGL2,
M
Mugen87 已提交
619
			glFormat = utils.convert( texture.format ),
T
Takahiro 已提交
620 621
			glType = utils.convert( texture.type ),
			glInternalFormat = getInternalFormat( glFormat, glType );
622

623
		setTextureParameters( textureType, texture, supportsMips );
624 625 626

		var mipmap, mipmaps = texture.mipmaps;

627
		if ( texture.isDepthTexture ) {
628 629 630

			// populate depth texture with dummy data

T
Takahiro 已提交
631
			glInternalFormat = _gl.DEPTH_COMPONENT;
632

R
Rich Harris 已提交
633
			if ( texture.type === FloatType ) {
634

T
Takahiro 已提交
635
				if ( ! capabilities.isWebGL2 ) throw new Error( 'Float Depth Texture only supported in WebGL2.0' );
T
Takahiro 已提交
636
				glInternalFormat = _gl.DEPTH_COMPONENT32F;
637

T
Takahiro 已提交
638
			} else if ( capabilities.isWebGL2 ) {
639 640

				// WebGL 2.0 requires signed internalformat for glTexImage2D
T
Takahiro 已提交
641
				glInternalFormat = _gl.DEPTH_COMPONENT16;
642 643 644

			}

T
Takahiro 已提交
645
			if ( texture.format === DepthFormat && glInternalFormat === _gl.DEPTH_COMPONENT ) {
T
Takahiro 已提交
646 647 648 649 650 651

				// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
				// DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
				// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
				if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) {

M
Mugen87 已提交
652
					console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' );
T
Takahiro 已提交
653 654

					texture.type = UnsignedShortType;
655
					glType = utils.convert( texture.type );
T
Takahiro 已提交
656 657 658 659 660

				}

			}

661 662
			// Depth stencil textures need the DEPTH_STENCIL internal format
			// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
663
			if ( texture.format === DepthStencilFormat ) {
664

T
Takahiro 已提交
665
				glInternalFormat = _gl.DEPTH_STENCIL;
666

T
Takahiro 已提交
667 668 669 670 671
				// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
				// DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
				// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
				if ( texture.type !== UnsignedInt248Type ) {

M
Mr.doob 已提交
672
					console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' );
T
Takahiro 已提交
673 674

					texture.type = UnsignedInt248Type;
675
					glType = utils.convert( texture.type );
T
Takahiro 已提交
676 677 678

				}

679 680
			}

T
Takahiro 已提交
681
			state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null );
682

683
		} else if ( texture.isDataTexture ) {
684 685 686 687 688

			// use manually created mipmaps if available
			// if there are no manual mipmaps
			// set 0 level mipmap and then use GL to generate other mipmap levels

689
			if ( mipmaps.length > 0 && supportsMips ) {
690 691 692 693

				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {

					mipmap = mipmaps[ i ];
T
Takahiro 已提交
694
					state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
695 696 697 698

				}

				texture.generateMipmaps = false;
699
				textureProperties.__maxMipLevel = mipmaps.length - 1;
700 701 702

			} else {

T
Takahiro 已提交
703
				state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data );
704
				textureProperties.__maxMipLevel = 0;
705 706 707

			}

708
		} else if ( texture.isCompressedTexture ) {
709 710 711 712 713

			for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {

				mipmap = mipmaps[ i ];

R
Rich Harris 已提交
714
				if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
715 716 717

					if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {

T
Takahiro 已提交
718
						state.compressedTexImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
719 720 721

					} else {

M
Mugen87 已提交
722
						console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
723 724 725 726 727

					}

				} else {

T
Takahiro 已提交
728
					state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
729 730 731 732 733

				}

			}

734
			textureProperties.__maxMipLevel = mipmaps.length - 1;
735

T
Takahiro 已提交
736 737 738 739 740
		} else if ( texture.isDataTexture2DArray ) {

			state.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
			textureProperties.__maxMipLevel = 0;

T
Takahiro 已提交
741
		} else if ( texture.isDataTexture3D ) {
A
artur.trzesiok 已提交
742

T
Takahiro 已提交
743
			state.texImage3D( _gl.TEXTURE_3D, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
A
artur.trzesiok 已提交
744 745
			textureProperties.__maxMipLevel = 0;

746 747 748 749 750 751 752 753
		} else {

			// regular Texture (image, video, canvas)

			// use manually created mipmaps if available
			// if there are no manual mipmaps
			// set 0 level mipmap and then use GL to generate other mipmap levels

754
			if ( mipmaps.length > 0 && supportsMips ) {
755 756 757 758

				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {

					mipmap = mipmaps[ i ];
T
Takahiro 已提交
759
					state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, glFormat, glType, mipmap );
760 761 762 763

				}

				texture.generateMipmaps = false;
764
				textureProperties.__maxMipLevel = mipmaps.length - 1;
765 766 767

			} else {

T
Takahiro 已提交
768
				state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, glFormat, glType, image );
769
				textureProperties.__maxMipLevel = 0;
770 771 772 773 774

			}

		}

775
		if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
776

777
			generateMipmap( _gl.TEXTURE_2D, texture, image.width, image.height );
778 779

		}
780 781 782 783 784 785 786 787 788 789

		textureProperties.__version = texture.version;

		if ( texture.onUpdate ) texture.onUpdate( texture );

	}

	// Render targets

	// Setup storage for target texture and bind it to correct framebuffer
M
Mr.doob 已提交
790
	function setupFrameBufferTexture( framebuffer, renderTarget, attachment, textureTarget ) {
791

792 793
		var glFormat = utils.convert( renderTarget.texture.format );
		var glType = utils.convert( renderTarget.texture.type );
T
Takahiro 已提交
794
		var glInternalFormat = getInternalFormat( glFormat, glType );
795
		state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
796 797 798 799 800 801 802
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );

	}

	// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
803
	function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) {
804 805 806 807 808

		_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );

		if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {

809 810 811 812 813 814 815 816 817 818 819 820
			if ( isMultisample ) {

				var samples = getRenderTargetSamples( renderTarget );

				_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );

			} else {

				_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );

			}

821 822 823 824
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

		} else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {

825 826 827 828
			if ( isMultisample ) {

				var samples = getRenderTargetSamples( renderTarget );

829
				_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height );
830 831 832 833 834 835 836 837

			} else {

				_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );

			}


838 839 840 841
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

		} else {

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
			var glFormat = utils.convert( renderTarget.texture.format );
			var glType = utils.convert( renderTarget.texture.type );
			var glInternalFormat = getInternalFormat( glFormat, glType );

			if ( isMultisample ) {

				var samples = getRenderTargetSamples( renderTarget );

				_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );

			} else {

				_gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height );

			}
857 858 859 860 861 862 863 864

		}

		_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );

	}

	// Setup resources for a Depth Texture for a FBO (needs an extension)
M
Mr.doob 已提交
865
	function setupDepthTexture( framebuffer, renderTarget ) {
866

867
		var isCube = ( renderTarget && renderTarget.isWebGLRenderTargetCube );
M
Mugen87 已提交
868
		if ( isCube ) throw new Error( 'Depth Texture with cube render targets is not supported' );
869 870 871

		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );

M
Mugen87 已提交
872
		if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) {
873

M
Mugen87 已提交
874
			throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' );
875 876 877 878

		}

		// upload an empty depth texture with framebuffer size
M
Mugen87 已提交
879
		if ( ! properties.get( renderTarget.depthTexture ).__webglTexture ||
880 881
				renderTarget.depthTexture.image.width !== renderTarget.width ||
				renderTarget.depthTexture.image.height !== renderTarget.height ) {
M
Mugen87 已提交
882

883 884 885
			renderTarget.depthTexture.image.width = renderTarget.width;
			renderTarget.depthTexture.image.height = renderTarget.height;
			renderTarget.depthTexture.needsUpdate = true;
M
Mugen87 已提交
886

887 888 889 890 891
		}

		setTexture2D( renderTarget.depthTexture, 0 );

		var webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
892

893
		if ( renderTarget.depthTexture.format === DepthFormat ) {
894 895 896

			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );

897
		} else if ( renderTarget.depthTexture.format === DepthStencilFormat ) {
898 899 900 901 902

			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );

		} else {

M
Mugen87 已提交
903
			throw new Error( 'Unknown depthTexture format' );
904 905

		}
906 907 908 909 910 911 912 913

	}

	// Setup GL resources for a non-texture depth buffer
	function setupDepthRenderbuffer( renderTarget ) {

		var renderTargetProperties = properties.get( renderTarget );

914
		var isCube = ( renderTarget.isWebGLRenderTargetCube === true );
915 916 917

		if ( renderTarget.depthTexture ) {

M
Mugen87 已提交
918
			if ( isCube ) throw new Error( 'target.depthTexture not supported in Cube render targets' );
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

			setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );

		} else {

			if ( isCube ) {

				renderTargetProperties.__webglDepthbuffer = [];

				for ( var i = 0; i < 6; i ++ ) {

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
					renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
					setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget );

				}

			} else {

				_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
				renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
				setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget );

			}

		}

		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );

	}

	// Set up GL resources for the render target
	function setupRenderTarget( renderTarget ) {

		var renderTargetProperties = properties.get( renderTarget );
		var textureProperties = properties.get( renderTarget.texture );

		renderTarget.addEventListener( 'dispose', onRenderTargetDispose );

		textureProperties.__webglTexture = _gl.createTexture();

M
Mugen87 已提交
960
		info.memory.textures ++;
961

962
		var isCube = ( renderTarget.isWebGLRenderTargetCube === true );
963
		var isMultisample = ( renderTarget.isWebGLMultisampleRenderTarget === true );
964
		var isMultiview = ( renderTarget.isWebGLMultiviewRenderTarget === true );
965
		var supportsMips = isPowerOfTwo( renderTarget ) || capabilities.isWebGL2;
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

		// Setup framebuffer

		if ( isCube ) {

			renderTargetProperties.__webglFramebuffer = [];

			for ( var i = 0; i < 6; i ++ ) {

				renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();

			}

		} else {

			renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();

983 984
			if ( isMultisample ) {

985
				if ( capabilities.isWebGL2 ) {
986

987 988
					renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
					renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer();
989

990 991 992 993 994 995
					_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer );
					var glFormat = utils.convert( renderTarget.texture.format );
					var glType = utils.convert( renderTarget.texture.type );
					var glInternalFormat = getInternalFormat( glFormat, glType );
					var samples = getRenderTargetSamples( renderTarget );
					_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
996

997 998 999
					_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
					_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer );
					_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
1000

1001
					if ( renderTarget.depthBuffer ) {
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011
						renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
						setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true );

					}

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );


				} else {
1012

1013 1014 1015
					console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );

				}
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
			} else if ( isMultiview ) {

				if ( capabilities.multiview ) {

					var width = renderTarget.width;
					var height = renderTarget.height;
					var numViews = renderTarget.numViews;

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );

					var ext = extensions.get( 'OVR_multiview2' );

1029 1030
					info.memory.textures += 2;

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
					var colorTexture = _gl.createTexture();
					_gl.bindTexture( _gl.TEXTURE_2D_ARRAY, colorTexture );
					_gl.texParameteri( _gl.TEXTURE_2D_ARRAY, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
					_gl.texParameteri( _gl.TEXTURE_2D_ARRAY, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
					_gl.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, _gl.RGBA8, width, height, numViews, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
					ext.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, colorTexture, 0, 0, numViews );

					var depthStencilTexture = _gl.createTexture();
					_gl.bindTexture( _gl.TEXTURE_2D_ARRAY, depthStencilTexture );
					_gl.texParameteri( _gl.TEXTURE_2D_ARRAY, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
					_gl.texParameteri( _gl.TEXTURE_2D_ARRAY, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
					_gl.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, _gl.DEPTH24_STENCIL8, width, height, numViews, 0, _gl.DEPTH_STENCIL, _gl.UNSIGNED_INT_24_8, null );
					ext.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, depthStencilTexture, 0, 0, numViews );

					var viewFramebuffers = new Array( numViews );
1046
					for ( var i = 0; i < numViews; ++ i ) {
1047

1048 1049 1050
						viewFramebuffers[ i ] = _gl.createFramebuffer();
						_gl.bindFramebuffer( _gl.FRAMEBUFFER, viewFramebuffers[ i ] );
						_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, colorTexture, 0, i );
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

					}

					renderTargetProperties.__webglColorTexture = colorTexture;
					renderTargetProperties.__webglDepthStencilTexture = depthStencilTexture;
					renderTargetProperties.__webglViewFramebuffers = viewFramebuffers;

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
					_gl.bindTexture( _gl.TEXTURE_2D_ARRAY, null );

1061
				} else {
1062

1063
					console.warn( 'THREE.WebGLRenderer: WebGLMultiviewRenderTarget can only be used with WebGL2 and Multiview extension support.' );
1064

1065
				}
1066

1067 1068
			}

1069 1070 1071 1072 1073 1074 1075
		}

		// Setup color buffer

		if ( isCube ) {

			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
1076
			setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, supportsMips );
1077 1078 1079 1080 1081 1082 1083

			for ( var i = 0; i < 6; i ++ ) {

				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );

			}

1084
			if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
1085

1086
				generateMipmap( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, renderTarget.width, renderTarget.height );
1087 1088 1089

			}

1090 1091
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );

1092
		} else if ( ! isMultiview ) {
1093 1094

			state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
1095
			setTextureParameters( _gl.TEXTURE_2D, renderTarget.texture, supportsMips );
1096 1097
			setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D );

1098
			if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
1099

1100
				generateMipmap( _gl.TEXTURE_2D, renderTarget.texture, renderTarget.width, renderTarget.height );
1101 1102 1103

			}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
			state.bindTexture( _gl.TEXTURE_2D, null );

		}

		// Setup depth and stencil buffers

		if ( renderTarget.depthBuffer ) {

			setupDepthRenderbuffer( renderTarget );

		}

	}

	function updateRenderTargetMipmap( renderTarget ) {

		var texture = renderTarget.texture;
1121
		var supportsMips = isPowerOfTwo( renderTarget ) || capabilities.isWebGL2;
1122

1123
		if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
1124

1125
			var target = renderTarget.isWebGLRenderTargetCube ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
1126 1127 1128
			var webglTexture = properties.get( texture ).__webglTexture;

			state.bindTexture( target, webglTexture );
1129
			generateMipmap( target, texture, renderTarget.width, renderTarget.height );
1130 1131 1132 1133 1134 1135
			state.bindTexture( target, null );

		}

	}

1136 1137 1138 1139
	function updateMultisampleRenderTarget( renderTarget ) {

		if ( renderTarget.isWebGLMultisampleRenderTarget ) {

1140 1141 1142 1143 1144 1145
			if ( capabilities.isWebGL2 ) {

				var renderTargetProperties = properties.get( renderTarget );

				_gl.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
				_gl.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
1146

1147 1148 1149
				var width = renderTarget.width;
				var height = renderTarget.height;
				var mask = _gl.COLOR_BUFFER_BIT;
1150

1151 1152
				if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT;
				if ( renderTarget.stencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT;
1153

1154
				_gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST );
1155

1156 1157 1158 1159 1160
			} else {

				console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );

			}
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172

		}

	}

	function getRenderTargetSamples( renderTarget ) {

		return ( capabilities.isWebGL2 && renderTarget.isWebGLMultisampleRenderTarget ) ?
			Math.min( capabilities.maxSamples, renderTarget.samples ) : 0;

	}

1173 1174
	function updateVideoTexture( texture ) {

M
Mugen87 已提交
1175
		var frame = info.render.frame;
1176 1177

		// Check the last frame we updated the VideoTexture
M
Mugen87 已提交
1178

J
Joel 已提交
1179
		if ( _videoTextures.get( texture ) !== frame ) {
M
Mugen87 已提交
1180

J
Joel 已提交
1181
			_videoTextures.set( texture, frame );
1182
			texture.update();
M
Mugen87 已提交
1183 1184 1185 1186 1187

		}

	}

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	// backwards compatibility

	var warnedTexture2D = false;
	var warnedTextureCube = false;

	function safeSetTexture2D( texture, slot ) {

		if ( texture && texture.isWebGLRenderTarget ) {

			if ( warnedTexture2D === false ) {

				console.warn( "THREE.WebGLTextures.safeSetTexture2D: don't use render targets as textures. Use their .texture property instead." );
				warnedTexture2D = true;

			}

			texture = texture.texture;

		}

		setTexture2D( texture, slot );

	}

	function safeSetTextureCube( texture, slot ) {

		if ( texture && texture.isWebGLRenderTargetCube ) {

			if ( warnedTextureCube === false ) {

				console.warn( "THREE.WebGLTextures.safeSetTextureCube: don't use cube render targets as textures. Use their .texture property instead." );
				warnedTextureCube = true;

			}

			texture = texture.texture;

		}

		// currently relying on the fact that WebGLRenderTargetCube.texture is a Texture and NOT a CubeTexture
		// TODO: unify these code paths
		if ( ( texture && texture.isCubeTexture ) ||
			( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {

			// CompressedTexture can have Array in image :/

			// this function alone should take care of cube textures
			setTextureCube( texture, slot );

		} else {

			// assumed: texture property of THREE.WebGLRenderTargetCube
			setTextureCubeDynamic( texture, slot );

		}

	}

	//

	this.allocateTextureUnit = allocateTextureUnit;
	this.resetTextureUnits = resetTextureUnits;

1251
	this.setTexture2D = setTexture2D;
T
Takahiro 已提交
1252
	this.setTexture2DArray = setTexture2DArray;
A
artur.trzesiok 已提交
1253
	this.setTexture3D = setTexture3D;
1254 1255 1256 1257
	this.setTextureCube = setTextureCube;
	this.setTextureCubeDynamic = setTextureCubeDynamic;
	this.setupRenderTarget = setupRenderTarget;
	this.updateRenderTargetMipmap = updateRenderTargetMipmap;
1258
	this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
1259

1260 1261
	this.safeSetTexture2D = safeSetTexture2D;
	this.safeSetTextureCube = safeSetTextureCube;
R
Rich Harris 已提交
1262

1263
}
R
Rich Harris 已提交
1264

1265
export { WebGLTextures };