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

THREE.WebGLRenderer = function ( parameters ) {

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

	parameters = parameters || {};

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

M
Mr.doob 已提交
17 18
	_width = _canvas.width,
	_height = _canvas.height,
19

20 21
	pixelRatio = 1,

22
	_alpha = parameters.alpha !== undefined ? parameters.alpha : false,
23
	_depth = parameters.depth !== undefined ? parameters.depth : true,
M
Mr.doob 已提交
24
	_stencil = parameters.stencil !== undefined ? parameters.stencil : true,
25 26
	_antialias = parameters.antialias !== undefined ? parameters.antialias : false,
	_premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
M
Mr.doob 已提交
27 28
	_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,

29 30
	_clearColor = new THREE.Color( 0x000000 ),
	_clearAlpha = 0;
M
Mr.doob 已提交
31

M
Mr.doob 已提交
32
	var lights = [];
M
Mr.doob 已提交
33

O
OpenShift guest 已提交
34
	var opaqueObjects = [];
M
Mr.doob 已提交
35
	var opaqueObjectsLastIndex = - 1;
36
	var transparentObjects = [];
M
Mr.doob 已提交
37
	var transparentObjectsLastIndex = - 1;
38

39 40
	var morphInfluences = new Float32Array( 8 );

41

M
Mr.doob 已提交
42 43 44
	var sprites = [];
	var lensFlares = [];

M
Mr.doob 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	// public properties

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

	// clearing

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

	// scene graph

	this.sortObjects = true;

	// physically based shading

63
	this.gammaFactor = 2.0;	// for backwards compatibility
M
Mr.doob 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	this.gammaInput = false;
	this.gammaOutput = false;

	// morphs

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

	// flags

	this.autoScaleCubemaps = true;

	// internal properties

	var _this = this,

	// internal state cache

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

	_usedTextureUnits = 0,

	_viewportX = 0,
	_viewportY = 0,
92 93
	_viewportWidth = _canvas.width,
	_viewportHeight = _canvas.height,
M
Mr.doob 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	_currentWidth = 0,
	_currentHeight = 0,

	// frustum

	_frustum = new THREE.Frustum(),

	 // camera matrices cache

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_direction = new THREE.Vector3(),

	_lightsNeedUpdate = true,

	_lights = {

		ambient: [ 0, 0, 0 ],
G
gero3 已提交
116
		directional: { length: 0, colors: [], positions: [] },
M
Mr.doob 已提交
117 118
		point: { length: 0, colors: [], positions: [], distances: [], decays: [] },
		spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [], decays: [] },
G
gero3 已提交
119
		hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
M
Mr.doob 已提交
120

121 122
	},

M
Mr.doob 已提交
123 124
	// info

125
	_infoMemory = {
126 127

		geometries: 0,
T
tschw 已提交
128
		textures: 0
129 130 131

	},

132
	_infoRender = {
133 134 135 136 137 138

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

M
Mr.doob 已提交
139 140
	};

M
Mr.doob 已提交
141
	this.info = {
142

M
Mr.doob 已提交
143 144
		render: _infoRender,
		memory: _infoMemory,
145
		programs: null
M
Mr.doob 已提交
146 147

	};
148

149

M
Mr.doob 已提交
150 151 152 153
	// initialize

	var _gl;

M
Mr.doob 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	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 已提交
169
			if ( _canvas.getContext( 'webgl' ) !== null ) {
170 171 172 173 174 175 176 177

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

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
178 179 180

		}

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

M
Mr.doob 已提交
183 184
	} catch ( error ) {

185
		console.error( 'THREE.WebGLRenderer: ' + error );
M
Mr.doob 已提交
186 187 188

	}

189 190
	var extensions = new THREE.WebGLExtensions( _gl );

191 192
	extensions.get( 'OES_texture_float' );
	extensions.get( 'OES_texture_float_linear' );
193 194
	extensions.get( 'OES_texture_half_float' );
	extensions.get( 'OES_texture_half_float_linear' );
195
	extensions.get( 'OES_standard_derivatives' );
B
Ben Adams 已提交
196
	extensions.get( 'ANGLE_instanced_arrays' );
197

198 199
	if ( extensions.get( 'OES_element_index_uint' ) ) {

200
		THREE.BufferGeometry.MaxIndex = 4294967296;
201 202 203

	}

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

206 207 208
	var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
	var properties = new THREE.WebGLProperties();
	var objects = new THREE.WebGLObjects( _gl, properties, this.info );
G
gero3 已提交
209
	var programCache = new THREE.WebGLPrograms( this, capabilities );
210

211 212
	this.info.programs = programCache.programs;

213 214
	var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
	var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
215

M
Mr.doob 已提交
216 217
	//

218
	function glClearColor( r, g, b, a ) {
219 220 221

		if ( _premultipliedAlpha === true ) {

222
			r *= a; g *= a; b *= a;
223 224 225

		}

226 227
		_gl.clearColor( r, g, b, a );

228
	}
229

230
	function setDefaultGLState() {
M
Mr.doob 已提交
231

M
Mr.doob 已提交
232
		state.init();
M
Mr.doob 已提交
233 234 235

		_gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );

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

238
	}
239

240
	function resetGLState() {
241 242 243 244

		_currentProgram = null;
		_currentCamera = null;

245
		_currentGeometryProgram = '';
246 247 248 249
		_currentMaterialId = - 1;

		_lightsNeedUpdate = true;

M
Mr.doob 已提交
250 251
		state.reset();

252
	}
M
Mr.doob 已提交
253 254 255 256

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
257
	this.capabilities = capabilities;
258
	this.extensions = extensions;
M
Mr.doob 已提交
259
	this.state = state;
M
Mr.doob 已提交
260

M
Mr.doob 已提交
261 262
	// shadow map

M
Mr.doob 已提交
263
	var shadowMap = new THREE.WebGLShadowMap( this, lights, objects );
M
Mr.doob 已提交
264

265
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
266

M
Mr.doob 已提交
267

M
Mr.doob 已提交
268 269 270 271 272
	// Plugins

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

M
Mr.doob 已提交
273 274 275 276 277 278 279 280
	// API

	this.getContext = function () {

		return _gl;

	};

281 282 283 284 285 286
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

287 288 289 290 291 292
	this.forceContextLoss = function () {

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

	};

293
	this.getMaxAnisotropy = ( function () {
M
Mr.doob 已提交
294

295
		var value;
M
Mr.doob 已提交
296

297
		return function getMaxAnisotropy() {
298

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

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

M
Mr.doob 已提交
303
			if ( extension !== null ) {
304

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

M
Mr.doob 已提交
307 308 309 310 311
			} else {

				value = 0;

			}
312 313 314 315 316 317

			return value;

		}

	} )();
M
Mr.doob 已提交
318 319 320

	this.getPrecision = function () {

G
gero3 已提交
321
		return capabilities.precision;
M
Mr.doob 已提交
322 323 324

	};

325 326 327 328 329 330 331 332
	this.getPixelRatio = function () {

		return pixelRatio;

	};

	this.setPixelRatio = function ( value ) {

333
		if ( value !== undefined ) pixelRatio = value;
334 335 336

	};

337 338 339 340 341 342 343 344 345
	this.getSize = function () {

		return {
			width: _width,
			height: _height
		};

	};

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

348 349 350
		_width = width;
		_height = height;

351 352
		_canvas.width = width * pixelRatio;
		_canvas.height = height * pixelRatio;
353

354
		if ( updateStyle !== false ) {
355

G
gero3 已提交
356 357
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
358

G
gero3 已提交
359
		}
M
Mr.doob 已提交
360

361
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
362 363 364 365 366

	};

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

367 368
		_viewportX = x * pixelRatio;
		_viewportY = y * pixelRatio;
M
Mr.doob 已提交
369

370 371
		_viewportWidth = width * pixelRatio;
		_viewportHeight = height * pixelRatio;
M
Mr.doob 已提交
372 373 374 375 376

		_gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );

	};

M
Mr.doob 已提交
377 378 379 380 381 382 383 384 385 386
	this.getViewport = function ( dimensions ) {

		dimensions.x = _viewportX / pixelRatio;
		dimensions.y = _viewportY / pixelRatio;

		dimensions.z = _viewportWidth / pixelRatio;
		dimensions.w = _viewportHeight / pixelRatio;

	};

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

389
		_gl.scissor(
390 391 392 393
			x * pixelRatio,
			y * pixelRatio,
			width * pixelRatio,
			height * pixelRatio
394
		);
M
Mr.doob 已提交
395 396 397

	};

398
	this.enableScissorTest = function ( boolean ) {
M
Mr.doob 已提交
399

M
Mr.doob 已提交
400
		state.setScissorTest( boolean );
M
Mr.doob 已提交
401 402 403 404 405

	};

	// Clearing

M
Mr.doob 已提交
406
	this.getClearColor = function () {
M
Mr.doob 已提交
407

M
Mr.doob 已提交
408
		return _clearColor;
M
Mr.doob 已提交
409 410 411

	};

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

M
Mr.doob 已提交
414
		_clearColor.set( color );
415

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

418
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
419 420 421

	};

M
Mr.doob 已提交
422
	this.getClearAlpha = function () {
M
Mr.doob 已提交
423

M
Mr.doob 已提交
424
		return _clearAlpha;
M
Mr.doob 已提交
425 426 427

	};

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

M
Mr.doob 已提交
430
		_clearAlpha = alpha;
M
Mr.doob 已提交
431

432
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
433 434 435 436 437 438 439 440 441 442 443 444

	};

	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 );
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

	};

	this.clearColor = function () {

		_gl.clear( _gl.COLOR_BUFFER_BIT );

	};

	this.clearDepth = function () {

		_gl.clear( _gl.DEPTH_BUFFER_BIT );

	};

	this.clearStencil = function () {

		_gl.clear( _gl.STENCIL_BUFFER_BIT );
M
Mr.doob 已提交
463 464 465 466 467 468 469 470 471 472

	};

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

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

	};

M
Mr.doob 已提交
473 474
	// Reset

475
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
476

D
dubejf 已提交
477 478 479 480 481 482
	this.dispose = function() {

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

	};

M
Mr.doob 已提交
483
	// Events
M
Mr.doob 已提交
484

D
dubejf 已提交
485 486 487 488 489 490 491 492 493 494 495
	function onContextLost( event ) {

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

	};

496
	function onTextureDispose( event ) {
M
Mr.doob 已提交
497 498 499 500 501 502 503

		var texture = event.target;

		texture.removeEventListener( 'dispose', onTextureDispose );

		deallocateTexture( texture );

504
		_infoMemory.textures --;
M
Mr.doob 已提交
505 506


507
	}
M
Mr.doob 已提交
508

509
	function onRenderTargetDispose( event ) {
M
Mr.doob 已提交
510 511 512 513 514 515 516

		var renderTarget = event.target;

		renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );

		deallocateRenderTarget( renderTarget );

517
		_infoMemory.textures --;
M
Mr.doob 已提交
518

519
	}
M
Mr.doob 已提交
520

521
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
522 523 524 525 526 527 528

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

529
	}
M
Mr.doob 已提交
530 531 532

	// Buffer deallocation

533
	function deallocateTexture( texture ) {
M
Mr.doob 已提交
534

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

537
		if ( texture.image && textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
538 539 540

			// cube texture

541
			_gl.deleteTexture( textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
542

543 544 545 546
		} else {

			// 2D texture

547
			if ( textureProperties.__webglInit === undefined ) return;
548

549
			_gl.deleteTexture( textureProperties.__webglTexture );
550

M
Mr.doob 已提交
551 552
		}

553
		// remove all webgl properties
554
		properties.delete( texture );
555

556
	}
M
Mr.doob 已提交
557

558
	function deallocateRenderTarget( renderTarget ) {
M
Mr.doob 已提交
559

560
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
561
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
562

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

M
Mr.doob 已提交
565
		_gl.deleteTexture( textureProperties.__webglTexture );
M
Mr.doob 已提交
566

M
Mr.doob 已提交
567 568 569 570
		if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {

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

571 572
				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
				_gl.deleteRenderbuffer( renderTargetProperties.__webglRenderbuffer[ i ] );
M
Mr.doob 已提交
573 574 575 576 577

			}

		} else {

578 579
			_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
			_gl.deleteRenderbuffer( renderTargetProperties.__webglRenderbuffer );
M
Mr.doob 已提交
580 581 582

		}

M
Mr.doob 已提交
583
		properties.delete( renderTarget.texture );
D
Daosheng Mu 已提交
584
		properties.delete( renderTarget );
M
Mr.doob 已提交
585

586
	}
M
Mr.doob 已提交
587

588
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
589

590 591 592 593
		releaseMaterialProgramReference( material );

		properties.delete( material );

594
	}
595 596


597
	function releaseMaterialProgramReference( material ) {
598

599
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
600 601 602

		material.program = undefined;

603
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
604

605
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
606

M
Mr.doob 已提交
607 608
		}

609
	}
M
Mr.doob 已提交
610 611 612 613 614

	// Buffer rendering

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

615
		state.initAttributes();
616

617
		var buffers = properties.get( object );
618

619 620 621 622
		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 已提交
623

624
		var attributes = program.getAttributes();
625

M
Mr.doob 已提交
626 627
		if ( object.hasPositions ) {

628
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
629
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
630

631 632
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
633 634 635 636 637

		}

		if ( object.hasNormals ) {

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

640
			if ( material.type !== 'MeshPhongMaterial' && material.type !== 'MeshPhysicalMaterial' && material.shading === THREE.FlatShading ) {
M
Mr.doob 已提交
641

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

644
					var array = object.normalArray;
M
Mr.doob 已提交
645

646 647 648
					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 已提交
649

650 651 652
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
653

654 655 656
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
657

658 659 660
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
661 662 663 664 665 666

				}

			}

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

668
			state.enableAttribute( attributes.normal );
669

670
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
671 672 673 674 675

		}

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

676
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
677
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
678

679
			state.enableAttribute( attributes.uv );
680

681
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
682 683 684 685 686

		}

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

687
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
688
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
689

690
			state.enableAttribute( attributes.color );
691

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

		}

696
		state.disableUnusedAttributes();
697

M
Mr.doob 已提交
698 699 700 701 702 703
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

704 705
	this.renderBufferDirect = function ( camera, lights, fog, geometry, material, object, group ) {

M
Mr.doob 已提交
706 707 708 709
		setMaterial( material );

		var program = setProgram( camera, lights, fog, material, object );

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

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

			}

			activeInfluences.sort( numericalSort );

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

743 744
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
745 746 747 748 749 750 751
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

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

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

752
					var index = influence[ 1 ];
M
Mr.doob 已提交
753

754 755
					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 已提交
756 757 758

				} else {

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

				}

			}

			var uniforms = program.getUniforms();

			if ( uniforms.morphTargetInfluences !== null ) {

				_gl.uniform1fv( uniforms.morphTargetInfluences, morphInfluences );

			}

			updateBuffers = true;

		}

M
Mr.doob 已提交
778 779
		//

780
		var index = geometry.index;
781 782
		var position = geometry.attributes.position;

783 784
		if ( material.wireframe === true ) {

785
			index = objects.getWireframeAttribute( geometry );
786 787 788

		}

789 790
		var renderer;

791
		if ( index !== null ) {
792

793 794
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
795

796
		} else {
797

798
			renderer = bufferRenderer;
799

800
		}
M
Mr.doob 已提交
801

802
		if ( updateBuffers ) {
M
Mr.doob 已提交
803

804
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
805

806
			if ( index !== null ) {
807

808
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
809 810 811

			}

812
		}
813

M
Mr.doob 已提交
814
		//
815

M
Mr.doob 已提交
816 817
		var dataStart = 0;
		var dataCount = Infinity;
818

M
Mr.doob 已提交
819
		if ( index !== null ) {
820

M
Mr.doob 已提交
821
			dataCount = index.count
822

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

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

M
Mr.doob 已提交
827
		}
828

M
Mr.doob 已提交
829 830
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
831

M
Mr.doob 已提交
832 833
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
834

M
Mr.doob 已提交
835 836 837 838 839 840
		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 );

		//
841

842
		if ( object instanceof THREE.Mesh ) {
843

844
			if ( material.wireframe === true ) {
845

846 847
				state.setLineWidth( material.wireframeLinewidth * pixelRatio );
				renderer.setMode( _gl.LINES );
848

849
			} else {
850

851
				renderer.setMode( _gl.TRIANGLES );
852

853
			}
854

855
			if ( geometry instanceof THREE.InstancedBufferGeometry && geometry.maxInstancedCount > 0 ) {
856

857
				renderer.renderInstances( geometry );
858

859
			} else {
860

M
Mr.doob 已提交
861
				renderer.render( drawStart, drawCount );
862

863
			}
864

865
		} else if ( object instanceof THREE.Line ) {
866

867
			var lineWidth = material.linewidth;
868

869
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
870

871
			state.setLineWidth( lineWidth * pixelRatio );
872

873
			if ( object instanceof THREE.LineSegments ) {
874

875
				renderer.setMode( _gl.LINES );
876

877
			} else {
878

879
				renderer.setMode( _gl.LINE_STRIP );
880 881

			}
M
Mr.doob 已提交
882

M
Mr.doob 已提交
883
			renderer.render( drawStart, drawCount );
884

885
		} else if ( object instanceof THREE.Points ) {
886 887

			renderer.setMode( _gl.POINTS );
M
Mr.doob 已提交
888
			renderer.render( drawStart, drawCount );
889

M
Mr.doob 已提交
890 891 892 893
		}

	};

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

M
Mr.doob 已提交
896
		var extension;
B
Ben Adams 已提交
897

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

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

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

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

M
Mr.doob 已提交
907 908 909
			}

		}
B
Ben Adams 已提交
910

911 912
		if ( startIndex === undefined ) startIndex = 0;

913 914
		state.initAttributes();

915
		var geometryAttributes = geometry.attributes;
916

917
		var programAttributes = program.getAttributes();
918

919
		var materialDefaultAttributeValues = material.defaultAttributeValues;
920

921
		for ( var name in programAttributes ) {
922

923
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
924

M
Mr.doob 已提交
925
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
926

927
				var geometryAttribute = geometryAttributes[ name ];
928

M
Mr.doob 已提交
929
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
930

931
					var size = geometryAttribute.itemSize;
G
gero3 已提交
932
					var buffer = objects.getAttributeBuffer( geometryAttribute );
933

B
Ben Adams 已提交
934
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
935

M
Mr.doob 已提交
936 937 938 939 940
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
944
							if ( geometry.maxInstancedCount === undefined ) {
945

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

M
Mr.doob 已提交
948
							}
B
Ben Adams 已提交
949

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

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

M
Mr.doob 已提交
954
						}
B
Ben Adams 已提交
955

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

M
Mr.doob 已提交
959
					} else {
B
Ben Adams 已提交
960

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

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

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

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

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

M
Mr.doob 已提交
971 972 973 974
						} else {

							state.enableAttribute( programAttribute );

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

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

B
Ben Adams 已提交
980
					}
M
Mr.doob 已提交
981

982 983
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
984
					var value = materialDefaultAttributeValues[ name ];
985

986
					if ( value !== undefined ) {
M
Mr.doob 已提交
987

988
						switch ( value.length ) {
M
Mr.doob 已提交
989

990 991 992
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
993

994 995 996
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
997

998 999 1000
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
1001

1002 1003
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
1004 1005

						}
M
Mr.doob 已提交
1006 1007 1008 1009 1010 1011 1012 1013

					}

				}

			}

		}
1014

1015
		state.disableUnusedAttributes();
1016

M
Mr.doob 已提交
1017 1018
	}

M
Mr.doob 已提交
1019 1020
	// Sorting

1021 1022 1023 1024 1025 1026
	function numericalSort ( a, b ) {

		return b[ 0 ] - a[ 0 ];

	}

M
Mr.doob 已提交
1027 1028
	function painterSortStable ( a, b ) {

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

U
unconed 已提交
1031
			return a.object.renderOrder - b.object.renderOrder;
1032

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

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

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

M
Mr.doob 已提交
1039
			return a.z - b.z;
M
Mr.doob 已提交
1040 1041 1042

		} else {

1043
			return a.id - b.id;
M
Mr.doob 已提交
1044 1045 1046

		}

1047
	}
M
Mr.doob 已提交
1048

1049 1050
	function reversePainterSortStable ( a, b ) {

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

U
unconed 已提交
1053
			return a.object.renderOrder - b.object.renderOrder;
1054 1055

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

M
Mr.doob 已提交
1057
			return b.z - a.z;
1058 1059 1060 1061 1062 1063 1064

		} else {

			return a.id - b.id;

		}

1065
	}
1066

M
Mr.doob 已提交
1067 1068 1069 1070 1071 1072
	// Rendering

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

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

1073
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1074 1075 1076 1077
			return;

		}

M
Mr.doob 已提交
1078
		var fog = scene.fog;
M
Mr.doob 已提交
1079 1080 1081

		// reset caching for this frame

1082
		_currentGeometryProgram = '';
1083
		_currentMaterialId = - 1;
1084
		_currentCamera = null;
M
Mr.doob 已提交
1085 1086 1087 1088
		_lightsNeedUpdate = true;

		// update scene graph

1089
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1090 1091 1092

		// update camera matrices and frustum

1093
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1094 1095 1096 1097 1098 1099

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

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

M
Mr.doob 已提交
1100
		lights.length = 0;
1101

M
Mr.doob 已提交
1102 1103
		opaqueObjectsLastIndex = - 1;
		transparentObjectsLastIndex = - 1;
1104

M
Mr.doob 已提交
1105 1106 1107
		sprites.length = 0;
		lensFlares.length = 0;

1108
		projectObject( scene, camera );
M
Mr.doob 已提交
1109

1110 1111 1112
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1113
		if ( _this.sortObjects === true ) {
1114 1115 1116

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

1118 1119
		}

M
Mr.doob 已提交
1120
		//
M
Mr.doob 已提交
1121

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

		//

1126 1127 1128 1129
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138

		this.setRenderTarget( renderTarget );

		if ( this.autoClear || forceClear ) {

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

		}

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

		if ( scene.overrideMaterial ) {

1143
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1144

1145 1146
			renderObjects( opaqueObjects, camera, lights, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, lights, fog, overrideMaterial );
1147

M
Mr.doob 已提交
1148 1149 1150 1151
		} else {

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

M
Mr.doob 已提交
1152
			state.setBlending( THREE.NoBlending );
1153
			renderObjects( opaqueObjects, camera, lights, fog );
M
Mr.doob 已提交
1154 1155 1156

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

1157
			renderObjects( transparentObjects, camera, lights, fog );
M
Mr.doob 已提交
1158 1159 1160 1161 1162

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1163 1164
		spritePlugin.render( scene, camera );
		lensFlarePlugin.render( scene, camera, _currentWidth, _currentHeight );
M
Mr.doob 已提交
1165 1166 1167

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

M
Mr.doob 已提交
1168 1169 1170 1171 1172
		if ( renderTarget ) {

			var texture = renderTarget.texture;
			var isTargetPowerOfTwo = isPowerOfTwo( renderTarget );
			if ( texture.generateMipmaps && isTargetPowerOfTwo && texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) {
M
Mr.doob 已提交
1173

M
Mr.doob 已提交
1174 1175 1176
				 updateRenderTargetMipmap( renderTarget );

			}
M
Mr.doob 已提交
1177 1178 1179 1180 1181

		}

		// Ensure depth buffer writing is enabled so it can be cleared on next render

M
Mr.doob 已提交
1182 1183
		state.setDepthTest( true );
		state.setDepthWrite( true );
1184
		state.setColorWrite( true );
M
Mr.doob 已提交
1185 1186 1187 1188

		// _gl.finish();

	};
M
Mr.doob 已提交
1189

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

1192
		var array, index;
M
Mr.doob 已提交
1193

1194
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1195 1196 1197

		if ( material.transparent ) {

1198 1199
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1200 1201 1202

		} else {

1203 1204 1205 1206 1207
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1208 1209
		// recycle existing render item or grow the array

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		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 已提交
1220 1221 1222

		} else {

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

		}

	}

1239
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1240

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

1243
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1244

1245
			if ( object instanceof THREE.Light ) {
M
Mr.doob 已提交
1246

1247
				lights.push( object );
M
Mr.doob 已提交
1248

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

1251
				sprites.push( object );
M
Mr.doob 已提交
1252

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

1255
				lensFlares.push( object );
M
Mr.doob 已提交
1256

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

1259
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1260

1261 1262
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1263

1264
				}
1265

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

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

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

1272
					object.skeleton.update();
1273

1274
				}
1275

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

1278
					var material = object.material;
1279

1280
					if ( material.visible === true ) {
M
Mr.doob 已提交
1281

1282
						if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1283

1284 1285 1286 1287
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1288

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

1291
						if ( material instanceof THREE.MeshFaceMaterial ) {
1292

1293 1294
							var groups = geometry.groups;
							var materials = material.materials;
1295

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

1298 1299
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1300

1301
								if ( groupMaterial.visible === true ) {
1302

1303 1304 1305
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1306

M
Mr.doob 已提交
1307
							}
M
Mr.doob 已提交
1308

1309
						} else {
M
Mr.doob 已提交
1310

1311
							pushRenderItem( object, geometry, material, _vector3.z, null );
1312

1313
						}
O
OpenShift guest 已提交
1314

1315
					}
M
Mr.doob 已提交
1316

1317
				}
M
Mr.doob 已提交
1318

1319
			}
M
Mr.doob 已提交
1320

M
Mr.doob 已提交
1321
		}
M
Mr.doob 已提交
1322

M
Mr.doob 已提交
1323
		var children = object.children;
M
Mr.doob 已提交
1324

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

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

1329
		}
1330

1331
	}
M
Mr.doob 已提交
1332

1333
	function renderObjects( renderList, camera, lights, fog, overrideMaterial ) {
M
Mr.doob 已提交
1334

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

1337
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1338

1339
			var object = renderItem.object;
M
Mr.doob 已提交
1340 1341 1342
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1343

1344 1345
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1346

M
Mr.doob 已提交
1347
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1348

M
Mr.doob 已提交
1349
				setMaterial( material );
M
Mr.doob 已提交
1350

M
Mr.doob 已提交
1351
				var program = setProgram( camera, lights, fog, material, object );
M
Mr.doob 已提交
1352

M
Mr.doob 已提交
1353
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1354

M
Mr.doob 已提交
1355
				object.render( function ( object ) {
M
Mr.doob 已提交
1356

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

M
Mr.doob 已提交
1359
				} );
1360

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

M
Mr.doob 已提交
1363
				_this.renderBufferDirect( camera, lights, fog, geometry, material, object, group );
M
Mr.doob 已提交
1364

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

1367
		}
M
Mr.doob 已提交
1368

1369
	}
G
gero3 已提交
1370

1371
	function initMaterial( material, lights, fog, object ) {
M
Mr.doob 已提交
1372

1373
		var materialProperties = properties.get( material );
G
gero3 已提交
1374 1375

		var parameters = programCache.getParameters( material, lights, fog, object );
G
gero3 已提交
1376
		var code = programCache.getProgramCode( material, parameters );
G
gero3 已提交
1377

1378
		var program = materialProperties.program;
T
tschw 已提交
1379
		var programChange = true;
1380

1381
		if ( program === undefined ) {
B
Ben Adams 已提交
1382

M
Mr.doob 已提交
1383 1384
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1385

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

M
Mr.doob 已提交
1388
			// changed glsl or parameters
1389
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1390

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

T
tschw 已提交
1393
			// same glsl and uniform list
T
tschw 已提交
1394 1395
			return;

T
tschw 已提交
1396
		} else {
B
Ben Adams 已提交
1397

T
tschw 已提交
1398 1399
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1400 1401 1402

		}

1403
		if ( programChange ) {
B
Ben Adams 已提交
1404

1405
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1406

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

1409 1410 1411 1412 1413 1414
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1415

1416
			} else {
B
Ben Adams 已提交
1417

1418 1419 1420 1421 1422 1423
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1424

1425
			}
G
gero3 已提交
1426

1427
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1428

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

1431 1432
			materialProperties.program = program;
			material.program = program;
1433 1434 1435

		}

1436
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1437 1438 1439 1440 1441

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

M
Mr.doob 已提交
1444
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

1458
			for ( i = 0; i < _this.maxMorphNormals; i ++ ) {
M
Mr.doob 已提交
1459

M
Mr.doob 已提交
1460
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1461 1462 1463 1464 1465 1466 1467 1468 1469

					material.numSupportedMorphNormals ++;

				}

			}

		}

1470
		materialProperties.uniformsList = [];
M
Mr.doob 已提交
1471

1472
		var uniformLocations = materialProperties.program.getUniforms();
M
Mr.doob 已提交
1473

1474
		for ( var u in materialProperties.__webglShader.uniforms ) {
M
Mr.doob 已提交
1475

1476
			var location = uniformLocations[ u ];
1477 1478

			if ( location ) {
G
gero3 已提交
1479

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

1482
			}
M
Mr.doob 已提交
1483 1484 1485

		}

M
Mr.doob 已提交
1486
	}
M
Mr.doob 已提交
1487

1488 1489
	function setMaterial( material ) {

M
Mr.doob 已提交
1490 1491
		setMaterialFaces( material );

1492 1493
		if ( material.transparent === true ) {

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

1496 1497 1498 1499
		} else {

			state.setBlending( THREE.NoBlending );

1500 1501
		}

B
Ben Adams 已提交
1502
		state.setDepthFunc( material.depthFunc );
M
Mr.doob 已提交
1503 1504
		state.setDepthTest( material.depthTest );
		state.setDepthWrite( material.depthWrite );
1505
		state.setColorWrite( material.colorWrite );
M
Mr.doob 已提交
1506
		state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
1507 1508 1509

	}

M
Mr.doob 已提交
1510 1511
	function setMaterialFaces( material ) {

1512
		material.side !== THREE.DoubleSide ? state.enable( _gl.CULL_FACE ) : state.disable( _gl.CULL_FACE );
M
Mr.doob 已提交
1513 1514 1515 1516
		state.setFlipSided( material.side === THREE.BackSide );

	}

M
Mr.doob 已提交
1517 1518 1519 1520
	function setProgram( camera, lights, fog, material, object ) {

		_usedTextureUnits = 0;

1521
		var materialProperties = properties.get( material );
1522

1523
		if ( material.needsUpdate || ! materialProperties.program ) {
M
Mr.doob 已提交
1524

1525
			initMaterial( material, lights, fog, object );
M
Mr.doob 已提交
1526 1527 1528 1529
			material.needsUpdate = false;

		}

1530
		var refreshProgram = false;
M
Mr.doob 已提交
1531
		var refreshMaterial = false;
1532
		var refreshLights = false;
M
Mr.doob 已提交
1533

1534
		var program = materialProperties.program,
1535
			p_uniforms = program.getUniforms(),
1536
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1537

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

1540 1541
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1542

1543
			refreshProgram = true;
M
Mr.doob 已提交
1544
			refreshMaterial = true;
1545
			refreshLights = true;
M
Mr.doob 已提交
1546 1547 1548 1549 1550

		}

		if ( material.id !== _currentMaterialId ) {

G
gero3 已提交
1551
			if ( _currentMaterialId === - 1 ) refreshLights = true;
M
Mr.doob 已提交
1552
			_currentMaterialId = material.id;
1553

M
Mr.doob 已提交
1554 1555 1556 1557
			refreshMaterial = true;

		}

1558
		if ( refreshProgram || camera !== _currentCamera ) {
M
Mr.doob 已提交
1559 1560 1561

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

G
gero3 已提交
1562
			if ( capabilities.logarithmicDepthBuffer ) {
1563

1564
				_gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1565 1566 1567 1568

			}


M
Mr.doob 已提交
1569 1570
			if ( camera !== _currentCamera ) _currentCamera = camera;

1571 1572 1573 1574 1575
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1576
				 material instanceof THREE.MeshPhysicalMaterial ||
1577 1578
				 material.envMap ) {

1579
				if ( p_uniforms.cameraPosition !== undefined ) {
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

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

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1590
				 material instanceof THREE.MeshBasicMaterial ||
1591
				 material instanceof THREE.MeshPhysicalMaterial ||
1592 1593 1594
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

1595
				if ( p_uniforms.viewMatrix !== undefined ) {
1596 1597

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

1599 1600 1601 1602
				}

			}

M
Mr.doob 已提交
1603 1604 1605 1606 1607 1608 1609 1610
		}

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

1611
			if ( object.bindMatrix && p_uniforms.bindMatrix !== undefined ) {
1612 1613 1614 1615 1616

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

			}

1617
			if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== undefined ) {
1618 1619 1620 1621 1622

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

			}

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

1625
				if ( p_uniforms.boneTexture !== undefined ) {
M
Mr.doob 已提交
1626 1627 1628 1629

					var textureUnit = getTextureUnit();

					_gl.uniform1i( p_uniforms.boneTexture, textureUnit );
1630
					_this.setTexture( object.skeleton.boneTexture, textureUnit );
M
Mr.doob 已提交
1631 1632 1633

				}

1634
				if ( p_uniforms.boneTextureWidth !== undefined ) {
1635

1636
					_gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
1637 1638 1639

				}

1640
				if ( p_uniforms.boneTextureHeight !== undefined ) {
1641

1642
					_gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
1643 1644 1645

				}

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

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

1650
					_gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
M
Mr.doob 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669

				}

			}

		}

		if ( refreshMaterial ) {

			// refresh uniforms common to several materials

			if ( fog && material.fog ) {

				refreshUniformsFog( m_uniforms, fog );

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1670
				 material instanceof THREE.MeshPhysicalMaterial ||
M
Mr.doob 已提交
1671 1672 1673 1674
				 material.lights ) {

				if ( _lightsNeedUpdate ) {

1675
					refreshLights = true;
T
tschw 已提交
1676
					setupLights( lights, camera );
M
Mr.doob 已提交
1677
					_lightsNeedUpdate = false;
G
gero3 已提交
1678

M
Mr.doob 已提交
1679 1680
				}

1681
				if ( refreshLights ) {
G
gero3 已提交
1682

1683
					refreshUniformsLights( m_uniforms, _lights );
1684
					markUniformsLightsNeedsUpdate( m_uniforms, true );
G
gero3 已提交
1685

1686
				} else {
G
gero3 已提交
1687

1688
					markUniformsLightsNeedsUpdate( m_uniforms, false );
G
gero3 已提交
1689

1690
				}
M
Mr.doob 已提交
1691 1692 1693 1694 1695

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1696
				 material instanceof THREE.MeshPhongMaterial ||
1697
				 material instanceof THREE.MeshPhysicalMaterial ) {
M
Mr.doob 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713

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

1714
			} else if ( material instanceof THREE.PointsMaterial ) {
M
Mr.doob 已提交
1715 1716 1717 1718 1719 1720 1721

				refreshUniformsParticle( m_uniforms, material );

			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

1722
			} else if ( material instanceof THREE.MeshPhysicalMaterial ) {
W
WestLangley 已提交
1723 1724 1725

				refreshUniformsStandard( m_uniforms, material );

M
Mr.doob 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
			} else if ( material instanceof THREE.MeshDepthMaterial ) {

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

			} else if ( material instanceof THREE.MeshNormalMaterial ) {

				m_uniforms.opacity.value = material.opacity;

			}

M
Mr.doob 已提交
1738
			if ( shadowMap.enabled ) {
M
Mr.doob 已提交
1739

1740 1741 1742 1743 1744
				if ( object.receiveShadow && ! material._shadowPass ) {

					refreshUniformsShadow( m_uniforms, lights, camera );

				}
M
Mr.doob 已提交
1745 1746 1747 1748 1749

			}

			// load common uniforms

1750
			loadUniformsGeneric( materialProperties.uniformsList );
M
Mr.doob 已提交
1751 1752 1753 1754 1755

		}

		loadUniformsMatrices( p_uniforms, object );

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

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

M
Mr.doob 已提交
1760 1761 1762 1763
		}

		return program;

M
Mr.doob 已提交
1764
	}
M
Mr.doob 已提交
1765 1766 1767 1768 1769 1770 1771

	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

1772
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
1773

1774
		if ( material.emissive ) {
M
Mr.doob 已提交
1775

1776
			uniforms.emissive.value = material.emissive;
M
Mr.doob 已提交
1777 1778 1779

		}

1780 1781 1782
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
1783

1784
		if ( material.aoMap ) {
1785

1786 1787
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
1788 1789 1790

		}

M
Mr.doob 已提交
1791
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
1792 1793 1794 1795 1796
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
1797
		// 6. emissive map
M
Mr.doob 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

1809 1810 1811 1812
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
1813 1814 1815 1816 1817 1818 1819 1820
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

1821 1822 1823 1824
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

1825 1826 1827 1828
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
1829 1830 1831 1832
		}

		if ( uvScaleMap !== undefined ) {

M
Mr.doob 已提交
1833
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) uvScaleMap = uvScaleMap.texture;
M
Mr.doob 已提交
1834 1835 1836 1837 1838 1839 1840 1841
			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;
1842
		uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
M
Mr.doob 已提交
1843

1844
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
1845 1846
		uniforms.refractionRatio.value = material.refractionRatio;

M
Mr.doob 已提交
1847
	}
M
Mr.doob 已提交
1848 1849 1850 1851 1852 1853

	function refreshUniformsLine ( uniforms, material ) {

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

M
Mr.doob 已提交
1854
	}
M
Mr.doob 已提交
1855 1856 1857 1858 1859 1860 1861

	function refreshUniformsDash ( uniforms, material ) {

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

M
Mr.doob 已提交
1862
	}
M
Mr.doob 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872

	function refreshUniformsParticle ( uniforms, material ) {

		uniforms.psColor.value = material.color;
		uniforms.opacity.value = material.opacity;
		uniforms.size.value = material.size;
		uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.

		uniforms.map.value = material.map;

1873 1874 1875 1876 1877 1878 1879 1880 1881
		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 已提交
1882
	}
M
Mr.doob 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898

	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 已提交
1899
	}
M
Mr.doob 已提交
1900 1901 1902

	function refreshUniformsPhong ( uniforms, material ) {

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

1906 1907 1908 1909
		if ( material.lightMap ) {

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

1911
		}
1912

1913
		if ( material.emissiveMap ) {
1914

1915
			uniforms.emissiveMap.value = material.emissiveMap;
1916

1917
		}
M
Mr.doob 已提交
1918

1919 1920 1921 1922
		if ( material.bumpMap ) {

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

1924
		}
M
Mr.doob 已提交
1925

1926 1927 1928 1929 1930 1931
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
1932

1933 1934 1935 1936 1937
		if ( material.displacementMap ) {

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

1939
		}
1940 1941 1942

	}

W
WestLangley 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
	function refreshUniformsStandard ( uniforms, material ) {

		uniforms.roughness.value = material.roughness;
		//uniforms.reflectivity.value = material.reflectivity; // part of uniforms common
		uniforms.metalness.value = material.metalness;

		if ( material.roughnessMap ) {

			uniforms.roughnessMap.value = material.roughnessMap;

		}

		if ( material.reflectivityMap ) {

			uniforms.reflectivityMap.value = material.reflectivityMap;

		}

		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;

		}

	}

M
Mr.doob 已提交
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	function refreshUniformsLights ( uniforms, lights ) {

		uniforms.ambientLightColor.value = lights.ambient;

		uniforms.directionalLightColor.value = lights.directional.colors;
		uniforms.directionalLightDirection.value = lights.directional.positions;

		uniforms.pointLightColor.value = lights.point.colors;
		uniforms.pointLightPosition.value = lights.point.positions;
		uniforms.pointLightDistance.value = lights.point.distances;
M
Mr.doob 已提交
2021
		uniforms.pointLightDecay.value = lights.point.decays;
M
Mr.doob 已提交
2022 2023 2024 2025 2026 2027 2028

		uniforms.spotLightColor.value = lights.spot.colors;
		uniforms.spotLightPosition.value = lights.spot.positions;
		uniforms.spotLightDistance.value = lights.spot.distances;
		uniforms.spotLightDirection.value = lights.spot.directions;
		uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
		uniforms.spotLightExponent.value = lights.spot.exponents;
M
Mr.doob 已提交
2029
		uniforms.spotLightDecay.value = lights.spot.decays;
M
Mr.doob 已提交
2030 2031 2032 2033 2034

		uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
		uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
		uniforms.hemisphereLightDirection.value = lights.hemi.positions;

M
Mr.doob 已提交
2035
	}
M
Mr.doob 已提交
2036

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

M
Mr.doob 已提交
2039
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2040

M
Mr.doob 已提交
2041
		uniforms.ambientLightColor.needsUpdate = value;
2042

M
Mr.doob 已提交
2043 2044
		uniforms.directionalLightColor.needsUpdate = value;
		uniforms.directionalLightDirection.needsUpdate = value;
2045

M
Mr.doob 已提交
2046 2047 2048 2049
		uniforms.pointLightColor.needsUpdate = value;
		uniforms.pointLightPosition.needsUpdate = value;
		uniforms.pointLightDistance.needsUpdate = value;
		uniforms.pointLightDecay.needsUpdate = value;
2050

M
Mr.doob 已提交
2051 2052 2053 2054 2055 2056 2057
		uniforms.spotLightColor.needsUpdate = value;
		uniforms.spotLightPosition.needsUpdate = value;
		uniforms.spotLightDistance.needsUpdate = value;
		uniforms.spotLightDirection.needsUpdate = value;
		uniforms.spotLightAngleCos.needsUpdate = value;
		uniforms.spotLightExponent.needsUpdate = value;
		uniforms.spotLightDecay.needsUpdate = value;
2058

M
Mr.doob 已提交
2059 2060 2061
		uniforms.hemisphereLightSkyColor.needsUpdate = value;
		uniforms.hemisphereLightGroundColor.needsUpdate = value;
		uniforms.hemisphereLightDirection.needsUpdate = value;
2062

M
Mr.doob 已提交
2063
	}
2064

M
Mr.doob 已提交
2065
	function refreshUniformsShadow ( uniforms, lights, camera ) {
M
Mr.doob 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074

		if ( uniforms.shadowMatrix ) {

			var j = 0;

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

				var light = lights[ i ];

M
Mr.doob 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
				if ( light.castShadow === true ) {

					if ( light instanceof THREE.PointLight || light instanceof THREE.SpotLight || light instanceof THREE.DirectionalLight ) {

						var shadow = light.shadow;

						if ( light instanceof THREE.PointLight ) {

							// 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();
							shadow.matrix.identity().setPosition( _vector3 );

							// for point lights we set the sign of the shadowDarkness uniform to be negative
							uniforms.shadowDarkness.value[ j ] = - shadow.darkness;

						} else {
M
Mr.doob 已提交
2092

M
Mr.doob 已提交
2093
							uniforms.shadowDarkness.value[ j ] = shadow.darkness;
M
Mr.doob 已提交
2094

M
Mr.doob 已提交
2095
						}
M
Mr.doob 已提交
2096

M
Mr.doob 已提交
2097 2098 2099 2100
						uniforms.shadowMatrix.value[ j ] = shadow.matrix;
						uniforms.shadowMap.value[ j ] = shadow.map;
						uniforms.shadowMapSize.value[ j ] = shadow.mapSize;
						uniforms.shadowBias.value[ j ] = shadow.bias;
M
Mr.doob 已提交
2101

M
Mr.doob 已提交
2102
						j ++;
M
Mr.doob 已提交
2103

M
Mr.doob 已提交
2104
					}
M
Mr.doob 已提交
2105 2106 2107 2108 2109 2110 2111

				}

			}

		}

M
Mr.doob 已提交
2112
	}
M
Mr.doob 已提交
2113 2114 2115 2116 2117

	// Uniforms (load to GPU)

	function loadUniformsMatrices ( uniforms, object ) {

2118
		_gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object.modelViewMatrix.elements );
M
Mr.doob 已提交
2119 2120 2121

		if ( uniforms.normalMatrix ) {

2122
			_gl.uniformMatrix3fv( uniforms.normalMatrix, false, object.normalMatrix.elements );
M
Mr.doob 已提交
2123 2124 2125

		}

M
Mr.doob 已提交
2126
	}
M
Mr.doob 已提交
2127 2128 2129 2130 2131

	function getTextureUnit() {

		var textureUnit = _usedTextureUnits;

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

G
gero3 已提交
2134
			console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
M
Mr.doob 已提交
2135 2136 2137 2138 2139 2140 2141

		}

		_usedTextureUnits += 1;

		return textureUnit;

M
Mr.doob 已提交
2142
	}
M
Mr.doob 已提交
2143

2144
	function loadUniformsGeneric ( uniforms ) {
M
Mr.doob 已提交
2145

M
Mr.doob 已提交
2146
		var texture, textureUnit;
M
Mr.doob 已提交
2147

M
Mr.doob 已提交
2148 2149 2150
		for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {

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

2152 2153
			// needsUpdate property is not added to all uniforms.
			if ( uniform.needsUpdate === false ) continue;
2154

M
Mr.doob 已提交
2155 2156
			var type = uniform.type;
			var value = uniform.value;
2157
			var location = uniforms[ j ][ 1 ];
M
Mr.doob 已提交
2158

2159
			switch ( type ) {
M
Mr.doob 已提交
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 2213 2214
				case '1i':
					_gl.uniform1i( location, value );
					break;

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

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

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

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

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

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

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

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

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

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

				case 'Matrix3fv':
					_gl.uniformMatrix3fv( location, false, value );
					break;

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

				//

M
Mr.doob 已提交
2215
				case 'i':
M
Mr.doob 已提交
2216

2217 2218
					// single integer
					_gl.uniform1i( location, value );
M
Mr.doob 已提交
2219

2220
					break;
M
Mr.doob 已提交
2221

2222
				case 'f':
M
Mr.doob 已提交
2223

2224 2225
					// single float
					_gl.uniform1f( location, value );
M
Mr.doob 已提交
2226

2227
					break;
M
Mr.doob 已提交
2228

2229
				case 'v2':
M
Mr.doob 已提交
2230

2231 2232
					// single THREE.Vector2
					_gl.uniform2f( location, value.x, value.y );
M
Mr.doob 已提交
2233

2234
					break;
M
Mr.doob 已提交
2235

2236
				case 'v3':
M
Mr.doob 已提交
2237

2238 2239
					// single THREE.Vector3
					_gl.uniform3f( location, value.x, value.y, value.z );
M
Mr.doob 已提交
2240

2241
					break;
M
Mr.doob 已提交
2242

M
Mr.doob 已提交
2243
				case 'v4':
M
Mr.doob 已提交
2244

2245 2246
					// single THREE.Vector4
					_gl.uniform4f( location, value.x, value.y, value.z, value.w );
M
Mr.doob 已提交
2247

2248
					break;
M
Mr.doob 已提交
2249

2250
				case 'c':
M
Mr.doob 已提交
2251

2252 2253
					// single THREE.Color
					_gl.uniform3f( location, value.r, value.g, value.b );
M
Mr.doob 已提交
2254

2255
					break;
M
Mr.doob 已提交
2256

2257
				case 'iv1':
M
Mr.doob 已提交
2258

2259 2260
					// flat array of integers (JS or typed array)
					_gl.uniform1iv( location, value );
M
Mr.doob 已提交
2261

2262
					break;
M
Mr.doob 已提交
2263

2264
				case 'iv':
M
Mr.doob 已提交
2265

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

2269
					break;
M
Mr.doob 已提交
2270

2271
				case 'fv1':
M
Mr.doob 已提交
2272

2273 2274
					// flat array of floats (JS or typed array)
					_gl.uniform1fv( location, value );
M
Mr.doob 已提交
2275

2276
					break;
M
Mr.doob 已提交
2277

2278
				case 'fv':
M
Mr.doob 已提交
2279

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

2283
					break;
M
Mr.doob 已提交
2284

2285
				case 'v2v':
M
Mr.doob 已提交
2286

2287
					// array of THREE.Vector2
M
Mr.doob 已提交
2288

2289
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2290

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

2293
					}
M
Mr.doob 已提交
2294

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

M
Mr.doob 已提交
2297 2298
						uniform._array[ i2 + 0 ] = value[ i ].x;
						uniform._array[ i2 + 1 ] = value[ i ].y;
M
Mr.doob 已提交
2299

2300
					}
M
Mr.doob 已提交
2301

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

2304
					break;
M
Mr.doob 已提交
2305

2306
				case 'v3v':
M
Mr.doob 已提交
2307

2308
					// array of THREE.Vector3
M
Mr.doob 已提交
2309

2310
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2311

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

2314
					}
M
Mr.doob 已提交
2315

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

M
Mr.doob 已提交
2318 2319 2320
						uniform._array[ i3 + 0 ] = value[ i ].x;
						uniform._array[ i3 + 1 ] = value[ i ].y;
						uniform._array[ i3 + 2 ] = value[ i ].z;
R
Ryan Tsao 已提交
2321

2322
					}
R
Ryan Tsao 已提交
2323

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

2326
					break;
R
Ryan Tsao 已提交
2327

2328
				case 'v4v':
R
Ryan Tsao 已提交
2329

2330
					// array of THREE.Vector4
R
Ryan Tsao 已提交
2331

2332
					if ( uniform._array === undefined ) {
R
Ryan Tsao 已提交
2333

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

2336
					}
M
Mr.doob 已提交
2337

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

M
Mr.doob 已提交
2340 2341 2342 2343
						uniform._array[ i4 + 0 ] = value[ i ].x;
						uniform._array[ i4 + 1 ] = value[ i ].y;
						uniform._array[ i4 + 2 ] = value[ i ].z;
						uniform._array[ i4 + 3 ] = value[ i ].w;
M
Mr.doob 已提交
2344

2345
					}
M
Mr.doob 已提交
2346

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

2349
					break;
M
Mr.doob 已提交
2350

2351
				case 'm3':
M
Mr.doob 已提交
2352

2353 2354
					// single THREE.Matrix3
					_gl.uniformMatrix3fv( location, false, value.elements );
M
Mr.doob 已提交
2355

2356
					break;
M
Mr.doob 已提交
2357

2358
				case 'm3v':
M
Mr.doob 已提交
2359

2360
					// array of THREE.Matrix3
M
Mr.doob 已提交
2361

2362
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2363

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

2366
					}
M
Mr.doob 已提交
2367

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

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

2372
					}
M
Mr.doob 已提交
2373

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

2376
					break;
M
Mr.doob 已提交
2377

2378
				case 'm4':
M
Mr.doob 已提交
2379

2380 2381
					// single THREE.Matrix4
					_gl.uniformMatrix4fv( location, false, value.elements );
M
Mr.doob 已提交
2382

2383
					break;
M
Mr.doob 已提交
2384

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

2387
					// array of THREE.Matrix4
M
Mr.doob 已提交
2388

2389
					if ( uniform._array === undefined ) {
M
Mr.doob 已提交
2390

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

2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
					}

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

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

					}

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

					break;
M
Mr.doob 已提交
2404

2405
				case 't':
M
Mr.doob 已提交
2406

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

2409 2410 2411 2412
					texture = value;
					textureUnit = getTextureUnit();

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

2414
					if ( ! texture ) continue;
M
Mr.doob 已提交
2415

2416
					if ( texture instanceof THREE.CubeTexture ||
G
gero3 已提交
2417 2418 2419
						 ( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {

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

2421
						setCubeTexture( texture, textureUnit );
M
Mr.doob 已提交
2422

2423 2424
					} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {

M
Mr.doob 已提交
2425 2426 2427 2428 2429
						setCubeTextureDynamic( texture.texture, textureUnit );

					} else if ( texture instanceof THREE.WebGLRenderTarget ) {

						_this.setTexture( texture.texture, textureUnit );
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440

					} else {

						_this.setTexture( texture, textureUnit );

					}

					break;

				case 'tv':

M
Mr.doob 已提交
2441
					// array of THREE.Texture (2d or cube)
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463

					if ( uniform._array === undefined ) {

						uniform._array = [];

					}

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

						uniform._array[ i ] = getTextureUnit();

					}

					_gl.uniform1iv( location, uniform._array );

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

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

						if ( ! texture ) continue;

M
Mr.doob 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
						if ( texture instanceof THREE.CubeTexture ||
							 ( texture.image instanceof Array && texture.image.length === 6 ) ) {

							// CompressedTexture can have Array in image :/

							setCubeTexture( texture, textureUnit );

						} else if ( texture instanceof THREE.WebGLRenderTarget ) {

							_this.setTexture( texture.texture, textureUnit );

						} else if ( texture instanceof THREE.WebGLRenderTargetCube ) {

							setCubeTextureDynamic( texture.texture, textureUnit );

						} else {

							_this.setTexture( texture, textureUnit );

						}
2484 2485 2486 2487 2488 2489

					}

					break;

				default:
2490

2491
					console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
2492

M
Mr.doob 已提交
2493 2494 2495 2496
			}

		}

M
Mr.doob 已提交
2497
	}
M
Mr.doob 已提交
2498 2499 2500

	function setColorLinear( array, offset, color, intensity ) {

M
Mr.doob 已提交
2501
		array[ offset + 0 ] = color.r * intensity;
M
Mr.doob 已提交
2502 2503 2504
		array[ offset + 1 ] = color.g * intensity;
		array[ offset + 2 ] = color.b * intensity;

M
Mr.doob 已提交
2505
	}
M
Mr.doob 已提交
2506

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

B
brason 已提交
2509
		var l, ll, light,
M
Mr.doob 已提交
2510 2511
		r = 0, g = 0, b = 0,
		color, skyColor, groundColor,
B
brason 已提交
2512
		intensity,
M
Mr.doob 已提交
2513 2514 2515 2516
		distance,

		zlights = _lights,

T
tschw 已提交
2517 2518
		viewMatrix = camera.matrixWorldInverse,

M
Mr.doob 已提交
2519 2520 2521 2522 2523 2524
		dirColors = zlights.directional.colors,
		dirPositions = zlights.directional.positions,

		pointColors = zlights.point.colors,
		pointPositions = zlights.point.positions,
		pointDistances = zlights.point.distances,
M
Mr.doob 已提交
2525
		pointDecays = zlights.point.decays,
M
Mr.doob 已提交
2526 2527 2528 2529 2530 2531 2532

		spotColors = zlights.spot.colors,
		spotPositions = zlights.spot.positions,
		spotDistances = zlights.spot.distances,
		spotDirections = zlights.spot.directions,
		spotAnglesCos = zlights.spot.anglesCos,
		spotExponents = zlights.spot.exponents,
M
Mr.doob 已提交
2533
		spotDecays = zlights.spot.decays,
M
Mr.doob 已提交
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565

		hemiSkyColors = zlights.hemi.skyColors,
		hemiGroundColors = zlights.hemi.groundColors,
		hemiPositions = zlights.hemi.positions,

		dirLength = 0,
		pointLength = 0,
		spotLength = 0,
		hemiLength = 0,

		dirCount = 0,
		pointCount = 0,
		spotCount = 0,
		hemiCount = 0,

		dirOffset = 0,
		pointOffset = 0,
		spotOffset = 0,
		hemiOffset = 0;

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

			light = lights[ l ];

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

			if ( light instanceof THREE.AmbientLight ) {

				if ( ! light.visible ) continue;

2566 2567 2568
				r += color.r;
				g += color.g;
				b += color.b;
M
Mr.doob 已提交
2569 2570 2571 2572 2573 2574 2575

			} else if ( light instanceof THREE.DirectionalLight ) {

				dirCount += 1;

				if ( ! light.visible ) continue;

2576 2577
				_direction.setFromMatrixPosition( light.matrixWorld );
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2578
				_direction.sub( _vector3 );
T
tschw 已提交
2579
				_direction.transformDirection( viewMatrix );
M
Mr.doob 已提交
2580 2581 2582

				dirOffset = dirLength * 3;

M
Mr.doob 已提交
2583
				dirPositions[ dirOffset + 0 ] = _direction.x;
M
Mr.doob 已提交
2584 2585 2586
				dirPositions[ dirOffset + 1 ] = _direction.y;
				dirPositions[ dirOffset + 2 ] = _direction.z;

2587
				setColorLinear( dirColors, dirOffset, color, intensity );
M
Mr.doob 已提交
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598

				dirLength += 1;

			} else if ( light instanceof THREE.PointLight ) {

				pointCount += 1;

				if ( ! light.visible ) continue;

				pointOffset = pointLength * 3;

2599
				setColorLinear( pointColors, pointOffset, color, intensity );
M
Mr.doob 已提交
2600

2601
				_vector3.setFromMatrixPosition( light.matrixWorld );
T
tschw 已提交
2602
				_vector3.applyMatrix4( viewMatrix );
M
Mr.doob 已提交
2603

M
Mr.doob 已提交
2604
				pointPositions[ pointOffset + 0 ] = _vector3.x;
M
Mr.doob 已提交
2605 2606 2607
				pointPositions[ pointOffset + 1 ] = _vector3.y;
				pointPositions[ pointOffset + 2 ] = _vector3.z;

M
Mr.doob 已提交
2608
				// distance is 0 if decay is 0, because there is no attenuation at all.
M
Mr.doob 已提交
2609
				pointDistances[ pointLength ] = distance;
M
Mr.doob 已提交
2610
				pointDecays[ pointLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621

				pointLength += 1;

			} else if ( light instanceof THREE.SpotLight ) {

				spotCount += 1;

				if ( ! light.visible ) continue;

				spotOffset = spotLength * 3;

2622
				setColorLinear( spotColors, spotOffset, color, intensity );
M
Mr.doob 已提交
2623

G
gero3 已提交
2624
				_direction.setFromMatrixPosition( light.matrixWorld );
T
tschw 已提交
2625
				_vector3.copy( _direction ).applyMatrix4( viewMatrix );
M
Mr.doob 已提交
2626

T
tschw 已提交
2627 2628 2629
				spotPositions[ spotOffset + 0 ] = _vector3.x;
				spotPositions[ spotOffset + 1 ] = _vector3.y;
				spotPositions[ spotOffset + 2 ] = _vector3.z;
M
Mr.doob 已提交
2630 2631 2632

				spotDistances[ spotLength ] = distance;

2633
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2634
				_direction.sub( _vector3 );
T
tschw 已提交
2635
				_direction.transformDirection( viewMatrix );
M
Mr.doob 已提交
2636

M
Mr.doob 已提交
2637
				spotDirections[ spotOffset + 0 ] = _direction.x;
M
Mr.doob 已提交
2638 2639 2640 2641 2642
				spotDirections[ spotOffset + 1 ] = _direction.y;
				spotDirections[ spotOffset + 2 ] = _direction.z;

				spotAnglesCos[ spotLength ] = Math.cos( light.angle );
				spotExponents[ spotLength ] = light.exponent;
M
Mr.doob 已提交
2643
				spotDecays[ spotLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652

				spotLength += 1;

			} else if ( light instanceof THREE.HemisphereLight ) {

				hemiCount += 1;

				if ( ! light.visible ) continue;

2653
				_direction.setFromMatrixPosition( light.matrixWorld );
T
tschw 已提交
2654
				_direction.transformDirection( viewMatrix );
M
Mr.doob 已提交
2655 2656 2657

				hemiOffset = hemiLength * 3;

M
Mr.doob 已提交
2658
				hemiPositions[ hemiOffset + 0 ] = _direction.x;
M
Mr.doob 已提交
2659 2660 2661 2662 2663 2664
				hemiPositions[ hemiOffset + 1 ] = _direction.y;
				hemiPositions[ hemiOffset + 2 ] = _direction.z;

				skyColor = light.color;
				groundColor = light.groundColor;

2665 2666
				setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
				setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
M
Mr.doob 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691

				hemiLength += 1;

			}

		}

		// null eventual remains from removed lights
		// (this is to avoid if in shader)

		for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
		for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
		for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
		for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
		for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;

		zlights.directional.length = dirLength;
		zlights.point.length = pointLength;
		zlights.spot.length = spotLength;
		zlights.hemi.length = hemiLength;

		zlights.ambient[ 0 ] = r;
		zlights.ambient[ 1 ] = g;
		zlights.ambient[ 2 ] = b;

M
Mr.doob 已提交
2692
	}
M
Mr.doob 已提交
2693 2694 2695 2696 2697 2698 2699

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

		if ( cullFace === THREE.CullFaceNone ) {

2700
			state.disable( _gl.CULL_FACE );
M
Mr.doob 已提交
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727

		} else {

			if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {

				_gl.frontFace( _gl.CW );

			} else {

				_gl.frontFace( _gl.CCW );

			}

			if ( cullFace === THREE.CullFaceBack ) {

				_gl.cullFace( _gl.BACK );

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

				_gl.cullFace( _gl.FRONT );

			} else {

				_gl.cullFace( _gl.FRONT_AND_BACK );

			}

2728
			state.enable( _gl.CULL_FACE );
M
Mr.doob 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737

		}

	};

	// Textures

	function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {

2738 2739
		var extension;

M
Mr.doob 已提交
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
		if ( isImagePowerOfTwo ) {

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

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

		} else {

			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
			_gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
M
Mr.doob 已提交
2752 2753 2754

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

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

2757
			}
M
Mr.doob 已提交
2758 2759 2760 2761

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

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

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

2766
			}
M
Mr.doob 已提交
2767

M
Mr.doob 已提交
2768 2769
		}

2770 2771
		extension = extensions.get( 'EXT_texture_filter_anisotropic' );

2772
		if ( extension ) {
M
Mr.doob 已提交
2773

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

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

2779
				_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
2780
				properties.get( texture ).__currentAnisotropy = texture.anisotropy;
M
Mr.doob 已提交
2781 2782 2783 2784 2785

			}

		}

M
Mr.doob 已提交
2786
	}
M
Mr.doob 已提交
2787

2788
	function uploadTexture( textureProperties, texture, slot ) {
2789

2790
		if ( textureProperties.__webglInit === undefined ) {
2791

2792
			textureProperties.__webglInit = true;
2793 2794 2795

			texture.addEventListener( 'dispose', onTextureDispose );

2796
			textureProperties.__webglTexture = _gl.createTexture();
2797

2798
			_infoMemory.textures ++;
2799 2800

		}
M
Mr.doob 已提交
2801

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

H
Henri Astre 已提交
2805 2806 2807 2808
		_gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
		_gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
		_gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );

G
gero3 已提交
2809
		texture.image = clampToMaxSize( texture.image, capabilities.maxTextureSize );
2810

M
Mr.doob 已提交
2811 2812 2813 2814 2815 2816
		if ( textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false ) {

			texture.image = makePowerOfTwo( texture.image );

		}

H
Henri Astre 已提交
2817
		var image = texture.image,
M
Mr.doob 已提交
2818
		isImagePowerOfTwo = isPowerOfTwo( image ),
H
Henri Astre 已提交
2819 2820
		glFormat = paramThreeToGL( texture.format ),
		glType = paramThreeToGL( texture.type );
M
Mr.doob 已提交
2821

H
Henri Astre 已提交
2822
		setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
M
Mr.doob 已提交
2823

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

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

H
Henri Astre 已提交
2828 2829 2830
			// use manually created mipmaps if available
			// if there are no manual mipmaps
			// set 0 level mipmap and then use GL to generate other mipmap levels
M
Mr.doob 已提交
2831

H
Henri Astre 已提交
2832 2833 2834
			if ( mipmaps.length > 0 && isImagePowerOfTwo ) {

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

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

H
Henri Astre 已提交
2839
				}
M
Mr.doob 已提交
2840

H
Henri Astre 已提交
2841
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2842

H
Henri Astre 已提交
2843
			} else {
M
Mr.doob 已提交
2844

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

H
Henri Astre 已提交
2847
			}
M
Mr.doob 已提交
2848

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

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

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

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

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

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

2861
					} else {
M
Mr.doob 已提交
2862

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

2865
					}
M
Mr.doob 已提交
2866

H
Henri Astre 已提交
2867
				} else {
M
Mr.doob 已提交
2868

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

M
Mr.doob 已提交
2871 2872
				}

H
Henri Astre 已提交
2873 2874
			}

G
gero3 已提交
2875 2876 2877
		} else {

			// regular Texture (image, video, canvas)
H
Henri Astre 已提交
2878 2879 2880 2881 2882 2883

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

			if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
M
Mr.doob 已提交
2884

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

					mipmap = mipmaps[ i ];
2888
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
M
Mr.doob 已提交
2889 2890 2891

				}

H
Henri Astre 已提交
2892
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2893

H
Henri Astre 已提交
2894
			} else {
M
Mr.doob 已提交
2895

2896
				state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
M
Mr.doob 已提交
2897

H
Henri Astre 已提交
2898
			}
M
Mr.doob 已提交
2899

H
Henri Astre 已提交
2900
		}
M
Mr.doob 已提交
2901

H
Henri Astre 已提交
2902
		if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
M
Mr.doob 已提交
2903

2904
		textureProperties.__version = texture.version;
M
Mr.doob 已提交
2905

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

2908
	}
M
Mr.doob 已提交
2909

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

2912 2913 2914
		var textureProperties = properties.get( texture );

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

2916
			var image = texture.image;
M
Mr.doob 已提交
2917

2918 2919
			if ( image === undefined ) {

2920
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined', texture );
2921 2922 2923 2924
				return;

			}

2925
			if ( image.complete === false ) {
M
Mr.doob 已提交
2926

2927
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
2928 2929 2930 2931
				return;

			}

2932
			uploadTexture( textureProperties, texture, slot );
2933

2934
			return;
M
Mr.doob 已提交
2935 2936 2937

		}

B
Ben Adams 已提交
2938
		state.activeTexture( _gl.TEXTURE0 + slot );
2939
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
2940

M
Mr.doob 已提交
2941 2942 2943 2944
	};

	function clampToMaxSize ( image, maxSize ) {

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

2947 2948
			// Warning: Scaling through the canvas will only work with images that use
			// premultiplied alpha.
M
Mr.doob 已提交
2949

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

2952 2953 2954
			var canvas = document.createElement( 'canvas' );
			canvas.width = Math.floor( image.width * scale );
			canvas.height = Math.floor( image.height * scale );
M
Mr.doob 已提交
2955

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

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

2961 2962 2963
			return canvas;

		}
M
Mr.doob 已提交
2964

2965
		return image;
M
Mr.doob 已提交
2966 2967 2968

	}

M
Mr.doob 已提交
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
	function isPowerOfTwo( image ) {

		return THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height );

	}

	function textureNeedsPowerOfTwo( texture ) {

		if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) return true;
		if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) return true;

		return false;

	}

	function makePowerOfTwo( image ) {

		if ( image instanceof HTMLImageElement || image instanceof HTMLCanvasElement ) {

			var canvas = document.createElement( 'canvas' );
			canvas.width = THREE.Math.nearestPowerOfTwo( image.width );
			canvas.height = THREE.Math.nearestPowerOfTwo( image.height );

			var context = canvas.getContext( '2d' );
			context.drawImage( image, 0, 0, canvas.width, canvas.height );

			console.warn( 'THREE.WebGLRenderer: image is not power of two (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );

			return canvas;

		}

		return image;

	}

M
Mr.doob 已提交
3005 3006
	function setCubeTexture ( texture, slot ) {

3007
		var textureProperties = properties.get( texture );
3008

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

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

3013
				if ( ! textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
3014

3015 3016
					texture.addEventListener( 'dispose', onTextureDispose );

3017
					textureProperties.__image__webglTextureCube = _gl.createTexture();
M
Mr.doob 已提交
3018

3019
					_infoMemory.textures ++;
M
Mr.doob 已提交
3020 3021 3022

				}

B
Ben Adams 已提交
3023
				state.activeTexture( _gl.TEXTURE0 + slot );
3024
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3025 3026 3027

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

M
Mr.doob 已提交
3028 3029
				var isCompressed = texture instanceof THREE.CompressedTexture;
				var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
M
Mr.doob 已提交
3030 3031 3032 3033 3034

				var cubeImage = [];

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

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

G
gero3 已提交
3037
						cubeImage[ i ] = clampToMaxSize( texture.image[ i ], capabilities.maxCubemapSize );
M
Mr.doob 已提交
3038 3039 3040

					} else {

3041
						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
M
Mr.doob 已提交
3042 3043 3044 3045 3046 3047

					}

				}

				var image = cubeImage[ 0 ],
M
Mr.doob 已提交
3048
				isImagePowerOfTwo = isPowerOfTwo( image ),
M
Mr.doob 已提交
3049 3050 3051 3052 3053 3054 3055
				glFormat = paramThreeToGL( texture.format ),
				glType = paramThreeToGL( texture.type );

				setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );

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

3056
					if ( ! isCompressed ) {
M
Mr.doob 已提交
3057

M
Mr.doob 已提交
3058
						if ( isDataTexture ) {
3059

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

M
Mr.doob 已提交
3062
						} else {
3063

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

M
Mr.doob 已提交
3066 3067
						}

3068
					} else {
3069

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

3072
						for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
M
Mr.doob 已提交
3073 3074

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

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

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

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

3082
								} else {
M
Mr.doob 已提交
3083

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

3086
								}
M
Mr.doob 已提交
3087

3088
							} else {
M
Mr.doob 已提交
3089

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

3092
							}
M
Mr.doob 已提交
3093

3094
						}
M
Mr.doob 已提交
3095

M
Mr.doob 已提交
3096
					}
M
Mr.doob 已提交
3097

M
Mr.doob 已提交
3098 3099 3100 3101 3102 3103 3104 3105
				}

				if ( texture.generateMipmaps && isImagePowerOfTwo ) {

					_gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );

				}

3106
				textureProperties.__version = texture.version;
M
Mr.doob 已提交
3107

B
Ben Adams 已提交
3108
				if ( texture.onUpdate ) texture.onUpdate( texture );
M
Mr.doob 已提交
3109 3110 3111

			} else {

B
Ben Adams 已提交
3112
				state.activeTexture( _gl.TEXTURE0 + slot );
3113
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
3114 3115 3116 3117 3118

			}

		}

M
Mr.doob 已提交
3119
	}
M
Mr.doob 已提交
3120 3121 3122

	function setCubeTextureDynamic ( texture, slot ) {

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

M
Mr.doob 已提交
3126
	}
M
Mr.doob 已提交
3127 3128 3129 3130 3131 3132

	// Render targets

	function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {

		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
M
Mr.doob 已提交
3133
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
M
Mr.doob 已提交
3134

M
Mr.doob 已提交
3135
	}
M
Mr.doob 已提交
3136

M
Mr.doob 已提交
3137
	function setupRenderBuffer ( renderbuffer, renderTarget ) {
M
Mr.doob 已提交
3138 3139 3140 3141 3142 3143 3144 3145 3146

		_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );

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

			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

		/* For some reason this is not working. Defaulting to RGBA4.
3147
		} else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
M
Mr.doob 已提交
3148 3149 3150 3151

			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
		*/
G
gero3 已提交
3152

M
Mr.doob 已提交
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
		} else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {

			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

		} else {

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

		}

M
Mr.doob 已提交
3164
	}
M
Mr.doob 已提交
3165 3166 3167 3168 3169

	this.setRenderTarget = function ( renderTarget ) {

		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );

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

3172
			var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
3173
			var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
3174 3175 3176 3177 3178 3179

			if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
			if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;

			renderTarget.addEventListener( 'dispose', onRenderTargetDispose );

M
Mr.doob 已提交
3180
			textureProperties.__webglTexture = _gl.createTexture();
M
Mr.doob 已提交
3181

3182
			_infoMemory.textures ++;
M
Mr.doob 已提交
3183 3184 3185

			// Setup texture, create render and frame buffers

M
Mr.doob 已提交
3186 3187 3188
			var isTargetPowerOfTwo = isPowerOfTwo( renderTarget ),
				glFormat = paramThreeToGL( renderTarget.texture.format ),
				glType = paramThreeToGL( renderTarget.texture.type );
M
Mr.doob 已提交
3189 3190 3191

			if ( isCube ) {

3192 3193
				renderTargetProperties.__webglFramebuffer = [];
				renderTargetProperties.__webglRenderbuffer = [];
M
Mr.doob 已提交
3194

M
Mr.doob 已提交
3195
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
B
Ben Adams 已提交
3196

M
Mr.doob 已提交
3197
				setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, isTargetPowerOfTwo );
M
Mr.doob 已提交
3198 3199 3200

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

3201 3202
					renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
					renderTargetProperties.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
3203
					state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
M
Mr.doob 已提交
3204

3205 3206
					setupFrameBuffer( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
					setupRenderBuffer( renderTargetProperties.__webglRenderbuffer[ i ], renderTarget );
M
Mr.doob 已提交
3207 3208 3209

				}

M
Mr.doob 已提交
3210
				if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
M
Mr.doob 已提交
3211 3212 3213

			} else {

3214
				renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
M
Mr.doob 已提交
3215 3216 3217

				if ( renderTarget.shareDepthFrom ) {

3218
					renderTargetProperties.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
M
Mr.doob 已提交
3219 3220 3221

				} else {

3222
					renderTargetProperties.__webglRenderbuffer = _gl.createRenderbuffer();
M
Mr.doob 已提交
3223 3224 3225

				}

M
Mr.doob 已提交
3226 3227
				state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
				setTextureParameters( _gl.TEXTURE_2D, renderTarget.texture, isTargetPowerOfTwo );
M
Mr.doob 已提交
3228

3229
				state.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
M
Mr.doob 已提交
3230

3231
				setupFrameBuffer( renderTargetProperties.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
M
Mr.doob 已提交
3232 3233 3234 3235 3236

				if ( renderTarget.shareDepthFrom ) {

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

3237
						_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTargetProperties.__webglRenderbuffer );
M
Mr.doob 已提交
3238 3239 3240

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

3241
						_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTargetProperties.__webglRenderbuffer );
M
Mr.doob 已提交
3242 3243 3244 3245 3246

					}

				} else {

3247
					setupRenderBuffer( renderTargetProperties.__webglRenderbuffer, renderTarget );
M
Mr.doob 已提交
3248 3249 3250

				}

M
Mr.doob 已提交
3251
				if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
M
Mr.doob 已提交
3252 3253 3254 3255 3256 3257 3258

			}

			// Release everything

			if ( isCube ) {

B
Ben Adams 已提交
3259
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
M
Mr.doob 已提交
3260 3261 3262

			} else {

B
Ben Adams 已提交
3263
				state.bindTexture( _gl.TEXTURE_2D, null );
M
Mr.doob 已提交
3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275

			}

			_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
			_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );

		}

		var framebuffer, width, height, vx, vy;

		if ( renderTarget ) {

3276
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
3277

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

3280
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
3281 3282 3283

			} else {

3284
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314

			}

			width = renderTarget.width;
			height = renderTarget.height;

			vx = 0;
			vy = 0;

		} else {

			framebuffer = null;

			width = _viewportWidth;
			height = _viewportHeight;

			vx = _viewportX;
			vy = _viewportY;

		}

		if ( framebuffer !== _currentFramebuffer ) {

			_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
			_gl.viewport( vx, vy, width, height );

			_currentFramebuffer = framebuffer;

		}

M
Mr.doob 已提交
3315 3316 3317 3318 3319 3320 3321
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, 0 );

		}

M
Mr.doob 已提交
3322 3323 3324 3325 3326
		_currentWidth = width;
		_currentHeight = height;

	};

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

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

3331
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
3332
			return;
3333

G
gero3 已提交
3334
		}
3335

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

M
Mr.doob 已提交
3338
		if ( framebuffer ) {
3339

G
gero3 已提交
3340
			var restore = false;
3341

M
Mr.doob 已提交
3342
			if ( framebuffer !== _currentFramebuffer ) {
3343

M
Mr.doob 已提交
3344
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3345

G
gero3 已提交
3346
				restore = true;
3347

G
gero3 已提交
3348
			}
3349

M
Mr.doob 已提交
3350
			try {
3351

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

M
Mr.doob 已提交
3354 3355
				if ( texture.format !== THREE.RGBAFormat
					&& paramThreeToGL( texture.format ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
3356

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

M
Mr.doob 已提交
3360
				}
3361

M
Mr.doob 已提交
3362 3363 3364 3365
				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' ) ) ) {
3366

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

M
Mr.doob 已提交
3370
				}
3371

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

M
Mr.doob 已提交
3374
					_gl.readPixels( x, y, width, height, paramThreeToGL( texture.format ), paramThreeToGL( texture.type ), buffer );
3375

M
Mr.doob 已提交
3376
				} else {
M
Mr.doob 已提交
3377

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

				}
M
Mr.doob 已提交
3381

M
Mr.doob 已提交
3382
			} finally {
M
Mr.doob 已提交
3383

M
Mr.doob 已提交
3384 3385 3386
				if ( restore ) {

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

M
Mr.doob 已提交
3388 3389 3390
				}

			}
M
Mr.doob 已提交
3391 3392 3393

		}

M
Mr.doob 已提交
3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404
	};

	function updateRenderTargetMipmap( renderTarget ) {

		var target = renderTarget instanceof THREE.WebGLRenderTargetCube ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
		var texture = properties.get( renderTarget.texture ).__webglTexture;

		state.bindTexture( target, texture );
		_gl.generateMipmap( target );
		state.bindTexture( target, null );

M
Mr.doob 已提交
3405
	}
M
Mr.doob 已提交
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418

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

	function filterFallback ( f ) {

		if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {

			return _gl.NEAREST;

		}

		return _gl.LINEAR;

M
Mr.doob 已提交
3419
	}
M
Mr.doob 已提交
3420 3421 3422 3423 3424

	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

3425 3426
		var extension;

M
Mr.doob 已提交
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
		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;

3451 3452 3453 3454 3455 3456 3457 3458
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

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

		}

M
Mr.doob 已提交
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
		if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
		if ( p === THREE.RGBFormat ) return _gl.RGB;
		if ( p === THREE.RGBAFormat ) return _gl.RGBA;
		if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
		if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;

		if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
		if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
		if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;

		if ( p === THREE.ZeroFactor ) return _gl.ZERO;
		if ( p === THREE.OneFactor ) return _gl.ONE;
		if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
		if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
		if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
		if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
		if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
		if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;

		if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
		if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
		if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;

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

3484 3485 3486 3487 3488 3489
		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 已提交
3490 3491 3492

		}

3493 3494 3495
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

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

3497 3498 3499 3500
			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 已提交
3501 3502 3503

		}

3504 3505 3506
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
3507

3508 3509
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
3510 3511 3512

		}

M
Mr.doob 已提交
3513 3514
		return 0;

M
Mr.doob 已提交
3515
	}
M
Mr.doob 已提交
3516

M
Mr.doob 已提交
3517
	// DEPRECATED
3518

3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560
	this.supportsFloatTextures = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsFloatTextures() is now .extensions.get( \'OES_texture_float\' ).' );
		return extensions.get( 'OES_texture_float' );

	};

	this.supportsHalfFloatTextures = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsHalfFloatTextures() is now .extensions.get( \'OES_texture_half_float\' ).' );
		return extensions.get( 'OES_texture_half_float' );

	};

	this.supportsStandardDerivatives = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsStandardDerivatives() is now .extensions.get( \'OES_standard_derivatives\' ).' );
		return extensions.get( 'OES_standard_derivatives' );

	};

	this.supportsCompressedTextureS3TC = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsCompressedTextureS3TC() is now .extensions.get( \'WEBGL_compressed_texture_s3tc\' ).' );
		return extensions.get( 'WEBGL_compressed_texture_s3tc' );

	};

	this.supportsCompressedTexturePVRTC = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsCompressedTexturePVRTC() is now .extensions.get( \'WEBGL_compressed_texture_pvrtc\' ).' );
		return extensions.get( 'WEBGL_compressed_texture_pvrtc' );

	};

	this.supportsBlendMinMax = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsBlendMinMax() is now .extensions.get( \'EXT_blend_minmax\' ).' );
		return extensions.get( 'EXT_blend_minmax' );

	};

3561 3562
	this.supportsVertexTextures = function () {

G
gero3 已提交
3563
		return capabilities.vertexTextures;
3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575

	};

	this.supportsInstancedArrays = function () {

		console.warn( 'THREE.WebGLRenderer: .supportsInstancedArrays() is now .extensions.get( \'ANGLE_instanced_arrays\' ).' );
		return extensions.get( 'ANGLE_instanced_arrays' );

	};

	//

M
Mr.doob 已提交
3576 3577
	this.initMaterial = function () {

3578
		console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
M
Mr.doob 已提交
3579 3580

	};
M
Mr.doob 已提交
3581

M
Mr.doob 已提交
3582
	this.addPrePlugin = function () {
M
Mr.doob 已提交
3583

3584
		console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
M
Mr.doob 已提交
3585 3586 3587 3588 3589

	};

	this.addPostPlugin = function () {

3590
		console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
M
Mr.doob 已提交
3591 3592

	};
M
Mr.doob 已提交
3593

M
Mr.doob 已提交
3594 3595
	this.updateShadowMap = function () {

3596
		console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
M
Mr.doob 已提交
3597 3598 3599

	};

3600 3601 3602
	Object.defineProperties( this, {
		shadowMapEnabled: {
			get: function () {
G
gero3 已提交
3603

M
Mr.doob 已提交
3604
				return shadowMap.enabled;
G
gero3 已提交
3605

3606 3607
			},
			set: function ( value ) {
G
gero3 已提交
3608

3609
				console.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' );
M
Mr.doob 已提交
3610
				shadowMap.enabled = value;
G
gero3 已提交
3611

3612 3613 3614 3615
			}
		},
		shadowMapType: {
			get: function () {
G
gero3 已提交
3616

M
Mr.doob 已提交
3617
				return shadowMap.type;
G
gero3 已提交
3618

3619 3620
			},
			set: function ( value ) {
G
gero3 已提交
3621

3622
				console.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' );
M
Mr.doob 已提交
3623
				shadowMap.type = value;
G
gero3 已提交
3624

3625 3626 3627 3628
			}
		},
		shadowMapCullFace: {
			get: function () {
G
gero3 已提交
3629

M
Mr.doob 已提交
3630
				return shadowMap.cullFace;
G
gero3 已提交
3631

3632 3633
			},
			set: function ( value ) {
G
gero3 已提交
3634

3635
				console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace is now .shadowMap.cullFace.' );
M
Mr.doob 已提交
3636
				shadowMap.cullFace = value;
G
gero3 已提交
3637

3638 3639 3640 3641
			}
		},
		shadowMapDebug: {
			get: function () {
G
gero3 已提交
3642

M
Mr.doob 已提交
3643
				return shadowMap.debug;
G
gero3 已提交
3644

3645 3646
			},
			set: function ( value ) {
G
gero3 已提交
3647

3648
				console.warn( 'THREE.WebGLRenderer: .shadowMapDebug is now .shadowMap.debug.' );
M
Mr.doob 已提交
3649
				shadowMap.debug = value;
G
gero3 已提交
3650

3651 3652 3653 3654
			}
		}
	} );

M
Mr.doob 已提交
3655
};