WebGLRenderer.js 61.4 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 15

	parameters = parameters || {};

	var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( '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 );

34 35
	var emptyTexture = new THREE.Texture();

M
Mr.doob 已提交
36 37 38
	var sprites = [];
	var lensFlares = [];

M
Mr.doob 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	// 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 已提交
55 56 57 58 59
	// user-defined clipping

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

M
Mr.doob 已提交
60 61
	// physically based shading

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

66 67
	// physical lights

68
	this.physicallyCorrectLights = false;
69

B
Ben Houston 已提交
70 71 72 73 74 75
	// tone mapping

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

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

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

	// internal properties

	var _this = this,

	// internal state cache

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

94 95 96 97 98
	_currentScissor = new THREE.Vector4(),
	_currentScissorTest = null,

	_currentViewport = new THREE.Vector4(),

99 100
	//

M
Mr.doob 已提交
101 102
	_usedTextureUnits = 0,

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

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

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

	_pixelRatio = 1,

	_scissor = new THREE.Vector4( 0, 0, _width, _height ),
114 115
	_scissorTest = false,

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

M
Mr.doob 已提交
118 119 120 121
	// frustum

	_frustum = new THREE.Frustum(),

T
tschw 已提交
122 123
	// clipping

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

	_sphere = new THREE.Sphere(),

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

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_lights = {

140 141
		hash: '',

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

154
		shadows: []
M
Mr.doob 已提交
155

156 157
	},

M
Mr.doob 已提交
158 159
	// info

160
	_infoRender = {
161 162 163 164 165 166

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

M
Mr.doob 已提交
167 168
	};

M
Mr.doob 已提交
169
	this.info = {
170

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

			geometries: 0,
			textures: 0

		},
178
		programs: null
M
Mr.doob 已提交
179 180

	};
181

182

M
Mr.doob 已提交
183 184 185 186
	// initialize

	var _gl;

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

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

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
211 212 213

		}

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

		if ( _gl.getShaderPrecisionFormat === undefined ) {

			_gl.getShaderPrecisionFormat = function () {

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

			};

		}

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

M
Mr.doob 已提交
228 229
	} catch ( error ) {

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

	}

234 235
	var extensions = new THREE.WebGLExtensions( _gl );

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

244 245
	if ( extensions.get( 'OES_element_index_uint' ) ) {

246
		THREE.BufferGeometry.MaxIndex = 4294967296;
247 248 249

	}

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

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

259 260
	this.info.programs = programCache.programs;

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

M
Mr.doob 已提交
264 265
	//

266 267
	function getTargetPixelRatio() {

268
		return _currentRenderTarget === null ? _pixelRatio : 1;
269 270 271

	}

272
	function glClearColor( r, g, b, a ) {
273 274 275

		if ( _premultipliedAlpha === true ) {

276
			r *= a; g *= a; b *= a;
277 278 279

		}

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

282
	}
283

284
	function setDefaultGLState() {
M
Mr.doob 已提交
285

M
Mr.doob 已提交
286
		state.init();
M
Mr.doob 已提交
287

288 289
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
290

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

293
	}
294

295
	function resetGLState() {
296 297 298 299

		_currentProgram = null;
		_currentCamera = null;

300
		_currentGeometryProgram = '';
301 302
		_currentMaterialId = - 1;

M
Mr.doob 已提交
303 304
		state.reset();

305
	}
M
Mr.doob 已提交
306 307 308 309

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
310
	this.capabilities = capabilities;
311
	this.extensions = extensions;
312
	this.properties = properties;
M
Mr.doob 已提交
313
	this.state = state;
M
Mr.doob 已提交
314

M
Mr.doob 已提交
315 316
	// shadow map

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

319
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
320

M
Mr.doob 已提交
321

M
Mr.doob 已提交
322 323 324 325 326
	// Plugins

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

M
Mr.doob 已提交
327 328 329 330 331 332 333 334
	// API

	this.getContext = function () {

		return _gl;

	};

335 336 337 338 339 340
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

341 342 343 344 345 346
	this.forceContextLoss = function () {

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

	};

347
	this.getMaxAnisotropy = function () {
348

349
		return capabilities.getMaxAnisotropy();
350

351
	};
M
Mr.doob 已提交
352 353 354

	this.getPrecision = function () {

G
gero3 已提交
355
		return capabilities.precision;
M
Mr.doob 已提交
356 357 358

	};

359 360
	this.getPixelRatio = function () {

361
		return _pixelRatio;
362 363 364 365 366

	};

	this.setPixelRatio = function ( value ) {

367 368 369 370 371
		if ( value === undefined ) return;

		_pixelRatio = value;

		this.setSize( _viewport.z, _viewport.w, false );
372 373 374

	};

375 376 377
	this.getSize = function () {

		return {
378 379
			width: _width,
			height: _height
380 381 382 383
		};

	};

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

386 387 388
		_width = width;
		_height = height;

389 390
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
391

392
		if ( updateStyle !== false ) {
393

G
gero3 已提交
394 395
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
396

G
gero3 已提交
397
		}
M
Mr.doob 已提交
398

399
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
400 401 402 403 404

	};

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

405
		state.viewport( _viewport.set( x, y, width, height ) );
M
Mr.doob 已提交
406 407 408

	};

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

411
		state.scissor( _scissor.set( x, y, width, height ) );
M
Mr.doob 已提交
412 413 414

	};

415 416
	this.setScissorTest = function ( boolean ) {

417
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
418 419 420 421 422

	};

	// Clearing

M
Mr.doob 已提交
423
	this.getClearColor = function () {
M
Mr.doob 已提交
424

M
Mr.doob 已提交
425
		return _clearColor;
M
Mr.doob 已提交
426 427 428

	};

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

M
Mr.doob 已提交
431
		_clearColor.set( color );
432

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

435
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
436 437 438

	};

M
Mr.doob 已提交
439
	this.getClearAlpha = function () {
M
Mr.doob 已提交
440

M
Mr.doob 已提交
441
		return _clearAlpha;
M
Mr.doob 已提交
442 443 444

	};

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

M
Mr.doob 已提交
447
		_clearAlpha = alpha;
M
Mr.doob 已提交
448

449
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
450 451 452 453 454 455 456 457 458 459 460 461

	};

	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 );
462 463 464 465 466

	};

	this.clearColor = function () {

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

	};

	this.clearDepth = function () {

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

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
479
		this.clear( false, false, true );
M
Mr.doob 已提交
480 481 482 483 484 485 486 487 488 489

	};

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

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

	};

M
Mr.doob 已提交
490 491
	// Reset

492
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
493

D
dubejf 已提交
494 495
	this.dispose = function() {

496 497 498 499 500
		transparentObjects = [];
		transparentObjectsLastIndex = -1;
		opaqueObjects = [];
		opaqueObjectsLastIndex = -1;

D
dubejf 已提交
501 502 503 504
		_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );

	};

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

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

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

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

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

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

526
	}
M
Mr.doob 已提交
527 528 529

	// Buffer deallocation

530
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
531

532 533 534 535
		releaseMaterialProgramReference( material );

		properties.delete( material );

536
	}
537 538


539
	function releaseMaterialProgramReference( material ) {
540

541
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
542 543 544

		material.program = undefined;

545
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
546

547
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
548

M
Mr.doob 已提交
549 550
		}

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

	// Buffer rendering

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

557
		state.initAttributes();
558

559
		var buffers = properties.get( object );
560

561 562 563 564
		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 已提交
565

566
		var attributes = program.getAttributes();
567

M
Mr.doob 已提交
568 569
		if ( object.hasPositions ) {

570
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
571
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
572

573 574
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
575 576 577 578 579

		}

		if ( object.hasNormals ) {

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

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

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

586
					var array = object.normalArray;
M
Mr.doob 已提交
587

588 589 590
					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 已提交
591

592 593 594
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
595

596 597 598
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
599

600 601 602
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
603 604 605 606 607 608

				}

			}

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

610
			state.enableAttribute( attributes.normal );
611

612
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
613 614 615 616 617

		}

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

618
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
619
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
620

621
			state.enableAttribute( attributes.uv );
622

623
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
624 625 626 627 628

		}

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

629
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
630
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
631

632
			state.enableAttribute( attributes.color );
633

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

		}

638
		state.disableUnusedAttributes();
639

M
Mr.doob 已提交
640 641 642 643 644 645
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

646
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
647

M
Mr.doob 已提交
648 649
		setMaterial( material );

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

M
Mr.doob 已提交
652 653
		var updateBuffers = false;
		var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
M
Mr.doob 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

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

			}

677
			activeInfluences.sort( absNumericalSort );
M
Mr.doob 已提交
678 679 680 681 682 683 684

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

685 686
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
687 688 689 690 691 692 693
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

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

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

694
					var index = influence[ 1 ];
M
Mr.doob 已提交
695

696 697
					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 已提交
698 699 700

				} else {

701 702
					if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
					if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
M
Mr.doob 已提交
703 704 705 706 707

				}

			}

T
tschw 已提交
708 709
			program.getUniforms().setValue(
					_gl, 'morphTargetInfluences', morphInfluences );
M
Mr.doob 已提交
710 711 712 713 714

			updateBuffers = true;

		}

715 716
		//

717
		var index = geometry.index;
718 719
		var position = geometry.attributes.position;

720 721
		if ( material.wireframe === true ) {

722
			index = objects.getWireframeAttribute( geometry );
723 724 725

		}

726 727
		var renderer;

728
		if ( index !== null ) {
729

730 731
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
732

733
		} else {
734

735
			renderer = bufferRenderer;
736

737
		}
M
Mr.doob 已提交
738

739
		if ( updateBuffers ) {
M
Mr.doob 已提交
740

741
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
742

743
			if ( index !== null ) {
744

745
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
746 747 748

			}

749
		}
750

751 752
		//

M
Mr.doob 已提交
753 754
		var dataStart = 0;
		var dataCount = Infinity;
755

M
Mr.doob 已提交
756
		if ( index !== null ) {
757

M
Mr.doob 已提交
758
			dataCount = index.count;
759

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

M
Mr.doob 已提交
762
			dataCount = position.count;
763

M
Mr.doob 已提交
764
		}
765

M
Mr.doob 已提交
766 767
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
768

M
Mr.doob 已提交
769 770
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
771

M
Mr.doob 已提交
772 773 774 775 776 777
		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 );

		//
778

779
		if ( object instanceof THREE.Mesh ) {
780

781
			if ( material.wireframe === true ) {
782

783
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
784
				renderer.setMode( _gl.LINES );
785

786
			} else {
M
Mr.doob 已提交
787 788

				switch ( object.drawMode ) {
789

B
Ben Adams 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802
					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;

				}
803

804
			}
805

806

807
		} else if ( object instanceof THREE.Line ) {
808

809
			var lineWidth = material.linewidth;
810

811
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
812

813
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
814

815
			if ( object instanceof THREE.LineSegments ) {
816

817
				renderer.setMode( _gl.LINES );
818

819
			} else {
820

821
				renderer.setMode( _gl.LINE_STRIP );
822 823

			}
M
Mr.doob 已提交
824

825
		} else if ( object instanceof THREE.Points ) {
826 827

			renderer.setMode( _gl.POINTS );
828 829

		}
830

J
jfranc 已提交
831
		if ( geometry instanceof THREE.InstancedBufferGeometry ) {
M
Mr.doob 已提交
832 833 834

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
837
			}
838 839 840

		} else {

M
Mr.doob 已提交
841
			renderer.render( drawStart, drawCount );
842

M
Mr.doob 已提交
843 844 845 846
		}

	};

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

M
Mr.doob 已提交
849
		var extension;
B
Ben Adams 已提交
850

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

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

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

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

M
Mr.doob 已提交
860 861 862
			}

		}
B
Ben Adams 已提交
863

864 865
		if ( startIndex === undefined ) startIndex = 0;

866 867
		state.initAttributes();

868
		var geometryAttributes = geometry.attributes;
869

870
		var programAttributes = program.getAttributes();
871

872
		var materialDefaultAttributeValues = material.defaultAttributeValues;
873

874
		for ( var name in programAttributes ) {
875

876
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
877

M
Mr.doob 已提交
878
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
879

880
				var geometryAttribute = geometryAttributes[ name ];
881

M
Mr.doob 已提交
882
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
883

884
					var type = _gl.FLOAT;
885
					var array = geometryAttribute.array;
886 887
					var normalized = geometryAttribute.normalized;

888 889
					if ( array instanceof Float32Array ) {

890
						type = _gl.FLOAT;
891 892 893 894 895 896 897

					} else if ( array instanceof Float64Array ) {

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

					} else if ( array instanceof Uint16Array ) {

898 899
						type = _gl.UNSIGNED_SHORT;

900 901
					} else if ( array instanceof Int16Array ) {

902
						type = _gl.SHORT;
903 904 905

					} else if ( array instanceof Uint32Array ) {

906
						type = _gl.UNSIGNED_INT;
907 908 909

					} else if ( array instanceof Int32Array ) {

910
						type = _gl.INT;
911 912 913

					} else if ( array instanceof Int8Array ) {

914
						type = _gl.BYTE;
915 916 917

					} else if ( array instanceof Uint8Array ) {

918
						type = _gl.UNSIGNED_BYTE;
919 920

					}
921

922
					var size = geometryAttribute.itemSize;
G
gero3 已提交
923
					var buffer = objects.getAttributeBuffer( geometryAttribute );
924

B
Ben Adams 已提交
925
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
926

M
Mr.doob 已提交
927 928 929 930 931
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
935
							if ( geometry.maxInstancedCount === undefined ) {
936

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

M
Mr.doob 已提交
939
							}
B
Ben Adams 已提交
940

M
Mr.doob 已提交
941
						} else {
B
Ben Adams 已提交
942

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

M
Mr.doob 已提交
945
						}
B
Ben Adams 已提交
946

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

M
Mr.doob 已提交
950
					} else {
B
Ben Adams 已提交
951

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

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

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

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

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

M
Mr.doob 已提交
962 963 964 965
						} else {

							state.enableAttribute( programAttribute );

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

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

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

973 974
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
975
					var value = materialDefaultAttributeValues[ name ];
976

977
					if ( value !== undefined ) {
M
Mr.doob 已提交
978

979
						switch ( value.length ) {
M
Mr.doob 已提交
980

981 982 983
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
984

985 986 987
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
988

989 990 991
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
992

993 994
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
995 996

						}
M
Mr.doob 已提交
997 998 999 1000 1001 1002 1003 1004

					}

				}

			}

		}
1005

1006
		state.disableUnusedAttributes();
1007

M
Mr.doob 已提交
1008 1009
	}

M
Mr.doob 已提交
1010 1011
	// Sorting

1012
	function absNumericalSort( a, b ) {
1013

1014
		return Math.abs( b[ 0 ] ) - Math.abs( a[ 0 ] );
1015 1016 1017

	}

M
Mr.doob 已提交
1018 1019
	function painterSortStable ( a, b ) {

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

U
unconed 已提交
1022
			return a.object.renderOrder - b.object.renderOrder;
1023

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

M
Mr.doob 已提交
1026
			return a.material.id - b.material.id;
1027 1028

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

M
Mr.doob 已提交
1030
			return a.z - b.z;
M
Mr.doob 已提交
1031 1032 1033

		} else {

1034
			return a.id - b.id;
M
Mr.doob 已提交
1035 1036 1037

		}

1038
	}
M
Mr.doob 已提交
1039

1040 1041
	function reversePainterSortStable ( 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

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

M
Mr.doob 已提交
1048
			return b.z - a.z;
1049 1050 1051 1052 1053 1054 1055

		} else {

			return a.id - b.id;

		}

1056
	}
1057

M
Mr.doob 已提交
1058 1059 1060 1061 1062 1063
	// Rendering

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

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

1064
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1065 1066 1067 1068
			return;

		}

M
Mr.doob 已提交
1069
		var fog = scene.fog;
M
Mr.doob 已提交
1070 1071 1072

		// reset caching for this frame

1073
		_currentGeometryProgram = '';
1074
		_currentMaterialId = - 1;
1075
		_currentCamera = null;
M
Mr.doob 已提交
1076 1077 1078

		// update scene graph

1079
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1080 1081 1082

		// update camera matrices and frustum

1083
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1084 1085 1086 1087 1088 1089

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

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

M
Mr.doob 已提交
1090
		lights.length = 0;
1091

M
Mr.doob 已提交
1092 1093
		opaqueObjectsLastIndex = - 1;
		transparentObjectsLastIndex = - 1;
1094

M
Mr.doob 已提交
1095 1096 1097
		sprites.length = 0;
		lensFlares.length = 0;

T
tschw 已提交
1098 1099 1100
		_localClippingEnabled = this.localClippingEnabled;
		_clippingEnabled = _clipping.init(
				this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1101

1102
		projectObject( scene, camera );
M
Mr.doob 已提交
1103

T
tschw 已提交
1104

1105 1106 1107
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1108
		if ( _this.sortObjects === true ) {
1109 1110 1111

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

1113 1114
		}

M
Mr.doob 已提交
1115
		//
M
Mr.doob 已提交
1116

T
tschw 已提交
1117
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1118

1119 1120
		setupShadows( lights );

M
Mr.doob 已提交
1121
		shadowMap.render( scene, camera );
M
Mr.doob 已提交
1122

1123 1124
		setupLights( lights, camera );

T
tschw 已提交
1125
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1126

M
Mr.doob 已提交
1127 1128
		//

1129 1130 1131 1132
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1133

1134 1135 1136 1137 1138 1139
		if ( renderTarget === undefined ) {

			renderTarget = null;

		}

M
Mr.doob 已提交
1140 1141 1142 1143 1144 1145 1146 1147
		this.setRenderTarget( renderTarget );

		if ( this.autoClear || forceClear ) {

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

		}

1148
		//
M
Mr.doob 已提交
1149 1150 1151

		if ( scene.overrideMaterial ) {

1152
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1153

1154 1155
			renderObjects( opaqueObjects, camera, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, fog, overrideMaterial );
1156

M
Mr.doob 已提交
1157 1158 1159 1160
		} else {

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

M
Mr.doob 已提交
1161
			state.setBlending( THREE.NoBlending );
1162
			renderObjects( opaqueObjects, camera, fog );
M
Mr.doob 已提交
1163 1164 1165

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

1166
			renderObjects( transparentObjects, camera, fog );
M
Mr.doob 已提交
1167 1168 1169 1170 1171

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1172
		spritePlugin.render( scene, camera );
1173
		lensFlarePlugin.render( scene, camera, _currentViewport );
M
Mr.doob 已提交
1174 1175 1176

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

M
Mr.doob 已提交
1177 1178
		if ( renderTarget ) {

1179
			textures.updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1180 1181 1182

		}

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

M
Mr.doob 已提交
1185 1186
		state.setDepthTest( true );
		state.setDepthWrite( true );
1187
		state.setColorWrite( true );
M
Mr.doob 已提交
1188 1189 1190 1191

		// _gl.finish();

	};
M
Mr.doob 已提交
1192

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

1195
		var array, index;
M
Mr.doob 已提交
1196

1197
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1198 1199 1200

		if ( material.transparent ) {

1201 1202
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1203 1204 1205

		} else {

1206 1207 1208 1209 1210
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1211 1212
		// recycle existing render item or grow the array

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
		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 已提交
1223 1224 1225

		} else {

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
			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 已提交
1237 1238 1239 1240 1241

		}

	}

M
Mr.doob 已提交
1242 1243
	// TODO Duplicated code (Frustum)

T
tschw 已提交
1244 1245 1246 1247 1248 1249 1250
	function isObjectViewable( object ) {

		var geometry = object.geometry;

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

M
Mr.doob 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		_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 已提交
1269 1270

		if ( ! _frustum.intersectsSphere( sphere ) ) return false;
T
tschw 已提交
1271 1272 1273 1274

		var numPlanes = _clipping.numPlanes;

		if ( numPlanes === 0 ) return true;
T
tschw 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

		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 已提交
1287
		} while ( ++ i !== numPlanes );
T
tschw 已提交
1288 1289 1290 1291 1292

		return true;

	}

1293
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1294

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

1297
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1298

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

1301
				lights.push( object );
M
Mr.doob 已提交
1302

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

M
Mr.doob 已提交
1305
				if ( object.frustumCulled === false || isSpriteViewable( object ) === true ) {
1306 1307 1308 1309

					sprites.push( object );

				}
M
Mr.doob 已提交
1310

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

1313
				lensFlares.push( object );
M
Mr.doob 已提交
1314

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

1317
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1318

1319 1320
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1321

1322
				}
1323

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

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

1328
				if ( object instanceof THREE.SkinnedMesh ) {
M
Mr.doob 已提交
1329

1330
					object.skeleton.update();
1331

1332
				}
1333

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

1336
					var material = object.material;
1337

1338
					if ( material.visible === true ) {
M
Mr.doob 已提交
1339

1340
						if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1341

1342 1343 1344 1345
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1346

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

S
SUNAG 已提交
1349
						if ( material instanceof THREE.MultiMaterial ) {
1350

1351 1352
							var groups = geometry.groups;
							var materials = material.materials;
1353

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

1356 1357
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1358

1359
								if ( groupMaterial.visible === true ) {
1360

1361 1362 1363
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1364

M
Mr.doob 已提交
1365
							}
M
Mr.doob 已提交
1366

1367
						} else {
M
Mr.doob 已提交
1368

1369
							pushRenderItem( object, geometry, material, _vector3.z, null );
1370

1371
						}
O
OpenShift guest 已提交
1372

1373
					}
M
Mr.doob 已提交
1374

1375
				}
M
Mr.doob 已提交
1376

1377
			}
M
Mr.doob 已提交
1378

M
Mr.doob 已提交
1379
		}
M
Mr.doob 已提交
1380

M
Mr.doob 已提交
1381
		var children = object.children;
M
Mr.doob 已提交
1382

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

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

1387
		}
1388

1389
	}
M
Mr.doob 已提交
1390

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

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

1395
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1396

1397
			var object = renderItem.object;
M
Mr.doob 已提交
1398 1399 1400
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1401

1402 1403
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1404

M
Mr.doob 已提交
1405
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1406

M
Mr.doob 已提交
1407
				setMaterial( material );
M
Mr.doob 已提交
1408

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

M
Mr.doob 已提交
1411
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1412

M
Mr.doob 已提交
1413
				object.render( function ( object ) {
M
Mr.doob 已提交
1414

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

M
Mr.doob 已提交
1417
				} );
1418

M
Mr.doob 已提交
1419
			} else {
M
Mr.doob 已提交
1420

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

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

1425
		}
M
Mr.doob 已提交
1426

1427
	}
G
gero3 已提交
1428

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

1431
		var materialProperties = properties.get( material );
G
gero3 已提交
1432

T
tschw 已提交
1433
		var parameters = programCache.getParameters(
T
tschw 已提交
1434
				material, _lights, fog, _clipping.numPlanes, object );
T
tschw 已提交
1435

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

1438
		var program = materialProperties.program;
T
tschw 已提交
1439
		var programChange = true;
1440

1441
		if ( program === undefined ) {
B
Ben Adams 已提交
1442

M
Mr.doob 已提交
1443 1444
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1445

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

M
Mr.doob 已提交
1448
			// changed glsl or parameters
1449
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1450

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

T
tschw 已提交
1453
			// same glsl and uniform list
T
tschw 已提交
1454 1455
			return;

T
tschw 已提交
1456
		} else {
B
Ben Adams 已提交
1457

T
tschw 已提交
1458 1459
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1460 1461 1462

		}

1463
		if ( programChange ) {
B
Ben Adams 已提交
1464

1465
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1466

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

1469 1470 1471 1472 1473 1474
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1475

1476
			} else {
B
Ben Adams 已提交
1477

1478 1479 1480 1481 1482 1483
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1484

1485
			}
G
gero3 已提交
1486

1487
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1488

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

1491 1492
			materialProperties.program = program;
			material.program = program;
1493 1494 1495

		}

1496
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1497 1498 1499 1500 1501

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

M
Mr.doob 已提交
1504
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

M
Mr.doob 已提交
1520
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529

					material.numSupportedMorphNormals ++;

				}

			}

		}

T
tschw 已提交
1530 1531 1532 1533 1534 1535
		var uniforms = materialProperties.__webglShader.uniforms;

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

T
tschw 已提交
1536 1537
			materialProperties.numClippingPlanes = _clipping.numPlanes;
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1538 1539 1540

		}

M
Mr.doob 已提交
1541
		if ( material.lights ) {
1542

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

1545 1546
			materialProperties.lightsHash = _lights.hash;

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

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

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

1562 1563
		}

T
tschw 已提交
1564 1565 1566
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
					THREE.WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1567

T
tschw 已提交
1568 1569 1570
		materialProperties.uniformsList = uniformsList;
		materialProperties.dynamicUniforms =
				THREE.WebGLUniforms.splitDynamic( uniformsList, uniforms );
A
arose 已提交
1571

M
Mr.doob 已提交
1572
	}
M
Mr.doob 已提交
1573

1574 1575
	function setMaterial( material ) {

T
tschw 已提交
1576 1577 1578 1579 1580 1581
		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 已提交
1582

1583 1584
		if ( material.transparent === true ) {

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

1587 1588 1589 1590
		} else {

			state.setBlending( THREE.NoBlending );

1591 1592
		}

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

	}

1601
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1602 1603 1604

		_usedTextureUnits = 0;

1605
		var materialProperties = properties.get( material );
1606

T
tschw 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		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 已提交
1618
				_clipping.setState(
T
tschw 已提交
1619 1620 1621 1622 1623 1624
						material.clippingPlanes, material.clipShadows,
						camera, materialProperties, useCache );

			}

			if ( materialProperties.numClippingPlanes !== undefined &&
T
tschw 已提交
1625
				materialProperties.numClippingPlanes !== _clipping.numPlanes ) {
T
tschw 已提交
1626 1627 1628 1629 1630 1631 1632

				material.needsUpdate = true;

			}

		}

1633 1634 1635 1636 1637 1638
		if ( materialProperties.program === undefined ) {

			material.needsUpdate = true;

		}

1639 1640
		if ( materialProperties.lightsHash !== undefined &&
			materialProperties.lightsHash !== _lights.hash ) {
1641 1642 1643 1644 1645 1646

			material.needsUpdate = true;

		}

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

1648
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1649 1650 1651 1652
			material.needsUpdate = false;

		}

1653
		var refreshProgram = false;
M
Mr.doob 已提交
1654
		var refreshMaterial = false;
1655
		var refreshLights = false;
M
Mr.doob 已提交
1656

1657
		var program = materialProperties.program,
1658
			p_uniforms = program.getUniforms(),
1659
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1660

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

1663 1664
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1665

1666
			refreshProgram = true;
M
Mr.doob 已提交
1667
			refreshMaterial = true;
1668
			refreshLights = true;
M
Mr.doob 已提交
1669 1670 1671 1672 1673 1674

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1675

M
Mr.doob 已提交
1676 1677 1678 1679
			refreshMaterial = true;

		}

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

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

G
gero3 已提交
1684
			if ( capabilities.logarithmicDepthBuffer ) {
1685

T
tschw 已提交
1686 1687
				p_uniforms.setValue( _gl, 'logDepthBufFC',
						2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1688 1689 1690 1691

			}


1692 1693 1694 1695 1696 1697 1698 1699 1700
			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 已提交
1701
				refreshLights = true;		// remains set until update done
1702 1703

			}
M
Mr.doob 已提交
1704

1705 1706 1707 1708 1709
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1710
				 material instanceof THREE.MeshStandardMaterial ||
1711 1712
				 material.envMap ) {

T
tschw 已提交
1713 1714 1715
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1716

T
tschw 已提交
1717 1718
					uCamPos.setValue( _gl,
							_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1719 1720 1721 1722 1723 1724 1725

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1726
				 material instanceof THREE.MeshBasicMaterial ||
1727
				 material instanceof THREE.MeshStandardMaterial ||
1728 1729 1730
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

T
tschw 已提交
1731
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1732 1733 1734

			}

T
tschw 已提交
1735 1736
			p_uniforms.set( _gl, _this, 'toneMappingExposure' );
			p_uniforms.set( _gl, _this, 'toneMappingWhitePoint' );
M
Mr.doob 已提交
1737

M
Mr.doob 已提交
1738 1739 1740 1741 1742 1743 1744 1745
		}

		// 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 已提交
1746 1747
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1748

T
tschw 已提交
1749
			var skeleton = object.skeleton;
1750

T
tschw 已提交
1751
			if ( skeleton ) {
1752

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

T
tschw 已提交
1755 1756 1757
					p_uniforms.set( _gl, skeleton, 'boneTexture' );
					p_uniforms.set( _gl, skeleton, 'boneTextureWidth' );
					p_uniforms.set( _gl, skeleton, 'boneTextureHeight' );
M
Mr.doob 已提交
1758

T
tschw 已提交
1759
				} else {
M
Mr.doob 已提交
1760

T
tschw 已提交
1761
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770

				}

			}

		}

		if ( refreshMaterial ) {

M
Mr.doob 已提交
1771
			if ( material.lights ) {
M
Mr.doob 已提交
1772

1773
				// the current material requires lighting info
M
Mr.doob 已提交
1774

T
tschw 已提交
1775 1776 1777 1778 1779 1780
				// 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 已提交
1781

T
tschw 已提交
1782
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1783

T
tschw 已提交
1784
			}
G
gero3 已提交
1785

T
tschw 已提交
1786
			// refresh uniforms common to several materials
G
gero3 已提交
1787

T
tschw 已提交
1788
			if ( fog && material.fog ) {
G
gero3 已提交
1789

T
tschw 已提交
1790
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1791 1792 1793 1794 1795

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1796
				 material instanceof THREE.MeshPhongMaterial ||
W
WestLangley 已提交
1797 1798
				 material instanceof THREE.MeshStandardMaterial ||
				 material instanceof THREE.MeshDepthMaterial ) {
M
Mr.doob 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814

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

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

M
Mr.doob 已提交
1817
				refreshUniformsPoints( m_uniforms, material );
M
Mr.doob 已提交
1818

1819 1820 1821 1822
			} else if ( material instanceof THREE.MeshLambertMaterial ) {

				refreshUniformsLambert( m_uniforms, material );

M
Mr.doob 已提交
1823 1824 1825 1826
			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

W
WestLangley 已提交
1827 1828 1829 1830
			} else if ( material instanceof THREE.MeshPhysicalMaterial ) {

				refreshUniformsPhysical( m_uniforms, material );

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

1833
				refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1834

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

1837 1838 1839 1840 1841 1842 1843 1844
				if ( material.displacementMap ) {

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

				}

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

				m_uniforms.opacity.value = material.opacity;

			}

T
tschw 已提交
1851 1852
			THREE.WebGLUniforms.upload(
					_gl, materialProperties.uniformsList, m_uniforms, _this );
A
arose 已提交
1853 1854 1855

		}

M
Mr.doob 已提交
1856

T
tschw 已提交
1857
		// common matrices
M
Mr.doob 已提交
1858

T
tschw 已提交
1859 1860 1861
		p_uniforms.set( _gl, object, 'modelViewMatrix' );
		p_uniforms.set( _gl, object, 'normalMatrix' );
		p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
A
arose 已提交
1862 1863


T
tschw 已提交
1864
		// dynamic uniforms
A
arose 已提交
1865

T
tschw 已提交
1866
		var dynUniforms = materialProperties.dynamicUniforms;
A
arose 已提交
1867

T
tschw 已提交
1868
		if ( dynUniforms !== null ) {
A
arose 已提交
1869

T
tschw 已提交
1870 1871
			THREE.WebGLUniforms.evalDynamic(
					dynUniforms, m_uniforms, object, camera );
A
arose 已提交
1872

T
tschw 已提交
1873
			THREE.WebGLUniforms.upload( _gl, dynUniforms, m_uniforms, _this );
A
arose 已提交
1874 1875 1876

		}

T
tschw 已提交
1877
		return program;
A
arose 已提交
1878 1879 1880

	}

M
Mr.doob 已提交
1881 1882 1883 1884 1885 1886
	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

1887
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1888

1889
		if ( material.emissive ) {
M
Mr.doob 已提交
1890

1891
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1892 1893 1894

		}

1895 1896 1897
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
1898

1899
		if ( material.aoMap ) {
1900

1901 1902
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
1903 1904 1905

		}

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

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

1924 1925 1926 1927
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
1928 1929 1930 1931 1932 1933 1934 1935
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

1936 1937 1938 1939 1940 1941 1942 1943
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

1944 1945 1946 1947
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

1948 1949 1950 1951
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
1952 1953 1954 1955
		}

		if ( uvScaleMap !== undefined ) {

1956
			// backwards compatibility
M
Mr.doob 已提交
1957 1958 1959 1960 1961 1962
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) {

				uvScaleMap = uvScaleMap.texture;

			}

M
Mr.doob 已提交
1963 1964 1965 1966 1967 1968 1969 1970
			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;
1971 1972 1973 1974 1975 1976

		// 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 已提交
1977

1978
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
1979 1980
		uniforms.refractionRatio.value = material.refractionRatio;

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

	function refreshUniformsLine ( uniforms, material ) {

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

M
Mr.doob 已提交
1988
	}
M
Mr.doob 已提交
1989 1990 1991 1992 1993 1994 1995

	function refreshUniformsDash ( uniforms, material ) {

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

M
Mr.doob 已提交
1996
	}
M
Mr.doob 已提交
1997

M
Mr.doob 已提交
1998
	function refreshUniformsPoints ( uniforms, material ) {
M
Mr.doob 已提交
1999

2000
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2001
		uniforms.opacity.value = material.opacity;
2002
		uniforms.size.value = material.size * _pixelRatio;
T
tschw 已提交
2003
		uniforms.scale.value = _canvas.clientHeight * 0.5;
M
Mr.doob 已提交
2004 2005 2006

		uniforms.map.value = material.map;

2007 2008 2009 2010 2011 2012 2013 2014 2015
		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 已提交
2016
	}
M
Mr.doob 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032

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

2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
	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 已提交
2052 2053
	function refreshUniformsPhong ( uniforms, material ) {

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

2057 2058 2059 2060
		if ( material.lightMap ) {

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

2062
		}
2063

2064
		if ( material.emissiveMap ) {
2065

2066
			uniforms.emissiveMap.value = material.emissiveMap;
2067

2068
		}
M
Mr.doob 已提交
2069

2070 2071 2072 2073
		if ( material.bumpMap ) {

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

2075
		}
M
Mr.doob 已提交
2076

2077 2078 2079 2080 2081 2082
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2083

2084 2085 2086 2087 2088
		if ( material.displacementMap ) {

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

2090
		}
2091 2092 2093

	}

2094
	function refreshUniformsStandard ( uniforms, material ) {
W
WestLangley 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 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 2148 2149 2150 2151 2152 2153 2154

		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 已提交
2155 2156 2157 2158 2159 2160
	function refreshUniformsPhysical ( uniforms, material ) {

		refreshUniformsStandard( uniforms, material );

	}

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

M
Mr.doob 已提交
2163
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2164

M
Mr.doob 已提交
2165
		uniforms.ambientLightColor.needsUpdate = value;
2166

B
Ben Houston 已提交
2167 2168 2169 2170
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
		uniforms.hemisphereLights.needsUpdate = value;
2171

M
Mr.doob 已提交
2172
	}
2173

T
tschw 已提交
2174
	// Lighting
M
Mr.doob 已提交
2175

2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
	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 已提交
2196
	function setupLights ( lights, camera ) {
M
Mr.doob 已提交
2197

B
brason 已提交
2198
		var l, ll, light,
M
Mr.doob 已提交
2199
		r = 0, g = 0, b = 0,
B
Ben Houston 已提交
2200
		color,
B
brason 已提交
2201
		intensity,
M
Mr.doob 已提交
2202
		distance,
2203
		shadowMap,
M
Mr.doob 已提交
2204

2205
		viewMatrix = camera.matrixWorldInverse,
M
Mr.doob 已提交
2206

M
Mr.doob 已提交
2207
		directionalLength = 0,
M
Mr.doob 已提交
2208 2209
		pointLength = 0,
		spotLength = 0,
2210
		hemiLength = 0;
M
Mr.doob 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219

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

			light = lights[ l ];

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

2220
			shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : emptyTexture;
2221

M
Mr.doob 已提交
2222 2223
			if ( light instanceof THREE.AmbientLight ) {

2224 2225 2226
				r += color.r * intensity;
				g += color.g * intensity;
				b += color.b * intensity;
M
Mr.doob 已提交
2227 2228 2229

			} else if ( light instanceof THREE.DirectionalLight ) {

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

2232
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
M
Mr.doob 已提交
2233
				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
2234
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2235 2236 2237
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2238
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2239

2240
				if ( light.castShadow ) {
M
Mr.doob 已提交
2241

2242 2243 2244
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2245

2246 2247
				}

2248
				_lights.directionalShadowMap[ directionalLength ] = shadowMap;
2249
				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
2250
				_lights.directional[ directionalLength ++ ] = uniforms;
M
Mr.doob 已提交
2251

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

M
Mr.doob 已提交
2254
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2255 2256 2257

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

M
Mr.doob 已提交
2259 2260 2261 2262 2263 2264 2265 2266
				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 );

2267 2268
				uniforms.coneCos = Math.cos( light.angle );
				uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
M
Mr.doob 已提交
2269
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2270

2271
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2272

2273 2274
				if ( light.castShadow ) {

2275 2276 2277
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2278

2279 2280
				}

2281
				_lights.spotShadowMap[ spotLength ] = shadowMap;
2282
				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
M
Mr.doob 已提交
2283
				_lights.spot[ spotLength ++ ] = uniforms;
2284

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

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

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

M
Mr.doob 已提交
2292 2293
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
				uniforms.distance = light.distance;
M
Mr.doob 已提交
2294 2295
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;

2296
				uniforms.shadow = light.castShadow;
2297

M
Mr.doob 已提交
2298
				if ( light.castShadow ) {
2299

2300 2301 2302 2303 2304
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;

				}
2305

2306
				_lights.pointShadowMap[ pointLength ] = shadowMap;
2307

2308 2309 2310
				if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) {

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

2312 2313
				}

2314 2315 2316 2317 2318
				// 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 已提交
2319
				_lights.point[ pointLength ++ ] = uniforms;
2320

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

M
Mr.doob 已提交
2323
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2324 2325 2326 2327

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

M
Mr.doob 已提交
2329 2330
				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
M
Mr.doob 已提交
2331

M
Mr.doob 已提交
2332
				_lights.hemi[ hemiLength ++ ] = uniforms;
M
Mr.doob 已提交
2333 2334 2335 2336 2337

			}

		}

M
Mr.doob 已提交
2338 2339 2340
		_lights.ambient[ 0 ] = r;
		_lights.ambient[ 1 ] = g;
		_lights.ambient[ 2 ] = b;
M
Mr.doob 已提交
2341

M
Mr.doob 已提交
2342 2343
		_lights.directional.length = directionalLength;
		_lights.spot.length = spotLength;
2344
		_lights.point.length = pointLength;
M
Mr.doob 已提交
2345
		_lights.hemi.length = hemiLength;
2346

2347
		_lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + _lights.shadows.length;
2348

M
Mr.doob 已提交
2349
	}
M
Mr.doob 已提交
2350 2351 2352 2353 2354

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

T
tschw 已提交
2355 2356
		state.setCullFace( cullFace );
		state.setFlipSided( frontFaceDirection === THREE.FrontFaceDirectionCW );
M
Mr.doob 已提交
2357 2358 2359 2360 2361

	};

	// Textures

T
tschw 已提交
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
	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;

	}

2378
	this.allocTextureUnit = allocTextureUnit;
T
tschw 已提交
2379

2380
	// this.setTexture2D = setTexture2D;
2381
	this.setTexture2D = ( function() {
T
tschw 已提交
2382

2383
		var warned = false;
T
tschw 已提交
2384

2385 2386
		// backwards compatibility: peel texture.texture
		return function( texture, slot ) {
T
tschw 已提交
2387

2388
			if ( texture instanceof THREE.WebGLRenderTarget ) {
T
tschw 已提交
2389

2390
				if ( ! warned ) {
T
tschw 已提交
2391

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

2395
				}
T
tschw 已提交
2396

2397
				texture = texture.texture;
T
tschw 已提交
2398

2399
			}
T
tschw 已提交
2400

2401
			textures.setTexture2D( texture, slot );
T
tschw 已提交
2402

2403
		};
T
tschw 已提交
2404

2405
	}() );
T
tschw 已提交
2406

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
	this.setTexture = ( function() {

		var warned = false;

		return function( texture, slot ) {

			if ( ! warned ) {

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

			}

2420
			textures.setTexture2D( texture, slot );
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453

		};

	}() );

	this.setTextureCube = ( function() {

		var warned = false;

		return function( texture, slot ) {

			// 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
2454
				textures.setTextureCube( texture, slot );
2455 2456 2457 2458 2459

			} else {

				// assumed: texture property of THREE.WebGLRenderTargetCube

2460
				textures.setTextureCubeDynamic( texture, slot );
2461 2462 2463 2464 2465 2466

			}

		};

	}() );
T
tschw 已提交
2467

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

		return _currentRenderTarget;

M
Michael Herzog 已提交
2472
	};
2473

2474
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
2475

2476 2477
		_currentRenderTarget = renderTarget;

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

2480
			textures.setupRenderTarget( renderTarget );
M
Mr.doob 已提交
2481 2482 2483

		}

2484
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
2485
		var framebuffer;
M
Mr.doob 已提交
2486 2487 2488

		if ( renderTarget ) {

2489
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
2490

M
Mr.doob 已提交
2491 2492
			if ( isCube ) {

2493
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
2494 2495 2496

			} else {

2497
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
2498 2499 2500

			}

2501 2502
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
2503

2504
			_currentViewport.copy( renderTarget.viewport );
M
Mr.doob 已提交
2505 2506 2507 2508 2509

		} else {

			framebuffer = null;

2510
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
2511
			_currentScissorTest = _scissorTest;
2512

2513
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
M
Mr.doob 已提交
2514 2515 2516

		}

M
Mr.doob 已提交
2517
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
2518 2519 2520 2521 2522 2523

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

		}

2524 2525
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
2526

2527
		state.viewport( _currentViewport );
2528

M
Mr.doob 已提交
2529 2530 2531
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
2532
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, renderTarget.activeMipMapLevel );
M
Mr.doob 已提交
2533 2534 2535

		}

M
Mr.doob 已提交
2536 2537
	};

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

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

2542
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
2543
			return;
2544

G
gero3 已提交
2545
		}
2546

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

M
Mr.doob 已提交
2549
		if ( framebuffer ) {
2550

G
gero3 已提交
2551
			var restore = false;
2552

M
Mr.doob 已提交
2553
			if ( framebuffer !== _currentFramebuffer ) {
2554

M
Mr.doob 已提交
2555
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
2556

G
gero3 已提交
2557
				restore = true;
2558

G
gero3 已提交
2559
			}
2560

M
Mr.doob 已提交
2561
			try {
2562

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

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

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

M
Mr.doob 已提交
2570
				}
2571

M
Michael Herzog 已提交
2572 2573 2574 2575
				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' ) ) ) {
2576

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

M
Mr.doob 已提交
2580
				}
2581

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

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

2586
					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
2587 2588 2589 2590

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

					}
2591

M
Mr.doob 已提交
2592
				} else {
M
Mr.doob 已提交
2593

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

				}
M
Mr.doob 已提交
2597

M
Mr.doob 已提交
2598
			} finally {
M
Mr.doob 已提交
2599

M
Mr.doob 已提交
2600 2601 2602
				if ( restore ) {

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

M
Mr.doob 已提交
2604 2605 2606
				}

			}
M
Mr.doob 已提交
2607 2608 2609

		}

M
Mr.doob 已提交
2610 2611
	};

M
Mr.doob 已提交
2612 2613 2614 2615
	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

2616 2617
		var extension;

M
Mr.doob 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
		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;

2642 2643 2644 2645 2646 2647 2648 2649
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

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

		}

M
Mr.doob 已提交
2650 2651 2652 2653 2654
		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;
2655
		if ( p === THREE.DepthFormat ) return _gl.DEPTH_COMPONENT;
M
Mr.doob 已提交
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673

		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;

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

2676 2677 2678 2679 2680 2681
		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 已提交
2682 2683 2684

		}

2685 2686 2687
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

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

2689 2690 2691 2692
			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 已提交
2693 2694 2695

		}

2696 2697 2698 2699 2700 2701 2702 2703
		extension = extensions.get( 'WEBGL_compressed_texture_etc1' );

		if ( extension !== null ) {

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

		}

2704 2705 2706
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
2707

2708 2709
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
2710 2711 2712

		}

M
Mr.doob 已提交
2713 2714
		return 0;

M
Mr.doob 已提交
2715
	}
M
Mr.doob 已提交
2716 2717

};