WebGLRenderer.js 84.3 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	this.gammaInput = false;
	this.gammaOutput = false;

	// morphs

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

	// flags

	this.autoScaleCubemaps = true;

	// internal properties

	var _this = this,

	// internal state cache

	_currentProgram = null,
74
	_currentRenderTarget = null,
M
Mr.doob 已提交
75
	_currentFramebuffer = null,
76
	_currentMaterialId = - 1,
77
	_currentGeometryProgram = '',
M
Mr.doob 已提交
78 79
	_currentCamera = null,

80 81 82 83 84
	_currentScissor = new THREE.Vector4(),
	_currentScissorTest = null,

	_currentViewport = new THREE.Vector4(),

85 86
	//

M
Mr.doob 已提交
87 88
	_usedTextureUnits = 0,

M
Mr.doob 已提交
89 90 91 92 93 94 95 96 97 98 99
	//

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

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

	_pixelRatio = 1,

	_scissor = new THREE.Vector4( 0, 0, _width, _height ),
100 101
	_scissorTest = false,

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

M
Mr.doob 已提交
104 105 106 107
	// frustum

	_frustum = new THREE.Frustum(),

M
Mr.doob 已提交
108
	// camera matrices cache
M
Mr.doob 已提交
109 110 111 112 113 114 115 116 117

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_lights = {

118 119
		hash: '',

M
Mr.doob 已提交
120
		ambient: [ 0, 0, 0 ],
M
Mr.doob 已提交
121
		directional: [],
122 123
		directionalShadowMap: [],
		directionalShadowMatrix: [],
M
Mr.doob 已提交
124
		spot: [],
125 126
		spotShadowMap: [],
		spotShadowMatrix: [],
M
Mr.doob 已提交
127
		point: [],
128 129
		pointShadowMap: [],
		pointShadowMatrix: [],
130 131
		hemi: [],

132
		shadows: [],
133
		shadowsPointLight: 0
M
Mr.doob 已提交
134

135 136
	},

M
Mr.doob 已提交
137 138
	// info

139
	_infoMemory = {
140 141

		geometries: 0,
T
tschw 已提交
142
		textures: 0
143 144 145

	},

146
	_infoRender = {
147 148 149 150 151 152

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

M
Mr.doob 已提交
153 154
	};

M
Mr.doob 已提交
155
	this.info = {
156

M
Mr.doob 已提交
157 158
		render: _infoRender,
		memory: _infoMemory,
159
		programs: null
M
Mr.doob 已提交
160 161

	};
162

163

M
Mr.doob 已提交
164 165 166 167
	// initialize

	var _gl;

M
Mr.doob 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	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 已提交
183
			if ( _canvas.getContext( 'webgl' ) !== null ) {
184 185 186 187 188 189 190 191

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

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
192 193 194

		}

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

M
Mr.doob 已提交
197 198
	} catch ( error ) {

199
		console.error( 'THREE.WebGLRenderer: ' + error );
M
Mr.doob 已提交
200 201 202

	}

203 204
	var extensions = new THREE.WebGLExtensions( _gl );

205 206
	extensions.get( 'OES_texture_float' );
	extensions.get( 'OES_texture_float_linear' );
207 208
	extensions.get( 'OES_texture_half_float' );
	extensions.get( 'OES_texture_half_float_linear' );
209
	extensions.get( 'OES_standard_derivatives' );
B
Ben Adams 已提交
210
	extensions.get( 'ANGLE_instanced_arrays' );
211

212 213
	if ( extensions.get( 'OES_element_index_uint' ) ) {

214
		THREE.BufferGeometry.MaxIndex = 4294967296;
215 216 217

	}

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

220 221 222
	var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
	var properties = new THREE.WebGLProperties();
	var objects = new THREE.WebGLObjects( _gl, properties, this.info );
G
gero3 已提交
223
	var programCache = new THREE.WebGLPrograms( this, capabilities );
M
Mr.doob 已提交
224
	var lightCache = new THREE.WebGLLights();
225

226 227
	this.info.programs = programCache.programs;

228 229
	var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
	var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
230

M
Mr.doob 已提交
231 232
	//

233 234
	function getTargetPixelRatio() {

235
		return _currentRenderTarget === null ? _pixelRatio : 1;
236 237 238

	}

239
	function glClearColor( r, g, b, a ) {
240 241 242

		if ( _premultipliedAlpha === true ) {

243
			r *= a; g *= a; b *= a;
244 245 246

		}

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

249
	}
250

251
	function setDefaultGLState() {
M
Mr.doob 已提交
252

M
Mr.doob 已提交
253
		state.init();
M
Mr.doob 已提交
254

255 256
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
257

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

260
	}
261

262
	function resetGLState() {
263 264 265 266

		_currentProgram = null;
		_currentCamera = null;

267
		_currentGeometryProgram = '';
268 269
		_currentMaterialId = - 1;

M
Mr.doob 已提交
270 271
		state.reset();

272
	}
M
Mr.doob 已提交
273 274 275 276

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
277
	this.capabilities = capabilities;
278
	this.extensions = extensions;
279
	this.properties = properties;
M
Mr.doob 已提交
280
	this.state = state;
M
Mr.doob 已提交
281

M
Mr.doob 已提交
282 283
	// shadow map

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

286
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
287

M
Mr.doob 已提交
288

M
Mr.doob 已提交
289 290 291 292 293
	// Plugins

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

M
Mr.doob 已提交
294 295 296 297 298 299 300 301
	// API

	this.getContext = function () {

		return _gl;

	};

302 303 304 305 306 307
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

308 309 310 311 312 313
	this.forceContextLoss = function () {

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

	};

314
	this.getMaxAnisotropy = ( function () {
M
Mr.doob 已提交
315

316
		var value;
M
Mr.doob 已提交
317

318
		return function getMaxAnisotropy() {
319

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

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

M
Mr.doob 已提交
324
			if ( extension !== null ) {
325

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

M
Mr.doob 已提交
328 329 330 331 332
			} else {

				value = 0;

			}
333 334 335

			return value;

M
Mr.doob 已提交
336
		};
337 338

	} )();
M
Mr.doob 已提交
339 340 341

	this.getPrecision = function () {

G
gero3 已提交
342
		return capabilities.precision;
M
Mr.doob 已提交
343 344 345

	};

346 347
	this.getPixelRatio = function () {

348
		return _pixelRatio;
349 350 351 352 353

	};

	this.setPixelRatio = function ( value ) {

354 355 356 357 358
		if ( value === undefined ) return;

		_pixelRatio = value;

		this.setSize( _viewport.z, _viewport.w, false );
359 360 361

	};

362 363 364
	this.getSize = function () {

		return {
365 366
			width: _width,
			height: _height
367 368 369 370
		};

	};

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

373 374 375
		_width = width;
		_height = height;

376 377
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
378

379
		if ( updateStyle !== false ) {
380

G
gero3 已提交
381 382
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
383

G
gero3 已提交
384
		}
M
Mr.doob 已提交
385

386
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
387 388 389 390 391

	};

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

392
		state.viewport( _viewport.set( x, y, width, height ) );
M
Mr.doob 已提交
393 394 395

	};

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

398
		state.scissor( _scissor.set( x, y, width, height ) );
M
Mr.doob 已提交
399 400 401

	};

402 403
	this.setScissorTest = function ( boolean ) {

404
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
405 406 407 408 409

	};

	// Clearing

M
Mr.doob 已提交
410
	this.getClearColor = function () {
M
Mr.doob 已提交
411

M
Mr.doob 已提交
412
		return _clearColor;
M
Mr.doob 已提交
413 414 415

	};

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

M
Mr.doob 已提交
418
		_clearColor.set( color );
419

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

422
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
423 424 425

	};

M
Mr.doob 已提交
426
	this.getClearAlpha = function () {
M
Mr.doob 已提交
427

M
Mr.doob 已提交
428
		return _clearAlpha;
M
Mr.doob 已提交
429 430 431

	};

M
Mr.doob 已提交
432
	this.setClearAlpha = function ( alpha ) {
M
Mr.doob 已提交
433

M
Mr.doob 已提交
434
		_clearAlpha = alpha;
M
Mr.doob 已提交
435

436
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
437 438 439 440 441 442 443 444 445 446 447 448

	};

	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 );
449 450 451 452 453

	};

	this.clearColor = function () {

M
Mr.doob 已提交
454
		this.clear( true, false, false );
455 456 457 458 459

	};

	this.clearDepth = function () {

M
Mr.doob 已提交
460
		this.clear( false, true, false );
461 462 463 464 465

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
466
		this.clear( false, false, true );
M
Mr.doob 已提交
467 468 469 470 471 472 473 474 475 476

	};

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

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

	};

M
Mr.doob 已提交
477 478
	// Reset

479
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
480

D
dubejf 已提交
481 482 483 484 485 486
	this.dispose = function() {

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

	};

M
Mr.doob 已提交
487
	// Events
M
Mr.doob 已提交
488

D
dubejf 已提交
489 490 491 492 493 494 495 496 497
	function onContextLost( event ) {

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

M
Mr.doob 已提交
498
	}
D
dubejf 已提交
499

500
	function onTextureDispose( event ) {
M
Mr.doob 已提交
501 502 503 504 505 506 507

		var texture = event.target;

		texture.removeEventListener( 'dispose', onTextureDispose );

		deallocateTexture( texture );

508
		_infoMemory.textures --;
M
Mr.doob 已提交
509 510


511
	}
M
Mr.doob 已提交
512

513
	function onRenderTargetDispose( event ) {
M
Mr.doob 已提交
514 515 516 517 518 519 520

		var renderTarget = event.target;

		renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );

		deallocateRenderTarget( renderTarget );

521
		_infoMemory.textures --;
M
Mr.doob 已提交
522

523
	}
M
Mr.doob 已提交
524

525
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
526 527 528 529 530 531 532

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

533
	}
M
Mr.doob 已提交
534 535 536

	// Buffer deallocation

537
	function deallocateTexture( texture ) {
M
Mr.doob 已提交
538

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

541
		if ( texture.image && textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
542 543 544

			// cube texture

545
			_gl.deleteTexture( textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
546

547 548 549 550
		} else {

			// 2D texture

551
			if ( textureProperties.__webglInit === undefined ) return;
552

553
			_gl.deleteTexture( textureProperties.__webglTexture );
554

M
Mr.doob 已提交
555 556
		}

557
		// remove all webgl properties
558
		properties.delete( texture );
559

560
	}
M
Mr.doob 已提交
561

562
	function deallocateRenderTarget( renderTarget ) {
M
Mr.doob 已提交
563

564
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
565
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
566

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

M
Mr.doob 已提交
569
		_gl.deleteTexture( textureProperties.__webglTexture );
M
Mr.doob 已提交
570

M
Mr.doob 已提交
571 572 573 574
		if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {

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

575
				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
576
				_gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );
M
Mr.doob 已提交
577 578 579 580 581

			}

		} else {

582
			_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
583
			_gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );
M
Mr.doob 已提交
584 585 586

		}

M
Mr.doob 已提交
587
		properties.delete( renderTarget.texture );
D
Daosheng Mu 已提交
588
		properties.delete( renderTarget );
M
Mr.doob 已提交
589

590
	}
M
Mr.doob 已提交
591

592
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
593

594 595 596 597
		releaseMaterialProgramReference( material );

		properties.delete( material );

598
	}
599 600


601
	function releaseMaterialProgramReference( material ) {
602

603
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
604 605 606

		material.program = undefined;

607
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
608

609
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
610

M
Mr.doob 已提交
611 612
		}

613
	}
M
Mr.doob 已提交
614 615 616 617 618

	// Buffer rendering

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

619
		state.initAttributes();
620

621
		var buffers = properties.get( object );
622

623 624 625 626
		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 已提交
627

628
		var attributes = program.getAttributes();
629

M
Mr.doob 已提交
630 631
		if ( object.hasPositions ) {

632
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
633
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
634

635 636
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
637 638 639 640 641

		}

		if ( object.hasNormals ) {

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

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

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

648
					var array = object.normalArray;
M
Mr.doob 已提交
649

650 651 652
					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 已提交
653

654 655 656
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
657

658 659 660
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
661

662 663 664
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
665 666 667 668 669 670

				}

			}

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

672
			state.enableAttribute( attributes.normal );
673

674
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
675 676 677 678 679

		}

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

680
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
681
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
682

683
			state.enableAttribute( attributes.uv );
684

685
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
686 687 688 689 690

		}

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

691
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
692
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
693

694
			state.enableAttribute( attributes.color );
695

696
			_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
697 698 699

		}

700
		state.disableUnusedAttributes();
701

M
Mr.doob 已提交
702 703 704 705 706 707
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

708
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
709

M
Mr.doob 已提交
710 711
		setMaterial( material );

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

M
Mr.doob 已提交
714 715
		var updateBuffers = false;
		var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
M
Mr.doob 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

		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 ] );

			}

739
			activeInfluences.sort( absNumericalSort );
M
Mr.doob 已提交
740 741 742 743 744 745 746

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

747 748
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
749 750 751 752 753 754 755
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

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

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

756
					var index = influence[ 1 ];
M
Mr.doob 已提交
757

758 759
					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 已提交
760 761 762

				} else {

763 764
					if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
					if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
M
Mr.doob 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

				}

			}

			var uniforms = program.getUniforms();

			if ( uniforms.morphTargetInfluences !== null ) {

				_gl.uniform1fv( uniforms.morphTargetInfluences, morphInfluences );

			}

			updateBuffers = true;

		}

M
Mr.doob 已提交
782 783
		//

784
		var index = geometry.index;
785 786
		var position = geometry.attributes.position;

787 788
		if ( material.wireframe === true ) {

789
			index = objects.getWireframeAttribute( geometry );
790 791 792

		}

793 794
		var renderer;

795
		if ( index !== null ) {
796

797 798
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
799

800
		} else {
801

802
			renderer = bufferRenderer;
803

804
		}
M
Mr.doob 已提交
805

806
		if ( updateBuffers ) {
M
Mr.doob 已提交
807

808
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
809

810
			if ( index !== null ) {
811

812
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
813 814 815

			}

816
		}
817

M
Mr.doob 已提交
818
		//
819

M
Mr.doob 已提交
820 821
		var dataStart = 0;
		var dataCount = Infinity;
822

M
Mr.doob 已提交
823
		if ( index !== null ) {
824

M
Mr.doob 已提交
825
			dataCount = index.count;
826

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

M
Mr.doob 已提交
829
			dataCount = position.count;
830

M
Mr.doob 已提交
831
		}
832

M
Mr.doob 已提交
833 834
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
835

M
Mr.doob 已提交
836 837
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
838

M
Mr.doob 已提交
839 840 841 842 843 844
		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 );

		//
845

846
		if ( object instanceof THREE.Mesh ) {
847

848
			if ( material.wireframe === true ) {
849

850
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
851
				renderer.setMode( _gl.LINES );
852

853
			} else {
M
Mr.doob 已提交
854 855

				switch ( object.drawMode ) {
856

B
Ben Adams 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869
					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;

				}
870

871
			}
872

873

874
		} else if ( object instanceof THREE.Line ) {
875

876
			var lineWidth = material.linewidth;
877

878
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
879

880
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
881

882
			if ( object instanceof THREE.LineSegments ) {
883

884
				renderer.setMode( _gl.LINES );
885

886
			} else {
887

888
				renderer.setMode( _gl.LINE_STRIP );
889 890

			}
M
Mr.doob 已提交
891

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

			renderer.setMode( _gl.POINTS );
895 896

		}
897

898 899 900 901 902 903
		if ( geometry instanceof THREE.InstancedBufferGeometry && geometry.maxInstancedCount > 0 ) {

			renderer.renderInstances( geometry, drawStart, drawCount );

		} else {

M
Mr.doob 已提交
904
			renderer.render( drawStart, drawCount );
905

M
Mr.doob 已提交
906 907 908 909
		}

	};

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

M
Mr.doob 已提交
912
		var extension;
B
Ben Adams 已提交
913

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

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

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

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

M
Mr.doob 已提交
923 924 925
			}

		}
B
Ben Adams 已提交
926

927 928
		if ( startIndex === undefined ) startIndex = 0;

929 930
		state.initAttributes();

931
		var geometryAttributes = geometry.attributes;
932

933
		var programAttributes = program.getAttributes();
934

935
		var materialDefaultAttributeValues = material.defaultAttributeValues;
936

937
		for ( var name in programAttributes ) {
938

939
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
940

M
Mr.doob 已提交
941
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
942

943
				var geometryAttribute = geometryAttributes[ name ];
944

M
Mr.doob 已提交
945
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
946

947
					var size = geometryAttribute.itemSize;
G
gero3 已提交
948
					var buffer = objects.getAttributeBuffer( geometryAttribute );
949

B
Ben Adams 已提交
950
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
951

M
Mr.doob 已提交
952 953 954 955 956
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
960
							if ( geometry.maxInstancedCount === undefined ) {
961

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

M
Mr.doob 已提交
964
							}
B
Ben Adams 已提交
965

M
Mr.doob 已提交
966
						} else {
B
Ben Adams 已提交
967

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

M
Mr.doob 已提交
970
						}
B
Ben Adams 已提交
971

M
Mr.doob 已提交
972 973
						_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 已提交
974

M
Mr.doob 已提交
975
					} else {
B
Ben Adams 已提交
976

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

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

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

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

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

M
Mr.doob 已提交
987 988 989 990
						} else {

							state.enableAttribute( programAttribute );

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

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

B
Ben Adams 已提交
996
					}
M
Mr.doob 已提交
997

998 999
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
1000
					var value = materialDefaultAttributeValues[ name ];
1001

1002
					if ( value !== undefined ) {
M
Mr.doob 已提交
1003

1004
						switch ( value.length ) {
M
Mr.doob 已提交
1005

1006 1007 1008
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1009

1010 1011 1012
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1013

1014 1015 1016
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
1017

1018 1019
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
1020 1021

						}
M
Mr.doob 已提交
1022 1023 1024 1025 1026 1027 1028 1029

					}

				}

			}

		}
1030

1031
		state.disableUnusedAttributes();
1032

M
Mr.doob 已提交
1033 1034
	}

M
Mr.doob 已提交
1035 1036
	// Sorting

1037
	function absNumericalSort( a, b ) {
1038

1039
		return Math.abs( b[ 0 ] ) - Math.abs( a[ 0 ] );
1040 1041 1042

	}

M
Mr.doob 已提交
1043 1044
	function painterSortStable ( a, b ) {

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

U
unconed 已提交
1047
			return a.object.renderOrder - b.object.renderOrder;
1048

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

M
Mr.doob 已提交
1051
			return a.material.id - b.material.id;
1052 1053

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

M
Mr.doob 已提交
1055
			return a.z - b.z;
M
Mr.doob 已提交
1056 1057 1058

		} else {

1059
			return a.id - b.id;
M
Mr.doob 已提交
1060 1061 1062

		}

1063
	}
M
Mr.doob 已提交
1064

1065 1066
	function reversePainterSortStable ( a, b ) {

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

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

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

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

		} else {

			return a.id - b.id;

		}

1081
	}
1082

M
Mr.doob 已提交
1083 1084 1085 1086 1087 1088
	// Rendering

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

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

1089
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1090 1091 1092 1093
			return;

		}

M
Mr.doob 已提交
1094
		var fog = scene.fog;
M
Mr.doob 已提交
1095 1096 1097

		// reset caching for this frame

1098
		_currentGeometryProgram = '';
1099
		_currentMaterialId = - 1;
1100
		_currentCamera = null;
M
Mr.doob 已提交
1101 1102 1103

		// update scene graph

1104
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1105 1106 1107

		// update camera matrices and frustum

1108
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1109 1110 1111 1112 1113 1114

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

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

M
Mr.doob 已提交
1115
		lights.length = 0;
1116

M
Mr.doob 已提交
1117 1118
		opaqueObjectsLastIndex = - 1;
		transparentObjectsLastIndex = - 1;
1119

M
Mr.doob 已提交
1120 1121 1122
		sprites.length = 0;
		lensFlares.length = 0;

1123
		projectObject( scene, camera );
M
Mr.doob 已提交
1124

1125 1126 1127
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1128
		if ( _this.sortObjects === true ) {
1129 1130 1131

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

1133 1134
		}

1135 1136
		setupLights( lights, camera );

M
Mr.doob 已提交
1137
		//
M
Mr.doob 已提交
1138

M
Mr.doob 已提交
1139
		shadowMap.render( scene, camera );
M
Mr.doob 已提交
1140 1141 1142

		//

1143 1144 1145 1146
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1147

1148 1149 1150 1151 1152 1153
		if ( renderTarget === undefined ) {

			renderTarget = null;

		}

M
Mr.doob 已提交
1154 1155 1156 1157 1158 1159 1160 1161
		this.setRenderTarget( renderTarget );

		if ( this.autoClear || forceClear ) {

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

		}

1162
		//
M
Mr.doob 已提交
1163 1164 1165

		if ( scene.overrideMaterial ) {

1166
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1167

1168 1169
			renderObjects( opaqueObjects, camera, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, fog, overrideMaterial );
1170

M
Mr.doob 已提交
1171 1172 1173 1174
		} else {

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

M
Mr.doob 已提交
1175
			state.setBlending( THREE.NoBlending );
1176
			renderObjects( opaqueObjects, camera, fog );
M
Mr.doob 已提交
1177 1178 1179

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

1180
			renderObjects( transparentObjects, camera, fog );
M
Mr.doob 已提交
1181 1182 1183 1184 1185

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1186
		spritePlugin.render( scene, camera );
1187
		lensFlarePlugin.render( scene, camera, _currentViewport );
M
Mr.doob 已提交
1188 1189 1190

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

M
Mr.doob 已提交
1191 1192 1193
		if ( renderTarget ) {

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

M
Mr.doob 已提交
1195 1196 1197 1198 1199
			if ( texture.generateMipmaps && isPowerOfTwo( renderTarget ) &&
					texture.minFilter !== THREE.NearestFilter &&
					texture.minFilter !== THREE.LinearFilter ) {

				updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1200 1201

			}
M
Mr.doob 已提交
1202 1203 1204

		}

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

M
Mr.doob 已提交
1207 1208
		state.setDepthTest( true );
		state.setDepthWrite( true );
1209
		state.setColorWrite( true );
M
Mr.doob 已提交
1210 1211 1212 1213

		// _gl.finish();

	};
M
Mr.doob 已提交
1214

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

1217
		var array, index;
M
Mr.doob 已提交
1218

1219
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1220 1221 1222

		if ( material.transparent ) {

1223 1224
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1225 1226 1227

		} else {

1228 1229 1230 1231 1232
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1233 1234
		// recycle existing render item or grow the array

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		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 已提交
1245 1246 1247

		} else {

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
			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 已提交
1259 1260 1261 1262 1263

		}

	}

1264
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1265

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

1268
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1269

1270
			if ( object instanceof THREE.Light ) {
M
Mr.doob 已提交
1271

1272
				lights.push( object );
M
Mr.doob 已提交
1273

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

1276 1277 1278 1279 1280
				if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {

					sprites.push( object );

				}
M
Mr.doob 已提交
1281

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

1284
				lensFlares.push( object );
M
Mr.doob 已提交
1285

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

1288
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1289

1290 1291
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1292

1293
				}
1294

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

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

1299
				if ( object instanceof THREE.SkinnedMesh ) {
M
Mr.doob 已提交
1300

1301
					object.skeleton.update();
1302

1303
				}
1304

1305
				if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
1306

1307
					var material = object.material;
1308

1309
					if ( material.visible === true ) {
M
Mr.doob 已提交
1310

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

1313 1314 1315 1316
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1317

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

S
SUNAG 已提交
1320
						if ( material instanceof THREE.MultiMaterial ) {
1321

1322 1323
							var groups = geometry.groups;
							var materials = material.materials;
1324

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

1327 1328
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1329

1330
								if ( groupMaterial.visible === true ) {
1331

1332 1333 1334
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1335

M
Mr.doob 已提交
1336
							}
M
Mr.doob 已提交
1337

1338
						} else {
M
Mr.doob 已提交
1339

1340
							pushRenderItem( object, geometry, material, _vector3.z, null );
1341

1342
						}
O
OpenShift guest 已提交
1343

1344
					}
M
Mr.doob 已提交
1345

1346
				}
M
Mr.doob 已提交
1347

1348
			}
M
Mr.doob 已提交
1349

M
Mr.doob 已提交
1350
		}
M
Mr.doob 已提交
1351

M
Mr.doob 已提交
1352
		var children = object.children;
M
Mr.doob 已提交
1353

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

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

1358
		}
1359

1360
	}
M
Mr.doob 已提交
1361

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

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

1366
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1367

1368
			var object = renderItem.object;
M
Mr.doob 已提交
1369 1370 1371
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1372

1373 1374
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1375

M
Mr.doob 已提交
1376
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1377

M
Mr.doob 已提交
1378
				setMaterial( material );
M
Mr.doob 已提交
1379

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

M
Mr.doob 已提交
1382
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1383

M
Mr.doob 已提交
1384
				object.render( function ( object ) {
M
Mr.doob 已提交
1385

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

M
Mr.doob 已提交
1388
				} );
1389

M
Mr.doob 已提交
1390
			} else {
M
Mr.doob 已提交
1391

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

M
Mr.doob 已提交
1394
			}
M
Mr.doob 已提交
1395

1396
		}
M
Mr.doob 已提交
1397

1398
	}
G
gero3 已提交
1399

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

1402
		var materialProperties = properties.get( material );
G
gero3 已提交
1403

1404
		var parameters = programCache.getParameters( material, _lights, fog, object );
G
gero3 已提交
1405
		var code = programCache.getProgramCode( material, parameters );
G
gero3 已提交
1406

1407
		var program = materialProperties.program;
T
tschw 已提交
1408
		var programChange = true;
1409

1410
		if ( program === undefined ) {
B
Ben Adams 已提交
1411

M
Mr.doob 已提交
1412 1413
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1414

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

M
Mr.doob 已提交
1417
			// changed glsl or parameters
1418
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1419

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

T
tschw 已提交
1422
			// same glsl and uniform list
T
tschw 已提交
1423 1424
			return;

T
tschw 已提交
1425
		} else {
B
Ben Adams 已提交
1426

T
tschw 已提交
1427 1428
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1429 1430 1431

		}

1432
		if ( programChange ) {
B
Ben Adams 已提交
1433

1434
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1435

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

1438 1439 1440 1441 1442 1443
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1444

1445
			} else {
B
Ben Adams 已提交
1446

1447 1448 1449 1450 1451 1452
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1453

1454
			}
G
gero3 已提交
1455

1456
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1457

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

1460 1461
			materialProperties.program = program;
			material.program = program;
1462 1463 1464

		}

1465
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1466 1467 1468 1469 1470

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

M
Mr.doob 已提交
1473
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

M
Mr.doob 已提交
1489
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498

					material.numSupportedMorphNormals ++;

				}

			}

		}

1499
		materialProperties.uniformsList = [];
M
Mr.doob 已提交
1500

1501 1502
		var uniforms = materialProperties.__webglShader.uniforms,
			uniformLocations = materialProperties.program.getUniforms();
M
Mr.doob 已提交
1503

1504
		for ( var u in uniforms ) {
M
Mr.doob 已提交
1505

1506
			var location = uniformLocations[ u ];
1507 1508

			if ( location ) {
G
gero3 已提交
1509

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

1512
			}
M
Mr.doob 已提交
1513 1514 1515

		}

1516 1517
		if ( material instanceof THREE.MeshPhongMaterial ||
				material instanceof THREE.MeshLambertMaterial ||
1518
				material instanceof THREE.MeshStandardMaterial ||
1519 1520
				material.lights ) {

1521 1522
			// store the light setup it was created for

1523 1524
			materialProperties.lightsHash = _lights.hash;

1525 1526 1527 1528 1529
			// 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 已提交
1530
			uniforms.pointLights.value = _lights.point;
1531 1532
			uniforms.hemisphereLights.value = _lights.hemi;

1533 1534 1535 1536 1537 1538
			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;
1539

1540 1541
		}

A
arose 已提交
1542 1543 1544 1545 1546 1547 1548 1549
		// detect dynamic uniforms

		materialProperties.hasDynamicUniforms = false;

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

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

1550
			if ( uniform.dynamic === true ) {
A
arose 已提交
1551 1552 1553 1554 1555 1556 1557 1558

				materialProperties.hasDynamicUniforms = true;
				break;

			}

		}

M
Mr.doob 已提交
1559
	}
M
Mr.doob 已提交
1560

1561 1562
	function setMaterial( material ) {

M
Mr.doob 已提交
1563 1564
		setMaterialFaces( material );

1565 1566
		if ( material.transparent === true ) {

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

1569 1570 1571 1572
		} else {

			state.setBlending( THREE.NoBlending );

1573 1574
		}

B
Ben Adams 已提交
1575
		state.setDepthFunc( material.depthFunc );
M
Mr.doob 已提交
1576 1577
		state.setDepthTest( material.depthTest );
		state.setDepthWrite( material.depthWrite );
1578
		state.setColorWrite( material.colorWrite );
M
Mr.doob 已提交
1579
		state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
1580 1581 1582

	}

M
Mr.doob 已提交
1583 1584
	function setMaterialFaces( material ) {

1585
		material.side !== THREE.DoubleSide ? state.enable( _gl.CULL_FACE ) : state.disable( _gl.CULL_FACE );
M
Mr.doob 已提交
1586 1587 1588 1589
		state.setFlipSided( material.side === THREE.BackSide );

	}

1590
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1591 1592 1593

		_usedTextureUnits = 0;

1594
		var materialProperties = properties.get( material );
1595

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
		if ( materialProperties.program === undefined ) {

			material.needsUpdate = true;

		}

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

			material.needsUpdate = true;

		}

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

1611
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1612 1613 1614 1615
			material.needsUpdate = false;

		}

1616
		var refreshProgram = false;
M
Mr.doob 已提交
1617
		var refreshMaterial = false;
1618
		var refreshLights = false;
M
Mr.doob 已提交
1619

1620
		var program = materialProperties.program,
1621
			p_uniforms = program.getUniforms(),
1622
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1623

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

1626 1627
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1628

1629
			refreshProgram = true;
M
Mr.doob 已提交
1630
			refreshMaterial = true;
1631
			refreshLights = true;
M
Mr.doob 已提交
1632 1633 1634 1635 1636 1637

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1638

M
Mr.doob 已提交
1639 1640 1641 1642
			refreshMaterial = true;

		}

1643
		if ( refreshProgram || camera !== _currentCamera ) {
M
Mr.doob 已提交
1644 1645 1646

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

G
gero3 已提交
1647
			if ( capabilities.logarithmicDepthBuffer ) {
1648

1649
				_gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1650 1651 1652 1653

			}


1654 1655 1656 1657 1658 1659 1660 1661 1662
			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 已提交
1663
				refreshLights = true;		// remains set until update done
1664 1665

			}
M
Mr.doob 已提交
1666

1667 1668 1669 1670 1671
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1672
				 material instanceof THREE.MeshStandardMaterial ||
1673 1674
				 material.envMap ) {

1675
				if ( p_uniforms.cameraPosition !== undefined ) {
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685

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

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1686
				 material instanceof THREE.MeshBasicMaterial ||
1687
				 material instanceof THREE.MeshStandardMaterial ||
1688 1689 1690
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

1691
				if ( p_uniforms.viewMatrix !== undefined ) {
1692 1693

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

1695 1696 1697 1698
				}

			}

M
Mr.doob 已提交
1699 1700 1701 1702 1703 1704 1705 1706
		}

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

1707
			if ( object.bindMatrix && p_uniforms.bindMatrix !== undefined ) {
1708 1709 1710 1711 1712

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

			}

1713
			if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== undefined ) {
1714 1715 1716 1717 1718

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

			}

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

1721
				if ( p_uniforms.boneTexture !== undefined ) {
M
Mr.doob 已提交
1722 1723 1724 1725

					var textureUnit = getTextureUnit();

					_gl.uniform1i( p_uniforms.boneTexture, textureUnit );
1726
					_this.setTexture( object.skeleton.boneTexture, textureUnit );
M
Mr.doob 已提交
1727 1728 1729

				}

1730
				if ( p_uniforms.boneTextureWidth !== undefined ) {
1731

1732
					_gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
1733 1734 1735

				}

1736
				if ( p_uniforms.boneTextureHeight !== undefined ) {
1737

1738
					_gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
1739 1740 1741

				}

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

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

1746
					_gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
M
Mr.doob 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

				}

			}

		}

		if ( refreshMaterial ) {

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1758
				 material instanceof THREE.MeshStandardMaterial ||
M
Mr.doob 已提交
1759 1760
				 material.lights ) {

1761
				// the current material requires lighting info
M
Mr.doob 已提交
1762

T
tschw 已提交
1763 1764 1765 1766 1767 1768
				// 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 已提交
1769

T
tschw 已提交
1770
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1771

T
tschw 已提交
1772
			}
G
gero3 已提交
1773

T
tschw 已提交
1774
			// refresh uniforms common to several materials
G
gero3 已提交
1775

T
tschw 已提交
1776
			if ( fog && material.fog ) {
G
gero3 已提交
1777

T
tschw 已提交
1778
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1779 1780 1781 1782 1783

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1784
				 material instanceof THREE.MeshPhongMaterial ||
1785
				 material instanceof THREE.MeshStandardMaterial ) {
M
Mr.doob 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

				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 );

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

M
Mr.doob 已提交
1804
				refreshUniformsPoints( m_uniforms, material );
M
Mr.doob 已提交
1805

1806 1807 1808 1809
			} else if ( material instanceof THREE.MeshLambertMaterial ) {

				refreshUniformsLambert( m_uniforms, material );

M
Mr.doob 已提交
1810 1811 1812 1813
			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

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

1816
				refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1817

M
Mr.doob 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
			} 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

1832
			loadUniformsGeneric( materialProperties.uniformsList );
M
Mr.doob 已提交
1833 1834 1835 1836 1837

		}

		loadUniformsMatrices( p_uniforms, object );

1838
		if ( p_uniforms.modelMatrix !== undefined ) {
M
Mr.doob 已提交
1839 1840

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

M
Mr.doob 已提交
1842 1843
		}

1844
		if ( materialProperties.hasDynamicUniforms === true ) {
A
arose 已提交
1845

1846
			updateDynamicUniforms( materialProperties.uniformsList, object, camera );
A
arose 已提交
1847 1848 1849

		}

M
Mr.doob 已提交
1850 1851
		return program;

M
Mr.doob 已提交
1852
	}
M
Mr.doob 已提交
1853

1854
	function updateDynamicUniforms ( uniforms, object, camera ) {
A
arose 已提交
1855 1856 1857 1858 1859 1860

		var dynamicUniforms = [];

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

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

1863
			if ( onUpdateCallback !== undefined ) {
A
arose 已提交
1864

1865
				onUpdateCallback.bind( uniform )( object, camera );
A
arose 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
				dynamicUniforms.push( uniforms[ j ] );

			}

		}

		loadUniformsGeneric( dynamicUniforms );

	}

M
Mr.doob 已提交
1876 1877 1878 1879 1880 1881
	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

1882
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1883

1884
		if ( material.emissive ) {
M
Mr.doob 已提交
1885

1886
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1887 1888 1889

		}

1890 1891 1892
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
1893

1894
		if ( material.aoMap ) {
1895

1896 1897
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
1898 1899 1900

		}

M
Mr.doob 已提交
1901
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
1902 1903 1904 1905 1906
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
1907
		// 6. emissive map
M
Mr.doob 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

1919 1920 1921 1922
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
1923 1924 1925 1926 1927 1928 1929 1930
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

1931 1932 1933 1934 1935 1936 1937 1938
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

1939 1940 1941 1942
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

1943 1944 1945 1946
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
1947 1948 1949 1950
		}

		if ( uvScaleMap !== undefined ) {

M
Mr.doob 已提交
1951 1952 1953 1954 1955 1956
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) {

				uvScaleMap = uvScaleMap.texture;

			}

M
Mr.doob 已提交
1957 1958 1959 1960 1961 1962 1963 1964
			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;
1965
		uniforms.envMapEncoding.value = material.envMap.encoding;
1966
		uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
M
Mr.doob 已提交
1967

1968
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
1969 1970
		uniforms.refractionRatio.value = material.refractionRatio;

M
Mr.doob 已提交
1971
	}
M
Mr.doob 已提交
1972 1973 1974 1975 1976 1977

	function refreshUniformsLine ( uniforms, material ) {

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

M
Mr.doob 已提交
1978
	}
M
Mr.doob 已提交
1979 1980 1981 1982 1983 1984 1985

	function refreshUniformsDash ( uniforms, material ) {

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

M
Mr.doob 已提交
1986
	}
M
Mr.doob 已提交
1987

M
Mr.doob 已提交
1988
	function refreshUniformsPoints ( uniforms, material ) {
M
Mr.doob 已提交
1989

1990
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1991
		uniforms.opacity.value = material.opacity;
1992 1993
		uniforms.size.value = material.size * _pixelRatio;
		uniforms.scale.value = _canvas.clientHeight / 2.0; // TODO: Cache this.
M
Mr.doob 已提交
1994 1995 1996

		uniforms.map.value = material.map;

1997 1998 1999 2000 2001 2002 2003 2004 2005
		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 已提交
2006
	}
M
Mr.doob 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022

	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 已提交
2023
	}
M
Mr.doob 已提交
2024

2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
	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;
2037
			uniforms.emissiveMapEncoding.value = material.emissiveMap.encoding;
2038 2039 2040 2041 2042

		}

	}

M
Mr.doob 已提交
2043 2044
	function refreshUniformsPhong ( uniforms, material ) {

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

2048 2049 2050 2051
		if ( material.lightMap ) {

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

2053
		}
2054

2055
		if ( material.emissiveMap ) {
2056

2057
			uniforms.emissiveMap.value = material.emissiveMap;
2058
			uniforms.emissiveMapEncoding.value = material.emissiveMap.encoding;
2059

2060
		}
M
Mr.doob 已提交
2061

2062 2063 2064 2065
		if ( material.bumpMap ) {

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

2067
		}
M
Mr.doob 已提交
2068

2069 2070 2071 2072 2073 2074
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2075

2076 2077 2078 2079 2080
		if ( material.displacementMap ) {

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

2082
		}
2083 2084 2085

	}

2086
	function refreshUniformsStandard ( uniforms, material ) {
W
WestLangley 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

		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;
2113
			uniforms.emissiveMapEncoding.value = material.emissiveMap.encoding;
W
WestLangley 已提交
2114 2115 2116 2117 2118 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

		}

		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;

		}

	}

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

M
Mr.doob 已提交
2150
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2151

M
Mr.doob 已提交
2152
		uniforms.ambientLightColor.needsUpdate = value;
2153

B
Ben Houston 已提交
2154 2155 2156 2157
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
		uniforms.hemisphereLights.needsUpdate = value;
2158

M
Mr.doob 已提交
2159
	}
2160

M
Mr.doob 已提交
2161 2162 2163 2164
	// Uniforms (load to GPU)

	function loadUniformsMatrices ( uniforms, object ) {

2165
		_gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object.modelViewMatrix.elements );
M
Mr.doob 已提交
2166 2167 2168

		if ( uniforms.normalMatrix ) {

2169
			_gl.uniformMatrix3fv( uniforms.normalMatrix, false, object.normalMatrix.elements );
M
Mr.doob 已提交
2170 2171 2172

		}

M
Mr.doob 已提交
2173
	}
M
Mr.doob 已提交
2174 2175 2176 2177 2178

	function getTextureUnit() {

		var textureUnit = _usedTextureUnits;

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

G
gero3 已提交
2181
			console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
M
Mr.doob 已提交
2182 2183 2184 2185 2186 2187 2188

		}

		_usedTextureUnits += 1;

		return textureUnit;

M
Mr.doob 已提交
2189
	}
M
Mr.doob 已提交
2190

2191
	function loadUniformsGeneric ( uniforms ) {
M
Mr.doob 已提交
2192

M
Mr.doob 已提交
2193
		var texture, textureUnit;
M
Mr.doob 已提交
2194

M
Mr.doob 已提交
2195 2196 2197
		for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {

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

2199 2200
			// needsUpdate property is not added to all uniforms.
			if ( uniform.needsUpdate === false ) continue;
2201

M
Mr.doob 已提交
2202 2203
			var type = uniform.type;
			var value = uniform.value;
2204
			var location = uniforms[ j ][ 1 ];
M
Mr.doob 已提交
2205

2206
			switch ( type ) {
M
Mr.doob 已提交
2207

2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
				case '1i':
					_gl.uniform1i( location, value );
					break;

				case '1f':
					_gl.uniform1f( location, value );
					break;

				case '2f':
					_gl.uniform2f( location, value[ 0 ], value[ 1 ] );
					break;

				case '3f':
					_gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
					break;

				case '4f':
					_gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
					break;

				case '1iv':
					_gl.uniform1iv( location, value );
					break;

				case '3iv':
					_gl.uniform3iv( location, value );
					break;

				case '1fv':
					_gl.uniform1fv( location, value );
					break;

				case '2fv':
					_gl.uniform2fv( location, value );
					break;

				case '3fv':
					_gl.uniform3fv( location, value );
					break;

				case '4fv':
					_gl.uniform4fv( location, value );
					break;

2252 2253 2254 2255
				case 'Matrix2fv':
					_gl.uniformMatrix2fv( location, false, value );
					break;

2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
				case 'Matrix3fv':
					_gl.uniformMatrix3fv( location, false, value );
					break;

				case 'Matrix4fv':
					_gl.uniformMatrix4fv( location, false, value );
					break;

				//

M
Mr.doob 已提交
2266
				case 'i':
M
Mr.doob 已提交
2267

2268 2269
					// single integer
					_gl.uniform1i( location, value );
M
Mr.doob 已提交
2270

2271
					break;
M
Mr.doob 已提交
2272

2273
				case 'f':
M
Mr.doob 已提交
2274

2275 2276
					// single float
					_gl.uniform1f( location, value );
M
Mr.doob 已提交
2277

2278
					break;
M
Mr.doob 已提交
2279

2280
				case 'v2':
M
Mr.doob 已提交
2281

2282 2283
					// single THREE.Vector2
					_gl.uniform2f( location, value.x, value.y );
M
Mr.doob 已提交
2284

2285
					break;
M
Mr.doob 已提交
2286

2287
				case 'v3':
M
Mr.doob 已提交
2288

2289 2290
					// single THREE.Vector3
					_gl.uniform3f( location, value.x, value.y, value.z );
M
Mr.doob 已提交
2291

2292
					break;
M
Mr.doob 已提交
2293

M
Mr.doob 已提交
2294
				case 'v4':
M
Mr.doob 已提交
2295

2296 2297
					// single THREE.Vector4
					_gl.uniform4f( location, value.x, value.y, value.z, value.w );
M
Mr.doob 已提交
2298

2299
					break;
M
Mr.doob 已提交
2300

2301
				case 'c':
M
Mr.doob 已提交
2302

2303 2304
					// single THREE.Color
					_gl.uniform3f( location, value.r, value.g, value.b );
M
Mr.doob 已提交
2305

2306
					break;
M
Mr.doob 已提交
2307

2308
				/*
2309 2310 2311
				case 's':

					// TODO: Optimize this.
B
Ben Houston 已提交
2312
					for( var propertyName in uniform.properties ) {
M
Mr.doob 已提交
2313

B
Ben Houston 已提交
2314 2315 2316
						var property = uniform.properties[ propertyName ];
						var locationProperty =  location[ propertyName ];
						var valueProperty = value[ propertyName ];
M
Mr.doob 已提交
2317

2318
						switch( property.type ) {
2319 2320 2321
							case 'i':
								_gl.uniform1i( locationProperty, valueProperty );
								break;
2322
							case 'f':
B
Ben Houston 已提交
2323
								_gl.uniform1f( locationProperty, valueProperty );
2324 2325
								break;
							case 'v2':
B
Ben Houston 已提交
2326
								_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );
2327 2328
								break;
							case 'v3':
B
Ben Houston 已提交
2329
								_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );
2330 2331
								break;
							case 'v4':
B
Ben Houston 已提交
2332
								_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );
2333 2334
								break;
							case 'c':
B
Ben Houston 已提交
2335
								_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
2336 2337
								break;
						};
2338 2339 2340 2341

					}

					break;
2342
				*/
2343

2344
				case 'sa':
2345

2346
					// TODO: Optimize this.
M
Mr.doob 已提交
2347
					for ( var i = 0; i < value.length; i ++ ) {
2348

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

B
Ben Houston 已提交
2351 2352
							var property = uniform.properties[ propertyName ];
							var locationProperty =  location[ i ][ propertyName ];
2353
							var valueProperty = value[ i ][ propertyName ];
M
Mr.doob 已提交
2354

2355
							switch ( property.type ) {
2356 2357 2358
								case 'i':
									_gl.uniform1i( locationProperty, valueProperty );
									break;
2359
								case 'f':
B
Ben Houston 已提交
2360
									_gl.uniform1f( locationProperty, valueProperty );
2361 2362
									break;
								case 'v2':
B
Ben Houston 已提交
2363
									_gl.uniform2f( locationProperty, valueProperty.x, valueProperty.y );
2364 2365
									break;
								case 'v3':
B
Ben Houston 已提交
2366
									_gl.uniform3f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z );
2367 2368
									break;
								case 'v4':
B
Ben Houston 已提交
2369
									_gl.uniform4f( locationProperty, valueProperty.x, valueProperty.y, valueProperty.z, valueProperty.w );
2370 2371
									break;
								case 'c':
B
Ben Houston 已提交
2372
									_gl.uniform3f( locationProperty, valueProperty.r, valueProperty.g, valueProperty.b );
2373
									break;
M
Mr.doob 已提交
2374 2375 2376
								case 'm4':
									_gl.uniformMatrix4fv( locationProperty, false, valueProperty.elements );
									break;
M
Mr.doob 已提交
2377
							}
2378

2379
						}
2380 2381

					}
M
Mr.doob 已提交
2382

2383 2384
					break;

2385
				case 'iv1':
M
Mr.doob 已提交
2386

2387 2388
					// flat array of integers (JS or typed array)
					_gl.uniform1iv( location, value );
M
Mr.doob 已提交
2389

2390
					break;
M
Mr.doob 已提交
2391

2392
				case 'iv':
M
Mr.doob 已提交
2393

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

2397
					break;
M
Mr.doob 已提交
2398

2399
				case 'fv1':
M
Mr.doob 已提交
2400

2401 2402
					// flat array of floats (JS or typed array)
					_gl.uniform1fv( location, value );
M
Mr.doob 已提交
2403

2404
					break;
M
Mr.doob 已提交
2405

2406
				case 'fv':
M
Mr.doob 已提交
2407

2408 2409
					// flat array of floats with 3 x N size (JS or typed array)
					_gl.uniform3fv( location, value );
M
Mr.doob 已提交
2410

2411
					break;
M
Mr.doob 已提交
2412

2413
				case 'v2v':
M
Mr.doob 已提交
2414

2415
					// array of THREE.Vector2
M
Mr.doob 已提交
2416

2417
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2418

2419
						uniform._array = new Float32Array( 2 * value.length );
M
Mr.doob 已提交
2420

2421
					}
M
Mr.doob 已提交
2422

M
Mr.doob 已提交
2423
					for ( var i = 0, i2 = 0, il = value.length; i < il; i ++, i2 += 2 ) {
M
Mr.doob 已提交
2424

M
Mr.doob 已提交
2425 2426
						uniform._array[ i2 + 0 ] = value[ i ].x;
						uniform._array[ i2 + 1 ] = value[ i ].y;
M
Mr.doob 已提交
2427

2428
					}
M
Mr.doob 已提交
2429

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

2432
					break;
M
Mr.doob 已提交
2433

2434
				case 'v3v':
M
Mr.doob 已提交
2435

2436
					// array of THREE.Vector3
M
Mr.doob 已提交
2437

2438
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2439

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

2442
					}
M
Mr.doob 已提交
2443

M
Mr.doob 已提交
2444
					for ( var i = 0, i3 = 0, il = value.length; i < il; i ++, i3 += 3 ) {
R
Ryan Tsao 已提交
2445

M
Mr.doob 已提交
2446 2447 2448
						uniform._array[ i3 + 0 ] = value[ i ].x;
						uniform._array[ i3 + 1 ] = value[ i ].y;
						uniform._array[ i3 + 2 ] = value[ i ].z;
R
Ryan Tsao 已提交
2449

2450
					}
R
Ryan Tsao 已提交
2451

2452
					_gl.uniform3fv( location, uniform._array );
R
Ryan Tsao 已提交
2453

2454
					break;
R
Ryan Tsao 已提交
2455

2456
				case 'v4v':
R
Ryan Tsao 已提交
2457

2458
					// array of THREE.Vector4
R
Ryan Tsao 已提交
2459

2460
					if ( uniform._array === undefined ) {
R
Ryan Tsao 已提交
2461

2462
						uniform._array = new Float32Array( 4 * value.length );
R
Ryan Tsao 已提交
2463

2464
					}
M
Mr.doob 已提交
2465

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

M
Mr.doob 已提交
2468 2469 2470 2471
						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 已提交
2472

2473
					}
M
Mr.doob 已提交
2474

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

2477
					break;
M
Mr.doob 已提交
2478

2479 2480 2481 2482 2483 2484 2485
				case 'm2':

					// single THREE.Matrix2
					_gl.uniformMatrix2fv( location, false, value.elements );

					break;

2486
				case 'm3':
M
Mr.doob 已提交
2487

2488 2489
					// single THREE.Matrix3
					_gl.uniformMatrix3fv( location, false, value.elements );
M
Mr.doob 已提交
2490

2491
					break;
M
Mr.doob 已提交
2492

2493
				case 'm3v':
M
Mr.doob 已提交
2494

2495
					// array of THREE.Matrix3
M
Mr.doob 已提交
2496

2497
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2498

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

2501
					}
M
Mr.doob 已提交
2502

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

2505
						value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
M
Mr.doob 已提交
2506

2507
					}
M
Mr.doob 已提交
2508

2509
					_gl.uniformMatrix3fv( location, false, uniform._array );
M
Mr.doob 已提交
2510

2511
					break;
M
Mr.doob 已提交
2512

2513
				case 'm4':
M
Mr.doob 已提交
2514

2515 2516
					// single THREE.Matrix4
					_gl.uniformMatrix4fv( location, false, value.elements );
M
Mr.doob 已提交
2517

2518
					break;
M
Mr.doob 已提交
2519

2520
				case 'm4v':
M
Mr.doob 已提交
2521

2522
					// array of THREE.Matrix4
M
Mr.doob 已提交
2523

2524
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2525

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

2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
					}

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

						value[ i ].flattenToArrayOffset( uniform._array, i * 16 );

					}

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

					break;
M
Mr.doob 已提交
2539

2540
				case 't':
M
Mr.doob 已提交
2541

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

2544 2545 2546 2547
					texture = value;
					textureUnit = getTextureUnit();

					_gl.uniform1i( location, textureUnit );
M
Mr.doob 已提交
2548

2549
					if ( ! texture ) continue;
M
Mr.doob 已提交
2550

2551
					if ( texture instanceof THREE.CubeTexture ||
G
gero3 已提交
2552 2553 2554
						 ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {

						// CompressedTexture can have Array in image :/
M
Mr.doob 已提交
2555

2556
						setCubeTexture( texture, textureUnit );
M
Mr.doob 已提交
2557

2558 2559
					} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {

M
Mr.doob 已提交
2560 2561 2562 2563 2564
						setCubeTextureDynamic( texture.texture, textureUnit );

					} else if ( texture instanceof THREE.WebGLRenderTarget ) {

						_this.setTexture( texture.texture, textureUnit );
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575

					} else {

						_this.setTexture( texture, textureUnit );

					}

					break;

				case 'tv':

M
Mr.doob 已提交
2576
					// array of THREE.Texture (2d or cube)
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598

					if ( uniform._array === undefined ) {

						uniform._array = [];

					}

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

						uniform._array[ i ] = getTextureUnit();

					}

					_gl.uniform1iv( location, uniform._array );

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

						texture = uniform.value[ i ];
						textureUnit = uniform._array[ i ];

						if ( ! texture ) continue;

M
Mr.doob 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
						if ( texture instanceof THREE.CubeTexture ||
							 ( texture.image instanceof Array && texture.image.length === 6 ) ) {

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

						}
2619 2620 2621 2622 2623 2624

					}

					break;

				default:
2625

2626
					console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
2627

M
Mr.doob 已提交
2628 2629 2630 2631
			}

		}

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

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

B
brason 已提交
2636
		var l, ll, light,
M
Mr.doob 已提交
2637
		r = 0, g = 0, b = 0,
B
Ben Houston 已提交
2638
		color,
B
brason 已提交
2639
		intensity,
M
Mr.doob 已提交
2640 2641
		distance,

2642
		viewMatrix = camera.matrixWorldInverse,
M
Mr.doob 已提交
2643

M
Mr.doob 已提交
2644
		directionalLength = 0,
M
Mr.doob 已提交
2645 2646
		pointLength = 0,
		spotLength = 0,
2647 2648 2649
		hemiLength = 0,

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

2651 2652
		_lights.shadowsPointLight = 0;

M
Mr.doob 已提交
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
		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 ) {

2663 2664 2665
				r += color.r * intensity;
				g += color.g * intensity;
				b += color.b * intensity;
M
Mr.doob 已提交
2666 2667 2668

			} else if ( light instanceof THREE.DirectionalLight ) {

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

2671
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
M
Mr.doob 已提交
2672
				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
2673
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2674 2675 2676
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2677
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2678

2679
				if ( light.castShadow ) {
M
Mr.doob 已提交
2680

2681 2682 2683
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2684

2685
					_lights.shadows[ shadowsLength ++ ] = light;
2686

2687 2688
				}

2689 2690
				_lights.directionalShadowMap[ directionalLength ] = light.shadow.map;
				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
2691
				_lights.directional[ directionalLength ++ ] = uniforms;
M
Mr.doob 已提交
2692

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

M
Mr.doob 已提交
2695
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2696 2697 2698

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

M
Mr.doob 已提交
2700 2701 2702 2703 2704 2705 2706 2707 2708
				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 );

				uniforms.angleCos = Math.cos( light.angle );
2709
				uniforms.penumbra = Math.cos( light.angle ) * light.penumbra;
M
Mr.doob 已提交
2710
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2711

2712
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2713

2714 2715
				if ( light.castShadow ) {

2716 2717 2718
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2719

2720
					_lights.shadows[ shadowsLength ++ ] = light;
2721

2722 2723
				}

2724 2725
				_lights.spotShadowMap[ spotLength ] = light.shadow.map;
				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
M
Mr.doob 已提交
2726
				_lights.spot[ spotLength ++ ] = uniforms;
2727

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

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

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

M
Mr.doob 已提交
2735 2736
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
				uniforms.distance = light.distance;
M
Mr.doob 已提交
2737 2738
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;

2739
				uniforms.shadow = light.castShadow;
2740

M
Mr.doob 已提交
2741
				if ( light.castShadow ) {
2742

2743 2744 2745 2746 2747
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;

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

2749
				}
2750

2751
				_lights.pointShadowMap[ pointLength ] = light.shadow.map;
2752

2753 2754 2755
				if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) {

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

2757 2758
				}

2759 2760 2761 2762 2763
				// 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 已提交
2764
				_lights.point[ pointLength ++ ] = uniforms;
2765

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

M
Mr.doob 已提交
2768
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2769 2770 2771 2772

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

M
Mr.doob 已提交
2774 2775
				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
M
Mr.doob 已提交
2776

M
Mr.doob 已提交
2777
				_lights.hemi[ hemiLength ++ ] = uniforms;
M
Mr.doob 已提交
2778 2779 2780 2781 2782

			}

		}

M
Mr.doob 已提交
2783 2784 2785
		_lights.ambient[ 0 ] = r;
		_lights.ambient[ 1 ] = g;
		_lights.ambient[ 2 ] = b;
M
Mr.doob 已提交
2786

M
Mr.doob 已提交
2787 2788
		_lights.directional.length = directionalLength;
		_lights.spot.length = spotLength;
2789
		_lights.point.length = pointLength;
M
Mr.doob 已提交
2790
		_lights.hemi.length = hemiLength;
2791

2792 2793
		_lights.shadows.length = shadowsLength;

2794
		_lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + shadowsLength;
2795

M
Mr.doob 已提交
2796
	}
M
Mr.doob 已提交
2797 2798 2799 2800 2801 2802 2803

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

		if ( cullFace === THREE.CullFaceNone ) {

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

		} 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 );

			}

2832
			state.enable( _gl.CULL_FACE );
M
Mr.doob 已提交
2833 2834 2835 2836 2837 2838 2839

		}

	};

	// Textures

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

2842 2843
		var extension;

M
Mr.doob 已提交
2844
		if ( isPowerOfTwoImage ) {
M
Mr.doob 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855

			_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 已提交
2856 2857 2858

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

M
Mr.doob 已提交
2859
				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 已提交
2860

2861
			}
M
Mr.doob 已提交
2862 2863 2864 2865

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

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

M
Mr.doob 已提交
2868
				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 已提交
2869

2870
			}
M
Mr.doob 已提交
2871

M
Mr.doob 已提交
2872 2873
		}

2874 2875
		extension = extensions.get( 'EXT_texture_filter_anisotropic' );

2876
		if ( extension ) {
M
Mr.doob 已提交
2877

2878 2879
			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 已提交
2880

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

2883
				_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
2884
				properties.get( texture ).__currentAnisotropy = texture.anisotropy;
M
Mr.doob 已提交
2885 2886 2887 2888 2889

			}

		}

M
Mr.doob 已提交
2890
	}
M
Mr.doob 已提交
2891

2892
	function uploadTexture( textureProperties, texture, slot ) {
2893

2894
		if ( textureProperties.__webglInit === undefined ) {
2895

2896
			textureProperties.__webglInit = true;
2897 2898 2899

			texture.addEventListener( 'dispose', onTextureDispose );

2900
			textureProperties.__webglTexture = _gl.createTexture();
2901

2902
			_infoMemory.textures ++;
2903 2904

		}
M
Mr.doob 已提交
2905

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

H
Henri Astre 已提交
2909 2910 2911 2912
		_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 );

2913
		var image = clampToMaxSize( texture.image, capabilities.maxTextureSize );
2914

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

2917
			image = makePowerOfTwo( image );
M
Mr.doob 已提交
2918 2919 2920

		}

M
Mr.doob 已提交
2921
		var isPowerOfTwoImage = isPowerOfTwo( image ),
H
Henri Astre 已提交
2922 2923
		glFormat = paramThreeToGL( texture.format ),
		glType = paramThreeToGL( texture.type );
M
Mr.doob 已提交
2924

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

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

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

H
Henri Astre 已提交
2931 2932 2933
			// 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 已提交
2934

M
Mr.doob 已提交
2935
			if ( mipmaps.length > 0 && isPowerOfTwoImage ) {
H
Henri Astre 已提交
2936 2937

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

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

H
Henri Astre 已提交
2942
				}
M
Mr.doob 已提交
2943

H
Henri Astre 已提交
2944
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2945

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

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

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

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

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

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

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

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

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

2964
					} else {
M
Mr.doob 已提交
2965

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

2968
					}
M
Mr.doob 已提交
2969

H
Henri Astre 已提交
2970
				} else {
M
Mr.doob 已提交
2971

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

M
Mr.doob 已提交
2974 2975
				}

H
Henri Astre 已提交
2976 2977
			}

G
gero3 已提交
2978 2979 2980
		} else {

			// regular Texture (image, video, canvas)
H
Henri Astre 已提交
2981 2982 2983 2984 2985

			// 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 已提交
2986
			if ( mipmaps.length > 0 && isPowerOfTwoImage ) {
M
Mr.doob 已提交
2987

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

					mipmap = mipmaps[ i ];
2991
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
M
Mr.doob 已提交
2992 2993 2994

				}

H
Henri Astre 已提交
2995
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2996

H
Henri Astre 已提交
2997
			} else {
M
Mr.doob 已提交
2998

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

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

H
Henri Astre 已提交
3003
		}
M
Mr.doob 已提交
3004

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

3007
		textureProperties.__version = texture.version;
M
Mr.doob 已提交
3008

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

3011
	}
M
Mr.doob 已提交
3012

H
Henri Astre 已提交
3013
	this.setTexture = function ( texture, slot ) {
M
Mr.doob 已提交
3014

3015 3016 3017
		var textureProperties = properties.get( texture );

		if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
M
Mr.doob 已提交
3018

3019
			var image = texture.image;
M
Mr.doob 已提交
3020

3021 3022
			if ( image === undefined ) {

3023
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined', texture );
3024 3025 3026 3027
				return;

			}

3028
			if ( image.complete === false ) {
M
Mr.doob 已提交
3029

3030
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
3031 3032 3033 3034
				return;

			}

3035
			uploadTexture( textureProperties, texture, slot );
3036

3037
			return;
M
Mr.doob 已提交
3038 3039 3040

		}

B
Ben Adams 已提交
3041
		state.activeTexture( _gl.TEXTURE0 + slot );
3042
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
3043

M
Mr.doob 已提交
3044 3045 3046 3047
	};

	function clampToMaxSize ( image, maxSize ) {

3048
		if ( image.width > maxSize || image.height > maxSize ) {
M
Mr.doob 已提交
3049

3050 3051
			// Warning: Scaling through the canvas will only work with images that use
			// premultiplied alpha.
M
Mr.doob 已提交
3052

3053
			var scale = maxSize / Math.max( image.width, image.height );
M
Mr.doob 已提交
3054

3055 3056 3057
			var canvas = document.createElement( 'canvas' );
			canvas.width = Math.floor( image.width * scale );
			canvas.height = Math.floor( image.height * scale );
M
Mr.doob 已提交
3058

3059 3060
			var context = canvas.getContext( '2d' );
			context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
M
Mr.doob 已提交
3061

3062
			console.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
M
Mr.doob 已提交
3063

3064 3065 3066
			return canvas;

		}
M
Mr.doob 已提交
3067

3068
		return image;
M
Mr.doob 已提交
3069 3070 3071

	}

M
Mr.doob 已提交
3072 3073 3074 3075 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
	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 已提交
3108 3109
	function setCubeTexture ( texture, slot ) {

3110
		var textureProperties = properties.get( texture );
3111

M
Mr.doob 已提交
3112 3113
		if ( texture.image.length === 6 ) {

3114
			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
M
Mr.doob 已提交
3115

3116
				if ( ! textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
3117

3118 3119
					texture.addEventListener( 'dispose', onTextureDispose );

3120
					textureProperties.__image__webglTextureCube = _gl.createTexture();
M
Mr.doob 已提交
3121

3122
					_infoMemory.textures ++;
M
Mr.doob 已提交
3123 3124 3125

				}

B
Ben Adams 已提交
3126
				state.activeTexture( _gl.TEXTURE0 + slot );
3127
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3128 3129 3130

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

M
Mr.doob 已提交
3131 3132
				var isCompressed = texture instanceof THREE.CompressedTexture;
				var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
M
Mr.doob 已提交
3133 3134 3135 3136 3137

				var cubeImage = [];

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

3138
					if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
M
Mr.doob 已提交
3139

G
gero3 已提交
3140
						cubeImage[ i ] = clampToMaxSize( texture.image[ i ], capabilities.maxCubemapSize );
M
Mr.doob 已提交
3141 3142 3143

					} else {

3144
						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
M
Mr.doob 已提交
3145 3146 3147 3148 3149 3150

					}

				}

				var image = cubeImage[ 0 ],
M
Mr.doob 已提交
3151
				isPowerOfTwoImage = isPowerOfTwo( image ),
M
Mr.doob 已提交
3152 3153 3154
				glFormat = paramThreeToGL( texture.format ),
				glType = paramThreeToGL( texture.type );

M
Mr.doob 已提交
3155
				setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isPowerOfTwoImage );
M
Mr.doob 已提交
3156 3157 3158

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

3159
					if ( ! isCompressed ) {
M
Mr.doob 已提交
3160

M
Mr.doob 已提交
3161
						if ( isDataTexture ) {
3162

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

M
Mr.doob 已提交
3165
						} else {
3166

3167
							state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
3168

M
Mr.doob 已提交
3169 3170
						}

3171
					} else {
3172

M
Mr.doob 已提交
3173 3174
						var mipmap, mipmaps = cubeImage[ i ].mipmaps;

3175
						for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
M
Mr.doob 已提交
3176 3177

							mipmap = mipmaps[ j ];
M
Mr.doob 已提交
3178

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

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

3183
									state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
M
Mr.doob 已提交
3184

3185
								} else {
M
Mr.doob 已提交
3186

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

3189
								}
M
Mr.doob 已提交
3190

3191
							} else {
M
Mr.doob 已提交
3192

3193
								state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
M
Mr.doob 已提交
3194

3195
							}
M
Mr.doob 已提交
3196

3197
						}
M
Mr.doob 已提交
3198

M
Mr.doob 已提交
3199
					}
M
Mr.doob 已提交
3200

M
Mr.doob 已提交
3201 3202
				}

M
Mr.doob 已提交
3203
				if ( texture.generateMipmaps && isPowerOfTwoImage ) {
M
Mr.doob 已提交
3204 3205 3206 3207 3208

					_gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );

				}

3209
				textureProperties.__version = texture.version;
M
Mr.doob 已提交
3210

B
Ben Adams 已提交
3211
				if ( texture.onUpdate ) texture.onUpdate( texture );
M
Mr.doob 已提交
3212 3213 3214

			} else {

B
Ben Adams 已提交
3215
				state.activeTexture( _gl.TEXTURE0 + slot );
3216
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3217 3218 3219 3220 3221

			}

		}

M
Mr.doob 已提交
3222
	}
M
Mr.doob 已提交
3223 3224 3225

	function setCubeTextureDynamic ( texture, slot ) {

B
Ben Adams 已提交
3226
		state.activeTexture( _gl.TEXTURE0 + slot );
3227
		state.bindTexture( _gl.TEXTURE_CUBE_MAP, properties.get( texture ).__webglTexture );
M
Mr.doob 已提交
3228

M
Mr.doob 已提交
3229
	}
M
Mr.doob 已提交
3230 3231 3232

	// Render targets

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

3236 3237 3238
		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 已提交
3239
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3240 3241
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
M
Mr.doob 已提交
3242

M
Mr.doob 已提交
3243
	}
M
Mr.doob 已提交
3244

3245 3246
	// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
	function setupRenderBufferStorage ( renderbuffer, renderTarget ) {
M
Mr.doob 已提交
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261

		_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 {

3262
			// FIXME: We don't support !depth !stencil
M
Mr.doob 已提交
3263 3264 3265 3266
			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );

		}

3267
		_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
M
Mr.doob 已提交
3268

M
Mr.doob 已提交
3269
	}
M
Mr.doob 已提交
3270

3271
	// Setup GL resources for a non-texture depth buffer
M
Marius Kintel 已提交
3272
	function setupDepthRenderbuffer( renderTarget ) {
M
Mr.doob 已提交
3273

3274
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
3275 3276

		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
M
Mr.doob 已提交
3277

3278
		if ( isCube ) {
M
Mr.doob 已提交
3279

3280
			renderTargetProperties.__webglDepthbuffer = [];
M
Mr.doob 已提交
3281

3282
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3283

M
Marius Kintel 已提交
3284
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
3285 3286
				renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
				setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget );
M
Mr.doob 已提交
3287 3288

			}
B
Ben Adams 已提交
3289

M
Mr.doob 已提交
3290
		} else {
M
Mr.doob 已提交
3291

M
Marius Kintel 已提交
3292
			_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
3293 3294
			renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
			setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget );
M
Mr.doob 已提交
3295

3296
		}
M
Mr.doob 已提交
3297

M
Marius Kintel 已提交
3298
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
M
Mr.doob 已提交
3299

M
Mr.doob 已提交
3300
	}
M
Mr.doob 已提交
3301

3302
	// Set up GL resources for the render target
M
Marius Kintel 已提交
3303
	function setupRenderTarget( renderTarget ) {
M
Mr.doob 已提交
3304

3305 3306
		var renderTargetProperties = properties.get( renderTarget );
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
3307

3308
		renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
M
Mr.doob 已提交
3309

3310
		textureProperties.__webglTexture = _gl.createTexture();
M
Mr.doob 已提交
3311

3312
		_infoMemory.textures ++;
M
Mr.doob 已提交
3313

3314 3315
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
		var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height );
M
Mr.doob 已提交
3316

3317
		// Setup framebuffer
M
Mr.doob 已提交
3318

3319
		if ( isCube ) {
M
Mr.doob 已提交
3320

3321
			renderTargetProperties.__webglFramebuffer = [];
M
Mr.doob 已提交
3322

3323
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3324

3325
				renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
M
Mr.doob 已提交
3326

3327
			}
M
Mr.doob 已提交
3328

3329
		} else {
M
Mr.doob 已提交
3330

3331
			renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
M
Mr.doob 已提交
3332

3333
		}
M
Mr.doob 已提交
3334

3335
		// Setup color buffer
M
Mr.doob 已提交
3336

3337
		if ( isCube ) {
M
Mr.doob 已提交
3338

3339 3340
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
			setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, isTargetPowerOfTwo );
M
Mr.doob 已提交
3341

3342
			for ( var i = 0; i < 6; i ++ ) {
M
Mr.doob 已提交
3343

3344
				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
M
Mr.doob 已提交
3345 3346 3347

			}

3348 3349
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
M
Mr.doob 已提交
3350

3351
		} else {
M
Mr.doob 已提交
3352

3353 3354 3355
			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 已提交
3356

3357 3358
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
			state.bindTexture( _gl.TEXTURE_2D, null );
M
Mr.doob 已提交
3359

3360
		}
M
Mr.doob 已提交
3361

3362
		// Setup depth and stencil buffers
3363

3364
		if ( renderTarget.depthBuffer ) {
M
Mr.doob 已提交
3365

M
Marius Kintel 已提交
3366
			setupDepthRenderbuffer( renderTarget );
M
Mr.doob 已提交
3367

3368
		}
M
Mr.doob 已提交
3369

3370
	}
M
Mr.doob 已提交
3371

3372
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
3373

3374 3375
		_currentRenderTarget = renderTarget;

3376
		if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
M
Mr.doob 已提交
3377

M
Marius Kintel 已提交
3378
			setupRenderTarget( renderTarget );
M
Mr.doob 已提交
3379 3380 3381

		}

3382
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
3383
		var framebuffer;
M
Mr.doob 已提交
3384 3385 3386

		if ( renderTarget ) {

3387
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
3388

M
Mr.doob 已提交
3389 3390
			if ( isCube ) {

3391
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
3392 3393 3394

			} else {

3395
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
3396 3397 3398

			}

3399 3400
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
3401

3402
			_currentViewport.copy( renderTarget.viewport );
M
Mr.doob 已提交
3403 3404 3405 3406 3407

		} else {

			framebuffer = null;

3408
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
3409
			_currentScissorTest = _scissorTest;
3410

3411
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
M
Mr.doob 已提交
3412 3413 3414

		}

M
Mr.doob 已提交
3415
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
3416 3417 3418 3419 3420 3421

			_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
			_currentFramebuffer = framebuffer;

		}

3422 3423
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
3424

3425
		state.viewport( _currentViewport );
3426

M
Mr.doob 已提交
3427 3428 3429 3430 3431 3432 3433
		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 已提交
3434 3435
	};

M
Mr.doob 已提交
3436
	this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer ) {
3437

M
Mr.doob 已提交
3438
		if ( renderTarget instanceof THREE.WebGLRenderTarget === false ) {
3439

3440
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
3441
			return;
3442

G
gero3 已提交
3443
		}
3444

M
Mr.doob 已提交
3445
		var framebuffer = properties.get( renderTarget ).__webglFramebuffer;
3446

M
Mr.doob 已提交
3447
		if ( framebuffer ) {
3448

G
gero3 已提交
3449
			var restore = false;
3450

M
Mr.doob 已提交
3451
			if ( framebuffer !== _currentFramebuffer ) {
3452

M
Mr.doob 已提交
3453
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3454

G
gero3 已提交
3455
				restore = true;
3456

G
gero3 已提交
3457
			}
3458

M
Mr.doob 已提交
3459
			try {
3460

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

M
Mr.doob 已提交
3463 3464
				if ( texture.format !== THREE.RGBAFormat
					&& paramThreeToGL( texture.format ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
3465

M
Mr.doob 已提交
3466 3467
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
					return;
3468

M
Mr.doob 已提交
3469
				}
3470

M
Mr.doob 已提交
3471 3472 3473 3474
				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' ) ) ) {
3475

M
Mr.doob 已提交
3476 3477
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
					return;
3478

M
Mr.doob 已提交
3479
				}
3480

M
Mr.doob 已提交
3481
				if ( _gl.checkFramebufferStatus( _gl.FRAMEBUFFER ) === _gl.FRAMEBUFFER_COMPLETE ) {
3482

M
Mr.doob 已提交
3483
					_gl.readPixels( x, y, width, height, paramThreeToGL( texture.format ), paramThreeToGL( texture.type ), buffer );
3484

M
Mr.doob 已提交
3485
				} else {
M
Mr.doob 已提交
3486

M
Mr.doob 已提交
3487 3488 3489
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );

				}
M
Mr.doob 已提交
3490

M
Mr.doob 已提交
3491
			} finally {
M
Mr.doob 已提交
3492

M
Mr.doob 已提交
3493 3494 3495
				if ( restore ) {

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, _currentFramebuffer );
M
Mr.doob 已提交
3496

M
Mr.doob 已提交
3497 3498 3499
				}

			}
M
Mr.doob 已提交
3500 3501 3502

		}

M
Mr.doob 已提交
3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
	};

	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 已提交
3514
	}
M
Mr.doob 已提交
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527

	// 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 已提交
3528
	}
M
Mr.doob 已提交
3529 3530 3531 3532 3533

	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

3534 3535
		var extension;

M
Mr.doob 已提交
3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
		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;

3560 3561 3562 3563 3564 3565 3566 3567
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

			if ( p === THREE.HalfFloatType ) return extension.HALF_FLOAT_OES;

		}

M
Mr.doob 已提交
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
		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;

3591
		extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
M
Mr.doob 已提交
3592

3593 3594 3595 3596 3597 3598
		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 已提交
3599 3600 3601

		}

3602 3603 3604
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

		if ( extension !== null ) {
P
Pierre Lepers 已提交
3605

3606 3607 3608 3609
			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 已提交
3610 3611 3612

		}

3613 3614 3615 3616 3617 3618 3619 3620
		extension = extensions.get( 'WEBGL_compressed_texture_etc1' );

		if ( extension !== null ) {

			if ( p === THREE.RGB_ETC1_Format ) return extension.COMPRESSED_RGB_ETC1_WEBGL;

		}

3621 3622 3623
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
3624

3625 3626
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
3627 3628 3629

		}

M
Mr.doob 已提交
3630 3631
		return 0;

M
Mr.doob 已提交
3632
	}
M
Mr.doob 已提交
3633 3634

};