WebGLRenderer.js 84.8 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9
/**
 * @author supereggbert / http://www.paulbrunt.co.uk/
 * @author mrdoob / http://mrdoob.com/
 * @author alteredq / http://alteredqualia.com/
 * @author szimek / https://github.com/szimek/
 */

THREE.WebGLRenderer = function ( parameters ) {

10
	console.log( 'THREE.WebGLRenderer', THREE.REVISION );
M
Mr.doob 已提交
11 12 13 14

	parameters = parameters || {};

	var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
15
	_context = parameters.context !== undefined ? parameters.context : null,
M
Mr.doob 已提交
16

17
	_alpha = parameters.alpha !== undefined ? parameters.alpha : false,
18
	_depth = parameters.depth !== undefined ? parameters.depth : true,
M
Mr.doob 已提交
19
	_stencil = parameters.stencil !== undefined ? parameters.stencil : true,
20 21
	_antialias = parameters.antialias !== undefined ? parameters.antialias : false,
	_premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
M
Mr.doob 已提交
22
	_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false;
M
Mr.doob 已提交
23

M
Mr.doob 已提交
24
	var lights = [];
M
Mr.doob 已提交
25

O
OpenShift guest 已提交
26
	var opaqueObjects = [];
M
Mr.doob 已提交
27
	var opaqueObjectsLastIndex = - 1;
28
	var transparentObjects = [];
M
Mr.doob 已提交
29
	var transparentObjectsLastIndex = - 1;
30

31 32
	var morphInfluences = new Float32Array( 8 );

M
Mr.doob 已提交
33 34 35
	var sprites = [];
	var lensFlares = [];

M
Mr.doob 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	// public properties

	this.domElement = _canvas;
	this.context = null;

	// clearing

	this.autoClear = true;
	this.autoClearColor = true;
	this.autoClearDepth = true;
	this.autoClearStencil = true;

	// scene graph

	this.sortObjects = true;

	// physically based shading

54
	this.gammaFactor = 2.0;	// for backwards compatibility
M
Mr.doob 已提交
55 56 57
	this.gammaInput = false;
	this.gammaOutput = false;

B
Ben Houston 已提交
58 59 60 61 62 63
	// tone mapping

	this.toneMapping = THREE.LinearToneMapping;
	this.toneMappingExposure = 1.0;
	this.toneMappingWhitePoint = 1.0;

M
Mr.doob 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	// morphs

	this.maxMorphTargets = 8;
	this.maxMorphNormals = 4;

	// flags

	this.autoScaleCubemaps = true;

	// internal properties

	var _this = this,

	// internal state cache

	_currentProgram = null,
80
	_currentRenderTarget = null,
M
Mr.doob 已提交
81
	_currentFramebuffer = null,
82
	_currentMaterialId = - 1,
83
	_currentGeometryProgram = '',
M
Mr.doob 已提交
84 85
	_currentCamera = null,

86 87 88 89 90
	_currentScissor = new THREE.Vector4(),
	_currentScissorTest = null,

	_currentViewport = new THREE.Vector4(),

91 92
	//

M
Mr.doob 已提交
93 94
	_usedTextureUnits = 0,

M
Mr.doob 已提交
95 96 97 98 99 100 101 102 103 104 105
	//

	_clearColor = new THREE.Color( 0x000000 ),
	_clearAlpha = 0,

	_width = _canvas.width,
	_height = _canvas.height,

	_pixelRatio = 1,

	_scissor = new THREE.Vector4( 0, 0, _width, _height ),
106 107
	_scissorTest = false,

M
Mr.doob 已提交
108
	_viewport = new THREE.Vector4( 0, 0, _width, _height ),
109

M
Mr.doob 已提交
110 111 112 113
	// frustum

	_frustum = new THREE.Frustum(),

M
Mr.doob 已提交
114
	// camera matrices cache
M
Mr.doob 已提交
115 116 117 118 119 120 121 122 123

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_lights = {

124 125
		hash: '',

M
Mr.doob 已提交
126
		ambient: [ 0, 0, 0 ],
M
Mr.doob 已提交
127
		directional: [],
128 129
		directionalShadowMap: [],
		directionalShadowMatrix: [],
M
Mr.doob 已提交
130
		spot: [],
131 132
		spotShadowMap: [],
		spotShadowMatrix: [],
M
Mr.doob 已提交
133
		point: [],
134 135
		pointShadowMap: [],
		pointShadowMatrix: [],
136 137
		hemi: [],

138
		shadows: [],
139
		shadowsPointLight: 0
M
Mr.doob 已提交
140

141 142
	},

M
Mr.doob 已提交
143 144
	// info

145
	_infoMemory = {
146 147

		geometries: 0,
T
tschw 已提交
148
		textures: 0
149 150 151

	},

152
	_infoRender = {
153 154 155 156 157 158

		calls: 0,
		vertices: 0,
		faces: 0,
		points: 0

M
Mr.doob 已提交
159 160
	};

M
Mr.doob 已提交
161
	this.info = {
162

M
Mr.doob 已提交
163 164
		render: _infoRender,
		memory: _infoMemory,
165
		programs: null
M
Mr.doob 已提交
166 167

	};
168

169

M
Mr.doob 已提交
170 171 172 173
	// initialize

	var _gl;

M
Mr.doob 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	try {

		var attributes = {
			alpha: _alpha,
			depth: _depth,
			stencil: _stencil,
			antialias: _antialias,
			premultipliedAlpha: _premultipliedAlpha,
			preserveDrawingBuffer: _preserveDrawingBuffer
		};

		_gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );

		if ( _gl === null ) {

G
gero3 已提交
189
			if ( _canvas.getContext( 'webgl' ) !== null ) {
190 191 192 193 194 195 196 197

				throw 'Error creating WebGL context with your selected attributes.';

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
198 199 200

		}

201 202 203 204 205 206 207 208 209 210 211 212
		// Some experimental-webgl implementations do not have getShaderPrecisionFormat

		if ( _gl.getShaderPrecisionFormat === undefined ) {

			_gl.getShaderPrecisionFormat = function () {

				return { 'rangeMin': 1, 'rangeMax': 1, 'precision': 1 };

			};

		}

D
dubejf 已提交
213
		_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
214

M
Mr.doob 已提交
215 216
	} catch ( error ) {

217
		console.error( 'THREE.WebGLRenderer: ' + error );
M
Mr.doob 已提交
218 219 220

	}

221 222
	var extensions = new THREE.WebGLExtensions( _gl );

223 224
	extensions.get( 'OES_texture_float' );
	extensions.get( 'OES_texture_float_linear' );
225 226
	extensions.get( 'OES_texture_half_float' );
	extensions.get( 'OES_texture_half_float_linear' );
227
	extensions.get( 'OES_standard_derivatives' );
B
Ben Adams 已提交
228
	extensions.get( 'ANGLE_instanced_arrays' );
229

230 231
	if ( extensions.get( 'OES_element_index_uint' ) ) {

232
		THREE.BufferGeometry.MaxIndex = 4294967296;
233 234 235

	}

M
Mr.doob 已提交
236
	var capabilities = new THREE.WebGLCapabilities( _gl, extensions, parameters );
M
Mr.doob 已提交
237

238 239 240
	var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
	var properties = new THREE.WebGLProperties();
	var objects = new THREE.WebGLObjects( _gl, properties, this.info );
G
gero3 已提交
241
	var programCache = new THREE.WebGLPrograms( this, capabilities );
M
Mr.doob 已提交
242
	var lightCache = new THREE.WebGLLights();
243

244 245
	this.info.programs = programCache.programs;

246 247
	var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
	var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
248

M
Mr.doob 已提交
249 250
	//

251 252
	function getTargetPixelRatio() {

253
		return _currentRenderTarget === null ? _pixelRatio : 1;
254 255 256

	}

257
	function glClearColor( r, g, b, a ) {
258 259 260

		if ( _premultipliedAlpha === true ) {

261
			r *= a; g *= a; b *= a;
262 263 264

		}

M
Mr.doob 已提交
265
		state.clearColor( r, g, b, a );
266

267
	}
268

269
	function setDefaultGLState() {
M
Mr.doob 已提交
270

M
Mr.doob 已提交
271
		state.init();
M
Mr.doob 已提交
272

273 274
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
275

276
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
277

278
	}
279

280
	function resetGLState() {
281 282 283 284

		_currentProgram = null;
		_currentCamera = null;

285
		_currentGeometryProgram = '';
286 287
		_currentMaterialId = - 1;

M
Mr.doob 已提交
288 289
		state.reset();

290
	}
M
Mr.doob 已提交
291 292 293 294

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
295
	this.capabilities = capabilities;
296
	this.extensions = extensions;
297
	this.properties = properties;
M
Mr.doob 已提交
298
	this.state = state;
M
Mr.doob 已提交
299

M
Mr.doob 已提交
300 301
	// shadow map

M
Mr.doob 已提交
302
	var shadowMap = new THREE.WebGLShadowMap( this, _lights, objects );
M
Mr.doob 已提交
303

304
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
305

M
Mr.doob 已提交
306

M
Mr.doob 已提交
307 308 309 310 311
	// Plugins

	var spritePlugin = new THREE.SpritePlugin( this, sprites );
	var lensFlarePlugin = new THREE.LensFlarePlugin( this, lensFlares );

M
Mr.doob 已提交
312 313 314 315 316 317 318 319
	// API

	this.getContext = function () {

		return _gl;

	};

320 321 322 323 324 325
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

326 327 328 329 330 331
	this.forceContextLoss = function () {

		extensions.get( 'WEBGL_lose_context' ).loseContext();

	};

332
	this.getMaxAnisotropy = ( function () {
M
Mr.doob 已提交
333

334
		var value;
M
Mr.doob 已提交
335

336
		return function getMaxAnisotropy() {
337

M
Mr.doob 已提交
338
			if ( value !== undefined ) return value;
339

M
Mr.doob 已提交
340
			var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
341

M
Mr.doob 已提交
342
			if ( extension !== null ) {
343

M
Mr.doob 已提交
344
				value = _gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT );
345

M
Mr.doob 已提交
346 347 348 349 350
			} else {

				value = 0;

			}
351 352 353

			return value;

M
Mr.doob 已提交
354
		};
355 356

	} )();
M
Mr.doob 已提交
357 358 359

	this.getPrecision = function () {

G
gero3 已提交
360
		return capabilities.precision;
M
Mr.doob 已提交
361 362 363

	};

364 365
	this.getPixelRatio = function () {

366
		return _pixelRatio;
367 368 369 370 371

	};

	this.setPixelRatio = function ( value ) {

372 373 374 375 376
		if ( value === undefined ) return;

		_pixelRatio = value;

		this.setSize( _viewport.z, _viewport.w, false );
377 378 379

	};

380 381 382
	this.getSize = function () {

		return {
383 384
			width: _width,
			height: _height
385 386 387 388
		};

	};

389
	this.setSize = function ( width, height, updateStyle ) {
M
Mr.doob 已提交
390

391 392 393
		_width = width;
		_height = height;

394 395
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
396

397
		if ( updateStyle !== false ) {
398

G
gero3 已提交
399 400
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
401

G
gero3 已提交
402
		}
M
Mr.doob 已提交
403

404
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
405 406 407 408 409

	};

	this.setViewport = function ( x, y, width, height ) {

410
		state.viewport( _viewport.set( x, y, width, height ) );
M
Mr.doob 已提交
411 412 413

	};

414
	this.setScissor = function ( x, y, width, height ) {
M
Mr.doob 已提交
415

416
		state.scissor( _scissor.set( x, y, width, height ) );
M
Mr.doob 已提交
417 418 419

	};

420 421
	this.setScissorTest = function ( boolean ) {

422
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
423 424 425 426 427

	};

	// Clearing

M
Mr.doob 已提交
428
	this.getClearColor = function () {
M
Mr.doob 已提交
429

M
Mr.doob 已提交
430
		return _clearColor;
M
Mr.doob 已提交
431 432 433

	};

M
Mr.doob 已提交
434
	this.setClearColor = function ( color, alpha ) {
M
Mr.doob 已提交
435

M
Mr.doob 已提交
436
		_clearColor.set( color );
437

M
Mr.doob 已提交
438
		_clearAlpha = alpha !== undefined ? alpha : 1;
M
Mr.doob 已提交
439

440
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
441 442 443

	};

M
Mr.doob 已提交
444
	this.getClearAlpha = function () {
M
Mr.doob 已提交
445

M
Mr.doob 已提交
446
		return _clearAlpha;
M
Mr.doob 已提交
447 448 449

	};

M
Mr.doob 已提交
450
	this.setClearAlpha = function ( alpha ) {
M
Mr.doob 已提交
451

M
Mr.doob 已提交
452
		_clearAlpha = alpha;
M
Mr.doob 已提交
453

454
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
455 456 457 458 459 460 461 462 463 464 465 466

	};

	this.clear = function ( color, depth, stencil ) {

		var bits = 0;

		if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
		if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
		if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;

		_gl.clear( bits );
467 468 469 470 471

	};

	this.clearColor = function () {

M
Mr.doob 已提交
472
		this.clear( true, false, false );
473 474 475 476 477

	};

	this.clearDepth = function () {

M
Mr.doob 已提交
478
		this.clear( false, true, false );
479 480 481 482 483

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
484
		this.clear( false, false, true );
M
Mr.doob 已提交
485 486 487 488 489 490 491 492 493 494

	};

	this.clearTarget = function ( renderTarget, color, depth, stencil ) {

		this.setRenderTarget( renderTarget );
		this.clear( color, depth, stencil );

	};

M
Mr.doob 已提交
495 496
	// Reset

497
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
498

D
dubejf 已提交
499 500 501 502 503 504
	this.dispose = function() {

		_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );

	};

M
Mr.doob 已提交
505
	// Events
M
Mr.doob 已提交
506

D
dubejf 已提交
507 508 509 510 511 512 513 514 515
	function onContextLost( event ) {

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

M
Mr.doob 已提交
516
	}
D
dubejf 已提交
517

518
	function onTextureDispose( event ) {
M
Mr.doob 已提交
519 520 521 522 523 524 525

		var texture = event.target;

		texture.removeEventListener( 'dispose', onTextureDispose );

		deallocateTexture( texture );

526
		_infoMemory.textures --;
M
Mr.doob 已提交
527 528


529
	}
M
Mr.doob 已提交
530

531
	function onRenderTargetDispose( event ) {
M
Mr.doob 已提交
532 533 534 535 536 537 538

		var renderTarget = event.target;

		renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );

		deallocateRenderTarget( renderTarget );

539
		_infoMemory.textures --;
M
Mr.doob 已提交
540

541
	}
M
Mr.doob 已提交
542

543
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
544 545 546 547 548 549 550

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

551
	}
M
Mr.doob 已提交
552 553 554

	// Buffer deallocation

555
	function deallocateTexture( texture ) {
M
Mr.doob 已提交
556

557
		var textureProperties = properties.get( texture );
M
Mr.doob 已提交
558

559
		if ( texture.image && textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
560 561 562

			// cube texture

563
			_gl.deleteTexture( textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
564

565 566 567 568
		} else {

			// 2D texture

569
			if ( textureProperties.__webglInit === undefined ) return;
570

571
			_gl.deleteTexture( textureProperties.__webglTexture );
572

M
Mr.doob 已提交
573 574
		}

575
		// remove all webgl properties
576
		properties.delete( texture );
577

578
	}
M
Mr.doob 已提交
579

580
	function deallocateRenderTarget( renderTarget ) {
M
Mr.doob 已提交
581

582
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
583
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
584

M
Mr.doob 已提交
585
		if ( ! renderTarget || textureProperties.__webglTexture === undefined ) return;
M
Mr.doob 已提交
586

M
Mr.doob 已提交
587
		_gl.deleteTexture( textureProperties.__webglTexture );
M
Mr.doob 已提交
588

M
Mr.doob 已提交
589 590 591 592
		if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {

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

593
				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
594
				_gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );
M
Mr.doob 已提交
595 596 597 598 599

			}

		} else {

600
			_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
601
			_gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );
M
Mr.doob 已提交
602 603 604

		}

M
Mr.doob 已提交
605
		properties.delete( renderTarget.texture );
D
Daosheng Mu 已提交
606
		properties.delete( renderTarget );
M
Mr.doob 已提交
607

608
	}
M
Mr.doob 已提交
609

610
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
611

612 613 614 615
		releaseMaterialProgramReference( material );

		properties.delete( material );

616
	}
617 618


619
	function releaseMaterialProgramReference( material ) {
620

621
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
622 623 624

		material.program = undefined;

625
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
626

627
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
628

M
Mr.doob 已提交
629 630
		}

631
	}
M
Mr.doob 已提交
632 633 634 635 636

	// Buffer rendering

	this.renderBufferImmediate = function ( object, program, material ) {

637
		state.initAttributes();
638

639
		var buffers = properties.get( object );
640

641 642 643 644
		if ( object.hasPositions && ! buffers.position ) buffers.position = _gl.createBuffer();
		if ( object.hasNormals && ! buffers.normal ) buffers.normal = _gl.createBuffer();
		if ( object.hasUvs && ! buffers.uv ) buffers.uv = _gl.createBuffer();
		if ( object.hasColors && ! buffers.color ) buffers.color = _gl.createBuffer();
M
Mr.doob 已提交
645

646
		var attributes = program.getAttributes();
647

M
Mr.doob 已提交
648 649
		if ( object.hasPositions ) {

650
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
651
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
652

653 654
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
655 656 657 658 659

		}

		if ( object.hasNormals ) {

660
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
M
Mr.doob 已提交
661

662
			if ( material.type !== 'MeshPhongMaterial' && material.type !== 'MeshStandardMaterial' && material.shading === THREE.FlatShading ) {
M
Mr.doob 已提交
663

664
				for ( var i = 0, l = object.count * 3; i < l; i += 9 ) {
M
Mr.doob 已提交
665

666
					var array = object.normalArray;
M
Mr.doob 已提交
667

668 669 670
					var nx = ( array[ i + 0 ] + array[ i + 3 ] + array[ i + 6 ] ) / 3;
					var ny = ( array[ i + 1 ] + array[ i + 4 ] + array[ i + 7 ] ) / 3;
					var nz = ( array[ i + 2 ] + array[ i + 5 ] + array[ i + 8 ] ) / 3;
M
Mr.doob 已提交
671

672 673 674
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
675

676 677 678
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
679

680 681 682
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
683 684 685 686 687 688

				}

			}

			_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
689

690
			state.enableAttribute( attributes.normal );
691

692
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
693 694 695 696 697

		}

		if ( object.hasUvs && material.map ) {

698
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
699
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
700

701
			state.enableAttribute( attributes.uv );
702

703
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
704 705 706 707 708

		}

		if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {

709
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
710
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
711

712
			state.enableAttribute( attributes.color );
713

714
			_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
715 716 717

		}

718
		state.disableUnusedAttributes();
719

M
Mr.doob 已提交
720 721 722 723 724 725
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

726
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
727

M
Mr.doob 已提交
728 729
		setMaterial( material );

730
		var program = setProgram( camera, fog, material, object );
M
Mr.doob 已提交
731

M
Mr.doob 已提交
732 733
		var updateBuffers = false;
		var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
M
Mr.doob 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

		if ( geometryProgram !== _currentGeometryProgram ) {

			_currentGeometryProgram = geometryProgram;
			updateBuffers = true;

		}

		// morph targets

		var morphTargetInfluences = object.morphTargetInfluences;

		if ( morphTargetInfluences !== undefined ) {

			var activeInfluences = [];

			for ( var i = 0, l = morphTargetInfluences.length; i < l; i ++ ) {

				var influence = morphTargetInfluences[ i ];
				activeInfluences.push( [ influence, i ] );

			}

757
			activeInfluences.sort( absNumericalSort );
M
Mr.doob 已提交
758 759 760 761 762 763 764

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

765 766
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
767 768 769 770 771 772 773
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

				var influence = activeInfluences[ i ];
				morphInfluences[ i ] = influence[ 0 ];

				if ( influence[ 0 ] !== 0 ) {

774
					var index = influence[ 1 ];
M
Mr.doob 已提交
775

776 777
					if ( material.morphTargets === true && morphAttributes.position ) geometry.addAttribute( 'morphTarget' + i, morphAttributes.position[ index ] );
					if ( material.morphNormals === true && morphAttributes.normal ) geometry.addAttribute( 'morphNormal' + i, morphAttributes.normal[ index ] );
M
Mr.doob 已提交
778 779 780

				} else {

781 782
					if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
					if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
M
Mr.doob 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

				}

			}

			var uniforms = program.getUniforms();

			if ( uniforms.morphTargetInfluences !== null ) {

				_gl.uniform1fv( uniforms.morphTargetInfluences, morphInfluences );

			}

			updateBuffers = true;

		}

M
Mr.doob 已提交
800 801
		//

802
		var index = geometry.index;
803 804
		var position = geometry.attributes.position;

805 806
		if ( material.wireframe === true ) {

807
			index = objects.getWireframeAttribute( geometry );
808 809 810

		}

811 812
		var renderer;

813
		if ( index !== null ) {
814

815 816
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
817

818
		} else {
819

820
			renderer = bufferRenderer;
821

822
		}
M
Mr.doob 已提交
823

824
		if ( updateBuffers ) {
M
Mr.doob 已提交
825

826
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
827

828
			if ( index !== null ) {
829

830
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
831 832 833

			}

834
		}
835

M
Mr.doob 已提交
836
		//
837

M
Mr.doob 已提交
838 839
		var dataStart = 0;
		var dataCount = Infinity;
840

M
Mr.doob 已提交
841
		if ( index !== null ) {
842

M
Mr.doob 已提交
843
			dataCount = index.count;
844

M
Mr.doob 已提交
845
		} else if ( position !== undefined ) {
846

M
Mr.doob 已提交
847
			dataCount = position.count;
848

M
Mr.doob 已提交
849
		}
850

M
Mr.doob 已提交
851 852
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
853

M
Mr.doob 已提交
854 855
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
856

M
Mr.doob 已提交
857 858 859 860 861 862
		var drawStart = Math.max( dataStart, rangeStart, groupStart );
		var drawEnd = Math.min( dataStart + dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;

		var drawCount = Math.max( 0, drawEnd - drawStart + 1 );

		//
863

864
		if ( object instanceof THREE.Mesh ) {
865

866
			if ( material.wireframe === true ) {
867

868
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
869
				renderer.setMode( _gl.LINES );
870

871
			} else {
M
Mr.doob 已提交
872 873

				switch ( object.drawMode ) {
874

B
Ben Adams 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887
					case THREE.TrianglesDrawMode:
						renderer.setMode( _gl.TRIANGLES );
						break;

					case THREE.TriangleStripDrawMode:
						renderer.setMode( _gl.TRIANGLE_STRIP );
						break;

					case THREE.TriangleFanDrawMode:
						renderer.setMode( _gl.TRIANGLE_FAN );
						break;

				}
888

889
			}
890

891

892
		} else if ( object instanceof THREE.Line ) {
893

894
			var lineWidth = material.linewidth;
895

896
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
897

898
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
899

900
			if ( object instanceof THREE.LineSegments ) {
901

902
				renderer.setMode( _gl.LINES );
903

904
			} else {
905

906
				renderer.setMode( _gl.LINE_STRIP );
907 908

			}
M
Mr.doob 已提交
909

910
		} else if ( object instanceof THREE.Points ) {
911 912

			renderer.setMode( _gl.POINTS );
913 914

		}
915

J
jfranc 已提交
916
		if ( geometry instanceof THREE.InstancedBufferGeometry ) {
M
Mr.doob 已提交
917 918 919

			if ( geometry.maxInstancedCount > 0 ) {

J
jfranc 已提交
920
				renderer.renderInstances( geometry, drawStart, drawCount );
M
Mr.doob 已提交
921

J
jfranc 已提交
922
			}
923 924 925

		} else {

M
Mr.doob 已提交
926
			renderer.render( drawStart, drawCount );
927

M
Mr.doob 已提交
928 929 930 931
		}

	};

932
	function setupVertexAttributes( material, program, geometry, startIndex ) {
M
Mr.doob 已提交
933

M
Mr.doob 已提交
934
		var extension;
B
Ben Adams 已提交
935

M
Mr.doob 已提交
936
		if ( geometry instanceof THREE.InstancedBufferGeometry ) {
B
Ben Adams 已提交
937

M
Mr.doob 已提交
938
			extension = extensions.get( 'ANGLE_instanced_arrays' );
B
Ben Adams 已提交
939

M
Mr.doob 已提交
940
			if ( extension === null ) {
B
Ben Adams 已提交
941

942
				console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
M
Mr.doob 已提交
943
				return;
B
Ben Adams 已提交
944

M
Mr.doob 已提交
945 946 947
			}

		}
B
Ben Adams 已提交
948

949 950
		if ( startIndex === undefined ) startIndex = 0;

951 952
		state.initAttributes();

953
		var geometryAttributes = geometry.attributes;
954

955
		var programAttributes = program.getAttributes();
956

957
		var materialDefaultAttributeValues = material.defaultAttributeValues;
958

959
		for ( var name in programAttributes ) {
960

961
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
962

M
Mr.doob 已提交
963
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
964

965
				var geometryAttribute = geometryAttributes[ name ];
966

M
Mr.doob 已提交
967
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
968

969
					var size = geometryAttribute.itemSize;
G
gero3 已提交
970
					var buffer = objects.getAttributeBuffer( geometryAttribute );
971

B
Ben Adams 已提交
972
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
973

M
Mr.doob 已提交
974 975 976 977 978
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

						if ( data instanceof THREE.InstancedInterleavedBuffer ) {
M
Mr.doob 已提交
979

M
Mr.doob 已提交
980
							state.enableAttributeAndDivisor( programAttribute, data.meshPerAttribute, extension );
B
Ben Adams 已提交
981

M
Mr.doob 已提交
982
							if ( geometry.maxInstancedCount === undefined ) {
983

D
dubejf 已提交
984
								geometry.maxInstancedCount = data.meshPerAttribute * data.count;
B
Ben Adams 已提交
985

M
Mr.doob 已提交
986
							}
B
Ben Adams 已提交
987

M
Mr.doob 已提交
988
						} else {
B
Ben Adams 已提交
989

M
Mr.doob 已提交
990
							state.enableAttribute( programAttribute );
B
Ben Adams 已提交
991

M
Mr.doob 已提交
992
						}
B
Ben Adams 已提交
993

M
Mr.doob 已提交
994 995
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
						_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );
B
Ben Adams 已提交
996

M
Mr.doob 已提交
997
					} else {
B
Ben Adams 已提交
998

M
Mr.doob 已提交
999
						if ( geometryAttribute instanceof THREE.InstancedBufferAttribute ) {
B
Ben Adams 已提交
1000

M
Mr.doob 已提交
1001
							state.enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute, extension );
B
Ben Adams 已提交
1002

M
Mr.doob 已提交
1003
							if ( geometry.maxInstancedCount === undefined ) {
B
Ben Adams 已提交
1004

D
dubejf 已提交
1005
								geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * geometryAttribute.count;
B
Ben Adams 已提交
1006

M
Mr.doob 已提交
1007
							}
B
Ben Adams 已提交
1008

M
Mr.doob 已提交
1009 1010 1011 1012
						} else {

							state.enableAttribute( programAttribute );

M
Mr.doob 已提交
1013
						}
B
Ben Adams 已提交
1014

M
Mr.doob 已提交
1015 1016 1017
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
						_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32

B
Ben Adams 已提交
1018
					}
M
Mr.doob 已提交
1019

1020 1021
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
1022
					var value = materialDefaultAttributeValues[ name ];
1023

1024
					if ( value !== undefined ) {
M
Mr.doob 已提交
1025

1026
						switch ( value.length ) {
M
Mr.doob 已提交
1027

1028 1029 1030
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1031

1032 1033 1034
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1035

1036 1037 1038
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
1039

1040 1041
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
1042 1043

						}
M
Mr.doob 已提交
1044 1045 1046 1047 1048 1049 1050 1051

					}

				}

			}

		}
1052

1053
		state.disableUnusedAttributes();
1054

M
Mr.doob 已提交
1055 1056
	}

M
Mr.doob 已提交
1057 1058
	// Sorting

1059
	function absNumericalSort( a, b ) {
1060

1061
		return Math.abs( b[ 0 ] ) - Math.abs( a[ 0 ] );
1062 1063 1064

	}

M
Mr.doob 已提交
1065 1066
	function painterSortStable ( a, b ) {

U
unconed 已提交
1067
		if ( a.object.renderOrder !== b.object.renderOrder ) {
1068

U
unconed 已提交
1069
			return a.object.renderOrder - b.object.renderOrder;
1070

M
Mr.doob 已提交
1071
		} else if ( a.material.id !== b.material.id ) {
M
Mr.doob 已提交
1072

M
Mr.doob 已提交
1073
			return a.material.id - b.material.id;
1074 1075

		} else if ( a.z !== b.z ) {
M
Mr.doob 已提交
1076

M
Mr.doob 已提交
1077
			return a.z - b.z;
M
Mr.doob 已提交
1078 1079 1080

		} else {

1081
			return a.id - b.id;
M
Mr.doob 已提交
1082 1083 1084

		}

1085
	}
M
Mr.doob 已提交
1086

1087 1088
	function reversePainterSortStable ( a, b ) {

U
unconed 已提交
1089
		if ( a.object.renderOrder !== b.object.renderOrder ) {
1090

U
unconed 已提交
1091
			return a.object.renderOrder - b.object.renderOrder;
1092 1093

		} if ( a.z !== b.z ) {
1094

M
Mr.doob 已提交
1095
			return b.z - a.z;
1096 1097 1098 1099 1100 1101 1102

		} else {

			return a.id - b.id;

		}

1103
	}
1104

M
Mr.doob 已提交
1105 1106 1107 1108 1109 1110
	// Rendering

	this.render = function ( scene, camera, renderTarget, forceClear ) {

		if ( camera instanceof THREE.Camera === false ) {

1111
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1112 1113 1114 1115
			return;

		}

M
Mr.doob 已提交
1116
		var fog = scene.fog;
M
Mr.doob 已提交
1117 1118 1119

		// reset caching for this frame

1120
		_currentGeometryProgram = '';
1121
		_currentMaterialId = - 1;
1122
		_currentCamera = null;
M
Mr.doob 已提交
1123 1124 1125

		// update scene graph

1126
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1127 1128 1129

		// update camera matrices and frustum

1130
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1131 1132 1133 1134 1135 1136

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

		_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
		_frustum.setFromMatrix( _projScreenMatrix );

M
Mr.doob 已提交
1137
		lights.length = 0;
1138

M
Mr.doob 已提交
1139 1140
		opaqueObjectsLastIndex = - 1;
		transparentObjectsLastIndex = - 1;
1141

M
Mr.doob 已提交
1142 1143 1144
		sprites.length = 0;
		lensFlares.length = 0;

1145
		projectObject( scene, camera );
M
Mr.doob 已提交
1146

1147 1148 1149
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1150
		if ( _this.sortObjects === true ) {
1151 1152 1153

			opaqueObjects.sort( painterSortStable );
			transparentObjects.sort( reversePainterSortStable );
M
Mr.doob 已提交
1154

1155 1156
		}

1157 1158
		setupLights( lights, camera );

M
Mr.doob 已提交
1159
		//
M
Mr.doob 已提交
1160

M
Mr.doob 已提交
1161
		shadowMap.render( scene, camera );
M
Mr.doob 已提交
1162 1163 1164

		//

1165 1166 1167 1168
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1169

1170 1171 1172 1173 1174 1175
		if ( renderTarget === undefined ) {

			renderTarget = null;

		}

M
Mr.doob 已提交
1176 1177 1178 1179 1180 1181 1182 1183
		this.setRenderTarget( renderTarget );

		if ( this.autoClear || forceClear ) {

			this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );

		}

1184
		//
M
Mr.doob 已提交
1185 1186 1187

		if ( scene.overrideMaterial ) {

1188
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1189

1190 1191
			renderObjects( opaqueObjects, camera, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, fog, overrideMaterial );
1192

M
Mr.doob 已提交
1193 1194 1195 1196
		} else {

			// opaque pass (front-to-back order)

M
Mr.doob 已提交
1197
			state.setBlending( THREE.NoBlending );
1198
			renderObjects( opaqueObjects, camera, fog );
M
Mr.doob 已提交
1199 1200 1201

			// transparent pass (back-to-front order)

1202
			renderObjects( transparentObjects, camera, fog );
M
Mr.doob 已提交
1203 1204 1205 1206 1207

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1208
		spritePlugin.render( scene, camera );
1209
		lensFlarePlugin.render( scene, camera, _currentViewport );
M
Mr.doob 已提交
1210 1211 1212

		// Generate mipmap if we're using any kind of mipmap filtering

M
Mr.doob 已提交
1213 1214 1215
		if ( renderTarget ) {

			var texture = renderTarget.texture;
M
Mr.doob 已提交
1216

M
Mr.doob 已提交
1217 1218 1219 1220 1221
			if ( texture.generateMipmaps && isPowerOfTwo( renderTarget ) &&
					texture.minFilter !== THREE.NearestFilter &&
					texture.minFilter !== THREE.LinearFilter ) {

				updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1222 1223

			}
M
Mr.doob 已提交
1224 1225 1226

		}

1227
		// Ensure depth buffer writing is enabled so it can be cleared on next render
M
Mr.doob 已提交
1228

M
Mr.doob 已提交
1229 1230
		state.setDepthTest( true );
		state.setDepthWrite( true );
1231
		state.setColorWrite( true );
M
Mr.doob 已提交
1232 1233 1234 1235

		// _gl.finish();

	};
M
Mr.doob 已提交
1236

M
Mr.doob 已提交
1237 1238
	function pushRenderItem( object, geometry, material, z, group ) {

1239
		var array, index;
M
Mr.doob 已提交
1240

1241
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1242 1243 1244

		if ( material.transparent ) {

1245 1246
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1247 1248 1249

		} else {

1250 1251 1252 1253 1254
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1255 1256
		// recycle existing render item or grow the array

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
		var renderItem = array[ index ];

		if ( renderItem !== undefined ) {

			renderItem.id = object.id;
			renderItem.object = object;
			renderItem.geometry = geometry;
			renderItem.material = material;
			renderItem.z = _vector3.z;
			renderItem.group = group;
M
Mr.doob 已提交
1267 1268 1269

		} else {

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
			renderItem = {
				id: object.id,
				object: object,
				geometry: geometry,
				material: material,
				z: _vector3.z,
				group: group
			};

			// assert( index === array.length );
			array.push( renderItem );
M
Mr.doob 已提交
1281 1282 1283 1284 1285

		}

	}

1286
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1287

M
Mr.doob 已提交
1288
		if ( object.visible === false ) return;
M
Mr.doob 已提交
1289

1290
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1291

1292
			if ( object instanceof THREE.Light ) {
M
Mr.doob 已提交
1293

1294
				lights.push( object );
M
Mr.doob 已提交
1295

1296
			} else if ( object instanceof THREE.Sprite ) {
M
Mr.doob 已提交
1297

1298 1299 1300 1301 1302
				if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {

					sprites.push( object );

				}
M
Mr.doob 已提交
1303

1304
			} else if ( object instanceof THREE.LensFlare ) {
M
Mr.doob 已提交
1305

1306
				lensFlares.push( object );
M
Mr.doob 已提交
1307

1308
			} else if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1309

1310
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1311

1312 1313
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1314

1315
				}
1316

1317
				pushRenderItem( object, null, object.material, _vector3.z, null );
M
Mr.doob 已提交
1318

1319
			} else if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.Points ) {
M
Mr.doob 已提交
1320

1321
				if ( object instanceof THREE.SkinnedMesh ) {
M
Mr.doob 已提交
1322

1323
					object.skeleton.update();
1324

1325
				}
1326

1327
				if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
1328

1329
					var material = object.material;
1330

1331
					if ( material.visible === true ) {
M
Mr.doob 已提交
1332

1333
						if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1334

1335 1336 1337 1338
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1339

1340
						var geometry = objects.update( object );
M
Mr.doob 已提交
1341

S
SUNAG 已提交
1342
						if ( material instanceof THREE.MultiMaterial ) {
1343

1344 1345
							var groups = geometry.groups;
							var materials = material.materials;
1346

1347
							for ( var i = 0, l = groups.length; i < l; i ++ ) {
1348

1349 1350
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1351

1352
								if ( groupMaterial.visible === true ) {
1353

1354 1355 1356
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1357

M
Mr.doob 已提交
1358
							}
M
Mr.doob 已提交
1359

1360
						} else {
M
Mr.doob 已提交
1361

1362
							pushRenderItem( object, geometry, material, _vector3.z, null );
1363

1364
						}
O
OpenShift guest 已提交
1365

1366
					}
M
Mr.doob 已提交
1367

1368
				}
M
Mr.doob 已提交
1369

1370
			}
M
Mr.doob 已提交
1371

M
Mr.doob 已提交
1372
		}
M
Mr.doob 已提交
1373

M
Mr.doob 已提交
1374
		var children = object.children;
M
Mr.doob 已提交
1375

M
Mr.doob 已提交
1376 1377
		for ( var i = 0, l = children.length; i < l; i ++ ) {

1378
			projectObject( children[ i ], camera );
M
Mr.doob 已提交
1379

1380
		}
1381

1382
	}
M
Mr.doob 已提交
1383

1384
	function renderObjects( renderList, camera, fog, overrideMaterial ) {
M
Mr.doob 已提交
1385

M
Mr.doob 已提交
1386
		for ( var i = 0, l = renderList.length; i < l; i ++ ) {
M
Mr.doob 已提交
1387

1388
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1389

1390
			var object = renderItem.object;
M
Mr.doob 已提交
1391 1392 1393
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1394

1395 1396
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1397

M
Mr.doob 已提交
1398
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1399

M
Mr.doob 已提交
1400
				setMaterial( material );
M
Mr.doob 已提交
1401

1402
				var program = setProgram( camera, fog, material, object );
M
Mr.doob 已提交
1403

M
Mr.doob 已提交
1404
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1405

M
Mr.doob 已提交
1406
				object.render( function ( object ) {
M
Mr.doob 已提交
1407

M
Mr.doob 已提交
1408
					_this.renderBufferImmediate( object, program, material );
M
Mr.doob 已提交
1409

M
Mr.doob 已提交
1410
				} );
1411

M
Mr.doob 已提交
1412
			} else {
M
Mr.doob 已提交
1413

1414
				_this.renderBufferDirect( camera, fog, geometry, material, object, group );
M
Mr.doob 已提交
1415

M
Mr.doob 已提交
1416
			}
M
Mr.doob 已提交
1417

1418
		}
M
Mr.doob 已提交
1419

1420
	}
G
gero3 已提交
1421

1422
	function initMaterial( material, fog, object ) {
M
Mr.doob 已提交
1423

1424
		var materialProperties = properties.get( material );
G
gero3 已提交
1425

1426
		var parameters = programCache.getParameters( material, _lights, fog, object );
G
gero3 已提交
1427
		var code = programCache.getProgramCode( material, parameters );
G
gero3 已提交
1428

1429
		var program = materialProperties.program;
T
tschw 已提交
1430
		var programChange = true;
1431

1432
		if ( program === undefined ) {
B
Ben Adams 已提交
1433

M
Mr.doob 已提交
1434 1435
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1436

1437
		} else if ( program.code !== code ) {
B
Ben Adams 已提交
1438

M
Mr.doob 已提交
1439
			// changed glsl or parameters
1440
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1441

G
gero3 已提交
1442
		} else if ( parameters.shaderID !== undefined ) {
B
Ben Adams 已提交
1443

T
tschw 已提交
1444
			// same glsl and uniform list
T
tschw 已提交
1445 1446
			return;

T
tschw 已提交
1447
		} else {
B
Ben Adams 已提交
1448

T
tschw 已提交
1449 1450
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1451 1452 1453

		}

1454
		if ( programChange ) {
B
Ben Adams 已提交
1455

1456
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1457

1458
				var shader = THREE.ShaderLib[ parameters.shaderID ];
B
Ben Adams 已提交
1459

1460 1461 1462 1463 1464 1465
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1466

1467
			} else {
B
Ben Adams 已提交
1468

1469 1470 1471 1472 1473 1474
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1475

1476
			}
G
gero3 已提交
1477

1478
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1479

1480
			program = programCache.acquireProgram( material, parameters, code );
B
Ben Adams 已提交
1481

1482 1483
			materialProperties.program = program;
			material.program = program;
1484 1485 1486

		}

1487
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1488 1489 1490 1491 1492

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

1493
			for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
M
Mr.doob 已提交
1494

M
Mr.doob 已提交
1495
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

M
Mr.doob 已提交
1509
			for ( var i = 0; i < _this.maxMorphNormals; i ++ ) {
M
Mr.doob 已提交
1510

M
Mr.doob 已提交
1511
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520

					material.numSupportedMorphNormals ++;

				}

			}

		}

1521
		materialProperties.uniformsList = [];
M
Mr.doob 已提交
1522

1523 1524
		var uniforms = materialProperties.__webglShader.uniforms,
			uniformLocations = materialProperties.program.getUniforms();
M
Mr.doob 已提交
1525

1526
		for ( var u in uniforms ) {
M
Mr.doob 已提交
1527

1528
			var location = uniformLocations[ u ];
1529 1530

			if ( location ) {
G
gero3 已提交
1531

1532
				materialProperties.uniformsList.push( [ materialProperties.__webglShader.uniforms[ u ], location ] );
G
gero3 已提交
1533

1534
			}
M
Mr.doob 已提交
1535 1536 1537

		}

1538 1539
		if ( material instanceof THREE.MeshPhongMaterial ||
				material instanceof THREE.MeshLambertMaterial ||
1540
				material instanceof THREE.MeshStandardMaterial ||
1541 1542
				material.lights ) {

1543 1544
			// store the light setup it was created for

1545 1546
			materialProperties.lightsHash = _lights.hash;

1547 1548 1549 1550 1551
			// wire up the material to this renderer's lighting state

			uniforms.ambientLightColor.value = _lights.ambient;
			uniforms.directionalLights.value = _lights.directional;
			uniforms.spotLights.value = _lights.spot;
M
Mr.doob 已提交
1552
			uniforms.pointLights.value = _lights.point;
1553 1554
			uniforms.hemisphereLights.value = _lights.hemi;

1555 1556 1557 1558 1559 1560
			uniforms.directionalShadowMap.value = _lights.directionalShadowMap;
			uniforms.directionalShadowMatrix.value = _lights.directionalShadowMatrix;
			uniforms.spotShadowMap.value = _lights.spotShadowMap;
			uniforms.spotShadowMatrix.value = _lights.spotShadowMatrix;
			uniforms.pointShadowMap.value = _lights.pointShadowMap;
			uniforms.pointShadowMatrix.value = _lights.pointShadowMatrix;
1561

1562 1563
		}

A
arose 已提交
1564 1565 1566 1567 1568 1569 1570 1571
		// detect dynamic uniforms

		materialProperties.hasDynamicUniforms = false;

		for ( var j = 0, jl = materialProperties.uniformsList.length; j < jl; j ++ ) {

			var uniform = materialProperties.uniformsList[ j ][ 0 ];

1572
			if ( uniform.dynamic === true ) {
A
arose 已提交
1573 1574 1575 1576 1577 1578 1579 1580

				materialProperties.hasDynamicUniforms = true;
				break;

			}

		}

M
Mr.doob 已提交
1581
	}
M
Mr.doob 已提交
1582

1583 1584
	function setMaterial( material ) {

M
Mr.doob 已提交
1585 1586
		setMaterialFaces( material );

1587 1588
		if ( material.transparent === true ) {

M
Mr.doob 已提交
1589
			state.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha );
1590

1591 1592 1593 1594
		} else {

			state.setBlending( THREE.NoBlending );

1595 1596
		}

B
Ben Adams 已提交
1597
		state.setDepthFunc( material.depthFunc );
M
Mr.doob 已提交
1598 1599
		state.setDepthTest( material.depthTest );
		state.setDepthWrite( material.depthWrite );
1600
		state.setColorWrite( material.colorWrite );
M
Mr.doob 已提交
1601
		state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
1602 1603 1604

	}

M
Mr.doob 已提交
1605 1606
	function setMaterialFaces( material ) {

1607
		material.side !== THREE.DoubleSide ? state.enable( _gl.CULL_FACE ) : state.disable( _gl.CULL_FACE );
M
Mr.doob 已提交
1608 1609 1610 1611
		state.setFlipSided( material.side === THREE.BackSide );

	}

1612
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1613 1614 1615

		_usedTextureUnits = 0;

1616
		var materialProperties = properties.get( material );
1617

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
		if ( materialProperties.program === undefined ) {

			material.needsUpdate = true;

		}

		if ( materialProperties.lightsHash !== undefined &&
			materialProperties.lightsHash !== _lights.hash ) {

			material.needsUpdate = true;

		}

		if ( material.needsUpdate ) {
M
Mr.doob 已提交
1632

1633
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1634 1635 1636 1637
			material.needsUpdate = false;

		}

1638
		var refreshProgram = false;
M
Mr.doob 已提交
1639
		var refreshMaterial = false;
1640
		var refreshLights = false;
M
Mr.doob 已提交
1641

1642
		var program = materialProperties.program,
1643
			p_uniforms = program.getUniforms(),
1644
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1645

1646
		if ( program.id !== _currentProgram ) {
M
Mr.doob 已提交
1647

1648 1649
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1650

1651
			refreshProgram = true;
M
Mr.doob 已提交
1652
			refreshMaterial = true;
1653
			refreshLights = true;
M
Mr.doob 已提交
1654 1655 1656 1657 1658 1659

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1660

M
Mr.doob 已提交
1661 1662 1663 1664
			refreshMaterial = true;

		}

1665
		if ( refreshProgram || camera !== _currentCamera ) {
M
Mr.doob 已提交
1666 1667 1668

			_gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );

G
gero3 已提交
1669
			if ( capabilities.logarithmicDepthBuffer ) {
1670

1671
				_gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1672 1673 1674 1675

			}


1676 1677 1678 1679 1680 1681 1682 1683 1684
			if ( camera !== _currentCamera ) {

				_currentCamera = camera;

				// lighting uniforms depend on the camera so enforce an update
				// now, in case this material supports lights - or later, when
				// the next material that does gets activated:

				refreshMaterial = true;		// set to true on material change
T
tschw 已提交
1685
				refreshLights = true;		// remains set until update done
1686 1687

			}
M
Mr.doob 已提交
1688

1689 1690 1691 1692 1693
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1694
				 material instanceof THREE.MeshStandardMaterial ||
1695 1696
				 material.envMap ) {

1697
				if ( p_uniforms.cameraPosition !== undefined ) {
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707

					_vector3.setFromMatrixPosition( camera.matrixWorld );
					_gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1708
				 material instanceof THREE.MeshBasicMaterial ||
1709
				 material instanceof THREE.MeshStandardMaterial ||
1710 1711 1712
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

1713
				if ( p_uniforms.viewMatrix !== undefined ) {
1714 1715

					_gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
M
Mr.doob 已提交
1716

1717 1718 1719 1720
				}

			}

B
Ben Houston 已提交
1721 1722 1723 1724

			if ( p_uniforms.toneMappingExposure !== undefined ) {

				_gl.uniform1f( p_uniforms.toneMappingExposure, _this.toneMappingExposure );
1725

B
Ben Houston 已提交
1726 1727 1728 1729 1730 1731 1732
			}

			if ( p_uniforms.toneMappingWhitePoint !== undefined ) {

				_gl.uniform1f( p_uniforms.toneMappingWhitePoint, _this.toneMappingWhitePoint );

			}
M
Mr.doob 已提交
1733

M
Mr.doob 已提交
1734 1735 1736 1737 1738 1739 1740 1741
		}

		// skinning uniforms must be set even if material didn't change
		// auto-setting of texture unit for bone texture must go before other textures
		// not sure why, but otherwise weird things happen

		if ( material.skinning ) {

1742
			if ( object.bindMatrix && p_uniforms.bindMatrix !== undefined ) {
1743 1744 1745 1746 1747

				_gl.uniformMatrix4fv( p_uniforms.bindMatrix, false, object.bindMatrix.elements );

			}

1748
			if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== undefined ) {
1749 1750 1751 1752 1753

				_gl.uniformMatrix4fv( p_uniforms.bindMatrixInverse, false, object.bindMatrixInverse.elements );

			}

1754
			if ( capabilities.floatVertexTextures && object.skeleton && object.skeleton.useVertexTexture ) {
M
Mr.doob 已提交
1755

1756
				if ( p_uniforms.boneTexture !== undefined ) {
M
Mr.doob 已提交
1757 1758 1759 1760

					var textureUnit = getTextureUnit();

					_gl.uniform1i( p_uniforms.boneTexture, textureUnit );
1761
					_this.setTexture( object.skeleton.boneTexture, textureUnit );
M
Mr.doob 已提交
1762 1763 1764

				}

1765
				if ( p_uniforms.boneTextureWidth !== undefined ) {
1766

1767
					_gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
1768 1769 1770

				}

1771
				if ( p_uniforms.boneTextureHeight !== undefined ) {
1772

1773
					_gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
1774 1775 1776

				}

1777
			} else if ( object.skeleton && object.skeleton.boneMatrices ) {
M
Mr.doob 已提交
1778

1779
				if ( p_uniforms.boneGlobalMatrices !== undefined ) {
M
Mr.doob 已提交
1780

1781
					_gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
M
Mr.doob 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792

				}

			}

		}

		if ( refreshMaterial ) {

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1793
				 material instanceof THREE.MeshStandardMaterial ||
M
Mr.doob 已提交
1794 1795
				 material.lights ) {

1796
				// the current material requires lighting info
M
Mr.doob 已提交
1797

T
tschw 已提交
1798 1799 1800 1801 1802 1803
				// note: all lighting uniforms are always set correctly
				// they simply reference the renderer's state for their
				// values
				//
				// use the current material's .needsUpdate flags to set
				// the GL state when required
M
Mr.doob 已提交
1804

T
tschw 已提交
1805
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1806

T
tschw 已提交
1807
			}
G
gero3 已提交
1808

T
tschw 已提交
1809
			// refresh uniforms common to several materials
G
gero3 已提交
1810

T
tschw 已提交
1811
			if ( fog && material.fog ) {
G
gero3 已提交
1812

T
tschw 已提交
1813
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1814 1815 1816 1817 1818

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1819
				 material instanceof THREE.MeshPhongMaterial ||
1820
				 material instanceof THREE.MeshStandardMaterial ) {
M
Mr.doob 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836

				refreshUniformsCommon( m_uniforms, material );

			}

			// refresh single material specific uniforms

			if ( material instanceof THREE.LineBasicMaterial ) {

				refreshUniformsLine( m_uniforms, material );

			} else if ( material instanceof THREE.LineDashedMaterial ) {

				refreshUniformsLine( m_uniforms, material );
				refreshUniformsDash( m_uniforms, material );

1837
			} else if ( material instanceof THREE.PointsMaterial ) {
M
Mr.doob 已提交
1838

M
Mr.doob 已提交
1839
				refreshUniformsPoints( m_uniforms, material );
M
Mr.doob 已提交
1840

1841 1842 1843 1844
			} else if ( material instanceof THREE.MeshLambertMaterial ) {

				refreshUniformsLambert( m_uniforms, material );

M
Mr.doob 已提交
1845 1846 1847 1848
			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

1849
			} else if ( material instanceof THREE.MeshStandardMaterial ) {
W
WestLangley 已提交
1850

1851
				refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1852

M
Mr.doob 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
			} else if ( material instanceof THREE.MeshDepthMaterial ) {

				m_uniforms.mNear.value = camera.near;
				m_uniforms.mFar.value = camera.far;
				m_uniforms.opacity.value = material.opacity;

			} else if ( material instanceof THREE.MeshNormalMaterial ) {

				m_uniforms.opacity.value = material.opacity;

			}

			// load common uniforms

1867
			loadUniformsGeneric( materialProperties.uniformsList );
M
Mr.doob 已提交
1868 1869 1870 1871 1872

		}

		loadUniformsMatrices( p_uniforms, object );

1873
		if ( p_uniforms.modelMatrix !== undefined ) {
M
Mr.doob 已提交
1874 1875

			_gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
M
Mr.doob 已提交
1876

M
Mr.doob 已提交
1877 1878
		}

1879
		if ( materialProperties.hasDynamicUniforms === true ) {
A
arose 已提交
1880

1881
			updateDynamicUniforms( materialProperties.uniformsList, object, camera );
A
arose 已提交
1882 1883 1884

		}

M
Mr.doob 已提交
1885 1886
		return program;

M
Mr.doob 已提交
1887
	}
M
Mr.doob 已提交
1888

1889
	function updateDynamicUniforms ( uniforms, object, camera ) {
A
arose 已提交
1890 1891 1892 1893 1894 1895

		var dynamicUniforms = [];

		for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {

			var uniform = uniforms[ j ][ 0 ];
1896
			var onUpdateCallback = uniform.onUpdateCallback;
A
arose 已提交
1897

1898
			if ( onUpdateCallback !== undefined ) {
A
arose 已提交
1899

1900
				onUpdateCallback.bind( uniform )( object, camera );
A
arose 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
				dynamicUniforms.push( uniforms[ j ] );

			}

		}

		loadUniformsGeneric( dynamicUniforms );

	}

M
Mr.doob 已提交
1911 1912 1913 1914 1915 1916
	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

1917
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1918

1919
		if ( material.emissive ) {
M
Mr.doob 已提交
1920

1921
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1922 1923 1924

		}

1925 1926 1927
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
1928

1929
		if ( material.aoMap ) {
1930

1931 1932
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
1933 1934 1935

		}

M
Mr.doob 已提交
1936
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
1937 1938 1939 1940 1941
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
1942
		// 6. emissive map
M
Mr.doob 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

1954 1955 1956 1957
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
1958 1959 1960 1961 1962 1963 1964 1965
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

1966 1967 1968 1969 1970 1971 1972 1973
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

1974 1975 1976 1977
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

1978 1979 1980 1981
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
1982 1983 1984 1985
		}

		if ( uvScaleMap !== undefined ) {

M
Mr.doob 已提交
1986 1987 1988 1989 1990 1991
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) {

				uvScaleMap = uvScaleMap.texture;

			}

M
Mr.doob 已提交
1992 1993 1994 1995 1996 1997 1998 1999
			var offset = uvScaleMap.offset;
			var repeat = uvScaleMap.repeat;

			uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );

		}

		uniforms.envMap.value = material.envMap;
2000
		uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
M
Mr.doob 已提交
2001

2002
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
2003 2004
		uniforms.refractionRatio.value = material.refractionRatio;

M
Mr.doob 已提交
2005
	}
M
Mr.doob 已提交
2006 2007 2008 2009 2010 2011

	function refreshUniformsLine ( uniforms, material ) {

		uniforms.diffuse.value = material.color;
		uniforms.opacity.value = material.opacity;

M
Mr.doob 已提交
2012
	}
M
Mr.doob 已提交
2013 2014 2015 2016 2017 2018 2019

	function refreshUniformsDash ( uniforms, material ) {

		uniforms.dashSize.value = material.dashSize;
		uniforms.totalSize.value = material.dashSize + material.gapSize;
		uniforms.scale.value = material.scale;

M
Mr.doob 已提交
2020
	}
M
Mr.doob 已提交
2021

M
Mr.doob 已提交
2022
	function refreshUniformsPoints ( uniforms, material ) {
M
Mr.doob 已提交
2023

2024
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2025
		uniforms.opacity.value = material.opacity;
2026 2027
		uniforms.size.value = material.size * _pixelRatio;
		uniforms.scale.value = _canvas.clientHeight / 2.0; // TODO: Cache this.
M
Mr.doob 已提交
2028 2029 2030

		uniforms.map.value = material.map;

2031 2032 2033 2034 2035 2036 2037 2038 2039
		if ( material.map !== null ) {

			var offset = material.map.offset;
			var repeat = material.map.repeat;

			uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );

		}

M
Mr.doob 已提交
2040
	}
M
Mr.doob 已提交
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056

	function refreshUniformsFog ( uniforms, fog ) {

		uniforms.fogColor.value = fog.color;

		if ( fog instanceof THREE.Fog ) {

			uniforms.fogNear.value = fog.near;
			uniforms.fogFar.value = fog.far;

		} else if ( fog instanceof THREE.FogExp2 ) {

			uniforms.fogDensity.value = fog.density;

		}

M
Mr.doob 已提交
2057
	}
M
Mr.doob 已提交
2058

2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	function refreshUniformsLambert ( uniforms, material ) {

		if ( material.lightMap ) {

			uniforms.lightMap.value = material.lightMap;
			uniforms.lightMapIntensity.value = material.lightMapIntensity;

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2076 2077
	function refreshUniformsPhong ( uniforms, material ) {

2078
		uniforms.specular.value = material.specular;
M
Mr.doob 已提交
2079
		uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
M
Mr.doob 已提交
2080

2081 2082 2083 2084
		if ( material.lightMap ) {

			uniforms.lightMap.value = material.lightMap;
			uniforms.lightMapIntensity.value = material.lightMapIntensity;
M
Mr.doob 已提交
2085

2086
		}
2087

2088
		if ( material.emissiveMap ) {
2089

2090
			uniforms.emissiveMap.value = material.emissiveMap;
2091

2092
		}
M
Mr.doob 已提交
2093

2094 2095 2096 2097
		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
M
Mr.doob 已提交
2098

2099
		}
M
Mr.doob 已提交
2100

2101 2102 2103 2104 2105 2106
		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );

		}
M
Mr.doob 已提交
2107

2108 2109 2110 2111 2112
		if ( material.displacementMap ) {

			uniforms.displacementMap.value = material.displacementMap;
			uniforms.displacementScale.value = material.displacementScale;
			uniforms.displacementBias.value = material.displacementBias;
2113

2114
		}
2115 2116 2117

	}

2118
	function refreshUniformsStandard ( uniforms, material ) {
W
WestLangley 已提交
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

		uniforms.roughness.value = material.roughness;
		uniforms.metalness.value = material.metalness;

		if ( material.roughnessMap ) {

			uniforms.roughnessMap.value = material.roughnessMap;

		}

		if ( material.metalnessMap ) {

			uniforms.metalnessMap.value = material.metalnessMap;

		}

		if ( material.lightMap ) {

			uniforms.lightMap.value = material.lightMap;
			uniforms.lightMapIntensity.value = material.lightMapIntensity;

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );

		}

		if ( material.displacementMap ) {

			uniforms.displacementMap.value = material.displacementMap;
			uniforms.displacementScale.value = material.displacementScale;
			uniforms.displacementBias.value = material.displacementBias;

		}

		if ( material.envMap ) {

			//uniforms.envMap.value = material.envMap; // part of uniforms common
			uniforms.envMapIntensity.value = material.envMapIntensity;

		}

	}

2179 2180
	// If uniforms are marked as clean, they don't need to be loaded to the GPU.

M
Mr.doob 已提交
2181
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2182

M
Mr.doob 已提交
2183
		uniforms.ambientLightColor.needsUpdate = value;
2184

B
Ben Houston 已提交
2185 2186 2187 2188
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
		uniforms.hemisphereLights.needsUpdate = value;
2189

M
Mr.doob 已提交
2190
	}
2191

M
Mr.doob 已提交
2192 2193 2194 2195
	// Uniforms (load to GPU)

	function loadUniformsMatrices ( uniforms, object ) {

2196
		_gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object.modelViewMatrix.elements );
M
Mr.doob 已提交
2197 2198 2199

		if ( uniforms.normalMatrix ) {

2200
			_gl.uniformMatrix3fv( uniforms.normalMatrix, false, object.normalMatrix.elements );
M
Mr.doob 已提交
2201 2202 2203

		}

M
Mr.doob 已提交
2204
	}
M
Mr.doob 已提交
2205 2206 2207 2208 2209

	function getTextureUnit() {

		var textureUnit = _usedTextureUnits;

G
gero3 已提交
2210
		if ( textureUnit >= capabilities.maxTextures ) {
M
Mr.doob 已提交
2211

G
gero3 已提交
2212
			console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
M
Mr.doob 已提交
2213 2214 2215 2216 2217 2218 2219

		}

		_usedTextureUnits += 1;

		return textureUnit;

M
Mr.doob 已提交
2220
	}
M
Mr.doob 已提交
2221

2222
	function loadUniformsGeneric ( uniforms ) {
M
Mr.doob 已提交
2223

M
Mr.doob 已提交
2224
		var texture, textureUnit;
M
Mr.doob 已提交
2225

M
Mr.doob 已提交
2226 2227 2228
		for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {

			var uniform = uniforms[ j ][ 0 ];
M
Mr.doob 已提交
2229

2230 2231
			// needsUpdate property is not added to all uniforms.
			if ( uniform.needsUpdate === false ) continue;
2232

M
Mr.doob 已提交
2233 2234
			var type = uniform.type;
			var value = uniform.value;
2235
			var location = uniforms[ j ][ 1 ];
M
Mr.doob 已提交
2236

2237
			if ( type === '1i' ) {
M
Mr.doob 已提交
2238

2239
				_gl.uniform1i( location, value );
2240

2241
			} else if ( type === '1f' ) {
2242

2243
				_gl.uniform1f( location, value );
2244

2245
			} else if ( type === '2f' ) {
2246

2247
				_gl.uniform2f( location, value[ 0 ], value[ 1 ] );
2248

2249
			} else if ( type === '3f' ) {
2250

2251
				_gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
2252

2253
			} else if ( type === '4f' ) {
2254

2255
				_gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
2256

2257
			} else if ( type === '1iv' ) {
2258

2259
				_gl.uniform1iv( location, value );
2260

2261
			} else if ( type === '3iv' ) {
2262

2263
				_gl.uniform3iv( location, value );
2264

2265
			} else if ( type === '1fv' ) {
2266

2267
				_gl.uniform1fv( location, value );
2268

2269
			} else if ( type === '2fv' ) {
M
Mr.doob 已提交
2270

2271
				_gl.uniform2fv( location, value );
M
Mr.doob 已提交
2272

2273
			} else if ( type === '3fv' ) {
M
Mr.doob 已提交
2274

2275
				_gl.uniform3fv( location, value );
M
Mr.doob 已提交
2276

2277
			} else if ( type === '4fv' ) {
M
Mr.doob 已提交
2278

2279
				_gl.uniform4fv( location, value );
M
Mr.doob 已提交
2280

2281
			} else if ( type === 'Matrix2fv' ) {
M
Mr.doob 已提交
2282

2283
				_gl.uniformMatrix2fv( location, false, value );
M
Mr.doob 已提交
2284

2285
			} else if ( type === 'Matrix3fv' ) {
M
Mr.doob 已提交
2286

2287
				_gl.uniformMatrix3fv( location, false, value );
M
Mr.doob 已提交
2288

2289
			} else if ( type === 'Matrix4fv' ) {
M
Mr.doob 已提交
2290

2291
				_gl.uniformMatrix4fv( location, false, value );
M
Mr.doob 已提交
2292

2293
			//
M
Mr.doob 已提交
2294

2295
			} else if ( type === 'i' ) {
M
Mr.doob 已提交
2296

2297 2298
				// single integer
				_gl.uniform1i( location, value );
M
Mr.doob 已提交
2299

2300
			} else if ( type === 'f' ) {
M
Mr.doob 已提交
2301

2302 2303
				// single float
				_gl.uniform1f( location, value );
M
Mr.doob 已提交
2304

2305
			} else if ( type === 'v2' ) {
M
Mr.doob 已提交
2306

2307 2308
				// single THREE.Vector2
				_gl.uniform2f( location, value.x, value.y );
2309

2310
			} else if ( type === 'v3' ) {
M
Mr.doob 已提交
2311

2312 2313
				// single THREE.Vector3
				_gl.uniform3f( location, value.x, value.y, value.z );
M
Mr.doob 已提交
2314

2315
			} else if ( type === 'v4' ) {
2316

2317 2318
				// single THREE.Vector4
				_gl.uniform4f( location, value.x, value.y, value.z, value.w );
2319

2320
			} else if ( type === 'c' ) {
2321

2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
				// single THREE.Color
				_gl.uniform3f( location, value.r, value.g, value.b );

			/*
			} else if ( type === 's' ) {

				// TODO: Optimize this

				for ( var propertyName in uniform.properties ) {

					var property = uniform.properties[ propertyName ];
					var locationProperty =  location[ propertyName ];
					var valueProperty = value[ propertyName ];

					type = property.type;

					if ( type === 'i' ) {

						_gl.uniform1i( locationProperty, valueProperty );

					} else if ( type === 'f' ) {

						_gl.uniform1f( locationProperty, valueProperty );

					} else if ( type === 'v2' ) {

						_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );

					} else if ( type === 'v3' ) {

						_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );

					} else if ( type === 'v4' ) {

						_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );

					} else if ( type === 'c' ) {

						_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
2361 2362

					}
M
Mr.doob 已提交
2363

2364 2365
				}

2366

2367
			*/
M
Mr.doob 已提交
2368

2369
			} else if ( type === 'sa' ) {
M
Mr.doob 已提交
2370

2371
				// TODO: Optimize this
M
Mr.doob 已提交
2372

2373
				for ( var i = 0; i < value.length; i ++ ) {
M
Mr.doob 已提交
2374

2375
					for ( var propertyName in uniform.properties ) {
M
Mr.doob 已提交
2376

2377 2378 2379
						var property = uniform.properties[ propertyName ];
						var locationProperty =  location[ i ][ propertyName ];
						var valueProperty = value[ i ][ propertyName ];
M
Mr.doob 已提交
2380

2381
						type = property.type;
M
Mr.doob 已提交
2382

2383
						if ( type === 'i' ) {
M
Mr.doob 已提交
2384

2385
							_gl.uniform1i( locationProperty, valueProperty );
M
Mr.doob 已提交
2386

2387
						} else if ( type === 'f' ) {
M
Mr.doob 已提交
2388

2389
							_gl.uniform1f( locationProperty, valueProperty );
M
Mr.doob 已提交
2390

2391
						} else if ( type === 'v2' ) {
M
Mr.doob 已提交
2392

2393
							_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );
M
Mr.doob 已提交
2394

2395
						} else if ( type === 'v3' ) {
M
Mr.doob 已提交
2396

2397
							_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );
M
Mr.doob 已提交
2398

2399
						} else if ( type === 'v4' ) {
M
Mr.doob 已提交
2400

2401 2402 2403
							_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );

						} else if ( type === 'c' ) {
M
Mr.doob 已提交
2404

2405
							_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
M
Mr.doob 已提交
2406

2407 2408 2409 2410 2411
						} else if ( type === 'm4' ) {

							_gl.uniformMatrix4fv( locationProperty, false, valueProperty.elements );

						}
M
Mr.doob 已提交
2412

2413
					}
M
Mr.doob 已提交
2414

2415
				}
M
Mr.doob 已提交
2416

2417
			} else if ( type === 'iv1' ) {
M
Mr.doob 已提交
2418

2419 2420
				// flat array of integers (JS or typed array)
				_gl.uniform1iv( location, value );
M
Mr.doob 已提交
2421

2422
			} else if ( type === 'iv' ) {
M
Mr.doob 已提交
2423

2424 2425
				// flat array of integers with 3 x N size (JS or typed array)
				_gl.uniform3iv( location, value );
M
Mr.doob 已提交
2426

2427
			} else if ( type === 'fv1' ) {
M
Mr.doob 已提交
2428

2429 2430
				// flat array of floats (JS or typed array)
				_gl.uniform1fv( location, value );
M
Mr.doob 已提交
2431

2432
			} else if ( type === 'fv' ) {
R
Ryan Tsao 已提交
2433

2434 2435
				// flat array of floats with 3 x N size (JS or typed array)
				_gl.uniform3fv( location, value );
R
Ryan Tsao 已提交
2436

2437
			} else if ( type === 'v2v' ) {
R
Ryan Tsao 已提交
2438

2439
				// array of THREE.Vector2
R
Ryan Tsao 已提交
2440

2441
				if ( uniform._array === undefined ) {
R
Ryan Tsao 已提交
2442

2443
					uniform._array = new Float32Array( 2 * value.length );
R
Ryan Tsao 已提交
2444

2445
				}
R
Ryan Tsao 已提交
2446

2447
				for ( var i = 0, i2 = 0, il = value.length; i < il; i ++, i2 += 2 ) {
R
Ryan Tsao 已提交
2448

2449 2450
					uniform._array[ i2 + 0 ] = value[ i ].x;
					uniform._array[ i2 + 1 ] = value[ i ].y;
R
Ryan Tsao 已提交
2451

2452
				}
M
Mr.doob 已提交
2453

2454
				_gl.uniform2fv( location, uniform._array );
M
Mr.doob 已提交
2455

2456
			} else if ( type === 'v3v' ) {
M
Mr.doob 已提交
2457

2458
				// array of THREE.Vector3
M
Mr.doob 已提交
2459

2460
				if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2461

2462
					uniform._array = new Float32Array( 3 * value.length );
M
Mr.doob 已提交
2463

2464
				}
2465

2466
				for ( var i = 0, i3 = 0, il = value.length; i < il; i ++, i3 += 3 ) {
2467

2468 2469 2470
					uniform._array[ i3 + 0 ] = value[ i ].x;
					uniform._array[ i3 + 1 ] = value[ i ].y;
					uniform._array[ i3 + 2 ] = value[ i ].z;
2471

2472
				}
M
Mr.doob 已提交
2473

2474
				_gl.uniform3fv( location, uniform._array );
M
Mr.doob 已提交
2475

2476
			} else if ( type === 'v4v' ) {
M
Mr.doob 已提交
2477

2478
				// array of THREE.Vector4
M
Mr.doob 已提交
2479

2480
				if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2481

2482
					uniform._array = new Float32Array( 4 * value.length );
M
Mr.doob 已提交
2483

2484
				}
M
Mr.doob 已提交
2485

2486
				for ( var i = 0, i4 = 0, il = value.length; i < il; i ++, i4 += 4 ) {
M
Mr.doob 已提交
2487

2488 2489 2490 2491
					uniform._array[ i4 + 0 ] = value[ i ].x;
					uniform._array[ i4 + 1 ] = value[ i ].y;
					uniform._array[ i4 + 2 ] = value[ i ].z;
					uniform._array[ i4 + 3 ] = value[ i ].w;
M
Mr.doob 已提交
2492

2493
				}
M
Mr.doob 已提交
2494

2495
				_gl.uniform4fv( location, uniform._array );
M
Mr.doob 已提交
2496

2497
			} else if ( type === 'm2' ) {
M
Mr.doob 已提交
2498

2499 2500
				// single THREE.Matrix2
				_gl.uniformMatrix2fv( location, false, value.elements );
M
Mr.doob 已提交
2501

2502
			} else if ( type === 'm3' ) {
M
Mr.doob 已提交
2503

2504 2505
				// single THREE.Matrix3
				_gl.uniformMatrix3fv( location, false, value.elements );
M
Mr.doob 已提交
2506

2507
			} else if ( type === 'm3v' ) {
M
Mr.doob 已提交
2508

2509
				// array of THREE.Matrix3
M
Mr.doob 已提交
2510

2511
				if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2512

2513
					uniform._array = new Float32Array( 9 * value.length );
M
Mr.doob 已提交
2514

2515
				}
M
Mr.doob 已提交
2516

2517
				for ( var i = 0, il = value.length; i < il; i ++ ) {
2518

2519
					value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
2520

2521
				}
2522

2523
				_gl.uniformMatrix3fv( location, false, uniform._array );
2524

2525
			} else if ( type === 'm4' ) {
2526

2527 2528
				// single THREE.Matrix4
				_gl.uniformMatrix4fv( location, false, value.elements );
M
Mr.doob 已提交
2529

2530
			} else if ( type === 'm4v' ) {
M
Mr.doob 已提交
2531

2532
				// array of THREE.Matrix4
M
Mr.doob 已提交
2533

2534
				if ( uniform._array === undefined ) {
2535

2536
					uniform._array = new Float32Array( 16 * value.length );
M
Mr.doob 已提交
2537

2538
				}
M
Mr.doob 已提交
2539

2540
				for ( var i = 0, il = value.length; i < il; i ++ ) {
G
gero3 已提交
2541

2542
					value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
M
Mr.doob 已提交
2543

2544
				}
M
Mr.doob 已提交
2545

2546
				_gl.uniformMatrix4fv( location, false, uniform._array );
2547

2548
			} else if ( type === 't' ) {
M
Mr.doob 已提交
2549

2550
				// single THREE.Texture (2d or cube)
M
Mr.doob 已提交
2551

2552 2553
				texture = value;
				textureUnit = getTextureUnit();
2554

2555
				_gl.uniform1i( location, textureUnit );
2556

2557
				if ( ! texture ) continue;
2558

2559 2560
				if ( texture instanceof THREE.CubeTexture ||
					 ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
2561

2562
					// CompressedTexture can have Array in image :/
2563

2564
					setCubeTexture( texture, textureUnit );
2565

2566
				} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
2567

2568
					setCubeTextureDynamic( texture.texture, textureUnit );
2569

2570
				} else if ( texture instanceof THREE.WebGLRenderTarget ) {
2571

2572
					_this.setTexture( texture.texture, textureUnit );
2573

2574
				} else {
2575

2576
					_this.setTexture( texture, textureUnit );
2577

2578
				}
2579

2580
			} else if ( type === 'tv' ) {
2581

2582
				// array of THREE.Texture (2d or cube)
2583

2584
				if ( uniform._array === undefined ) {
2585

2586
					uniform._array = [];
2587

2588
				}
M
Mr.doob 已提交
2589

2590
				for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
M
Mr.doob 已提交
2591

2592
					uniform._array[ i ] = getTextureUnit();
M
Mr.doob 已提交
2593

2594
				}
M
Mr.doob 已提交
2595

2596
				_gl.uniform1iv( location, uniform._array );
M
Mr.doob 已提交
2597

2598
				for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
M
Mr.doob 已提交
2599

2600 2601
					texture = uniform.value[ i ];
					textureUnit = uniform._array[ i ];
M
Mr.doob 已提交
2602

2603
					if ( ! texture ) continue;
M
Mr.doob 已提交
2604

2605 2606
					if ( texture instanceof THREE.CubeTexture ||
						 ( texture.image instanceof Array && texture.image.length === 6 ) ) {
M
Mr.doob 已提交
2607

2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
						// CompressedTexture can have Array in image :/

						setCubeTexture( texture, textureUnit );

					} else if ( texture instanceof THREE.WebGLRenderTarget ) {

						_this.setTexture( texture.texture, textureUnit );

					} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {

						setCubeTextureDynamic( texture.texture, textureUnit );

					} else {

						_this.setTexture( texture, textureUnit );
2623 2624 2625

					}

2626
				}
2627

2628
			} else {
2629

2630
				console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
2631

M
Mr.doob 已提交
2632 2633 2634 2635
			}

		}

M
Mr.doob 已提交
2636
	}
M
Mr.doob 已提交
2637

T
tschw 已提交
2638
	function setupLights ( lights, camera ) {
M
Mr.doob 已提交
2639

B
brason 已提交
2640
		var l, ll, light,
M
Mr.doob 已提交
2641
		r = 0, g = 0, b = 0,
B
Ben Houston 已提交
2642
		color,
B
brason 已提交
2643
		intensity,
M
Mr.doob 已提交
2644 2645
		distance,

2646
		viewMatrix = camera.matrixWorldInverse,
M
Mr.doob 已提交
2647

M
Mr.doob 已提交
2648
		directionalLength = 0,
M
Mr.doob 已提交
2649 2650
		pointLength = 0,
		spotLength = 0,
2651 2652 2653
		hemiLength = 0,

		shadowsLength = 0;
M
Mr.doob 已提交
2654

2655 2656
		_lights.shadowsPointLight = 0;

M
Mr.doob 已提交
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
		for ( l = 0, ll = lights.length; l < ll; l ++ ) {

			light = lights[ l ];

			color = light.color;
			intensity = light.intensity;
			distance = light.distance;

			if ( light instanceof THREE.AmbientLight ) {

2667 2668 2669
				r += color.r * intensity;
				g += color.g * intensity;
				b += color.b * intensity;
M
Mr.doob 已提交
2670 2671 2672

			} else if ( light instanceof THREE.DirectionalLight ) {

M
Mr.doob 已提交
2673
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2674

2675
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
M
Mr.doob 已提交
2676
				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
2677
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2678 2679 2680
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2681
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2682

2683
				if ( light.castShadow ) {
M
Mr.doob 已提交
2684

2685 2686 2687
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2688

2689
					_lights.shadows[ shadowsLength ++ ] = light;
2690

2691 2692
				}

2693 2694
				_lights.directionalShadowMap[ directionalLength ] = light.shadow.map;
				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
2695
				_lights.directional[ directionalLength ++ ] = uniforms;
M
Mr.doob 已提交
2696

M
Mr.doob 已提交
2697
			} else if ( light instanceof THREE.SpotLight ) {
M
Mr.doob 已提交
2698

M
Mr.doob 已提交
2699
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2700 2701 2702

				uniforms.position.setFromMatrixPosition( light.matrixWorld );
				uniforms.position.applyMatrix4( viewMatrix );
M
Mr.doob 已提交
2703

M
Mr.doob 已提交
2704 2705 2706 2707 2708 2709 2710 2711
				uniforms.color.copy( color ).multiplyScalar( intensity );
				uniforms.distance = distance;

				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2712 2713
				uniforms.coneCos = Math.cos( light.angle );
				uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
M
Mr.doob 已提交
2714
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2715

2716
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2717

2718 2719
				if ( light.castShadow ) {

2720 2721 2722
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2723

2724
					_lights.shadows[ shadowsLength ++ ] = light;
2725

2726 2727
				}

2728 2729
				_lights.spotShadowMap[ spotLength ] = light.shadow.map;
				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
M
Mr.doob 已提交
2730
				_lights.spot[ spotLength ++ ] = uniforms;
2731

M
Mr.doob 已提交
2732
			} else if ( light instanceof THREE.PointLight ) {
M
Mr.doob 已提交
2733

M
Mr.doob 已提交
2734
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2735

M
Mr.doob 已提交
2736 2737
				uniforms.position.setFromMatrixPosition( light.matrixWorld );
				uniforms.position.applyMatrix4( viewMatrix );
M
Mr.doob 已提交
2738

M
Mr.doob 已提交
2739 2740
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
				uniforms.distance = light.distance;
M
Mr.doob 已提交
2741 2742
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;

2743
				uniforms.shadow = light.castShadow;
2744

M
Mr.doob 已提交
2745
				if ( light.castShadow ) {
2746

2747 2748 2749 2750 2751
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;

					_lights.shadows[ shadowsLength ++ ] = light;
M
Mr.doob 已提交
2752

2753
				}
2754

2755
				_lights.pointShadowMap[ pointLength ] = light.shadow.map;
2756

2757 2758 2759
				if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) {

					_lights.pointShadowMatrix[ pointLength ] = new THREE.Matrix4();
2760

2761 2762
				}

2763 2764 2765 2766 2767
				// for point lights we set the shadow matrix to be a translation-only matrix
				// equal to inverse of the light's position
				_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
				_lights.pointShadowMatrix[ pointLength ].identity().setPosition( _vector3 );

M
Mr.doob 已提交
2768
				_lights.point[ pointLength ++ ] = uniforms;
2769

M
Mr.doob 已提交
2770 2771
			} else if ( light instanceof THREE.HemisphereLight ) {

M
Mr.doob 已提交
2772
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2773 2774 2775 2776

				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
				uniforms.direction.transformDirection( viewMatrix );
				uniforms.direction.normalize();
M
Mr.doob 已提交
2777

M
Mr.doob 已提交
2778 2779
				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
M
Mr.doob 已提交
2780

M
Mr.doob 已提交
2781
				_lights.hemi[ hemiLength ++ ] = uniforms;
M
Mr.doob 已提交
2782 2783 2784 2785 2786

			}

		}

M
Mr.doob 已提交
2787 2788 2789
		_lights.ambient[ 0 ] = r;
		_lights.ambient[ 1 ] = g;
		_lights.ambient[ 2 ] = b;
M
Mr.doob 已提交
2790

M
Mr.doob 已提交
2791 2792
		_lights.directional.length = directionalLength;
		_lights.spot.length = spotLength;
2793
		_lights.point.length = pointLength;
M
Mr.doob 已提交
2794
		_lights.hemi.length = hemiLength;
2795

2796 2797
		_lights.shadows.length = shadowsLength;

2798
		_lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + shadowsLength;
2799

M
Mr.doob 已提交
2800
	}
M
Mr.doob 已提交
2801 2802 2803 2804 2805 2806 2807

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

		if ( cullFace === THREE.CullFaceNone ) {

2808
			state.disable( _gl.CULL_FACE );
M
Mr.doob 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835

		} else {

			if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {

				_gl.frontFace( _gl.CW );

			} else {

				_gl.frontFace( _gl.CCW );

			}

			if ( cullFace === THREE.CullFaceBack ) {

				_gl.cullFace( _gl.BACK );

			} else if ( cullFace === THREE.CullFaceFront ) {

				_gl.cullFace( _gl.FRONT );

			} else {

				_gl.cullFace( _gl.FRONT_AND_BACK );

			}

2836
			state.enable( _gl.CULL_FACE );
M
Mr.doob 已提交
2837 2838 2839 2840 2841 2842 2843

		}

	};

	// Textures

M
Mr.doob 已提交
2844
	function setTextureParameters ( textureType, texture, isPowerOfTwoImage ) {
M
Mr.doob 已提交
2845

2846 2847
		var extension;

M
Mr.doob 已提交
2848
		if ( isPowerOfTwoImage ) {
M
Mr.doob 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859

			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );

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

		} else {

			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
M
Mr.doob 已提交
2860 2861 2862

			if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) {

M
Mr.doob 已提交
2863
				console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.', texture );
M
Mr.doob 已提交
2864

2865
			}
M
Mr.doob 已提交
2866 2867 2868 2869

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

M
Mr.doob 已提交
2870 2871
			if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) {

M
Mr.doob 已提交
2872
				console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.', texture );
M
Mr.doob 已提交
2873

2874
			}
M
Mr.doob 已提交
2875

M
Mr.doob 已提交
2876 2877
		}

2878 2879
		extension = extensions.get( 'EXT_texture_filter_anisotropic' );

2880
		if ( extension ) {
M
Mr.doob 已提交
2881

2882 2883
			if ( texture.type === THREE.FloatType && extensions.get( 'OES_texture_float_linear' ) === null ) return;
			if ( texture.type === THREE.HalfFloatType && extensions.get( 'OES_texture_half_float_linear' ) === null ) return;
M
Mr.doob 已提交
2884

2885
			if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
M
Mr.doob 已提交
2886

2887
				_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
2888
				properties.get( texture ).__currentAnisotropy = texture.anisotropy;
M
Mr.doob 已提交
2889 2890 2891 2892 2893

			}

		}

M
Mr.doob 已提交
2894
	}
M
Mr.doob 已提交
2895

2896
	function uploadTexture( textureProperties, texture, slot ) {
2897

2898
		if ( textureProperties.__webglInit === undefined ) {
2899

2900
			textureProperties.__webglInit = true;
2901 2902 2903

			texture.addEventListener( 'dispose', onTextureDispose );

2904
			textureProperties.__webglTexture = _gl.createTexture();
2905

2906
			_infoMemory.textures ++;
2907 2908

		}
M
Mr.doob 已提交
2909

B
Ben Adams 已提交
2910
		state.activeTexture( _gl.TEXTURE0 + slot );
2911
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
M
Mr.doob 已提交
2912

H
Henri Astre 已提交
2913 2914 2915 2916
		_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 );

2917
		var image = clampToMaxSize( texture.image, capabilities.maxTextureSize );
2918

2919
		if ( textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( image ) === false ) {
M
Mr.doob 已提交
2920

2921
			image = makePowerOfTwo( image );
M
Mr.doob 已提交
2922 2923 2924

		}

M
Mr.doob 已提交
2925
		var isPowerOfTwoImage = isPowerOfTwo( image ),
H
Henri Astre 已提交
2926 2927
		glFormat = paramThreeToGL( texture.format ),
		glType = paramThreeToGL( texture.type );
M
Mr.doob 已提交
2928

M
Mr.doob 已提交
2929
		setTextureParameters( _gl.TEXTURE_2D, texture, isPowerOfTwoImage );
M
Mr.doob 已提交
2930

H
Henri Astre 已提交
2931
		var mipmap, mipmaps = texture.mipmaps;
M
Mr.doob 已提交
2932

H
Henri Astre 已提交
2933
		if ( texture instanceof THREE.DataTexture ) {
M
Mr.doob 已提交
2934

H
Henri Astre 已提交
2935 2936 2937
			// 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
M
Mr.doob 已提交
2938

M
Mr.doob 已提交
2939
			if ( mipmaps.length > 0 && isPowerOfTwoImage ) {
H
Henri Astre 已提交
2940 2941

				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
M
Mr.doob 已提交
2942

H
Henri Astre 已提交
2943
					mipmap = mipmaps[ i ];
2944
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
M
Mr.doob 已提交
2945

H
Henri Astre 已提交
2946
				}
M
Mr.doob 已提交
2947

H
Henri Astre 已提交
2948
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2949

H
Henri Astre 已提交
2950
			} else {
M
Mr.doob 已提交
2951

2952
				state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
M
Mr.doob 已提交
2953

H
Henri Astre 已提交
2954
			}
M
Mr.doob 已提交
2955

H
Henri Astre 已提交
2956
		} else if ( texture instanceof THREE.CompressedTexture ) {
M
Mr.doob 已提交
2957

H
Henri Astre 已提交
2958
			for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
M
Mr.doob 已提交
2959

H
Henri Astre 已提交
2960
				mipmap = mipmaps[ i ];
M
Mr.doob 已提交
2961

2962
				if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
M
Mr.doob 已提交
2963

2964
					if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {
M
Mr.doob 已提交
2965

2966
						state.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
M
Mr.doob 已提交
2967

2968
					} else {
M
Mr.doob 已提交
2969

2970
						console.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()" );
M
Mr.doob 已提交
2971

2972
					}
M
Mr.doob 已提交
2973

H
Henri Astre 已提交
2974
				} else {
M
Mr.doob 已提交
2975

2976
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
M
Mr.doob 已提交
2977

M
Mr.doob 已提交
2978 2979
				}

H
Henri Astre 已提交
2980 2981
			}

G
gero3 已提交
2982 2983 2984
		} else {

			// regular Texture (image, video, canvas)
H
Henri Astre 已提交
2985 2986 2987 2988 2989

			// 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

M
Mr.doob 已提交
2990
			if ( mipmaps.length > 0 && isPowerOfTwoImage ) {
M
Mr.doob 已提交
2991

2992
				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
M
Mr.doob 已提交
2993 2994

					mipmap = mipmaps[ i ];
2995
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
M
Mr.doob 已提交
2996 2997 2998

				}

H
Henri Astre 已提交
2999
				texture.generateMipmaps = false;
M
Mr.doob 已提交
3000

H
Henri Astre 已提交
3001
			} else {
M
Mr.doob 已提交
3002

3003
				state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, image );
M
Mr.doob 已提交
3004

H
Henri Astre 已提交
3005
			}
M
Mr.doob 已提交
3006

H
Henri Astre 已提交
3007
		}
M
Mr.doob 已提交
3008

M
Mr.doob 已提交
3009
		if ( texture.generateMipmaps && isPowerOfTwoImage ) _gl.generateMipmap( _gl.TEXTURE_2D );
M
Mr.doob 已提交
3010

3011
		textureProperties.__version = texture.version;
M
Mr.doob 已提交
3012

B
Ben Adams 已提交
3013
		if ( texture.onUpdate ) texture.onUpdate( texture );
M
Mr.doob 已提交
3014

3015
	}
M
Mr.doob 已提交
3016

H
Henri Astre 已提交
3017
	this.setTexture = function ( texture, slot ) {
M
Mr.doob 已提交
3018

3019 3020 3021
		var textureProperties = properties.get( texture );

		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
M
Mr.doob 已提交
3022

3023
			var image = texture.image;
M
Mr.doob 已提交
3024

3025 3026
			if ( image === undefined ) {

3027
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined', texture );
3028 3029 3030 3031
				return;

			}

3032
			if ( image.complete === false ) {
M
Mr.doob 已提交
3033

3034
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
3035 3036 3037 3038
				return;

			}

3039
			uploadTexture( textureProperties, texture, slot );
3040

3041
			return;
M
Mr.doob 已提交
3042 3043 3044

		}

B
Ben Adams 已提交
3045
		state.activeTexture( _gl.TEXTURE0 + slot );
3046
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
3047

M
Mr.doob 已提交
3048 3049 3050 3051
	};

	function clampToMaxSize ( image, maxSize ) {

3052
		if ( image.width > maxSize || image.height > maxSize ) {
M
Mr.doob 已提交
3053

3054 3055
			// Warning: Scaling through the canvas will only work with images that use
			// premultiplied alpha.
M
Mr.doob 已提交
3056

3057
			var scale = maxSize / Math.max( image.width, image.height );
M
Mr.doob 已提交
3058

3059 3060 3061
			var canvas = document.createElement( 'canvas' );
			canvas.width = Math.floor( image.width * scale );
			canvas.height = Math.floor( image.height * scale );
M
Mr.doob 已提交
3062

3063 3064
			var context = canvas.getContext( '2d' );
			context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
M
Mr.doob 已提交
3065

3066
			console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
M
Mr.doob 已提交
3067

3068 3069 3070
			return canvas;

		}
M
Mr.doob 已提交
3071

3072
		return image;
M
Mr.doob 已提交
3073 3074 3075

	}

M
Mr.doob 已提交
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
	function isPowerOfTwo( image ) {

		return THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height );

	}

	function textureNeedsPowerOfTwo( texture ) {

		if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) return true;
		if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) return true;

		return false;

	}

	function makePowerOfTwo( image ) {

		if ( image instanceof HTMLImageElement || image instanceof HTMLCanvasElement ) {

			var canvas = document.createElement( 'canvas' );
			canvas.width = THREE.Math.nearestPowerOfTwo( image.width );
			canvas.height = THREE.Math.nearestPowerOfTwo( image.height );

			var context = canvas.getContext( '2d' );
			context.drawImage( image, 0, 0, canvas.width, canvas.height );

			console.warn( 'THREE.WebGLRenderer: image is not power of two (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );

			return canvas;

		}

		return image;

	}

M
Mr.doob 已提交
3112 3113
	function setCubeTexture ( texture, slot ) {

3114
		var textureProperties = properties.get( texture );
3115

M
Mr.doob 已提交
3116 3117
		if ( texture.image.length === 6 ) {

3118
			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
M
Mr.doob 已提交
3119

3120
				if ( ! textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
3121

3122 3123
					texture.addEventListener( 'dispose', onTextureDispose );

3124
					textureProperties.__image__webglTextureCube = _gl.createTexture();
M
Mr.doob 已提交
3125

3126
					_infoMemory.textures ++;
M
Mr.doob 已提交
3127 3128 3129

				}

B
Ben Adams 已提交
3130
				state.activeTexture( _gl.TEXTURE0 + slot );
3131
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3132 3133 3134

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

M
Mr.doob 已提交
3135 3136
				var isCompressed = texture instanceof THREE.CompressedTexture;
				var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
M
Mr.doob 已提交
3137 3138 3139 3140 3141

				var cubeImage = [];

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

3142
					if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
M
Mr.doob 已提交
3143

G
gero3 已提交
3144
						cubeImage[ i ] = clampToMaxSize( texture.image[ i ], capabilities.maxCubemapSize );
M
Mr.doob 已提交
3145 3146 3147

					} else {

3148
						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
M
Mr.doob 已提交
3149 3150 3151 3152 3153 3154

					}

				}

				var image = cubeImage[ 0 ],
M
Mr.doob 已提交
3155
				isPowerOfTwoImage = isPowerOfTwo( image ),
M
Mr.doob 已提交
3156 3157 3158
				glFormat = paramThreeToGL( texture.format ),
				glType = paramThreeToGL( texture.type );

M
Mr.doob 已提交
3159
				setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isPowerOfTwoImage );
M
Mr.doob 已提交
3160 3161 3162

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

3163
					if ( ! isCompressed ) {
M
Mr.doob 已提交
3164

M
Mr.doob 已提交
3165
						if ( isDataTexture ) {
3166

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

M
Mr.doob 已提交
3169
						} else {
3170

3171
							state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
3172

M
Mr.doob 已提交
3173 3174
						}

3175
					} else {
3176

M
Mr.doob 已提交
3177 3178
						var mipmap, mipmaps = cubeImage[ i ].mipmaps;

3179
						for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
M
Mr.doob 已提交
3180 3181

							mipmap = mipmaps[ j ];
M
Mr.doob 已提交
3182

3183
							if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
M
Mr.doob 已提交
3184

3185
								if ( state.getCompressedTextureFormats().indexOf( glFormat ) > - 1 ) {
M
Mr.doob 已提交
3186

3187
									state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
M
Mr.doob 已提交
3188

3189
								} else {
M
Mr.doob 已提交
3190

3191
									console.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setCubeTexture()" );
M
Mr.doob 已提交
3192

3193
								}
M
Mr.doob 已提交
3194

3195
							} else {
M
Mr.doob 已提交
3196

3197
								state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
M
Mr.doob 已提交
3198

3199
							}
M
Mr.doob 已提交
3200

3201
						}
M
Mr.doob 已提交
3202

M
Mr.doob 已提交
3203
					}
M
Mr.doob 已提交
3204

M
Mr.doob 已提交
3205 3206
				}

M
Mr.doob 已提交
3207
				if ( texture.generateMipmaps && isPowerOfTwoImage ) {
M
Mr.doob 已提交
3208 3209 3210 3211 3212

					_gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );

				}

3213
				textureProperties.__version = texture.version;
M
Mr.doob 已提交
3214

B
Ben Adams 已提交
3215
				if ( texture.onUpdate ) texture.onUpdate( texture );
M
Mr.doob 已提交
3216 3217 3218

			} else {

B
Ben Adams 已提交
3219
				state.activeTexture( _gl.TEXTURE0 + slot );
3220
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3221 3222 3223 3224 3225

			}

		}

M
Mr.doob 已提交
3226
	}
M
Mr.doob 已提交
3227 3228 3229

	function setCubeTextureDynamic ( texture, slot ) {

B
Ben Adams 已提交
3230
		state.activeTexture( _gl.TEXTURE0 + slot );
3231
		state.bindTexture( _gl.TEXTURE_CUBE_MAP, properties.get( texture ).__webglTexture );
M
Mr.doob 已提交
3232

M
Mr.doob 已提交
3233
	}
M
Mr.doob 已提交
3234 3235 3236

	// Render targets

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

3240 3241 3242
		var glFormat = paramThreeToGL( renderTarget.texture.format );
		var glType = paramThreeToGL( renderTarget.texture.type );
		state.texImage2D( textureTarget, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
M
Mr.doob 已提交
3243
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3244 3245
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
M
Mr.doob 已提交
3246

M
Mr.doob 已提交
3247
	}
M
Mr.doob 已提交
3248

3249 3250
	// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
	function setupRenderBufferStorage ( renderbuffer, renderTarget ) {
M
Mr.doob 已提交
3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265

		_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );

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

			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

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

			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

		} else {

3266
			// FIXME: We don't support !depth !stencil
M
Mr.doob 已提交
3267 3268 3269 3270
			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );

		}

3271
		_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
M
Mr.doob 已提交
3272

M
Mr.doob 已提交
3273
	}
M
Mr.doob 已提交
3274

3275
	// Setup GL resources for a non-texture depth buffer
M
Marius Kintel 已提交
3276
	function setupDepthRenderbuffer( renderTarget ) {
M
Mr.doob 已提交
3277

3278
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
3279 3280

		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
M
Mr.doob 已提交
3281

3282
		if ( isCube ) {
M
Mr.doob 已提交
3283

3284
			renderTargetProperties.__webglDepthbuffer = [];
M
Mr.doob 已提交
3285

3286
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3287

M
Marius Kintel 已提交
3288
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
3289 3290
				renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
				setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget );
M
Mr.doob 已提交
3291 3292

			}
B
Ben Adams 已提交
3293

M
Mr.doob 已提交
3294
		} else {
M
Mr.doob 已提交
3295

M
Marius Kintel 已提交
3296
			_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
3297 3298
			renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
			setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget );
M
Mr.doob 已提交
3299

3300
		}
M
Mr.doob 已提交
3301

M
Marius Kintel 已提交
3302
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
M
Mr.doob 已提交
3303

M
Mr.doob 已提交
3304
	}
M
Mr.doob 已提交
3305

3306
	// Set up GL resources for the render target
M
Marius Kintel 已提交
3307
	function setupRenderTarget( renderTarget ) {
M
Mr.doob 已提交
3308

3309 3310
		var renderTargetProperties = properties.get( renderTarget );
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
3311

3312
		renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
M
Mr.doob 已提交
3313

3314
		textureProperties.__webglTexture = _gl.createTexture();
M
Mr.doob 已提交
3315

3316
		_infoMemory.textures ++;
M
Mr.doob 已提交
3317

3318 3319
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
		var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height );
M
Mr.doob 已提交
3320

3321
		// Setup framebuffer
M
Mr.doob 已提交
3322

3323
		if ( isCube ) {
M
Mr.doob 已提交
3324

3325
			renderTargetProperties.__webglFramebuffer = [];
M
Mr.doob 已提交
3326

3327
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3328

3329
				renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
M
Mr.doob 已提交
3330

3331
			}
M
Mr.doob 已提交
3332

3333
		} else {
M
Mr.doob 已提交
3334

3335
			renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
M
Mr.doob 已提交
3336

3337
		}
M
Mr.doob 已提交
3338

3339
		// Setup color buffer
M
Mr.doob 已提交
3340

3341
		if ( isCube ) {
M
Mr.doob 已提交
3342

3343 3344
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
			setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, isTargetPowerOfTwo );
M
Mr.doob 已提交
3345

3346
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3347

3348
				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
M
Mr.doob 已提交
3349 3350 3351

			}

3352 3353
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
M
Mr.doob 已提交
3354

3355
		} else {
M
Mr.doob 已提交
3356

3357 3358 3359
			state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
			setTextureParameters( _gl.TEXTURE_2D, renderTarget.texture, isTargetPowerOfTwo );
			setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D );
M
Mr.doob 已提交
3360

3361 3362
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
			state.bindTexture( _gl.TEXTURE_2D, null );
M
Mr.doob 已提交
3363

3364
		}
M
Mr.doob 已提交
3365

3366
		// Setup depth and stencil buffers
3367

3368
		if ( renderTarget.depthBuffer ) {
M
Mr.doob 已提交
3369

M
Marius Kintel 已提交
3370
			setupDepthRenderbuffer( renderTarget );
M
Mr.doob 已提交
3371

3372
		}
M
Mr.doob 已提交
3373

3374
	}
M
Mr.doob 已提交
3375

3376 3377 3378 3379 3380 3381
	this.getCurrentRenderTarget = function() {

		return _currentRenderTarget;

	}

3382
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
3383

3384 3385
		_currentRenderTarget = renderTarget;

3386
		if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
M
Mr.doob 已提交
3387

M
Marius Kintel 已提交
3388
			setupRenderTarget( renderTarget );
M
Mr.doob 已提交
3389 3390 3391

		}

3392
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
3393
		var framebuffer;
M
Mr.doob 已提交
3394 3395 3396

		if ( renderTarget ) {

3397
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
3398

M
Mr.doob 已提交
3399 3400
			if ( isCube ) {

3401
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
3402 3403 3404

			} else {

3405
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
3406 3407 3408

			}

3409 3410
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
3411

3412
			_currentViewport.copy( renderTarget.viewport );
M
Mr.doob 已提交
3413 3414 3415 3416 3417

		} else {

			framebuffer = null;

3418
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
3419
			_currentScissorTest = _scissorTest;
3420

3421
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
M
Mr.doob 已提交
3422 3423 3424

		}

M
Mr.doob 已提交
3425
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
3426 3427 3428 3429 3430 3431

			_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
			_currentFramebuffer = framebuffer;

		}

3432 3433
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
3434

3435
		state.viewport( _currentViewport );
3436

M
Mr.doob 已提交
3437 3438 3439 3440 3441 3442 3443
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, 0 );

		}

M
Mr.doob 已提交
3444 3445
	};

M
Mr.doob 已提交
3446
	this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer ) {
3447

M
Mr.doob 已提交
3448
		if ( renderTarget instanceof THREE.WebGLRenderTarget === false ) {
3449

3450
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
3451
			return;
3452

G
gero3 已提交
3453
		}
3454

M
Mr.doob 已提交
3455
		var framebuffer = properties.get( renderTarget ).__webglFramebuffer;
3456

M
Mr.doob 已提交
3457
		if ( framebuffer ) {
3458

G
gero3 已提交
3459
			var restore = false;
3460

M
Mr.doob 已提交
3461
			if ( framebuffer !== _currentFramebuffer ) {
3462

M
Mr.doob 已提交
3463
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3464

G
gero3 已提交
3465
				restore = true;
3466

G
gero3 已提交
3467
			}
3468

M
Mr.doob 已提交
3469
			try {
3470

M
Mr.doob 已提交
3471
				var texture = renderTarget.texture;
3472

M
Mr.doob 已提交
3473 3474
				if ( texture.format !== THREE.RGBAFormat
					&& paramThreeToGL( texture.format ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
3475

M
Mr.doob 已提交
3476 3477
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
					return;
3478

M
Mr.doob 已提交
3479
				}
3480

M
Mr.doob 已提交
3481 3482 3483 3484
				if ( texture.type !== THREE.UnsignedByteType
					&& paramThreeToGL( texture.type ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_TYPE )
					&& ! ( texture.type === THREE.FloatType && extensions.get( 'WEBGL_color_buffer_float' ) )
					&& ! ( texture.type === THREE.HalfFloatType && extensions.get( 'EXT_color_buffer_half_float' ) ) ) {
3485

M
Mr.doob 已提交
3486 3487
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
					return;
3488

M
Mr.doob 已提交
3489
				}
3490

M
Mr.doob 已提交
3491
				if ( _gl.checkFramebufferStatus( _gl.FRAMEBUFFER ) === _gl.FRAMEBUFFER_COMPLETE ) {
3492

M
Mr.doob 已提交
3493
					_gl.readPixels( x, y, width, height, paramThreeToGL( texture.format ), paramThreeToGL( texture.type ), buffer );
3494

M
Mr.doob 已提交
3495
				} else {
M
Mr.doob 已提交
3496

M
Mr.doob 已提交
3497 3498 3499
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );

				}
M
Mr.doob 已提交
3500

M
Mr.doob 已提交
3501
			} finally {
M
Mr.doob 已提交
3502

M
Mr.doob 已提交
3503 3504 3505
				if ( restore ) {

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, _currentFramebuffer );
M
Mr.doob 已提交
3506

M
Mr.doob 已提交
3507 3508 3509
				}

			}
M
Mr.doob 已提交
3510 3511 3512

		}

M
Mr.doob 已提交
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
	};

	function updateRenderTargetMipmap( renderTarget ) {

		var target = renderTarget instanceof THREE.WebGLRenderTargetCube ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
		var texture = properties.get( renderTarget.texture ).__webglTexture;

		state.bindTexture( target, texture );
		_gl.generateMipmap( target );
		state.bindTexture( target, null );

M
Mr.doob 已提交
3524
	}
M
Mr.doob 已提交
3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537

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

	function filterFallback ( f ) {

		if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {

			return _gl.NEAREST;

		}

		return _gl.LINEAR;

M
Mr.doob 已提交
3538
	}
M
Mr.doob 已提交
3539 3540 3541 3542 3543

	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

3544 3545
		var extension;

M
Mr.doob 已提交
3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
		if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
		if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
		if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;

		if ( p === THREE.NearestFilter ) return _gl.NEAREST;
		if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
		if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;

		if ( p === THREE.LinearFilter ) return _gl.LINEAR;
		if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
		if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;

		if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
		if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
		if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
		if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;

		if ( p === THREE.ByteType ) return _gl.BYTE;
		if ( p === THREE.ShortType ) return _gl.SHORT;
		if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
		if ( p === THREE.IntType ) return _gl.INT;
		if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
		if ( p === THREE.FloatType ) return _gl.FLOAT;

3570 3571 3572 3573 3574 3575 3576 3577
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

			if ( p === THREE.HalfFloatType ) return extension.HALF_FLOAT_OES;

		}

M
Mr.doob 已提交
3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600
		if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
		if ( p === THREE.RGBFormat ) return _gl.RGB;
		if ( p === THREE.RGBAFormat ) return _gl.RGBA;
		if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
		if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;

		if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
		if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
		if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;

		if ( p === THREE.ZeroFactor ) return _gl.ZERO;
		if ( p === THREE.OneFactor ) return _gl.ONE;
		if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
		if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
		if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
		if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
		if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
		if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;

		if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
		if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
		if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;

3601
		extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
M
Mr.doob 已提交
3602

3603 3604 3605 3606 3607 3608
		if ( extension !== null ) {

			if ( p === THREE.RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
			if ( p === THREE.RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
			if ( p === THREE.RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
			if ( p === THREE.RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
M
Mr.doob 已提交
3609 3610 3611

		}

3612 3613 3614
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

		if ( extension !== null ) {
P
Pierre Lepers 已提交
3615

3616 3617 3618 3619
			if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
			if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
			if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
			if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
P
Pierre Lepers 已提交
3620 3621 3622

		}

3623 3624 3625 3626 3627 3628 3629 3630
		extension = extensions.get( 'WEBGL_compressed_texture_etc1' );

		if ( extension !== null ) {

			if ( p === THREE.RGB_ETC1_Format ) return extension.COMPRESSED_RGB_ETC1_WEBGL;

		}

3631 3632 3633
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
3634

3635 3636
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
3637 3638 3639

		}

M
Mr.doob 已提交
3640 3641
		return 0;

M
Mr.doob 已提交
3642
	}
M
Mr.doob 已提交
3643 3644

};