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

THREE.WebGLRenderer = function ( parameters ) {

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

	parameters = parameters || {};

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

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

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

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

32 33
	var morphInfluences = new Float32Array( 8 );

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

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

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

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

	// clearing

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

	// scene graph

	this.sortObjects = true;

T
tschw 已提交
55 56 57 58 59
	// user-defined clipping

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

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

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

66 67
	// physical lights

68
	this.physicallyCorrectLights = false;
69

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

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

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

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

	// flags

	this.autoScaleCubemaps = true;

	// internal properties

	var _this = this,

	// internal state cache

	_currentProgram = null,
92
	_currentRenderTarget = null,
M
Mr.doob 已提交
93
	_currentFramebuffer = null,
94
	_currentMaterialId = - 1,
95
	_currentGeometryProgram = '',
M
Mr.doob 已提交
96 97
	_currentCamera = null,

98 99 100 101 102
	_currentScissor = new THREE.Vector4(),
	_currentScissorTest = null,

	_currentViewport = new THREE.Vector4(),

103 104
	//

M
Mr.doob 已提交
105 106
	_usedTextureUnits = 0,

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

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

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

	_pixelRatio = 1,

	_scissor = new THREE.Vector4( 0, 0, _width, _height ),
118 119
	_scissorTest = false,

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

M
Mr.doob 已提交
122 123 124 125
	// frustum

	_frustum = new THREE.Frustum(),

T
tschw 已提交
126 127
	// clipping

T
tschw 已提交
128
	_clipping = new THREE.WebGLClipping(),
T
tschw 已提交
129 130 131 132 133
	_clippingEnabled = false,
	_localClippingEnabled = false,

	_sphere = new THREE.Sphere(),

M
Mr.doob 已提交
134
	// camera matrices cache
M
Mr.doob 已提交
135 136 137 138 139 140 141 142 143

	_projScreenMatrix = new THREE.Matrix4(),

	_vector3 = new THREE.Vector3(),

	// light arrays cache

	_lights = {

144 145
		hash: '',

M
Mr.doob 已提交
146
		ambient: [ 0, 0, 0 ],
M
Mr.doob 已提交
147
		directional: [],
148 149
		directionalShadowMap: [],
		directionalShadowMatrix: [],
M
Mr.doob 已提交
150
		spot: [],
151 152
		spotShadowMap: [],
		spotShadowMatrix: [],
M
Mr.doob 已提交
153
		point: [],
154 155
		pointShadowMap: [],
		pointShadowMatrix: [],
156 157
		hemi: [],

158
		shadows: []
M
Mr.doob 已提交
159

160 161
	},

M
Mr.doob 已提交
162 163
	// info

164
	_infoMemory = {
165 166

		geometries: 0,
T
tschw 已提交
167
		textures: 0
168 169 170

	},

171
	_infoRender = {
172 173 174 175 176 177

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

M
Mr.doob 已提交
178 179
	};

M
Mr.doob 已提交
180
	this.info = {
181

M
Mr.doob 已提交
182 183
		render: _infoRender,
		memory: _infoMemory,
184
		programs: null
M
Mr.doob 已提交
185 186

	};
187

188

M
Mr.doob 已提交
189 190 191 192
	// initialize

	var _gl;

M
Mr.doob 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	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 已提交
208
			if ( _canvas.getContext( 'webgl' ) !== null ) {
209 210 211 212 213 214 215 216

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

			} else {

				throw 'Error creating WebGL context.';

			}
M
Mr.doob 已提交
217 218 219

		}

220 221 222 223 224 225 226 227 228 229 230 231
		// Some experimental-webgl implementations do not have getShaderPrecisionFormat

		if ( _gl.getShaderPrecisionFormat === undefined ) {

			_gl.getShaderPrecisionFormat = function () {

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

			};

		}

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

M
Mr.doob 已提交
234 235
	} catch ( error ) {

236
		console.error( 'THREE.WebGLRenderer: ' + error );
M
Mr.doob 已提交
237 238 239

	}

240
	var _isWebGL2 = (typeof WebGL2RenderingContext !== 'undefined' && _gl instanceof WebGL2RenderingContext);
241 242
	var extensions = new THREE.WebGLExtensions( _gl );

243
	extensions.get( 'WEBGL_depth_texture' );
244 245
	extensions.get( 'OES_texture_float' );
	extensions.get( 'OES_texture_float_linear' );
246 247
	extensions.get( 'OES_texture_half_float' );
	extensions.get( 'OES_texture_half_float_linear' );
248
	extensions.get( 'OES_standard_derivatives' );
B
Ben Adams 已提交
249
	extensions.get( 'ANGLE_instanced_arrays' );
250

251 252
	if ( extensions.get( 'OES_element_index_uint' ) ) {

253
		THREE.BufferGeometry.MaxIndex = 4294967296;
254 255 256

	}

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

259 260 261
	var state = new THREE.WebGLState( _gl, extensions, paramThreeToGL );
	var properties = new THREE.WebGLProperties();
	var objects = new THREE.WebGLObjects( _gl, properties, this.info );
G
gero3 已提交
262
	var programCache = new THREE.WebGLPrograms( this, capabilities );
M
Mr.doob 已提交
263
	var lightCache = new THREE.WebGLLights();
264

265 266
	this.info.programs = programCache.programs;

267 268
	var bufferRenderer = new THREE.WebGLBufferRenderer( _gl, extensions, _infoRender );
	var indexedBufferRenderer = new THREE.WebGLIndexedBufferRenderer( _gl, extensions, _infoRender );
269

M
Mr.doob 已提交
270 271
	//

272 273
	function getTargetPixelRatio() {

274
		return _currentRenderTarget === null ? _pixelRatio : 1;
275 276 277

	}

278
	function glClearColor( r, g, b, a ) {
279 280 281

		if ( _premultipliedAlpha === true ) {

282
			r *= a; g *= a; b *= a;
283 284 285

		}

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

288
	}
289

290
	function setDefaultGLState() {
M
Mr.doob 已提交
291

M
Mr.doob 已提交
292
		state.init();
M
Mr.doob 已提交
293

294 295
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
296

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

299
	}
300

301
	function resetGLState() {
302 303 304 305

		_currentProgram = null;
		_currentCamera = null;

306
		_currentGeometryProgram = '';
307 308
		_currentMaterialId = - 1;

M
Mr.doob 已提交
309 310
		state.reset();

311
	}
M
Mr.doob 已提交
312 313 314 315

	setDefaultGLState();

	this.context = _gl;
M
Mr.doob 已提交
316
	this.capabilities = capabilities;
317
	this.extensions = extensions;
318
	this.properties = properties;
M
Mr.doob 已提交
319
	this.state = state;
M
Mr.doob 已提交
320

M
Mr.doob 已提交
321 322
	// shadow map

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

325
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
326

M
Mr.doob 已提交
327

M
Mr.doob 已提交
328 329 330 331 332
	// Plugins

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

M
Mr.doob 已提交
333 334 335 336 337 338 339 340
	// API

	this.getContext = function () {

		return _gl;

	};

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

		return _gl.getContextAttributes();

	};

347 348 349 350 351 352
	this.forceContextLoss = function () {

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

	};

353
	this.getMaxAnisotropy = ( function () {
M
Mr.doob 已提交
354

355
		var value;
M
Mr.doob 已提交
356

357
		return function getMaxAnisotropy() {
358

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

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

M
Mr.doob 已提交
363
			if ( extension !== null ) {
364

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

M
Mr.doob 已提交
367 368 369 370 371
			} else {

				value = 0;

			}
372 373 374

			return value;

M
Mr.doob 已提交
375
		};
376 377

	} )();
M
Mr.doob 已提交
378 379 380

	this.getPrecision = function () {

G
gero3 已提交
381
		return capabilities.precision;
M
Mr.doob 已提交
382 383 384

	};

385 386
	this.getPixelRatio = function () {

387
		return _pixelRatio;
388 389 390 391 392

	};

	this.setPixelRatio = function ( value ) {

393 394 395 396 397
		if ( value === undefined ) return;

		_pixelRatio = value;

		this.setSize( _viewport.z, _viewport.w, false );
398 399 400

	};

401 402 403
	this.getSize = function () {

		return {
404 405
			width: _width,
			height: _height
406 407 408 409
		};

	};

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

412 413 414
		_width = width;
		_height = height;

415 416
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
417

418
		if ( updateStyle !== false ) {
419

G
gero3 已提交
420 421
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
422

G
gero3 已提交
423
		}
M
Mr.doob 已提交
424

425
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
426 427 428 429 430

	};

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

431
		state.viewport( _viewport.set( x, y, width, height ) );
M
Mr.doob 已提交
432 433 434

	};

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

437
		state.scissor( _scissor.set( x, y, width, height ) );
M
Mr.doob 已提交
438 439 440

	};

441 442
	this.setScissorTest = function ( boolean ) {

443
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
444 445 446 447 448

	};

	// Clearing

M
Mr.doob 已提交
449
	this.getClearColor = function () {
M
Mr.doob 已提交
450

M
Mr.doob 已提交
451
		return _clearColor;
M
Mr.doob 已提交
452 453 454

	};

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

M
Mr.doob 已提交
457
		_clearColor.set( color );
458

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

461
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
462 463 464

	};

M
Mr.doob 已提交
465
	this.getClearAlpha = function () {
M
Mr.doob 已提交
466

M
Mr.doob 已提交
467
		return _clearAlpha;
M
Mr.doob 已提交
468 469 470

	};

M
Mr.doob 已提交
471
	this.setClearAlpha = function ( alpha ) {
M
Mr.doob 已提交
472

M
Mr.doob 已提交
473
		_clearAlpha = alpha;
M
Mr.doob 已提交
474

475
		glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
M
Mr.doob 已提交
476 477 478 479 480 481 482 483 484 485 486 487

	};

	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 );
488 489 490 491 492

	};

	this.clearColor = function () {

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

	};

	this.clearDepth = function () {

M
Mr.doob 已提交
499
		this.clear( false, true, false );
500 501 502 503 504

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
505
		this.clear( false, false, true );
M
Mr.doob 已提交
506 507 508 509 510 511 512 513 514 515

	};

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

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

	};

M
Mr.doob 已提交
516 517
	// Reset

518
	this.resetGLState = resetGLState;
M
Mr.doob 已提交
519

D
dubejf 已提交
520 521
	this.dispose = function() {

522 523 524 525 526
		transparentObjects = [];
		transparentObjectsLastIndex = -1;
		opaqueObjects = [];
		opaqueObjectsLastIndex = -1;

D
dubejf 已提交
527 528 529 530
		_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );

	};

M
Mr.doob 已提交
531
	// Events
M
Mr.doob 已提交
532

D
dubejf 已提交
533 534 535 536 537 538 539 540 541
	function onContextLost( event ) {

		event.preventDefault();

		resetGLState();
		setDefaultGLState();

		properties.clear();

M
Mr.doob 已提交
542
	}
D
dubejf 已提交
543

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

		var texture = event.target;

		texture.removeEventListener( 'dispose', onTextureDispose );

		deallocateTexture( texture );

552
		_infoMemory.textures --;
M
Mr.doob 已提交
553 554


555
	}
M
Mr.doob 已提交
556

557
	function onRenderTargetDispose( event ) {
M
Mr.doob 已提交
558 559 560 561 562 563 564

		var renderTarget = event.target;

		renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );

		deallocateRenderTarget( renderTarget );

565
		_infoMemory.textures --;
M
Mr.doob 已提交
566

567
	}
M
Mr.doob 已提交
568

569
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
570 571 572 573 574 575 576

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

577
	}
M
Mr.doob 已提交
578 579 580

	// Buffer deallocation

581
	function deallocateTexture( texture ) {
M
Mr.doob 已提交
582

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

585
		if ( texture.image && textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
586 587 588

			// cube texture

589
			_gl.deleteTexture( textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
590

591 592 593 594
		} else {

			// 2D texture

595
			if ( textureProperties.__webglInit === undefined ) return;
596

597
			_gl.deleteTexture( textureProperties.__webglTexture );
598

M
Mr.doob 已提交
599 600
		}

601
		// remove all webgl properties
602
		properties.delete( texture );
603

604
	}
M
Mr.doob 已提交
605

606
	function deallocateRenderTarget( renderTarget ) {
M
Mr.doob 已提交
607

608
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
609
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
610

611
		if ( ! renderTarget ) return;
M
Mr.doob 已提交
612

613 614 615 616 617 618 619 620 621 622 623
		if ( textureProperties.__webglTexture !== undefined ) {

			_gl.deleteTexture( textureProperties.__webglTexture );

		}

		if ( renderTarget.depthTexture ) {

			renderTarget.depthTexture.dispose();

		}
M
Mr.doob 已提交
624

M
Mr.doob 已提交
625 626 627 628
		if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {

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

629
				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
630
				if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );
M
Mr.doob 已提交
631 632 633 634 635

			}

		} else {

636
			_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
637
			if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );
M
Mr.doob 已提交
638 639 640

		}

M
Mr.doob 已提交
641
		properties.delete( renderTarget.texture );
D
Daosheng Mu 已提交
642
		properties.delete( renderTarget );
M
Mr.doob 已提交
643

644
	}
M
Mr.doob 已提交
645

646
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
647

648 649 650 651
		releaseMaterialProgramReference( material );

		properties.delete( material );

652
	}
653 654


655
	function releaseMaterialProgramReference( material ) {
656

657
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
658 659 660

		material.program = undefined;

661
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
662

663
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
664

M
Mr.doob 已提交
665 666
		}

667
	}
M
Mr.doob 已提交
668 669 670 671 672

	// Buffer rendering

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

673
		state.initAttributes();
674

675
		var buffers = properties.get( object );
676

677 678 679 680
		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 已提交
681

682
		var attributes = program.getAttributes();
683

M
Mr.doob 已提交
684 685
		if ( object.hasPositions ) {

686
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
687
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
688

689 690
			state.enableAttribute( attributes.position );
			_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
691 692 693 694 695

		}

		if ( object.hasNormals ) {

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

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

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

702
					var array = object.normalArray;
M
Mr.doob 已提交
703

704 705 706
					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 已提交
707

708 709 710
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
711

712 713 714
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
715

716 717 718
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
719 720 721 722 723 724

				}

			}

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

726
			state.enableAttribute( attributes.normal );
727

728
			_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
729 730 731 732 733

		}

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

734
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
735
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
736

737
			state.enableAttribute( attributes.uv );
738

739
			_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
740 741 742 743 744

		}

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

745
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
746
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
747

748
			state.enableAttribute( attributes.color );
749

750
			_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
751 752 753

		}

754
		state.disableUnusedAttributes();
755

M
Mr.doob 已提交
756 757 758 759 760 761
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

762
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
763

M
Mr.doob 已提交
764 765
		setMaterial( material );

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

M
Mr.doob 已提交
768 769
		var updateBuffers = false;
		var geometryProgram = geometry.id + '_' + program.id + '_' + material.wireframe;
M
Mr.doob 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

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

			}

793
			activeInfluences.sort( absNumericalSort );
M
Mr.doob 已提交
794 795 796 797 798 799 800

			if ( activeInfluences.length > 8 ) {

				activeInfluences.length = 8;

			}

801 802
			var morphAttributes = geometry.morphAttributes;

M
Mr.doob 已提交
803 804 805 806 807 808 809
			for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {

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

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

810
					var index = influence[ 1 ];
M
Mr.doob 已提交
811

812 813
					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 已提交
814 815 816

				} else {

817 818
					if ( material.morphTargets === true ) geometry.removeAttribute( 'morphTarget' + i );
					if ( material.morphNormals === true ) geometry.removeAttribute( 'morphNormal' + i );
M
Mr.doob 已提交
819 820 821 822 823

				}

			}

T
tschw 已提交
824 825
			program.getUniforms().setValue(
					_gl, 'morphTargetInfluences', morphInfluences );
M
Mr.doob 已提交
826 827 828 829 830

			updateBuffers = true;

		}

831 832
		//

833
		var index = geometry.index;
834 835
		var position = geometry.attributes.position;

836 837
		if ( material.wireframe === true ) {

838
			index = objects.getWireframeAttribute( geometry );
839 840 841

		}

842 843
		var renderer;

844
		if ( index !== null ) {
845

846 847
			renderer = indexedBufferRenderer;
			renderer.setIndex( index );
848

849
		} else {
850

851
			renderer = bufferRenderer;
852

853
		}
M
Mr.doob 已提交
854

855
		if ( updateBuffers ) {
M
Mr.doob 已提交
856

857
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
858

859
			if ( index !== null ) {
860

861
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
862 863 864

			}

865
		}
866

867 868
		//

M
Mr.doob 已提交
869 870
		var dataStart = 0;
		var dataCount = Infinity;
871

M
Mr.doob 已提交
872
		if ( index !== null ) {
873

M
Mr.doob 已提交
874
			dataCount = index.count;
875

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

M
Mr.doob 已提交
878
			dataCount = position.count;
879

M
Mr.doob 已提交
880
		}
881

M
Mr.doob 已提交
882 883
		var rangeStart = geometry.drawRange.start;
		var rangeCount = geometry.drawRange.count;
884

M
Mr.doob 已提交
885 886
		var groupStart = group !== null ? group.start : 0;
		var groupCount = group !== null ? group.count : Infinity;
887

M
Mr.doob 已提交
888 889 890 891 892 893
		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 );

		//
894

895
		if ( object instanceof THREE.Mesh ) {
896

897
			if ( material.wireframe === true ) {
898

899
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
900
				renderer.setMode( _gl.LINES );
901

902
			} else {
M
Mr.doob 已提交
903 904

				switch ( object.drawMode ) {
905

B
Ben Adams 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918
					case THREE.TrianglesDrawMode:
						renderer.setMode( _gl.TRIANGLES );
						break;

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

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

				}
919

920
			}
921

922

923
		} else if ( object instanceof THREE.Line ) {
924

925
			var lineWidth = material.linewidth;
926

927
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
928

929
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
930

931
			if ( object instanceof THREE.LineSegments ) {
932

933
				renderer.setMode( _gl.LINES );
934

935
			} else {
936

937
				renderer.setMode( _gl.LINE_STRIP );
938 939

			}
M
Mr.doob 已提交
940

941
		} else if ( object instanceof THREE.Points ) {
942 943

			renderer.setMode( _gl.POINTS );
944 945

		}
946

J
jfranc 已提交
947
		if ( geometry instanceof THREE.InstancedBufferGeometry ) {
M
Mr.doob 已提交
948 949 950

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
953
			}
954 955 956

		} else {

M
Mr.doob 已提交
957
			renderer.render( drawStart, drawCount );
958

M
Mr.doob 已提交
959 960 961 962
		}

	};

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

M
Mr.doob 已提交
965
		var extension;
B
Ben Adams 已提交
966

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

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

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

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

M
Mr.doob 已提交
976 977 978
			}

		}
B
Ben Adams 已提交
979

980 981
		if ( startIndex === undefined ) startIndex = 0;

982 983
		state.initAttributes();

984
		var geometryAttributes = geometry.attributes;
985

986
		var programAttributes = program.getAttributes();
987

988
		var materialDefaultAttributeValues = material.defaultAttributeValues;
989

990
		for ( var name in programAttributes ) {
991

992
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
993

M
Mr.doob 已提交
994
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
995

996
				var geometryAttribute = geometryAttributes[ name ];
997

M
Mr.doob 已提交
998
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
999

1000
					var type = _gl.FLOAT;
1001
					var array = geometryAttribute.array;
1002 1003
					var normalized = geometryAttribute.normalized;

1004 1005
					if ( array instanceof Float32Array ) {

1006
						type = _gl.FLOAT;
1007 1008 1009 1010 1011 1012 1013

					} else if ( array instanceof Float64Array ) {

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

					} else if ( array instanceof Uint16Array ) {

1014 1015
						type = _gl.UNSIGNED_SHORT;

1016 1017
					} else if ( array instanceof Int16Array ) {

1018
						type = _gl.SHORT;
1019 1020 1021

					} else if ( array instanceof Uint32Array ) {

1022
						type = _gl.UNSIGNED_INT;
1023 1024 1025

					} else if ( array instanceof Int32Array ) {

1026
						type = _gl.INT;
1027 1028 1029

					} else if ( array instanceof Int8Array ) {

1030
						type = _gl.BYTE;
1031 1032 1033

					} else if ( array instanceof Uint8Array ) {

1034
						type = _gl.UNSIGNED_BYTE;
1035 1036

					}
1037

1038
					var size = geometryAttribute.itemSize;
G
gero3 已提交
1039
					var buffer = objects.getAttributeBuffer( geometryAttribute );
1040

B
Ben Adams 已提交
1041
					if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
1042

M
Mr.doob 已提交
1043 1044 1045 1046 1047
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
1051
							if ( geometry.maxInstancedCount === undefined ) {
1052

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

M
Mr.doob 已提交
1055
							}
B
Ben Adams 已提交
1056

M
Mr.doob 已提交
1057
						} else {
B
Ben Adams 已提交
1058

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

M
Mr.doob 已提交
1061
						}
B
Ben Adams 已提交
1062

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

M
Mr.doob 已提交
1066
					} else {
B
Ben Adams 已提交
1067

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

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

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

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

M
Mr.doob 已提交
1076
							}
B
Ben Adams 已提交
1077

M
Mr.doob 已提交
1078 1079 1080 1081
						} else {

							state.enableAttribute( programAttribute );

M
Mr.doob 已提交
1082
						}
B
Ben Adams 已提交
1083

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

B
Ben Adams 已提交
1087
					}
M
Mr.doob 已提交
1088

1089 1090
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
1091
					var value = materialDefaultAttributeValues[ name ];
1092

1093
					if ( value !== undefined ) {
M
Mr.doob 已提交
1094

1095
						switch ( value.length ) {
M
Mr.doob 已提交
1096

1097 1098 1099
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1100

1101 1102 1103
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
1104

1105 1106 1107
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
1108

1109 1110
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
1111 1112

						}
M
Mr.doob 已提交
1113 1114 1115 1116 1117 1118 1119 1120

					}

				}

			}

		}
1121

1122
		state.disableUnusedAttributes();
1123

M
Mr.doob 已提交
1124 1125
	}

M
Mr.doob 已提交
1126 1127
	// Sorting

1128
	function absNumericalSort( a, b ) {
1129

1130
		return Math.abs( b[ 0 ] ) - Math.abs( a[ 0 ] );
1131 1132 1133

	}

M
Mr.doob 已提交
1134 1135
	function painterSortStable ( a, b ) {

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

U
unconed 已提交
1138
			return a.object.renderOrder - b.object.renderOrder;
1139

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

M
Mr.doob 已提交
1142
			return a.material.id - b.material.id;
1143 1144

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

M
Mr.doob 已提交
1146
			return a.z - b.z;
M
Mr.doob 已提交
1147 1148 1149

		} else {

1150
			return a.id - b.id;
M
Mr.doob 已提交
1151 1152 1153

		}

1154
	}
M
Mr.doob 已提交
1155

1156 1157
	function reversePainterSortStable ( a, b ) {

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

U
unconed 已提交
1160
			return a.object.renderOrder - b.object.renderOrder;
1161 1162

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

M
Mr.doob 已提交
1164
			return b.z - a.z;
1165 1166 1167 1168 1169 1170 1171

		} else {

			return a.id - b.id;

		}

1172
	}
1173

M
Mr.doob 已提交
1174 1175 1176 1177 1178 1179
	// Rendering

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

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

1180
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1181 1182 1183 1184
			return;

		}

M
Mr.doob 已提交
1185
		var fog = scene.fog;
M
Mr.doob 已提交
1186 1187 1188

		// reset caching for this frame

1189
		_currentGeometryProgram = '';
1190
		_currentMaterialId = - 1;
1191
		_currentCamera = null;
M
Mr.doob 已提交
1192 1193 1194

		// update scene graph

1195
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1196 1197 1198

		// update camera matrices and frustum

1199
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1200 1201 1202 1203 1204 1205

		camera.matrixWorldInverse.getInverse( camera.matrixWorld );

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

M
Mr.doob 已提交
1206
		lights.length = 0;
1207

M
Mr.doob 已提交
1208 1209
		opaqueObjectsLastIndex = - 1;
		transparentObjectsLastIndex = - 1;
1210

M
Mr.doob 已提交
1211 1212 1213
		sprites.length = 0;
		lensFlares.length = 0;

T
tschw 已提交
1214 1215 1216
		_localClippingEnabled = this.localClippingEnabled;
		_clippingEnabled = _clipping.init(
				this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1217

1218
		projectObject( scene, camera );
M
Mr.doob 已提交
1219

T
tschw 已提交
1220

1221 1222 1223
		opaqueObjects.length = opaqueObjectsLastIndex + 1;
		transparentObjects.length = transparentObjectsLastIndex + 1;

M
Mr.doob 已提交
1224
		if ( _this.sortObjects === true ) {
1225 1226 1227

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

1229 1230
		}

M
Mr.doob 已提交
1231
		//
M
Mr.doob 已提交
1232

T
tschw 已提交
1233
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1234

1235 1236
		setupShadows( lights );

M
Mr.doob 已提交
1237
		shadowMap.render( scene, camera );
M
Mr.doob 已提交
1238

1239 1240
		setupLights( lights, camera );

T
tschw 已提交
1241
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1242

M
Mr.doob 已提交
1243 1244
		//

1245 1246 1247 1248
		_infoRender.calls = 0;
		_infoRender.vertices = 0;
		_infoRender.faces = 0;
		_infoRender.points = 0;
M
Mr.doob 已提交
1249

1250 1251 1252 1253 1254 1255
		if ( renderTarget === undefined ) {

			renderTarget = null;

		}

M
Mr.doob 已提交
1256 1257 1258 1259 1260 1261 1262 1263
		this.setRenderTarget( renderTarget );

		if ( this.autoClear || forceClear ) {

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

		}

1264
		//
M
Mr.doob 已提交
1265 1266 1267

		if ( scene.overrideMaterial ) {

1268
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1269

1270 1271
			renderObjects( opaqueObjects, camera, fog, overrideMaterial );
			renderObjects( transparentObjects, camera, fog, overrideMaterial );
1272

M
Mr.doob 已提交
1273 1274 1275 1276
		} else {

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

M
Mr.doob 已提交
1277
			state.setBlending( THREE.NoBlending );
1278
			renderObjects( opaqueObjects, camera, fog );
M
Mr.doob 已提交
1279 1280 1281

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

1282
			renderObjects( transparentObjects, camera, fog );
M
Mr.doob 已提交
1283 1284 1285 1286 1287

		}

		// custom render plugins (post pass)

M
Mr.doob 已提交
1288
		spritePlugin.render( scene, camera );
1289
		lensFlarePlugin.render( scene, camera, _currentViewport );
M
Mr.doob 已提交
1290 1291 1292

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

M
Mr.doob 已提交
1293 1294 1295
		if ( renderTarget ) {

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

M
Mr.doob 已提交
1297 1298 1299 1300 1301
			if ( texture.generateMipmaps && isPowerOfTwo( renderTarget ) &&
					texture.minFilter !== THREE.NearestFilter &&
					texture.minFilter !== THREE.LinearFilter ) {

				updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1302 1303

			}
M
Mr.doob 已提交
1304 1305 1306

		}

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

M
Mr.doob 已提交
1309 1310
		state.setDepthTest( true );
		state.setDepthWrite( true );
1311
		state.setColorWrite( true );
M
Mr.doob 已提交
1312 1313 1314 1315

		// _gl.finish();

	};
M
Mr.doob 已提交
1316

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

1319
		var array, index;
M
Mr.doob 已提交
1320

1321
		// allocate the next position in the appropriate array
M
Mr.doob 已提交
1322 1323 1324

		if ( material.transparent ) {

1325 1326
			array = transparentObjects;
			index = ++ transparentObjectsLastIndex;
M
Mr.doob 已提交
1327 1328 1329

		} else {

1330 1331 1332 1333 1334
			array = opaqueObjects;
			index = ++ opaqueObjectsLastIndex;

		}

1335 1336
		// recycle existing render item or grow the array

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		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 已提交
1347 1348 1349

		} else {

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
			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 已提交
1361 1362 1363 1364 1365

		}

	}

M
Mr.doob 已提交
1366 1367
	// TODO Duplicated code (Frustum)

T
tschw 已提交
1368 1369 1370 1371 1372 1373 1374
	function isObjectViewable( object ) {

		var geometry = object.geometry;

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

M
Mr.doob 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
		_sphere.copy( geometry.boundingSphere ).
			applyMatrix4( object.matrixWorld );

		return isSphereViewable( _sphere );

	}

	function isSpriteViewable( sprite ) {

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

		return isSphereViewable( _sphere );

	}

	function isSphereViewable( sphere ) {
T
tschw 已提交
1393 1394

		if ( ! _frustum.intersectsSphere( sphere ) ) return false;
T
tschw 已提交
1395 1396 1397 1398

		var numPlanes = _clipping.numPlanes;

		if ( numPlanes === 0 ) return true;
T
tschw 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

		var planes = _this.clippingPlanes,

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

		do {

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

T
tschw 已提交
1411
		} while ( ++ i !== numPlanes );
T
tschw 已提交
1412 1413 1414 1415 1416

		return true;

	}

1417
	function projectObject( object, camera ) {
M
Mr.doob 已提交
1418

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

1421
		if ( object.layers.test( camera.layers ) ) {
M
Mr.doob 已提交
1422

1423
			if ( object instanceof THREE.Light ) {
M
Mr.doob 已提交
1424

1425
				lights.push( object );
M
Mr.doob 已提交
1426

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

M
Mr.doob 已提交
1429
				if ( object.frustumCulled === false || isSpriteViewable( object ) === true ) {
1430 1431 1432 1433

					sprites.push( object );

				}
M
Mr.doob 已提交
1434

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

1437
				lensFlares.push( object );
M
Mr.doob 已提交
1438

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

1441
				if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1442

1443 1444
					_vector3.setFromMatrixPosition( object.matrixWorld );
					_vector3.applyProjection( _projScreenMatrix );
M
Mr.doob 已提交
1445

1446
				}
1447

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

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

1452
				if ( object instanceof THREE.SkinnedMesh ) {
M
Mr.doob 已提交
1453

1454
					object.skeleton.update();
1455

1456
				}
1457

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

1460
					var material = object.material;
1461

1462
					if ( material.visible === true ) {
M
Mr.doob 已提交
1463

1464
						if ( _this.sortObjects === true ) {
M
Mr.doob 已提交
1465

1466 1467 1468 1469
							_vector3.setFromMatrixPosition( object.matrixWorld );
							_vector3.applyProjection( _projScreenMatrix );

						}
M
Mr.doob 已提交
1470

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

S
SUNAG 已提交
1473
						if ( material instanceof THREE.MultiMaterial ) {
1474

1475 1476
							var groups = geometry.groups;
							var materials = material.materials;
1477

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

1480 1481
								var group = groups[ i ];
								var groupMaterial = materials[ group.materialIndex ];
1482

1483
								if ( groupMaterial.visible === true ) {
1484

1485 1486 1487
									pushRenderItem( object, geometry, groupMaterial, _vector3.z, group );

								}
M
Mr.doob 已提交
1488

M
Mr.doob 已提交
1489
							}
M
Mr.doob 已提交
1490

1491
						} else {
M
Mr.doob 已提交
1492

1493
							pushRenderItem( object, geometry, material, _vector3.z, null );
1494

1495
						}
O
OpenShift guest 已提交
1496

1497
					}
M
Mr.doob 已提交
1498

1499
				}
M
Mr.doob 已提交
1500

1501
			}
M
Mr.doob 已提交
1502

M
Mr.doob 已提交
1503
		}
M
Mr.doob 已提交
1504

M
Mr.doob 已提交
1505
		var children = object.children;
M
Mr.doob 已提交
1506

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

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

1511
		}
1512

1513
	}
M
Mr.doob 已提交
1514

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

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

1519
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1520

1521
			var object = renderItem.object;
M
Mr.doob 已提交
1522 1523 1524
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1525

1526 1527
			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
M
Mr.doob 已提交
1528

M
Mr.doob 已提交
1529
			if ( object instanceof THREE.ImmediateRenderObject ) {
M
Mr.doob 已提交
1530

M
Mr.doob 已提交
1531
				setMaterial( material );
M
Mr.doob 已提交
1532

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

M
Mr.doob 已提交
1535
				_currentGeometryProgram = '';
M
Mr.doob 已提交
1536

M
Mr.doob 已提交
1537
				object.render( function ( object ) {
M
Mr.doob 已提交
1538

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

M
Mr.doob 已提交
1541
				} );
1542

M
Mr.doob 已提交
1543
			} else {
M
Mr.doob 已提交
1544

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

M
Mr.doob 已提交
1547
			}
M
Mr.doob 已提交
1548

1549
		}
M
Mr.doob 已提交
1550

1551
	}
G
gero3 已提交
1552

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

1555
		var materialProperties = properties.get( material );
G
gero3 已提交
1556

T
tschw 已提交
1557
		var parameters = programCache.getParameters(
T
tschw 已提交
1558
				material, _lights, fog, _clipping.numPlanes, object );
T
tschw 已提交
1559

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

1562
		var program = materialProperties.program;
T
tschw 已提交
1563
		var programChange = true;
1564

1565
		if ( program === undefined ) {
B
Ben Adams 已提交
1566

M
Mr.doob 已提交
1567 1568
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1569

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

M
Mr.doob 已提交
1572
			// changed glsl or parameters
1573
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1574

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

T
tschw 已提交
1577
			// same glsl and uniform list
T
tschw 已提交
1578 1579
			return;

T
tschw 已提交
1580
		} else {
B
Ben Adams 已提交
1581

T
tschw 已提交
1582 1583
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1584 1585 1586

		}

1587
		if ( programChange ) {
B
Ben Adams 已提交
1588

1589
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1590

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

1593 1594 1595 1596 1597 1598
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
					vertexShader: shader.vertexShader,
					fragmentShader: shader.fragmentShader
				};
B
Ben Adams 已提交
1599

1600
			} else {
B
Ben Adams 已提交
1601

1602 1603 1604 1605 1606 1607
				materialProperties.__webglShader = {
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
					fragmentShader: material.fragmentShader
				};
G
gero3 已提交
1608

1609
			}
G
gero3 已提交
1610

1611
			material.__webglShader = materialProperties.__webglShader;
G
gero3 已提交
1612

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

1615 1616
			materialProperties.program = program;
			material.program = program;
1617 1618 1619

		}

1620
		var attributes = program.getAttributes();
M
Mr.doob 已提交
1621 1622 1623 1624 1625

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

M
Mr.doob 已提交
1628
				if ( attributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

M
Mr.doob 已提交
1644
				if ( attributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653

					material.numSupportedMorphNormals ++;

				}

			}

		}

T
tschw 已提交
1654 1655 1656 1657 1658 1659
		var uniforms = materialProperties.__webglShader.uniforms;

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

T
tschw 已提交
1660 1661
			materialProperties.numClippingPlanes = _clipping.numPlanes;
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1662 1663 1664

		}

M
Mr.doob 已提交
1665
		if ( material.lights ) {
1666

1667 1668
			// store the light setup it was created for

1669 1670
			materialProperties.lightsHash = _lights.hash;

1671 1672 1673 1674 1675
			// wire up the material to this renderer's lighting state

			uniforms.ambientLightColor.value = _lights.ambient;
			uniforms.directionalLights.value = _lights.directional;
			uniforms.spotLights.value = _lights.spot;
M
Mr.doob 已提交
1676
			uniforms.pointLights.value = _lights.point;
1677 1678
			uniforms.hemisphereLights.value = _lights.hemi;

1679 1680 1681 1682 1683 1684
			uniforms.directionalShadowMap.value = _lights.directionalShadowMap;
			uniforms.directionalShadowMatrix.value = _lights.directionalShadowMatrix;
			uniforms.spotShadowMap.value = _lights.spotShadowMap;
			uniforms.spotShadowMatrix.value = _lights.spotShadowMatrix;
			uniforms.pointShadowMap.value = _lights.pointShadowMap;
			uniforms.pointShadowMatrix.value = _lights.pointShadowMatrix;
1685

1686 1687
		}

T
tschw 已提交
1688 1689 1690
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
					THREE.WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1691

T
tschw 已提交
1692 1693 1694
		materialProperties.uniformsList = uniformsList;
		materialProperties.dynamicUniforms =
				THREE.WebGLUniforms.splitDynamic( uniformsList, uniforms );
A
arose 已提交
1695

M
Mr.doob 已提交
1696
	}
M
Mr.doob 已提交
1697

1698 1699
	function setMaterial( material ) {

T
tschw 已提交
1700 1701 1702 1703 1704 1705
		if ( material.side !== THREE.DoubleSide )
			state.enable( _gl.CULL_FACE );
		else
			state.disable( _gl.CULL_FACE );

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

1707 1708
		if ( material.transparent === true ) {

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

1711 1712 1713 1714
		} else {

			state.setBlending( THREE.NoBlending );

1715 1716
		}

B
Ben Adams 已提交
1717
		state.setDepthFunc( material.depthFunc );
M
Mr.doob 已提交
1718 1719
		state.setDepthTest( material.depthTest );
		state.setDepthWrite( material.depthWrite );
1720
		state.setColorWrite( material.colorWrite );
M
Mr.doob 已提交
1721
		state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
1722 1723 1724

	}

1725
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1726 1727 1728

		_usedTextureUnits = 0;

1729
		var materialProperties = properties.get( material );
1730

T
tschw 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
		if ( _clippingEnabled ) {

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

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

				// we might want to call this function with some ClippingGroup
				// object instead of the material, once it becomes feasible
				// (#8465, #8379)
T
tschw 已提交
1742
				_clipping.setState(
T
tschw 已提交
1743 1744 1745 1746 1747 1748
						material.clippingPlanes, material.clipShadows,
						camera, materialProperties, useCache );

			}

			if ( materialProperties.numClippingPlanes !== undefined &&
T
tschw 已提交
1749
				materialProperties.numClippingPlanes !== _clipping.numPlanes ) {
T
tschw 已提交
1750 1751 1752 1753 1754 1755 1756

				material.needsUpdate = true;

			}

		}

1757 1758 1759 1760 1761 1762
		if ( materialProperties.program === undefined ) {

			material.needsUpdate = true;

		}

1763 1764
		if ( materialProperties.lightsHash !== undefined &&
			materialProperties.lightsHash !== _lights.hash ) {
1765 1766 1767 1768 1769 1770

			material.needsUpdate = true;

		}

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

1772
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1773 1774 1775 1776
			material.needsUpdate = false;

		}

1777
		var refreshProgram = false;
M
Mr.doob 已提交
1778
		var refreshMaterial = false;
1779
		var refreshLights = false;
M
Mr.doob 已提交
1780

1781
		var program = materialProperties.program,
1782
			p_uniforms = program.getUniforms(),
1783
			m_uniforms = materialProperties.__webglShader.uniforms;
M
Mr.doob 已提交
1784

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

1787 1788
			_gl.useProgram( program.program );
			_currentProgram = program.id;
M
Mr.doob 已提交
1789

1790
			refreshProgram = true;
M
Mr.doob 已提交
1791
			refreshMaterial = true;
1792
			refreshLights = true;
M
Mr.doob 已提交
1793 1794 1795 1796 1797 1798

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1799

M
Mr.doob 已提交
1800 1801 1802 1803
			refreshMaterial = true;

		}

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

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

G
gero3 已提交
1808
			if ( capabilities.logarithmicDepthBuffer ) {
1809

T
tschw 已提交
1810 1811
				p_uniforms.setValue( _gl, 'logDepthBufFC',
						2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1812 1813 1814 1815

			}


1816 1817 1818 1819 1820 1821 1822 1823 1824
			if ( camera !== _currentCamera ) {

				_currentCamera = camera;

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

				refreshMaterial = true;		// set to true on material change
T
tschw 已提交
1825
				refreshLights = true;		// remains set until update done
1826 1827

			}
M
Mr.doob 已提交
1828

1829 1830 1831 1832 1833
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

			if ( material instanceof THREE.ShaderMaterial ||
				 material instanceof THREE.MeshPhongMaterial ||
1834
				 material instanceof THREE.MeshStandardMaterial ||
1835 1836
				 material.envMap ) {

T
tschw 已提交
1837 1838 1839
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1840

T
tschw 已提交
1841 1842
					uCamPos.setValue( _gl,
							_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1843 1844 1845 1846 1847 1848 1849

				}

			}

			if ( material instanceof THREE.MeshPhongMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
1850
				 material instanceof THREE.MeshBasicMaterial ||
1851
				 material instanceof THREE.MeshStandardMaterial ||
1852 1853 1854
				 material instanceof THREE.ShaderMaterial ||
				 material.skinning ) {

T
tschw 已提交
1855
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1856 1857 1858

			}

T
tschw 已提交
1859 1860
			p_uniforms.set( _gl, _this, 'toneMappingExposure' );
			p_uniforms.set( _gl, _this, 'toneMappingWhitePoint' );
M
Mr.doob 已提交
1861

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

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

		if ( material.skinning ) {

T
tschw 已提交
1870 1871
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1872

T
tschw 已提交
1873
			var skeleton = object.skeleton;
1874

T
tschw 已提交
1875
			if ( skeleton ) {
1876

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

T
tschw 已提交
1879 1880 1881
					p_uniforms.set( _gl, skeleton, 'boneTexture' );
					p_uniforms.set( _gl, skeleton, 'boneTextureWidth' );
					p_uniforms.set( _gl, skeleton, 'boneTextureHeight' );
M
Mr.doob 已提交
1882

T
tschw 已提交
1883
				} else {
M
Mr.doob 已提交
1884

T
tschw 已提交
1885
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894

				}

			}

		}

		if ( refreshMaterial ) {

M
Mr.doob 已提交
1895
			if ( material.lights ) {
M
Mr.doob 已提交
1896

1897
				// the current material requires lighting info
M
Mr.doob 已提交
1898

T
tschw 已提交
1899 1900 1901 1902 1903 1904
				// note: all lighting uniforms are always set correctly
				// they simply reference the renderer's state for their
				// values
				//
				// use the current material's .needsUpdate flags to set
				// the GL state when required
M
Mr.doob 已提交
1905

T
tschw 已提交
1906
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1907

T
tschw 已提交
1908
			}
G
gero3 已提交
1909

T
tschw 已提交
1910
			// refresh uniforms common to several materials
G
gero3 已提交
1911

T
tschw 已提交
1912
			if ( fog && material.fog ) {
G
gero3 已提交
1913

T
tschw 已提交
1914
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1915 1916 1917 1918 1919

			}

			if ( material instanceof THREE.MeshBasicMaterial ||
				 material instanceof THREE.MeshLambertMaterial ||
W
WestLangley 已提交
1920
				 material instanceof THREE.MeshPhongMaterial ||
W
WestLangley 已提交
1921 1922
				 material instanceof THREE.MeshStandardMaterial ||
				 material instanceof THREE.MeshDepthMaterial ) {
M
Mr.doob 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938

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

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

M
Mr.doob 已提交
1941
				refreshUniformsPoints( m_uniforms, material );
M
Mr.doob 已提交
1942

1943 1944 1945 1946
			} else if ( material instanceof THREE.MeshLambertMaterial ) {

				refreshUniformsLambert( m_uniforms, material );

M
Mr.doob 已提交
1947 1948 1949 1950
			} else if ( material instanceof THREE.MeshPhongMaterial ) {

				refreshUniformsPhong( m_uniforms, material );

W
WestLangley 已提交
1951 1952 1953 1954
			} else if ( material instanceof THREE.MeshPhysicalMaterial ) {

				refreshUniformsPhysical( m_uniforms, material );

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

1957
				refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1958

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

1961 1962 1963 1964 1965 1966 1967 1968
				if ( material.displacementMap ) {

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

				}

M
Mr.doob 已提交
1969 1970 1971 1972 1973 1974
			} else if ( material instanceof THREE.MeshNormalMaterial ) {

				m_uniforms.opacity.value = material.opacity;

			}

T
tschw 已提交
1975 1976
			THREE.WebGLUniforms.upload(
					_gl, materialProperties.uniformsList, m_uniforms, _this );
A
arose 已提交
1977 1978 1979

		}

M
Mr.doob 已提交
1980

T
tschw 已提交
1981
		// common matrices
M
Mr.doob 已提交
1982

T
tschw 已提交
1983 1984 1985
		p_uniforms.set( _gl, object, 'modelViewMatrix' );
		p_uniforms.set( _gl, object, 'normalMatrix' );
		p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
A
arose 已提交
1986 1987


T
tschw 已提交
1988
		// dynamic uniforms
A
arose 已提交
1989

T
tschw 已提交
1990
		var dynUniforms = materialProperties.dynamicUniforms;
A
arose 已提交
1991

T
tschw 已提交
1992
		if ( dynUniforms !== null ) {
A
arose 已提交
1993

T
tschw 已提交
1994 1995
			THREE.WebGLUniforms.evalDynamic(
					dynUniforms, m_uniforms, object, camera );
A
arose 已提交
1996

T
tschw 已提交
1997
			THREE.WebGLUniforms.upload( _gl, dynUniforms, m_uniforms, _this );
A
arose 已提交
1998 1999 2000

		}

T
tschw 已提交
2001
		return program;
A
arose 已提交
2002 2003 2004

	}

M
Mr.doob 已提交
2005 2006 2007 2008 2009 2010
	// Uniforms (refresh uniforms objects)

	function refreshUniformsCommon ( uniforms, material ) {

		uniforms.opacity.value = material.opacity;

2011
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2012

2013
		if ( material.emissive ) {
M
Mr.doob 已提交
2014

2015
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
2016 2017 2018

		}

2019 2020 2021
		uniforms.map.value = material.map;
		uniforms.specularMap.value = material.specularMap;
		uniforms.alphaMap.value = material.alphaMap;
M
Mr.doob 已提交
2022

2023
		if ( material.aoMap ) {
2024

2025 2026
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
2027 2028 2029

		}

M
Mr.doob 已提交
2030
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
2031 2032 2033 2034 2035
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
2036
		// 6. emissive map
M
Mr.doob 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

2048 2049 2050 2051
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
2052 2053 2054 2055 2056 2057 2058 2059
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

2060 2061 2062 2063 2064 2065 2066 2067
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

2068 2069 2070 2071
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

2072 2073 2074 2075
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
2076 2077 2078 2079
		}

		if ( uvScaleMap !== undefined ) {

2080
			// backwards compatibility
M
Mr.doob 已提交
2081 2082 2083 2084 2085 2086
			if ( uvScaleMap instanceof THREE.WebGLRenderTarget ) {

				uvScaleMap = uvScaleMap.texture;

			}

M
Mr.doob 已提交
2087 2088 2089 2090 2091 2092 2093 2094
			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;
2095 2096 2097 2098 2099 2100

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

2102
		uniforms.reflectivity.value = material.reflectivity;
M
Mr.doob 已提交
2103 2104
		uniforms.refractionRatio.value = material.refractionRatio;

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

	function refreshUniformsLine ( uniforms, material ) {

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

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

	function refreshUniformsDash ( uniforms, material ) {

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

M
Mr.doob 已提交
2120
	}
M
Mr.doob 已提交
2121

M
Mr.doob 已提交
2122
	function refreshUniformsPoints ( uniforms, material ) {
M
Mr.doob 已提交
2123

2124
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2125
		uniforms.opacity.value = material.opacity;
2126
		uniforms.size.value = material.size * _pixelRatio;
T
tschw 已提交
2127
		uniforms.scale.value = _canvas.clientHeight * 0.5;
M
Mr.doob 已提交
2128 2129 2130

		uniforms.map.value = material.map;

2131 2132 2133 2134 2135 2136 2137 2138 2139
		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 已提交
2140
	}
M
Mr.doob 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156

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

2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	function refreshUniformsLambert ( uniforms, material ) {

		if ( material.lightMap ) {

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

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2176 2177
	function refreshUniformsPhong ( uniforms, material ) {

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

2181 2182 2183 2184
		if ( material.lightMap ) {

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

2186
		}
2187

2188
		if ( material.emissiveMap ) {
2189

2190
			uniforms.emissiveMap.value = material.emissiveMap;
2191

2192
		}
M
Mr.doob 已提交
2193

2194 2195 2196 2197
		if ( material.bumpMap ) {

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

2199
		}
M
Mr.doob 已提交
2200

2201 2202 2203 2204 2205 2206
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2207

2208 2209 2210 2211 2212
		if ( material.displacementMap ) {

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

2214
		}
2215 2216 2217

	}

2218
	function refreshUniformsStandard ( uniforms, material ) {
W
WestLangley 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278

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

		if ( material.roughnessMap ) {

			uniforms.roughnessMap.value = material.roughnessMap;

		}

		if ( material.metalnessMap ) {

			uniforms.metalnessMap.value = material.metalnessMap;

		}

		if ( material.lightMap ) {

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

		}

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

		if ( material.bumpMap ) {

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

		}

		if ( material.normalMap ) {

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

		}

		if ( material.displacementMap ) {

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

		}

		if ( material.envMap ) {

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

		}

	}

W
WestLangley 已提交
2279 2280 2281 2282 2283 2284
	function refreshUniformsPhysical ( uniforms, material ) {

		refreshUniformsStandard( uniforms, material );

	}

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

M
Mr.doob 已提交
2287
	function markUniformsLightsNeedsUpdate ( uniforms, value ) {
2288

M
Mr.doob 已提交
2289
		uniforms.ambientLightColor.needsUpdate = value;
2290

B
Ben Houston 已提交
2291 2292 2293 2294
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
		uniforms.hemisphereLights.needsUpdate = value;
2295

M
Mr.doob 已提交
2296
	}
2297

T
tschw 已提交
2298
	// Lighting
M
Mr.doob 已提交
2299

2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
	function setupShadows ( lights ) {

		var lightShadowsLength = 0;

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

			var light = lights[ i ];

			if ( light.castShadow ) {

				_lights.shadows[ lightShadowsLength ++ ] = light;

			}

		}

		_lights.shadows.length = lightShadowsLength;

	}

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

B
brason 已提交
2322
		var l, ll, light,
M
Mr.doob 已提交
2323
		r = 0, g = 0, b = 0,
B
Ben Houston 已提交
2324
		color,
B
brason 已提交
2325
		intensity,
M
Mr.doob 已提交
2326
		distance,
2327
		shadowMap,
M
Mr.doob 已提交
2328

2329
		viewMatrix = camera.matrixWorldInverse,
M
Mr.doob 已提交
2330

M
Mr.doob 已提交
2331
		directionalLength = 0,
M
Mr.doob 已提交
2332 2333
		pointLength = 0,
		spotLength = 0,
2334
		hemiLength = 0;
M
Mr.doob 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343

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

			light = lights[ l ];

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

2344
			shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : emptyTexture;
2345

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

2348 2349 2350
				r += color.r * intensity;
				g += color.g * intensity;
				b += color.b * intensity;
M
Mr.doob 已提交
2351 2352 2353

			} else if ( light instanceof THREE.DirectionalLight ) {

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

2356
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
M
Mr.doob 已提交
2357
				uniforms.direction.setFromMatrixPosition( light.matrixWorld );
2358
				_vector3.setFromMatrixPosition( light.target.matrixWorld );
M
Mr.doob 已提交
2359 2360 2361
				uniforms.direction.sub( _vector3 );
				uniforms.direction.transformDirection( viewMatrix );

2362
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2363

2364
				if ( light.castShadow ) {
M
Mr.doob 已提交
2365

2366 2367 2368
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2369

2370 2371
				}

2372
				_lights.directionalShadowMap[ directionalLength ] = shadowMap;
2373
				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
2374
				_lights.directional[ directionalLength ++ ] = uniforms;
M
Mr.doob 已提交
2375

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

M
Mr.doob 已提交
2378
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2379 2380 2381

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

M
Mr.doob 已提交
2383 2384 2385 2386 2387 2388 2389 2390
				uniforms.color.copy( color ).multiplyScalar( intensity );
				uniforms.distance = distance;

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

2391 2392
				uniforms.coneCos = Math.cos( light.angle );
				uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
M
Mr.doob 已提交
2393
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;
M
Mr.doob 已提交
2394

2395
				uniforms.shadow = light.castShadow;
M
Mr.doob 已提交
2396

2397 2398
				if ( light.castShadow ) {

2399 2400 2401
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;
2402

2403 2404
				}

2405
				_lights.spotShadowMap[ spotLength ] = shadowMap;
2406
				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
M
Mr.doob 已提交
2407
				_lights.spot[ spotLength ++ ] = uniforms;
2408

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

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

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

M
Mr.doob 已提交
2416 2417
				uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
				uniforms.distance = light.distance;
M
Mr.doob 已提交
2418 2419
				uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay;

2420
				uniforms.shadow = light.castShadow;
2421

M
Mr.doob 已提交
2422
				if ( light.castShadow ) {
2423

2424 2425 2426 2427 2428
					uniforms.shadowBias = light.shadow.bias;
					uniforms.shadowRadius = light.shadow.radius;
					uniforms.shadowMapSize = light.shadow.mapSize;

				}
2429

2430
				_lights.pointShadowMap[ pointLength ] = shadowMap;
2431

2432 2433 2434
				if ( _lights.pointShadowMatrix[ pointLength ] === undefined ) {

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

2436 2437
				}

2438 2439 2440 2441 2442
				// for point lights we set the shadow matrix to be a translation-only matrix
				// equal to inverse of the light's position
				_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
				_lights.pointShadowMatrix[ pointLength ].identity().setPosition( _vector3 );

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

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

M
Mr.doob 已提交
2447
				var uniforms = lightCache.get( light );
M
Mr.doob 已提交
2448 2449 2450 2451

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

M
Mr.doob 已提交
2453 2454
				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
M
Mr.doob 已提交
2455

M
Mr.doob 已提交
2456
				_lights.hemi[ hemiLength ++ ] = uniforms;
M
Mr.doob 已提交
2457 2458 2459 2460 2461

			}

		}

M
Mr.doob 已提交
2462 2463 2464
		_lights.ambient[ 0 ] = r;
		_lights.ambient[ 1 ] = g;
		_lights.ambient[ 2 ] = b;
M
Mr.doob 已提交
2465

M
Mr.doob 已提交
2466 2467
		_lights.directional.length = directionalLength;
		_lights.spot.length = spotLength;
2468
		_lights.point.length = pointLength;
M
Mr.doob 已提交
2469
		_lights.hemi.length = hemiLength;
2470

2471
		_lights.hash = directionalLength + ',' + pointLength + ',' + spotLength + ',' + hemiLength + ',' + _lights.shadows.length;
2472

M
Mr.doob 已提交
2473
	}
M
Mr.doob 已提交
2474 2475 2476 2477 2478

	// GL state setting

	this.setFaceCulling = function ( cullFace, frontFaceDirection ) {

T
tschw 已提交
2479 2480
		state.setCullFace( cullFace );
		state.setFlipSided( frontFaceDirection === THREE.FrontFaceDirectionCW );
M
Mr.doob 已提交
2481 2482 2483 2484 2485

	};

	// Textures

T
tschw 已提交
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
	function allocTextureUnit() {

		var textureUnit = _usedTextureUnits;

		if ( textureUnit >= capabilities.maxTextures ) {

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

		}

		_usedTextureUnits += 1;

		return textureUnit;

	}

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

2504 2505
		var extension;

M
Mr.doob 已提交
2506
		if ( isPowerOfTwoImage ) {
M
Mr.doob 已提交
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517

			_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 已提交
2518 2519 2520

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

M
Mr.doob 已提交
2521
				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 已提交
2522

2523
			}
M
Mr.doob 已提交
2524 2525 2526 2527

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

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

M
Mr.doob 已提交
2530
				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 已提交
2531

2532
			}
M
Mr.doob 已提交
2533

M
Mr.doob 已提交
2534 2535
		}

2536 2537
		extension = extensions.get( 'EXT_texture_filter_anisotropic' );

2538
		if ( extension ) {
M
Mr.doob 已提交
2539

2540 2541
			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 已提交
2542

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

2545
				_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
2546
				properties.get( texture ).__currentAnisotropy = texture.anisotropy;
M
Mr.doob 已提交
2547 2548 2549 2550 2551

			}

		}

M
Mr.doob 已提交
2552
	}
M
Mr.doob 已提交
2553

2554
	function uploadTexture( textureProperties, texture, slot ) {
2555

2556
		if ( textureProperties.__webglInit === undefined ) {
2557

2558
			textureProperties.__webglInit = true;
2559 2560 2561

			texture.addEventListener( 'dispose', onTextureDispose );

2562
			textureProperties.__webglTexture = _gl.createTexture();
2563

2564
			_infoMemory.textures ++;
2565 2566

		}
M
Mr.doob 已提交
2567

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

H
Henri Astre 已提交
2571 2572 2573 2574
		_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 );

2575
		var image = clampToMaxSize( texture.image, capabilities.maxTextureSize );
2576

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

2579
			image = makePowerOfTwo( image );
M
Mr.doob 已提交
2580 2581 2582

		}

M
Mr.doob 已提交
2583
		var isPowerOfTwoImage = isPowerOfTwo( image ),
H
Henri Astre 已提交
2584 2585
		glFormat = paramThreeToGL( texture.format ),
		glType = paramThreeToGL( texture.type );
M
Mr.doob 已提交
2586

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

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

2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
		if ( texture instanceof THREE.DepthTexture ) {

			// populate depth texture with dummy data

			var internalFormat = _gl.DEPTH_COMPONENT;

			if ( texture.type === THREE.FloatType ) {

				if ( !_isWebGL2 ) throw new Error('Float Depth Texture only supported in WebGL2.0');
				internalFormat = _gl.DEPTH_COMPONENT32F;

			} else if ( _isWebGL2 ) {

				// WebGL 2.0 requires signed internalformat for glTexImage2D
				internalFormat = _gl.DEPTH_COMPONENT16;

			}

			state.texImage2D( _gl.TEXTURE_2D, 0, internalFormat, image.width, image.height, 0, glFormat, glType, null );

		} else if ( texture instanceof THREE.DataTexture ) {
M
Mr.doob 已提交
2612

H
Henri Astre 已提交
2613 2614 2615
			// 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 已提交
2616

M
Mr.doob 已提交
2617
			if ( mipmaps.length > 0 && isPowerOfTwoImage ) {
H
Henri Astre 已提交
2618 2619

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

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

H
Henri Astre 已提交
2624
				}
M
Mr.doob 已提交
2625

H
Henri Astre 已提交
2626
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2627

H
Henri Astre 已提交
2628
			} else {
M
Mr.doob 已提交
2629

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

H
Henri Astre 已提交
2632
			}
M
Mr.doob 已提交
2633

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

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

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

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

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

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

2646
					} else {
M
Mr.doob 已提交
2647

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

2650
					}
M
Mr.doob 已提交
2651

H
Henri Astre 已提交
2652
				} else {
M
Mr.doob 已提交
2653

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

M
Mr.doob 已提交
2656 2657
				}

H
Henri Astre 已提交
2658 2659
			}

G
gero3 已提交
2660 2661 2662
		} else {

			// regular Texture (image, video, canvas)
H
Henri Astre 已提交
2663 2664 2665 2666 2667

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

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

					mipmap = mipmaps[ i ];
2673
					state.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
M
Mr.doob 已提交
2674 2675 2676

				}

H
Henri Astre 已提交
2677
				texture.generateMipmaps = false;
M
Mr.doob 已提交
2678

H
Henri Astre 已提交
2679
			} else {
M
Mr.doob 已提交
2680

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

H
Henri Astre 已提交
2683
			}
M
Mr.doob 已提交
2684

H
Henri Astre 已提交
2685
		}
M
Mr.doob 已提交
2686

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

2689
		textureProperties.__version = texture.version;
M
Mr.doob 已提交
2690

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

2693
	}
M
Mr.doob 已提交
2694

T
tschw 已提交
2695 2696
	function setTexture2D( texture, slot ) {

2697 2698 2699
		var textureProperties = properties.get( texture );

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

2701
			var image = texture.image;
M
Mr.doob 已提交
2702

2703 2704
			if ( image === undefined ) {

2705
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined', texture );
2706 2707 2708 2709
				return;

			}

2710
			if ( image.complete === false ) {
M
Mr.doob 已提交
2711

2712
				console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
2713 2714 2715 2716
				return;

			}

2717
			uploadTexture( textureProperties, texture, slot );
2718

2719
			return;
M
Mr.doob 已提交
2720 2721 2722

		}

B
Ben Adams 已提交
2723
		state.activeTexture( _gl.TEXTURE0 + slot );
2724
		state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
2725

M
Michael Herzog 已提交
2726
	}
M
Mr.doob 已提交
2727 2728 2729

	function clampToMaxSize ( image, maxSize ) {

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

2732 2733
			// Warning: Scaling through the canvas will only work with images that use
			// premultiplied alpha.
M
Mr.doob 已提交
2734

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

2737 2738 2739
			var canvas = document.createElement( 'canvas' );
			canvas.width = Math.floor( image.width * scale );
			canvas.height = Math.floor( image.height * scale );
M
Mr.doob 已提交
2740

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

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

2746 2747 2748
			return canvas;

		}
M
Mr.doob 已提交
2749

2750
		return image;
M
Mr.doob 已提交
2751 2752 2753

	}

M
Mr.doob 已提交
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
	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;

	}

2790
	function setTextureCube ( texture, slot ) {
M
Mr.doob 已提交
2791

2792
		var textureProperties = properties.get( texture );
2793

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

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

2798
				if ( ! textureProperties.__image__webglTextureCube ) {
M
Mr.doob 已提交
2799

2800 2801
					texture.addEventListener( 'dispose', onTextureDispose );

2802
					textureProperties.__image__webglTextureCube = _gl.createTexture();
M
Mr.doob 已提交
2803

2804
					_infoMemory.textures ++;
M
Mr.doob 已提交
2805 2806 2807

				}

B
Ben Adams 已提交
2808
				state.activeTexture( _gl.TEXTURE0 + slot );
2809
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
2810 2811 2812

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

M
Mr.doob 已提交
2813 2814
				var isCompressed = texture instanceof THREE.CompressedTexture;
				var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
M
Mr.doob 已提交
2815 2816 2817 2818 2819

				var cubeImage = [];

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

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

G
gero3 已提交
2822
						cubeImage[ i ] = clampToMaxSize( texture.image[ i ], capabilities.maxCubemapSize );
M
Mr.doob 已提交
2823 2824 2825

					} else {

2826
						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
M
Mr.doob 已提交
2827 2828 2829 2830 2831 2832

					}

				}

				var image = cubeImage[ 0 ],
M
Mr.doob 已提交
2833
				isPowerOfTwoImage = isPowerOfTwo( image ),
M
Mr.doob 已提交
2834 2835 2836
				glFormat = paramThreeToGL( texture.format ),
				glType = paramThreeToGL( texture.type );

M
Mr.doob 已提交
2837
				setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isPowerOfTwoImage );
M
Mr.doob 已提交
2838 2839 2840

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

2841
					if ( ! isCompressed ) {
M
Mr.doob 已提交
2842

M
Mr.doob 已提交
2843
						if ( isDataTexture ) {
2844

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

M
Mr.doob 已提交
2847
						} else {
2848

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

M
Mr.doob 已提交
2851 2852
						}

2853
					} else {
2854

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

2857
						for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
M
Mr.doob 已提交
2858 2859

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

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

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

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

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

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

2871
								}
M
Mr.doob 已提交
2872

2873
							} else {
M
Mr.doob 已提交
2874

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

2877
							}
M
Mr.doob 已提交
2878

2879
						}
M
Mr.doob 已提交
2880

M
Mr.doob 已提交
2881
					}
M
Mr.doob 已提交
2882

M
Mr.doob 已提交
2883 2884
				}

M
Mr.doob 已提交
2885
				if ( texture.generateMipmaps && isPowerOfTwoImage ) {
M
Mr.doob 已提交
2886 2887 2888 2889 2890

					_gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );

				}

2891
				textureProperties.__version = texture.version;
M
Mr.doob 已提交
2892

B
Ben Adams 已提交
2893
				if ( texture.onUpdate ) texture.onUpdate( texture );
M
Mr.doob 已提交
2894 2895 2896

			} else {

B
Ben Adams 已提交
2897
				state.activeTexture( _gl.TEXTURE0 + slot );
2898
				state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__image__webglTextureCube );
M
Mr.doob 已提交
2899 2900 2901 2902 2903

			}

		}

M
Mr.doob 已提交
2904
	}
M
Mr.doob 已提交
2905

2906
	function setTextureCubeDynamic ( texture, slot ) {
M
Mr.doob 已提交
2907

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

M
Mr.doob 已提交
2911
	}
M
Mr.doob 已提交
2912

2913
	this.allocTextureUnit = allocTextureUnit;
T
tschw 已提交
2914

2915 2916
	//this.setTexture2D = setTexture2D;
	this.setTexture2D = ( function() {
T
tschw 已提交
2917

2918
		var warned = false;
T
tschw 已提交
2919

2920 2921
		// backwards compatibility: peel texture.texture
		return function( texture, slot ) {
T
tschw 已提交
2922

2923
			if ( texture instanceof THREE.WebGLRenderTarget ) {
T
tschw 已提交
2924

2925
				if ( ! warned ) {
T
tschw 已提交
2926

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

2930
				}
T
tschw 已提交
2931

2932
				texture = texture.texture;
T
tschw 已提交
2933

2934
			}
T
tschw 已提交
2935

2936
			setTexture2D( texture, slot );
T
tschw 已提交
2937

2938
		};
T
tschw 已提交
2939

2940
	}() );
T
tschw 已提交
2941

2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 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
	this.setTexture = ( function() {

		var warned = false;

		return function( texture, slot ) {

			if ( ! warned ) {

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

			}

			_this.setTexture2D( texture, slot );

		};

	}() );

	this.setTextureCube = ( function() {

		var warned = false;

		return function( texture, slot ) {

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

				if ( ! warned ) {

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

				}

				texture = texture.texture;

			}

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

				// CompressedTexture can have Array in image :/

				// this function alone should take care of cube textures
				setTextureCube( texture, slot );

			} else {

				// assumed: texture property of THREE.WebGLRenderTargetCube

				setTextureCubeDynamic( texture, slot );

			}

		};

	}() );
T
tschw 已提交
3002

M
Mr.doob 已提交
3003 3004
	// Render targets

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

3008 3009 3010
		var glFormat = paramThreeToGL( renderTarget.texture.format );
		var glType = paramThreeToGL( renderTarget.texture.type );
		state.texImage2D( textureTarget, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
M
Mr.doob 已提交
3011
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3012 3013
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
		_gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
M
Mr.doob 已提交
3014

M
Mr.doob 已提交
3015
	}
M
Mr.doob 已提交
3016

3017 3018
	// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
	function setupRenderBufferStorage ( renderbuffer, renderTarget ) {
M
Mr.doob 已提交
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033

		_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );

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

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

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

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

		} else {

3034
			// FIXME: We don't support !depth !stencil
M
Mr.doob 已提交
3035 3036 3037 3038
			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );

		}

3039
		_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
M
Mr.doob 已提交
3040

M
Mr.doob 已提交
3041
	}
M
Mr.doob 已提交
3042

3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
	// Setup resources for a Depth Texture for a FBO (needs an extension)
	function setupDepthTexture ( framebuffer, renderTarget ) {

		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
		if ( isCube ) throw new Error('Depth Texture with cube render targets is not supported!');

		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );

		if ( !( renderTarget.depthTexture instanceof THREE.DepthTexture ) ) {

			throw new Error('renderTarget.depthTexture must be an instance of THREE.DepthTexture');

		}

		// upload an empty depth texture with framebuffer size
		if ( !properties.get( renderTarget.depthTexture ).__webglTexture ||
				renderTarget.depthTexture.image.width !== renderTarget.width ||
				renderTarget.depthTexture.image.height !== renderTarget.height ) {
			renderTarget.depthTexture.image.width = renderTarget.width;
			renderTarget.depthTexture.image.height = renderTarget.height;
			renderTarget.depthTexture.needsUpdate = true;
		}

3066
		_this.setTexture2D( renderTarget.depthTexture, 0 );
3067 3068 3069 3070 3071 3072

		var webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
		_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );

	}

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

3076
		var renderTargetProperties = properties.get( renderTarget );
M
Mr.doob 已提交
3077 3078

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

3080
		if ( renderTarget.depthTexture ) {
M
Mr.doob 已提交
3081

3082
			if ( isCube ) throw new Error('target.depthTexture not supported in Cube render targets');
M
Mr.doob 已提交
3083

3084 3085 3086
			setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );

		} else {
M
Mr.doob 已提交
3087

3088
			if ( isCube ) {
M
Mr.doob 已提交
3089

3090
				renderTargetProperties.__webglDepthbuffer = [];
B
Ben Adams 已提交
3091

3092 3093 3094 3095 3096
				for ( var i = 0; i < 6; i ++ ) {

					_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
					renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
					setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget );
M
Mr.doob 已提交
3097

3098 3099 3100 3101 3102 3103 3104 3105 3106
				}

			} else {

				_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
				renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
				setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget );

			}
M
Mr.doob 已提交
3107

3108
		}
M
Mr.doob 已提交
3109

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

M
Mr.doob 已提交
3112
	}
M
Mr.doob 已提交
3113

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

3117 3118
		var renderTargetProperties = properties.get( renderTarget );
		var textureProperties = properties.get( renderTarget.texture );
M
Mr.doob 已提交
3119

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

3122
		textureProperties.__webglTexture = _gl.createTexture();
M
Mr.doob 已提交
3123

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

3126 3127
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
		var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height );
M
Mr.doob 已提交
3128

3129
		// Setup framebuffer
M
Mr.doob 已提交
3130

3131
		if ( isCube ) {
M
Mr.doob 已提交
3132

3133
			renderTargetProperties.__webglFramebuffer = [];
M
Mr.doob 已提交
3134

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

3137
				renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
M
Mr.doob 已提交
3138

3139
			}
M
Mr.doob 已提交
3140

3141
		} else {
M
Mr.doob 已提交
3142

3143
			renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
M
Mr.doob 已提交
3144

3145
		}
M
Mr.doob 已提交
3146

3147
		// Setup color buffer
M
Mr.doob 已提交
3148

3149
		if ( isCube ) {
M
Mr.doob 已提交
3150

3151 3152
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
			setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget.texture, isTargetPowerOfTwo );
M
Mr.doob 已提交
3153

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

3156
				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
M
Mr.doob 已提交
3157 3158 3159

			}

3160 3161
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
			state.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
M
Mr.doob 已提交
3162

3163
		} else {
M
Mr.doob 已提交
3164

3165 3166 3167
			state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
			setTextureParameters( _gl.TEXTURE_2D, renderTarget.texture, isTargetPowerOfTwo );
			setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D );
M
Mr.doob 已提交
3168

3169 3170
			if ( renderTarget.texture.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
			state.bindTexture( _gl.TEXTURE_2D, null );
M
Mr.doob 已提交
3171

3172
		}
M
Mr.doob 已提交
3173

3174
		// Setup depth and stencil buffers
3175

3176
		if ( renderTarget.depthBuffer ) {
M
Mr.doob 已提交
3177

M
Marius Kintel 已提交
3178
			setupDepthRenderbuffer( renderTarget );
M
Mr.doob 已提交
3179

3180
		}
M
Mr.doob 已提交
3181

3182
	}
M
Mr.doob 已提交
3183

3184 3185 3186 3187
	this.getCurrentRenderTarget = function() {

		return _currentRenderTarget;

M
Michael Herzog 已提交
3188
	};
3189

3190
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
3191

3192 3193
		_currentRenderTarget = renderTarget;

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

M
Marius Kintel 已提交
3196
			setupRenderTarget( renderTarget );
M
Mr.doob 已提交
3197 3198 3199

		}

3200
		var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
3201
		var framebuffer;
M
Mr.doob 已提交
3202 3203 3204

		if ( renderTarget ) {

3205
			var renderTargetProperties = properties.get( renderTarget );
F
Fordy 已提交
3206

M
Mr.doob 已提交
3207 3208
			if ( isCube ) {

3209
				framebuffer = renderTargetProperties.__webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
3210 3211 3212

			} else {

3213
				framebuffer = renderTargetProperties.__webglFramebuffer;
M
Mr.doob 已提交
3214 3215 3216

			}

3217 3218
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
3219

3220
			_currentViewport.copy( renderTarget.viewport );
M
Mr.doob 已提交
3221 3222 3223 3224 3225

		} else {

			framebuffer = null;

3226
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
3227
			_currentScissorTest = _scissorTest;
3228

3229
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
M
Mr.doob 已提交
3230 3231 3232

		}

M
Mr.doob 已提交
3233
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
3234 3235 3236 3237 3238 3239

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

		}

3240 3241
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
3242

3243
		state.viewport( _currentViewport );
3244

M
Mr.doob 已提交
3245 3246 3247
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
3248
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, renderTarget.activeMipMapLevel );
M
Mr.doob 已提交
3249 3250 3251

		}

M
Mr.doob 已提交
3252 3253
	};

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

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

3258
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
3259
			return;
3260

G
gero3 已提交
3261
		}
3262

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

M
Mr.doob 已提交
3265
		if ( framebuffer ) {
3266

G
gero3 已提交
3267
			var restore = false;
3268

M
Mr.doob 已提交
3269
			if ( framebuffer !== _currentFramebuffer ) {
3270

M
Mr.doob 已提交
3271
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
3272

G
gero3 已提交
3273
				restore = true;
3274

G
gero3 已提交
3275
			}
3276

M
Mr.doob 已提交
3277
			try {
3278

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

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

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

M
Mr.doob 已提交
3286
				}
3287

M
Michael Herzog 已提交
3288 3289 3290 3291
				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' ) ) ) {
3292

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

M
Mr.doob 已提交
3296
				}
3297

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

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

3302
					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
3303 3304 3305 3306

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

					}
3307

M
Mr.doob 已提交
3308
				} else {
M
Mr.doob 已提交
3309

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

				}
M
Mr.doob 已提交
3313

M
Mr.doob 已提交
3314
			} finally {
M
Mr.doob 已提交
3315

M
Mr.doob 已提交
3316 3317 3318
				if ( restore ) {

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

M
Mr.doob 已提交
3320 3321 3322
				}

			}
M
Mr.doob 已提交
3323 3324 3325

		}

M
Mr.doob 已提交
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
	};

	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 已提交
3337
	}
M
Mr.doob 已提交
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350

	// 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 已提交
3351
	}
M
Mr.doob 已提交
3352 3353 3354 3355 3356

	// Map three.js constants to WebGL constants

	function paramThreeToGL ( p ) {

3357 3358
		var extension;

M
Mr.doob 已提交
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
		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;

3383 3384 3385 3386 3387 3388 3389 3390
		extension = extensions.get( 'OES_texture_half_float' );

		if ( extension !== null ) {

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

		}

M
Mr.doob 已提交
3391 3392 3393 3394 3395
		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;
3396
		if ( p === THREE.DepthFormat ) return _gl.DEPTH_COMPONENT;
M
Mr.doob 已提交
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414

		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;

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

3417 3418 3419 3420 3421 3422
		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 已提交
3423 3424 3425

		}

3426 3427 3428
		extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );

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

3430 3431 3432 3433
			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 已提交
3434 3435 3436

		}

3437 3438 3439 3440 3441 3442 3443 3444
		extension = extensions.get( 'WEBGL_compressed_texture_etc1' );

		if ( extension !== null ) {

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

		}

3445 3446 3447
		extension = extensions.get( 'EXT_blend_minmax' );

		if ( extension !== null ) {
3448

3449 3450
			if ( p === THREE.MinEquation ) return extension.MIN_EXT;
			if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
3451 3452 3453

		}

M
Mr.doob 已提交
3454 3455
		return 0;

M
Mr.doob 已提交
3456
	}
M
Mr.doob 已提交
3457 3458

};