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

THREE.WebGLRenderer = function ( parameters ) {

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

	parameters = parameters || {};

E
Eli Grey 已提交
15
	var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ),
16
	_context = parameters.context !== undefined ? parameters.context : null,
M
Mr.doob 已提交
17

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

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

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

32 33
	var morphInfluences = new Float32Array( 8 );

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

M
Mr.doob 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	// 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;

T
tschw 已提交
53 54 55 56 57
	// user-defined clipping

	this.clippingPlanes = [];
	this.localClippingEnabled = false;

M
Mr.doob 已提交
58 59
	// physically based shading

60
	this.gammaFactor = 2.0;	// for backwards compatibility
M
Mr.doob 已提交
61 62 63
	this.gammaInput = false;
	this.gammaOutput = false;

64 65
	// physical lights

66
	this.physicallyCorrectLights = false;
67

B
Ben Houston 已提交
68 69 70 71 72 73
	// tone mapping

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

M
Mr.doob 已提交
74 75 76 77 78 79 80 81 82 83 84 85
	// morphs

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

	// internal properties

	var _this = this,

	// internal state cache

	_currentProgram = null,
86
	_currentRenderTarget = null,
M
Mr.doob 已提交
87
	_currentFramebuffer = null,
88
	_currentMaterialId = - 1,
89
	_currentGeometryProgram = '',
M
Mr.doob 已提交
90 91
	_currentCamera = null,

92 93 94 95 96
	_currentScissor = new THREE.Vector4(),
	_currentScissorTest = null,

	_currentViewport = new THREE.Vector4(),

97 98
	//

M
Mr.doob 已提交
99 100
	_usedTextureUnits = 0,

M
Mr.doob 已提交
101 102 103 104 105 106 107 108 109 110 111
	//

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

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

	_pixelRatio = 1,

	_scissor = new THREE.Vector4( 0, 0, _width, _height ),
112 113
	_scissorTest = false,

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

M
Mr.doob 已提交
116 117 118 119
	// frustum

	_frustum = new THREE.Frustum(),

T
tschw 已提交
120 121
	// clipping

T
tschw 已提交
122
	_clipping = new THREE.WebGLClipping(),
T
tschw 已提交
123 124 125 126 127
	_clippingEnabled = false,
	_localClippingEnabled = false,

	_sphere = new THREE.Sphere(),

M
Mr.doob 已提交
128
	// camera matrices cache
M
Mr.doob 已提交
129 130 131 132 133 134 135 136 137

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_lights = {

138 139
		hash: '',

M
Mr.doob 已提交
140
		ambient: [ 0, 0, 0 ],
M
Mr.doob 已提交
141
		directional: [],
142 143
		directionalShadowMap: [],
		directionalShadowMatrix: [],
M
Mr.doob 已提交
144
		spot: [],
145 146
		spotShadowMap: [],
		spotShadowMatrix: [],
M
Mr.doob 已提交
147
		point: [],
148 149
		pointShadowMap: [],
		pointShadowMatrix: [],
150 151
		hemi: [],

152
		shadows: []
M
Mr.doob 已提交
153

154 155
	},

M
Mr.doob 已提交
156 157
	// info

158
	_infoRender = {
159 160 161 162 163 164

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

M
Mr.doob 已提交
165 166
	};

M
Mr.doob 已提交
167
	this.info = {
168

M
Mr.doob 已提交
169
		render: _infoRender,
170 171 172 173 174 175
		memory: {

			geometries: 0,
			textures: 0

		},
176
		programs: null
M
Mr.doob 已提交
177 178

	};
179

180

M
Mr.doob 已提交
181 182 183 184
	// initialize

	var _gl;

M
Mr.doob 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	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 已提交
200
			if ( _canvas.getContext( 'webgl' ) !== null ) {
201 202 203 204 205 206 207 208

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

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
209 210 211

		}

212 213 214 215 216 217 218 219 220 221 222 223
		// Some experimental-webgl implementations do not have getShaderPrecisionFormat

		if ( _gl.getShaderPrecisionFormat === undefined ) {

			_gl.getShaderPrecisionFormat = function () {

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

			};

		}

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

M
Mr.doob 已提交
226 227
	} catch ( error ) {

228
		console.error( 'THREE.WebGLRenderer: ' + error );
M
Mr.doob 已提交
229 230 231

	}

232 233
	var extensions = new THREE.WebGLExtensions( _gl );

234
	extensions.get( 'WEBGL_depth_texture' );
235 236
	extensions.get( 'OES_texture_float' );
	extensions.get( 'OES_texture_float_linear' );
237 238
	extensions.get( 'OES_texture_half_float' );
	extensions.get( 'OES_texture_half_float_linear' );
239
	extensions.get( 'OES_standard_derivatives' );
B
Ben Adams 已提交
240
	extensions.get( 'ANGLE_instanced_arrays' );
241

242 243
	if ( extensions.get( 'OES_element_index_uint' ) ) {

244
		THREE.BufferGeometry.MaxIndex = 4294967296;
245 246 247

	}

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

250 251
	var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
	var properties = new THREE.WebGLProperties();
252
	var textures = new THREE.WebGLTextures( _gl, extensions, state, properties, capabilities, paramThreeToGL, this.info );
253
	var objects = new THREE.WebGLObjects( _gl, properties, this.info );
G
gero3 已提交
254
	var programCache = new THREE.WebGLPrograms( this, capabilities );
M
Mr.doob 已提交
255
	var lightCache = new THREE.WebGLLights();
256

257 258
	this.info.programs = programCache.programs;

259 260
	var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
	var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
261

M
Mr.doob 已提交
262 263
	//

M
Mr.doob 已提交
264 265 266 267 268 269 270
	var backgroundCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
	var backgroundCamera2 = new THREE.PerspectiveCamera();
	var backgroundPlaneMesh = new THREE.Mesh(
		new THREE.PlaneBufferGeometry( 2, 2 ),
		new THREE.MeshBasicMaterial( { depthTest: false, depthWrite: false } )
	);
	var backgroundBoxShader = THREE.ShaderLib[ 'cube' ];
M
Mr.doob 已提交
271
	var backgroundBoxSubdiv = capabilities.logarithmicDepthBuffer ? 4 : 1; // Workaround for logarithmicDepthBuffer #9235
M
Mr.doob 已提交
272
	var backgroundBoxMesh = new THREE.Mesh(
273
		new THREE.BoxBufferGeometry( 5, 5, 5, backgroundBoxSubdiv, backgroundBoxSubdiv, backgroundBoxSubdiv ),
M
Mr.doob 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287
		new THREE.ShaderMaterial( {
			uniforms: backgroundBoxShader.uniforms,
			vertexShader: backgroundBoxShader.vertexShader,
			fragmentShader: backgroundBoxShader.fragmentShader,
			depthTest: false,
			depthWrite: false,
			side: THREE.BackSide
		} )
	);
	objects.update( backgroundPlaneMesh );
	objects.update( backgroundBoxMesh );

	//

288 289
	function getTargetPixelRatio() {

290
		return _currentRenderTarget === null ? _pixelRatio : 1;
291 292 293

	}

294
	function glClearColor( r, g, b, a ) {
295 296 297

		if ( _premultipliedAlpha === true ) {

298
			r *= a; g *= a; b *= a;
299 300 301

		}

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

304
	}
305

306
	function setDefaultGLState() {
M
Mr.doob 已提交
307

M
Mr.doob 已提交
308
		state.init();
M
Mr.doob 已提交
309

310 311
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
312

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

315
	}
316

317
	function resetGLState() {
318 319 320 321

		_currentProgram = null;
		_currentCamera = null;

322
		_currentGeometryProgram = '';
323 324
		_currentMaterialId = - 1;

M
Mr.doob 已提交
325 326
		state.reset();

327
	}
M
Mr.doob 已提交
328 329 330 331

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
332
	this.capabilities = capabilities;
333
	this.extensions = extensions;
334
	this.properties = properties;
M
Mr.doob 已提交
335
	this.state = state;
M
Mr.doob 已提交
336

M
Mr.doob 已提交
337 338
	// shadow map

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

341
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
342

M
Mr.doob 已提交
343

M
Mr.doob 已提交
344 345 346 347 348
	// Plugins

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

M
Mr.doob 已提交
349 350 351 352 353 354 355 356
	// API

	this.getContext = function () {

		return _gl;

	};

357 358 359 360 361 362
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

363 364 365 366 367 368
	this.forceContextLoss = function () {

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

	};

369
	this.getMaxAnisotropy = function () {
370

371
		return capabilities.getMaxAnisotropy();
372

373
	};
M
Mr.doob 已提交
374 375 376

	this.getPrecision = function () {

G
gero3 已提交
377
		return capabilities.precision;
M
Mr.doob 已提交
378 379 380

	};

381 382
	this.getPixelRatio = function () {

383
		return _pixelRatio;
384 385 386 387 388

	};

	this.setPixelRatio = function ( value ) {

389 390 391 392 393
		if ( value === undefined ) return;

		_pixelRatio = value;

		this.setSize( _viewport.z, _viewport.w, false );
394 395 396

	};

397 398 399
	this.getSize = function () {

		return {
400 401
			width: _width,
			height: _height
402 403 404 405
		};

	};

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

408 409 410
		_width = width;
		_height = height;

411 412
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
413

414
		if ( updateStyle !== false ) {
415

G
gero3 已提交
416 417
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
418

G
gero3 已提交
419
		}
M
Mr.doob 已提交
420

421
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
422 423 424 425 426

	};

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

427
		state.viewport( _viewport.set( x, y, width, height ) );
M
Mr.doob 已提交
428 429 430

	};

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

433
		state.scissor( _scissor.set( x, y, width, height ) );
M
Mr.doob 已提交
434 435 436

	};

437 438
	this.setScissorTest = function ( boolean ) {

439
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
440 441 442 443 444

	};

	// Clearing

M
Mr.doob 已提交
445
	this.getClearColor = function () {
M
Mr.doob 已提交
446

M
Mr.doob 已提交
447
		return _clearColor;
M
Mr.doob 已提交
448 449 450

	};

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

M
Mr.doob 已提交
453
		_clearColor.set( color );
454

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

457
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
458 459 460

	};

M
Mr.doob 已提交
461
	this.getClearAlpha = function () {
M
Mr.doob 已提交
462

M
Mr.doob 已提交
463
		return _clearAlpha;
M
Mr.doob 已提交
464 465 466

	};

M
Mr.doob 已提交
467
	this.setClearAlpha = function ( alpha ) {
M
Mr.doob 已提交
468

M
Mr.doob 已提交
469
		_clearAlpha = alpha;
M
Mr.doob 已提交
470

471
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
472 473 474 475 476 477 478 479 480 481 482 483

	};

	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 );
484 485 486 487 488

	};

	this.clearColor = function () {

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

	};

	this.clearDepth = function () {

M
Mr.doob 已提交
495
		this.clear( false, true, false );
496 497 498 499 500

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
501
		this.clear( false, false, true );
M
Mr.doob 已提交
502 503 504 505 506 507 508 509 510 511

	};

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

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

	};

M
Mr.doob 已提交
512 513
	// Reset

514
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
515

D
dubejf 已提交
516 517
	this.dispose = function() {

518 519 520 521 522
		transparentObjects = [];
		transparentObjectsLastIndex = -1;
		opaqueObjects = [];
		opaqueObjectsLastIndex = -1;

D
dubejf 已提交
523 524 525 526
		_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );

	};

M
Mr.doob 已提交
527
	// Events
M
Mr.doob 已提交
528

D
dubejf 已提交
529 530 531 532 533 534 535 536 537
	function onContextLost( event ) {

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

M
Mr.doob 已提交
538
	}
D
dubejf 已提交
539

540
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
541 542 543 544 545 546 547

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

548
	}
M
Mr.doob 已提交
549 550 551

	// Buffer deallocation

552
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
553

554 555 556 557
		releaseMaterialProgramReference( material );

		properties.delete( material );

558
	}
559 560


561
	function releaseMaterialProgramReference( material ) {
562

563
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
564 565 566

		material.program = undefined;

567
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
568

569
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
570

M
Mr.doob 已提交
571 572
		}

573
	}
M
Mr.doob 已提交
574 575 576 577 578

	// Buffer rendering

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

579
		state.initAttributes();
580

581
		var buffers = properties.get( object );
582

583 584 585 586
		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 已提交
587

588
		var attributes = program.getAttributes();
589

M
Mr.doob 已提交
590 591
		if ( object.hasPositions ) {

592
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
593
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
594

595 596
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
597 598 599 600 601

		}

		if ( object.hasNormals ) {

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

W
WestLangley 已提交
604
			if ( material.type !== 'MeshPhongMaterial' && material.type !== 'MeshStandardMaterial' && material.type !== 'MeshPhysicalMaterial' && material.shading === THREE.FlatShading ) {
M
Mr.doob 已提交
605

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

608
					var array = object.normalArray;
M
Mr.doob 已提交
609

610 611 612
					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 已提交
613

614 615 616
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
617

618 619 620
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
621

622 623 624
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
625 626 627 628 629 630

				}

			}

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

632
			state.enableAttribute( attributes.normal );
633

634
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
635 636 637 638 639

		}

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

640
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
641
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
642

643
			state.enableAttribute( attributes.uv );
644

645
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
646 647 648 649 650

		}

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

651
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
652
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
653

654
			state.enableAttribute( attributes.color );
655

656
			_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
657 658 659

		}

660
		state.disableUnusedAttributes();
661

M
Mr.doob 已提交
662 663 664 665 666 667
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

668
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
669

M
Mr.doob 已提交
670 671
		setMaterial( material );

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

M
Mr.doob 已提交
674 675
		var updateBuffers = false;
		var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
M
Mr.doob 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

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

			}

699
			activeInfluences.sort( absNumericalSort );
M
Mr.doob 已提交
700 701 702 703 704 705 706

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

707 708
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
709 710 711 712 713 714 715
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

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

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

716
					var index = influence[ 1 ];
M
Mr.doob 已提交
717

718 719
					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 已提交
720 721 722

				} else {

723 724
					if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
					if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
M
Mr.doob 已提交
725 726 727 728 729

				}

			}

T
tschw 已提交
730 731
			program.getUniforms().setValue(
					_gl, 'morphTargetInfluences', morphInfluences );
M
Mr.doob 已提交
732 733 734 735 736

			updateBuffers = true;

		}

737 738
		//

739
		var index = geometry.index;
740 741
		var position = geometry.attributes.position;

742 743
		if ( material.wireframe === true ) {

744
			index = objects.getWireframeAttribute( geometry );
745 746 747

		}

748 749
		var renderer;

750
		if ( index !== null ) {
751

752 753
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
754

755
		} else {
756

757
			renderer = bufferRenderer;
758

759
		}
M
Mr.doob 已提交
760

761
		if ( updateBuffers ) {
M
Mr.doob 已提交
762

763
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
764

765
			if ( index !== null ) {
766

767
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
768 769 770

			}

771
		}
772

773 774
		//

M
Mr.doob 已提交
775 776
		var dataStart = 0;
		var dataCount = Infinity;
777

M
Mr.doob 已提交
778
		if ( index !== null ) {
779

M
Mr.doob 已提交
780
			dataCount = index.count;
781

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

M
Mr.doob 已提交
784
			dataCount = position.count;
785

M
Mr.doob 已提交
786
		}
787

M
Mr.doob 已提交
788 789
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
790

M
Mr.doob 已提交
791 792
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
793

M
Mr.doob 已提交
794 795 796 797 798 799
		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 );

		//
800

801
		if ( object instanceof THREE.Mesh ) {
802

803
			if ( material.wireframe === true ) {
804

805
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
806
				renderer.setMode( _gl.LINES );
807

808
			} else {
M
Mr.doob 已提交
809 810

				switch ( object.drawMode ) {
811

B
Ben Adams 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824
					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;

				}
825

826
			}
827

828

829
		} else if ( object instanceof THREE.Line ) {
830

831
			var lineWidth = material.linewidth;
832

833
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
834

835
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
836

837
			if ( object instanceof THREE.LineSegments ) {
838

839
				renderer.setMode( _gl.LINES );
840

841
			} else {
842

843
				renderer.setMode( _gl.LINE_STRIP );
844 845

			}
M
Mr.doob 已提交
846

847
		} else if ( object instanceof THREE.Points ) {
848 849

			renderer.setMode( _gl.POINTS );
850 851

		}
852

J
jfranc 已提交
853
		if ( geometry instanceof THREE.InstancedBufferGeometry ) {
M
Mr.doob 已提交
854 855 856

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
859
			}
860 861 862

		} else {

M
Mr.doob 已提交
863
			renderer.render( drawStart, drawCount );
864

M
Mr.doob 已提交
865 866 867 868
		}

	};

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

M
Mr.doob 已提交
871
		var extension;
B
Ben Adams 已提交
872

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

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

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

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

M
Mr.doob 已提交
882 883 884
			}

		}
B
Ben Adams 已提交
885

886 887
		if ( startIndex === undefined ) startIndex = 0;

888 889
		state.initAttributes();

890
		var geometryAttributes = geometry.attributes;
891

892
		var programAttributes = program.getAttributes();
893

894
		var materialDefaultAttributeValues = material.defaultAttributeValues;
895

896
		for ( var name in programAttributes ) {
897

898
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
899

M
Mr.doob 已提交
900
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
901

902
				var geometryAttribute = geometryAttributes[ name ];
903

M
Mr.doob 已提交
904
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
905

906
					var type = _gl.FLOAT;
907
					var array = geometryAttribute.array;
908 909
					var normalized = geometryAttribute.normalized;

910 911
					if ( array instanceof Float32Array ) {

912
						type = _gl.FLOAT;
913 914 915 916 917 918 919

					} else if ( array instanceof Float64Array ) {

						console.warn("Unsupported data buffer format: Float64Array");

					} else if ( array instanceof Uint16Array ) {

920 921
						type = _gl.UNSIGNED_SHORT;

922 923
					} else if ( array instanceof Int16Array ) {

924
						type = _gl.SHORT;
925 926 927

					} else if ( array instanceof Uint32Array ) {

928
						type = _gl.UNSIGNED_INT;
929 930 931

					} else if ( array instanceof Int32Array ) {

932
						type = _gl.INT;
933 934 935

					} else if ( array instanceof Int8Array ) {

936
						type = _gl.BYTE;
937 938 939

					} else if ( array instanceof Uint8Array ) {

940
						type = _gl.UNSIGNED_BYTE;
941 942

					}
943

944
					var size = geometryAttribute.itemSize;
G
gero3 已提交
945
					var buffer = objects.getAttributeBuffer( geometryAttribute );
946

B
Ben Adams 已提交
947
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
948

M
Mr.doob 已提交
949 950 951 952 953
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
957
							if ( geometry.maxInstancedCount === undefined ) {
958

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

M
Mr.doob 已提交
961
							}
B
Ben Adams 已提交
962

M
Mr.doob 已提交
963
						} else {
B
Ben Adams 已提交
964

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

M
Mr.doob 已提交
967
						}
B
Ben Adams 已提交
968

M
Mr.doob 已提交
969
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
970
						_gl.vertexAttribPointer( programAttribute, size, type, normalized, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );
B
Ben Adams 已提交
971

M
Mr.doob 已提交
972
					} else {
B
Ben Adams 已提交
973

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

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

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

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

M
Mr.doob 已提交
982
							}
B
Ben Adams 已提交
983

M
Mr.doob 已提交
984 985 986 987
						} else {

							state.enableAttribute( programAttribute );

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

M
Mr.doob 已提交
990
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
991
						_gl.vertexAttribPointer( programAttribute, size, type, normalized, 0, startIndex * size * geometryAttribute.array.BYTES_PER_ELEMENT );
M
Mr.doob 已提交
992

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

995 996
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
997
					var value = materialDefaultAttributeValues[ name ];
998

999
					if ( value !== undefined ) {
M
Mr.doob 已提交
1000

1001
						switch ( value.length ) {
M
Mr.doob 已提交
1002

1003 1004 1005
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1006

1007 1008 1009
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1010

1011 1012 1013
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
1014

1015 1016
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
1017 1018

						}
M
Mr.doob 已提交
1019 1020 1021 1022 1023 1024 1025 1026

					}

				}

			}

		}
1027

1028
		state.disableUnusedAttributes();
1029

M
Mr.doob 已提交
1030 1031
	}

M
Mr.doob 已提交
1032 1033
	// Sorting

1034
	function absNumericalSort( a, b ) {
1035

1036
		return Math.abs( b[ 0 ] ) - Math.abs( a[ 0 ] );
1037 1038 1039

	}

M
Mr.doob 已提交
1040 1041
	function painterSortStable ( a, b ) {

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

U
unconed 已提交
1044
			return a.object.renderOrder - b.object.renderOrder;
1045

1046 1047 1048 1049
		} else if ( a.material.program && b.material.program && a.material.program !== b.material.program ) {

			return a.material.program.id - b.material.program.id;

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

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

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

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

		} else {

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

		}

1064
	}
M
Mr.doob 已提交
1065

1066 1067
	function reversePainterSortStable ( a, b ) {

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

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

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

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

		} else {

			return a.id - b.id;

		}

1082
	}
1083

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

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

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

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

		}

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

		// reset caching for this frame

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

		// update scene graph

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

		// update camera matrices and frustum

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

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

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

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

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

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

T
tschw 已提交
1124
		_localClippingEnabled = this.localClippingEnabled;
M
Mr.doob 已提交
1125
		_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1126

1127
		projectObject( scene, camera );
M
Mr.doob 已提交
1128

1129 1130 1131
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1132
		if ( _this.sortObjects === true ) {
1133 1134 1135

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

1137 1138
		}

M
Mr.doob 已提交
1139
		//
M
Mr.doob 已提交
1140

T
tschw 已提交
1141
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1142

1143 1144
		setupShadows( lights );

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

1147 1148
		setupLights( lights, camera );

T
tschw 已提交
1149
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1150

M
Mr.doob 已提交
1151 1152
		//

1153 1154 1155 1156
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1157

1158 1159 1160 1161 1162 1163
		if ( renderTarget === undefined ) {

			renderTarget = null;

		}

M
Mr.doob 已提交
1164 1165
		this.setRenderTarget( renderTarget );

M
Mr.doob 已提交
1166 1167 1168 1169 1170 1171 1172 1173
		//

		var background = scene.background;

		if ( background === null ) {

			glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
		} else if ( background instanceof THREE.Color ) {

			glClearColor( background.r, background.g, background.b, 1 );

		}

		if ( this.autoClear || forceClear ) {

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

		}

		if ( background instanceof THREE.CubeTexture ) {
M
Mr.doob 已提交
1187

1188
			backgroundCamera2.projectionMatrix.copy( camera.projectionMatrix );
M
Mr.doob 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203

			backgroundCamera2.matrixWorld.extractRotation( camera.matrixWorld );
			backgroundCamera2.matrixWorldInverse.getInverse( backgroundCamera2.matrixWorld );

			backgroundBoxMesh.material.uniforms[ "tCube" ].value = background;
			backgroundBoxMesh.modelViewMatrix.multiplyMatrices( backgroundCamera2.matrixWorldInverse, backgroundBoxMesh.matrixWorld );

			_this.renderBufferDirect( backgroundCamera2, null, backgroundBoxMesh.geometry, backgroundBoxMesh.material, backgroundBoxMesh, null );

		} else if ( background instanceof THREE.Texture ) {

			backgroundPlaneMesh.material.map = background;

			_this.renderBufferDirect( backgroundCamera, null, backgroundPlaneMesh.geometry, backgroundPlaneMesh.material, backgroundPlaneMesh, null );

M
Mr.doob 已提交
1204 1205
		}

1206
		//
M
Mr.doob 已提交
1207 1208 1209

		if ( scene.overrideMaterial ) {

1210
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1211

1212 1213
			renderObjects( opaqueObjects, camera, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, fog, overrideMaterial );
1214

M
Mr.doob 已提交
1215 1216 1217 1218
		} else {

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

M
Mr.doob 已提交
1219
			state.setBlending( THREE.NoBlending );
1220
			renderObjects( opaqueObjects, camera, fog );
M
Mr.doob 已提交
1221 1222 1223

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

1224
			renderObjects( transparentObjects, camera, fog );
M
Mr.doob 已提交
1225 1226 1227 1228 1229

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1230
		spritePlugin.render( scene, camera );
1231
		lensFlarePlugin.render( scene, camera, _currentViewport );
M
Mr.doob 已提交
1232 1233 1234

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

M
Mr.doob 已提交
1235 1236
		if ( renderTarget ) {

1237
			textures.updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1238 1239 1240

		}

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

M
Mr.doob 已提交
1243 1244
		state.setDepthTest( true );
		state.setDepthWrite( true );
1245
		state.setColorWrite( true );
M
Mr.doob 已提交
1246 1247 1248 1249

		// _gl.finish();

	};
M
Mr.doob 已提交
1250

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

1253
		var array, index;
M
Mr.doob 已提交
1254

1255
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1256 1257 1258

		if ( material.transparent ) {

1259 1260
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1261 1262 1263

		} else {

1264 1265 1266 1267 1268
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1269 1270
		// recycle existing render item or grow the array

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
		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 已提交
1281 1282 1283

		} else {

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
			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 已提交
1295 1296 1297 1298 1299

		}

	}

M
Mr.doob 已提交
1300 1301
	// TODO Duplicated code (Frustum)

T
tschw 已提交
1302 1303 1304 1305 1306 1307 1308
	function isObjectViewable( object ) {

		var geometry = object.geometry;

		if ( geometry.boundingSphere === null )
			geometry.computeBoundingSphere();

M
Mr.doob 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		_sphere.copy( geometry.boundingSphere ).
			applyMatrix4( object.matrixWorld );

		return isSphereViewable( _sphere );

	}

	function isSpriteViewable( sprite ) {

		_sphere.center.set( 0, 0, 0 );
		_sphere.radius = 0.7071067811865476;
		_sphere.applyMatrix4( sprite.matrixWorld );

		return isSphereViewable( _sphere );

	}

	function isSphereViewable( sphere ) {
T
tschw 已提交
1327 1328

		if ( ! _frustum.intersectsSphere( sphere ) ) return false;
T
tschw 已提交
1329 1330 1331 1332

		var numPlanes = _clipping.numPlanes;

		if ( numPlanes === 0 ) return true;
T
tschw 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344

		var planes = _this.clippingPlanes,

			center = sphere.center,
			negRad = - sphere.radius,
			i = 0;

		do {

			// out when deeper than radius in the negative halfspace
			if ( planes[ i ].distanceToPoint( center ) < negRad ) return false;

T
tschw 已提交
1345
		} while ( ++ i !== numPlanes );
T
tschw 已提交
1346 1347 1348 1349 1350

		return true;

	}

1351
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1352

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

1355
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1356

1357
			if ( object instanceof THREE.Light ) {
M
Mr.doob 已提交
1358

1359
				lights.push( object );
M
Mr.doob 已提交
1360

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

M
Mr.doob 已提交
1363
				if ( object.frustumCulled === false || isSpriteViewable( object ) === true ) {
1364 1365 1366 1367

					sprites.push( object );

				}
M
Mr.doob 已提交
1368

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

1371
				lensFlares.push( object );
M
Mr.doob 已提交
1372

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

1375
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1376

1377 1378
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1379

1380
				}
1381

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

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

1386
				if ( object instanceof THREE.SkinnedMesh ) {
M
Mr.doob 已提交
1387

1388
					object.skeleton.update();
1389

1390
				}
1391

T
tschw 已提交
1392
				if ( object.frustumCulled === false || isObjectViewable( object ) === true ) {
1393

1394
					var material = object.material;
1395

1396
					if ( material.visible === true ) {
M
Mr.doob 已提交
1397

1398
						if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1399

1400 1401 1402 1403
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1404

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

S
SUNAG 已提交
1407
						if ( material instanceof THREE.MultiMaterial ) {
1408

1409 1410
							var groups = geometry.groups;
							var materials = material.materials;
1411

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

1414 1415
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1416

1417
								if ( groupMaterial.visible === true ) {
1418

1419 1420 1421
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1422

M
Mr.doob 已提交
1423
							}
M
Mr.doob 已提交
1424

1425
						} else {
M
Mr.doob 已提交
1426

1427
							pushRenderItem( object, geometry, material, _vector3.z, null );
1428

1429
						}
O
OpenShift guest 已提交
1430

1431
					}
M
Mr.doob 已提交
1432

1433
				}
M
Mr.doob 已提交
1434

1435
			}
M
Mr.doob 已提交
1436

M
Mr.doob 已提交
1437
		}
M
Mr.doob 已提交
1438

M
Mr.doob 已提交
1439
		var children = object.children;
M
Mr.doob 已提交
1440

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

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

1445
		}
1446

1447
	}
M
Mr.doob 已提交
1448

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

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

1453
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1454

1455
			var object = renderItem.object;
M
Mr.doob 已提交
1456 1457 1458
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1459

1460 1461
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1462

M
Mr.doob 已提交
1463
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1464

M
Mr.doob 已提交
1465
				setMaterial( material );
M
Mr.doob 已提交
1466

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

M
Mr.doob 已提交
1469
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1470

M
Mr.doob 已提交
1471
				object.render( function ( object ) {
M
Mr.doob 已提交
1472

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

M
Mr.doob 已提交
1475
				} );
1476

M
Mr.doob 已提交
1477
			} else {
M
Mr.doob 已提交
1478

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

M
Mr.doob 已提交
1481
			}
M
Mr.doob 已提交
1482

1483
		}
M
Mr.doob 已提交
1484

1485
	}
G
gero3 已提交
1486

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

1489
		var materialProperties = properties.get( material );
G
gero3 已提交
1490

T
tschw 已提交
1491
		var parameters = programCache.getParameters(
T
tschw 已提交
1492
				material, _lights, fog, _clipping.numPlanes, object );
T
tschw 已提交
1493

G
gero3 已提交
1494
		var code = programCache.getProgramCode( material, parameters );
G
gero3 已提交
1495

1496
		var program = materialProperties.program;
T
tschw 已提交
1497
		var programChange = true;
1498

1499
		if ( program === undefined ) {
B
Ben Adams 已提交
1500

M
Mr.doob 已提交
1501 1502
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1503

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

M
Mr.doob 已提交
1506
			// changed glsl or parameters
1507
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1508

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

T
tschw 已提交
1511
			// same glsl and uniform list
T
tschw 已提交
1512 1513
			return;

T
tschw 已提交
1514
		} else {
B
Ben Adams 已提交
1515

T
tschw 已提交
1516 1517
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1518 1519 1520

		}

1521
		if ( programChange ) {
B
Ben Adams 已提交
1522

1523
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1524

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

1527 1528 1529 1530 1531 1532
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1533

1534
			} else {
B
Ben Adams 已提交
1535

1536 1537 1538 1539 1540 1541
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1542

1543
			}
G
gero3 已提交
1544

1545
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1546

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

1549 1550
			materialProperties.program = program;
			material.program = program;
1551 1552 1553

		}

1554
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1555 1556 1557 1558 1559

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

M
Mr.doob 已提交
1562
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

M
Mr.doob 已提交
1578
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587

					material.numSupportedMorphNormals ++;

				}

			}

		}

T
tschw 已提交
1588 1589 1590 1591 1592 1593
		var uniforms = materialProperties.__webglShader.uniforms;

		if ( ! ( material instanceof THREE.ShaderMaterial ) &&
				! ( material instanceof THREE.RawShaderMaterial ) ||
				material.clipping === true ) {

T
tschw 已提交
1594 1595
			materialProperties.numClippingPlanes = _clipping.numPlanes;
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1596 1597 1598

		}

M
Mr.doob 已提交
1599
		if ( material.lights ) {
1600

1601 1602
			// store the light setup it was created for

1603 1604
			materialProperties.lightsHash = _lights.hash;

1605 1606 1607 1608 1609
			// 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 已提交
1610
			uniforms.pointLights.value = _lights.point;
1611 1612
			uniforms.hemisphereLights.value = _lights.hemi;

1613 1614 1615 1616 1617 1618
			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;
1619

1620 1621
		}

T
tschw 已提交
1622 1623 1624
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
					THREE.WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1625

T
tschw 已提交
1626 1627 1628
		materialProperties.uniformsList = uniformsList;
		materialProperties.dynamicUniforms =
				THREE.WebGLUniforms.splitDynamic( uniformsList, uniforms );
A
arose 已提交
1629

M
Mr.doob 已提交
1630
	}
M
Mr.doob 已提交
1631

1632 1633
	function setMaterial( material ) {

T
tschw 已提交
1634 1635 1636 1637 1638 1639
		if ( material.side !== THREE.DoubleSide )
			state.enable( _gl.CULL_FACE );
		else
			state.disable( _gl.CULL_FACE );

		state.setFlipSided( material.side === THREE.BackSide );
M
Mr.doob 已提交
1640

1641 1642
		if ( material.transparent === true ) {

1643
			state.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha );
1644

1645 1646 1647 1648
		} else {

			state.setBlending( THREE.NoBlending );

1649 1650
		}

B
Ben Adams 已提交
1651
		state.setDepthFunc( material.depthFunc );
M
Mr.doob 已提交
1652 1653
		state.setDepthTest( material.depthTest );
		state.setDepthWrite( material.depthWrite );
1654
		state.setColorWrite( material.colorWrite );
M
Mr.doob 已提交
1655
		state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
1656 1657 1658

	}

1659
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1660 1661 1662

		_usedTextureUnits = 0;

1663
		var materialProperties = properties.get( material );
1664

T
tschw 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
		if ( _clippingEnabled ) {

			if ( _localClippingEnabled || camera !== _currentCamera ) {

				var useCache =
						camera === _currentCamera &&
						material.id === _currentMaterialId;

				// we might want to call this function with some ClippingGroup
				// object instead of the material, once it becomes feasible
				// (#8465, #8379)
T
tschw 已提交
1676
				_clipping.setState(
T
tschw 已提交
1677 1678 1679 1680 1681 1682
						material.clippingPlanes, material.clipShadows,
						camera, materialProperties, useCache );

			}

			if ( materialProperties.numClippingPlanes !== undefined &&
T
tschw 已提交
1683
				materialProperties.numClippingPlanes !== _clipping.numPlanes ) {
T
tschw 已提交
1684 1685 1686 1687 1688 1689 1690

				material.needsUpdate = true;

			}

		}

1691 1692 1693 1694 1695 1696
		if ( materialProperties.program === undefined ) {

			material.needsUpdate = true;

		}

1697 1698
		if ( materialProperties.lightsHash !== undefined &&
			materialProperties.lightsHash !== _lights.hash ) {
1699 1700 1701 1702 1703 1704

			material.needsUpdate = true;

		}

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

1706
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1707 1708 1709 1710
			material.needsUpdate = false;

		}

1711
		var refreshProgram = false;
M
Mr.doob 已提交
1712
		var refreshMaterial = false;
1713
		var refreshLights = false;
M
Mr.doob 已提交
1714

1715
		var program = materialProperties.program,
1716
			p_uniforms = program.getUniforms(),
1717
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1718

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

1721 1722
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1723

1724
			refreshProgram = true;
M
Mr.doob 已提交
1725
			refreshMaterial = true;
1726
			refreshLights = true;
M
Mr.doob 已提交
1727 1728 1729 1730 1731 1732

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1733

M
Mr.doob 已提交
1734 1735 1736 1737
			refreshMaterial = true;

		}

1738
		if ( refreshProgram || camera !== _currentCamera ) {
M
Mr.doob 已提交
1739

T
tschw 已提交
1740
			p_uniforms.set( _gl, camera, 'projectionMatrix' );
M
Mr.doob 已提交
1741

G
gero3 已提交
1742
			if ( capabilities.logarithmicDepthBuffer ) {
1743

T
tschw 已提交
1744 1745
				p_uniforms.setValue( _gl, 'logDepthBufFC',
						2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1746 1747 1748 1749

			}


1750 1751 1752 1753 1754 1755 1756 1757 1758
			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 已提交
1759
				refreshLights = true;		// remains set until update done
1760 1761

			}
M
Mr.doob 已提交
1762

1763 1764 1765 1766 1767
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1768
				 material instanceof THREE.MeshStandardMaterial ||
1769 1770
				 material.envMap ) {

T
tschw 已提交
1771 1772 1773
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1774

T
tschw 已提交
1775 1776
					uCamPos.setValue( _gl,
							_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1777 1778 1779 1780 1781 1782 1783

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1784
				 material instanceof THREE.MeshBasicMaterial ||
1785
				 material instanceof THREE.MeshStandardMaterial ||
1786 1787 1788
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

T
tschw 已提交
1789
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1790 1791 1792

			}

T
tschw 已提交
1793 1794
			p_uniforms.set( _gl, _this, 'toneMappingExposure' );
			p_uniforms.set( _gl, _this, 'toneMappingWhitePoint' );
M
Mr.doob 已提交
1795

M
Mr.doob 已提交
1796 1797 1798 1799 1800 1801 1802 1803
		}

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

T
tschw 已提交
1804 1805
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1806

T
tschw 已提交
1807
			var skeleton = object.skeleton;
1808

T
tschw 已提交
1809
			if ( skeleton ) {
1810

T
tschw 已提交
1811
				if ( capabilities.floatVertexTextures && skeleton.useVertexTexture ) {
M
Mr.doob 已提交
1812

T
tschw 已提交
1813 1814 1815
					p_uniforms.set( _gl, skeleton, 'boneTexture' );
					p_uniforms.set( _gl, skeleton, 'boneTextureWidth' );
					p_uniforms.set( _gl, skeleton, 'boneTextureHeight' );
M
Mr.doob 已提交
1816

T
tschw 已提交
1817
				} else {
M
Mr.doob 已提交
1818

T
tschw 已提交
1819
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828

				}

			}

		}

		if ( refreshMaterial ) {

M
Mr.doob 已提交
1829
			if ( material.lights ) {
M
Mr.doob 已提交
1830

1831
				// the current material requires lighting info
M
Mr.doob 已提交
1832

T
tschw 已提交
1833 1834 1835 1836 1837 1838
				// 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 已提交
1839

T
tschw 已提交
1840
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1841

T
tschw 已提交
1842
			}
G
gero3 已提交
1843

T
tschw 已提交
1844
			// refresh uniforms common to several materials
G
gero3 已提交
1845

T
tschw 已提交
1846
			if ( fog && material.fog ) {
G
gero3 已提交
1847

T
tschw 已提交
1848
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1849 1850 1851 1852 1853

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1854
				 material instanceof THREE.MeshPhongMaterial ||
W
WestLangley 已提交
1855 1856
				 material instanceof THREE.MeshStandardMaterial ||
				 material instanceof THREE.MeshDepthMaterial ) {
M
Mr.doob 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872

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

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

M
Mr.doob 已提交
1875
				refreshUniformsPoints( m_uniforms, material );
M
Mr.doob 已提交
1876

1877 1878 1879 1880
			} else if ( material instanceof THREE.MeshLambertMaterial ) {

				refreshUniformsLambert( m_uniforms, material );

M
Mr.doob 已提交
1881 1882 1883 1884
			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

W
WestLangley 已提交
1885 1886 1887 1888
			} else if ( material instanceof THREE.MeshPhysicalMaterial ) {

				refreshUniformsPhysical( m_uniforms, material );

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

1891
				refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1892

M
Mr.doob 已提交
1893 1894
			} else if ( material instanceof THREE.MeshDepthMaterial ) {

1895 1896 1897 1898 1899 1900 1901 1902
				if ( material.displacementMap ) {

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

				}

M
Mr.doob 已提交
1903 1904 1905 1906 1907 1908
			} else if ( material instanceof THREE.MeshNormalMaterial ) {

				m_uniforms.opacity.value = material.opacity;

			}

T
tschw 已提交
1909 1910
			THREE.WebGLUniforms.upload(
					_gl, materialProperties.uniformsList, m_uniforms, _this );
A
arose 已提交
1911 1912 1913

		}

M
Mr.doob 已提交
1914

T
tschw 已提交
1915
		// common matrices
M
Mr.doob 已提交
1916

T
tschw 已提交
1917 1918 1919
		p_uniforms.set( _gl, object, 'modelViewMatrix' );
		p_uniforms.set( _gl, object, 'normalMatrix' );
		p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
A
arose 已提交
1920 1921


T
tschw 已提交
1922
		// dynamic uniforms
A
arose 已提交
1923

T
tschw 已提交
1924
		var dynUniforms = materialProperties.dynamicUniforms;
A
arose 已提交
1925

T
tschw 已提交
1926
		if ( dynUniforms !== null ) {
A
arose 已提交
1927

T
tschw 已提交
1928 1929
			THREE.WebGLUniforms.evalDynamic(
					dynUniforms, m_uniforms, object, camera );
A
arose 已提交
1930

T
tschw 已提交
1931
			THREE.WebGLUniforms.upload( _gl, dynUniforms, m_uniforms, _this );
A
arose 已提交
1932 1933 1934

		}

T
tschw 已提交
1935
		return program;
A
arose 已提交
1936 1937 1938

	}

M
Mr.doob 已提交
1939 1940 1941 1942 1943 1944
	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

1945
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1946

1947
		if ( material.emissive ) {
M
Mr.doob 已提交
1948

1949
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1950 1951 1952

		}

1953 1954 1955
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
1956

1957
		if ( material.aoMap ) {
1958

1959 1960
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
1961 1962 1963

		}

M
Mr.doob 已提交
1964
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
1965 1966 1967 1968 1969
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
1970
		// 6. emissive map
M
Mr.doob 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

1982 1983 1984 1985
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
1986 1987 1988 1989 1990 1991 1992 1993
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

1994 1995 1996 1997 1998 1999 2000 2001
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

2002 2003 2004 2005
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

2006 2007 2008 2009
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
2010 2011 2012 2013
		}

		if ( uvScaleMap !== undefined ) {

2014
			// backwards compatibility
M
Mr.doob 已提交
2015 2016 2017 2018 2019 2020
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) {

				uvScaleMap = uvScaleMap.texture;

			}

M
Mr.doob 已提交
2021 2022 2023 2024 2025 2026 2027 2028
			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;
2029 2030 2031 2032 2033 2034

		// don't flip CubeTexture envMaps, flip everything else:
		//  WebGLRenderTargetCube will be flipped for backwards compatibility
		//  WebGLRenderTargetCube.texture will be flipped because it's a Texture and NOT a CubeTexture
		// this check must be handled differently, or removed entirely, if WebGLRenderTargetCube uses a CubeTexture in the future
		uniforms.flipEnvMap.value = ( ! ( material.envMap instanceof THREE.CubeTexture ) ) ? 1 : - 1;
M
Mr.doob 已提交
2035

2036
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
2037 2038
		uniforms.refractionRatio.value = material.refractionRatio;

M
Mr.doob 已提交
2039
	}
M
Mr.doob 已提交
2040 2041 2042 2043 2044 2045

	function refreshUniformsLine ( uniforms, material ) {

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

M
Mr.doob 已提交
2046
	}
M
Mr.doob 已提交
2047 2048 2049 2050 2051 2052 2053

	function refreshUniformsDash ( uniforms, material ) {

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

M
Mr.doob 已提交
2054
	}
M
Mr.doob 已提交
2055

M
Mr.doob 已提交
2056
	function refreshUniformsPoints ( uniforms, material ) {
M
Mr.doob 已提交
2057

2058
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2059
		uniforms.opacity.value = material.opacity;
2060
		uniforms.size.value = material.size * _pixelRatio;
T
tschw 已提交
2061
		uniforms.scale.value = _canvas.clientHeight * 0.5;
M
Mr.doob 已提交
2062 2063 2064

		uniforms.map.value = material.map;

2065 2066 2067 2068 2069 2070 2071 2072 2073
		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 已提交
2074
	}
M
Mr.doob 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090

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

2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
	function refreshUniformsLambert ( uniforms, material ) {

		if ( material.lightMap ) {

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

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2110 2111
	function refreshUniformsPhong ( uniforms, material ) {

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

2115 2116 2117 2118
		if ( material.lightMap ) {

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

2120
		}
2121

2122
		if ( material.emissiveMap ) {
2123

2124
			uniforms.emissiveMap.value = material.emissiveMap;
2125

2126
		}
M
Mr.doob 已提交
2127

2128 2129 2130 2131
		if ( material.bumpMap ) {

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

2133
		}
M
Mr.doob 已提交
2134

2135 2136 2137 2138 2139 2140
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2141

2142 2143 2144 2145 2146
		if ( material.displacementMap ) {

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

2148
		}
2149 2150 2151

	}

2152
	function refreshUniformsStandard ( uniforms, material ) {
W
WestLangley 已提交
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212

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

		if ( material.roughnessMap ) {

			uniforms.roughnessMap.value = material.roughnessMap;

		}

		if ( material.metalnessMap ) {

			uniforms.metalnessMap.value = material.metalnessMap;

		}

		if ( material.lightMap ) {

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

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

		if ( material.bumpMap ) {

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

		}

		if ( material.normalMap ) {

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

		}

		if ( material.displacementMap ) {

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

		}

		if ( material.envMap ) {

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

		}

	}

W
WestLangley 已提交
2213 2214
	function refreshUniformsPhysical ( uniforms, material ) {

2215 2216 2217
		uniforms.clearCoat.value = material.clearCoat;
		uniforms.clearCoatRoughness.value = material.clearCoatRoughness;

W
WestLangley 已提交
2218 2219 2220 2221
		refreshUniformsStandard( uniforms, material );

	}

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

M
Mr.doob 已提交
2224
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2225

M
Mr.doob 已提交
2226
		uniforms.ambientLightColor.needsUpdate = value;
2227

B
Ben Houston 已提交
2228 2229 2230 2231
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
		uniforms.hemisphereLights.needsUpdate = value;
2232

M
Mr.doob 已提交
2233
	}
2234

T
tschw 已提交
2235
	// Lighting
M
Mr.doob 已提交
2236

2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
	function setupShadows ( lights ) {

		var lightShadowsLength = 0;

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

			var light = lights[ i ];

			if ( light.castShadow ) {

				_lights.shadows[ lightShadowsLength ++ ] = light;

			}

		}

		_lights.shadows.length = lightShadowsLength;

	}

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

B
brason 已提交
2259
		var l, ll, light,
M
Mr.doob 已提交
2260
		r = 0, g = 0, b = 0,
B
Ben Houston 已提交
2261
		color,
B
brason 已提交
2262
		intensity,
M
Mr.doob 已提交
2263
		distance,
2264
		shadowMap,
M
Mr.doob 已提交
2265

2266
		viewMatrix = camera.matrixWorldInverse,
M
Mr.doob 已提交
2267

M
Mr.doob 已提交
2268
		directionalLength = 0,
M
Mr.doob 已提交
2269 2270
		pointLength = 0,
		spotLength = 0,
2271
		hemiLength = 0;
M
Mr.doob 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280

		for ( l = 0, ll = lights.length; l < ll; l ++ ) {

			light = lights[ l ];

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

2281
			shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
2282

M
Mr.doob 已提交
2283 2284
			if ( light instanceof THREE.AmbientLight ) {

2285 2286 2287
				r += color.r * intensity;
				g += color.g * intensity;
				b += color.b * intensity;
M
Mr.doob 已提交
2288 2289 2290

			} else if ( light instanceof THREE.DirectionalLight ) {

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

2293
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
M
Mr.doob 已提交
2294
				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
2295
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2296 2297 2298
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2299
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2300

2301
				if ( light.castShadow ) {
M
Mr.doob 已提交
2302

2303 2304 2305
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2306

2307 2308
				}

2309
				_lights.directionalShadowMap[ directionalLength ] = shadowMap;
2310
				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
2311
				_lights.directional[ directionalLength ++ ] = uniforms;
M
Mr.doob 已提交
2312

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

M
Mr.doob 已提交
2315
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2316 2317 2318

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

M
Mr.doob 已提交
2320 2321 2322 2323 2324 2325 2326 2327
				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 );

2328 2329
				uniforms.coneCos = Math.cos( light.angle );
				uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
M
Mr.doob 已提交
2330
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2331

2332
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2333

2334 2335
				if ( light.castShadow ) {

2336 2337 2338
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2339

2340 2341
				}

2342
				_lights.spotShadowMap[ spotLength ] = shadowMap;
2343
				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
M
Mr.doob 已提交
2344
				_lights.spot[ spotLength ++ ] = uniforms;
2345

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

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

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

M
Mr.doob 已提交
2353 2354
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
				uniforms.distance = light.distance;
M
Mr.doob 已提交
2355 2356
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;

2357
				uniforms.shadow = light.castShadow;
2358

M
Mr.doob 已提交
2359
				if ( light.castShadow ) {
2360

2361 2362 2363 2364 2365
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;

				}
2366

2367
				_lights.pointShadowMap[ pointLength ] = shadowMap;
2368

2369 2370 2371
				if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) {

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

2373 2374
				}

2375 2376 2377 2378 2379
				// 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 已提交
2380
				_lights.point[ pointLength ++ ] = uniforms;
2381

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

M
Mr.doob 已提交
2384
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2385 2386 2387 2388

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

M
Mr.doob 已提交
2390 2391
				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
M
Mr.doob 已提交
2392

M
Mr.doob 已提交
2393
				_lights.hemi[ hemiLength ++ ] = uniforms;
M
Mr.doob 已提交
2394 2395 2396 2397 2398

			}

		}

M
Mr.doob 已提交
2399 2400 2401
		_lights.ambient[ 0 ] = r;
		_lights.ambient[ 1 ] = g;
		_lights.ambient[ 2 ] = b;
M
Mr.doob 已提交
2402

M
Mr.doob 已提交
2403 2404
		_lights.directional.length = directionalLength;
		_lights.spot.length = spotLength;
2405
		_lights.point.length = pointLength;
M
Mr.doob 已提交
2406
		_lights.hemi.length = hemiLength;
2407

2408
		_lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + _lights.shadows.length;
2409

M
Mr.doob 已提交
2410
	}
M
Mr.doob 已提交
2411 2412 2413 2414 2415

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

T
tschw 已提交
2416 2417
		state.setCullFace( cullFace );
		state.setFlipSided( frontFaceDirection === THREE.FrontFaceDirectionCW );
M
Mr.doob 已提交
2418 2419 2420 2421 2422

	};

	// Textures

T
tschw 已提交
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
	function allocTextureUnit() {

		var textureUnit = _usedTextureUnits;

		if ( textureUnit >= capabilities.maxTextures ) {

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

		}

		_usedTextureUnits += 1;

		return textureUnit;

	}

2439
	this.allocTextureUnit = allocTextureUnit;
T
tschw 已提交
2440

2441
	// this.setTexture2D = setTexture2D;
2442
	this.setTexture2D = ( function() {
T
tschw 已提交
2443

2444
		var warned = false;
T
tschw 已提交
2445

2446
		// backwards compatibility: peel texture.texture
W
WestLangley 已提交
2447
		return function setTexture2D( texture, slot ) {
T
tschw 已提交
2448

2449
			if ( texture instanceof THREE.WebGLRenderTarget ) {
T
tschw 已提交
2450

2451
				if ( ! warned ) {
T
tschw 已提交
2452

2453 2454
					console.warn( "THREE.WebGLRenderer.setTexture2D: don't use render targets as textures. Use their .texture property instead." );
					warned = true;
T
tschw 已提交
2455

2456
				}
T
tschw 已提交
2457

2458
				texture = texture.texture;
T
tschw 已提交
2459

2460
			}
T
tschw 已提交
2461

2462
			textures.setTexture2D( texture, slot );
T
tschw 已提交
2463

2464
		};
T
tschw 已提交
2465

2466
	}() );
T
tschw 已提交
2467

2468 2469 2470 2471
	this.setTexture = ( function() {

		var warned = false;

W
WestLangley 已提交
2472
		return function setTexture( texture, slot ) {
2473 2474 2475 2476 2477 2478 2479 2480

			if ( ! warned ) {

				console.warn( "THREE.WebGLRenderer: .setTexture is deprecated, use setTexture2D instead." );
				warned = true;

			}

2481
			textures.setTexture2D( texture, slot );
2482 2483 2484 2485 2486 2487 2488 2489 2490

		};

	}() );

	this.setTextureCube = ( function() {

		var warned = false;

W
WestLangley 已提交
2491
		return function setTextureCube( texture, slot ) {
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514

			// backwards compatibility: peel texture.texture
			if ( texture instanceof THREE.WebGLRenderTargetCube ) {

				if ( ! warned ) {

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

				}

				texture = texture.texture;

			}

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

				// CompressedTexture can have Array in image :/

				// this function alone should take care of cube textures
2515
				textures.setTextureCube( texture, slot );
2516 2517 2518 2519 2520

			} else {

				// assumed: texture property of THREE.WebGLRenderTargetCube

2521
				textures.setTextureCubeDynamic( texture, slot );
2522 2523 2524 2525 2526 2527

			}

		};

	}() );
T
tschw 已提交
2528

2529 2530 2531 2532
	this.getCurrentRenderTarget = function() {

		return _currentRenderTarget;

M
Michael Herzog 已提交
2533
	};
2534

2535
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
2536

2537 2538
		_currentRenderTarget = renderTarget;

2539
		if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
M
Mr.doob 已提交
2540

2541
			textures.setupRenderTarget( renderTarget );
M
Mr.doob 已提交
2542 2543 2544

		}

2545
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
2546
		var framebuffer;
M
Mr.doob 已提交
2547 2548 2549

		if ( renderTarget ) {

2550
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
2551

M
Mr.doob 已提交
2552 2553
			if ( isCube ) {

2554
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
2555 2556 2557

			} else {

2558
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
2559 2560 2561

			}

2562 2563
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
2564

2565
			_currentViewport.copy( renderTarget.viewport );
M
Mr.doob 已提交
2566 2567 2568 2569 2570

		} else {

			framebuffer = null;

2571
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
2572
			_currentScissorTest = _scissorTest;
2573

2574
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
M
Mr.doob 已提交
2575 2576 2577

		}

M
Mr.doob 已提交
2578
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
2579 2580 2581 2582 2583 2584

			_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
			_currentFramebuffer = framebuffer;

		}

2585 2586
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
2587

2588
		state.viewport( _currentViewport );
2589

M
Mr.doob 已提交
2590 2591 2592
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
2593
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, renderTarget.activeMipMapLevel );
M
Mr.doob 已提交
2594 2595 2596

		}

M
Mr.doob 已提交
2597 2598
	};

M
Mr.doob 已提交
2599
	this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer ) {
2600

M
Mr.doob 已提交
2601
		if ( renderTarget instanceof THREE.WebGLRenderTarget === false ) {
2602

2603
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
2604
			return;
2605

G
gero3 已提交
2606
		}
2607

M
Mr.doob 已提交
2608
		var framebuffer = properties.get( renderTarget ).__webglFramebuffer;
2609

M
Mr.doob 已提交
2610
		if ( framebuffer ) {
2611

G
gero3 已提交
2612
			var restore = false;
2613

M
Mr.doob 已提交
2614
			if ( framebuffer !== _currentFramebuffer ) {
2615

M
Mr.doob 已提交
2616
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
2617

G
gero3 已提交
2618
				restore = true;
2619

G
gero3 已提交
2620
			}
2621

M
Mr.doob 已提交
2622
			try {
2623

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

M
Michael Herzog 已提交
2626
				if ( texture.format !== THREE.RGBAFormat && paramThreeToGL( texture.format ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
2627

M
Mr.doob 已提交
2628 2629
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
					return;
2630

M
Mr.doob 已提交
2631
				}
2632

M
Michael Herzog 已提交
2633 2634 2635 2636
				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' ) ) ) {
2637

M
Mr.doob 已提交
2638 2639
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
					return;
2640

M
Mr.doob 已提交
2641
				}
2642

M
Mr.doob 已提交
2643
				if ( _gl.checkFramebufferStatus( _gl.FRAMEBUFFER ) === _gl.FRAMEBUFFER_COMPLETE ) {
2644

2645 2646
					// the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604)

2647
					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
2648 2649 2650 2651

						_gl.readPixels( x, y, width, height, paramThreeToGL( texture.format ), paramThreeToGL( texture.type ), buffer );

					}
2652

M
Mr.doob 已提交
2653
				} else {
M
Mr.doob 已提交
2654

M
Mr.doob 已提交
2655 2656 2657
					console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );

				}
M
Mr.doob 已提交
2658

M
Mr.doob 已提交
2659
			} finally {
M
Mr.doob 已提交
2660

M
Mr.doob 已提交
2661 2662 2663
				if ( restore ) {

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, _currentFramebuffer );
M
Mr.doob 已提交
2664

M
Mr.doob 已提交
2665 2666 2667
				}

			}
M
Mr.doob 已提交
2668 2669 2670

		}

M
Mr.doob 已提交
2671 2672
	};

M
Mr.doob 已提交
2673 2674 2675 2676
	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

2677 2678
		var extension;

M
Mr.doob 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
		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;

2703 2704 2705 2706 2707 2708 2709 2710
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

			if ( p === THREE.HalfFloatType ) return extension.HALF_FLOAT_OES;

		}

M
Mr.doob 已提交
2711 2712 2713 2714 2715
		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;
2716
		if ( p === THREE.DepthFormat ) return _gl.DEPTH_COMPONENT;
M
Mr.doob 已提交
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734

		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;

2735
		extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
M
Mr.doob 已提交
2736

2737 2738 2739 2740 2741 2742
		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 已提交
2743 2744 2745

		}

2746 2747 2748
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

		if ( extension !== null ) {
P
Pierre Lepers 已提交
2749

2750 2751 2752 2753
			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 已提交
2754 2755 2756

		}

2757 2758 2759 2760 2761 2762 2763 2764
		extension = extensions.get( 'WEBGL_compressed_texture_etc1' );

		if ( extension !== null ) {

			if ( p === THREE.RGB_ETC1_Format ) return extension.COMPRESSED_RGB_ETC1_WEBGL;

		}

2765 2766 2767
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
2768

2769 2770
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
2771 2772 2773

		}

M
Mr.doob 已提交
2774 2775
		return 0;

M
Mr.doob 已提交
2776
	}
M
Mr.doob 已提交
2777 2778

};