WebGLRenderer.js 60.7 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9
import {
	REVISION,
	RGBAFormat,
	HalfFloatType,
	FloatType,
	UnsignedByteType,
	TriangleFanDrawMode,
	TriangleStripDrawMode,
	TrianglesDrawMode,
10 11
	LinearToneMapping,
	BackSide
M
Mr.doob 已提交
12
} from '../constants.js';
B
bentok 已提交
13 14
import { _Math } from '../math/Math.js';
import { DataTexture } from '../textures/DataTexture.js';
M
Mr.doob 已提交
15 16 17
import { Frustum } from '../math/Frustum.js';
import { Matrix4 } from '../math/Matrix4.js';
import { ShaderLib } from './shaders/ShaderLib.js';
B
bentok 已提交
18
import { UniformsLib } from './shaders/UniformsLib.js';
M
Mr.doob 已提交
19
import { cloneUniforms } from './shaders/UniformsUtils.js';
20
import { Vector2 } from '../math/Vector2.js';
M
Mr.doob 已提交
21 22
import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js';
M
Mr.doob 已提交
23
import { WebGLAnimation } from './webgl/WebGLAnimation.js';
B
bentok 已提交
24 25 26
import { WebGLAttributes } from './webgl/WebGLAttributes.js';
import { WebGLBackground } from './webgl/WebGLBackground.js';
import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer.js';
M
Mr.doob 已提交
27 28 29
import { WebGLCapabilities } from './webgl/WebGLCapabilities.js';
import { WebGLClipping } from './webgl/WebGLClipping.js';
import { WebGLExtensions } from './webgl/WebGLExtensions.js';
B
bentok 已提交
30
import { WebGLGeometries } from './webgl/WebGLGeometries.js';
M
Mr.doob 已提交
31 32 33
import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer.js';
import { WebGLInfo } from './webgl/WebGLInfo.js';
import { WebGLMorphtargets } from './webgl/WebGLMorphtargets.js';
B
bentok 已提交
34 35 36
import { WebGLObjects } from './webgl/WebGLObjects.js';
import { WebGLPrograms } from './webgl/WebGLPrograms.js';
import { WebGLProperties } from './webgl/WebGLProperties.js';
M
Mr.doob 已提交
37 38 39
import { WebGLRenderLists } from './webgl/WebGLRenderLists.js';
import { WebGLRenderStates } from './webgl/WebGLRenderStates.js';
import { WebGLShadowMap } from './webgl/WebGLShadowMap.js';
B
bentok 已提交
40
import { WebGLState } from './webgl/WebGLState.js';
M
Mr.doob 已提交
41 42
import { WebGLTextures } from './webgl/WebGLTextures.js';
import { WebGLUniforms } from './webgl/WebGLUniforms.js';
B
bentok 已提交
43
import { WebGLUtils } from './webgl/WebGLUtils.js';
M
Mr.doob 已提交
44
import { WebVRManager } from './webvr/WebVRManager.js';
M
Mr.doob 已提交
45
import { WebXRManager } from './webvr/WebXRManager.js';
R
Rich Harris 已提交
46

M
Mr.doob 已提交
47 48 49 50 51
/**
 * @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 已提交
52
 * @author tschw
M
Mr.doob 已提交
53 54
 */

M
Mr.doob 已提交
55
function WebGLRenderer( parameters ) {
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57
	console.log( 'THREE.WebGLRenderer', REVISION );
M
Mr.doob 已提交
58 59 60

	parameters = parameters || {};

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

64 65 66 67 68
		_alpha = parameters.alpha !== undefined ? parameters.alpha : false,
		_depth = parameters.depth !== undefined ? parameters.depth : true,
		_stencil = parameters.stencil !== undefined ? parameters.stencil : true,
		_antialias = parameters.antialias !== undefined ? parameters.antialias : false,
		_premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
69 70
		_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
		_powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default';
M
Mr.doob 已提交
71

72
	var currentRenderList = null;
73
	var currentRenderState = null;
M
Mr.doob 已提交
74

M
Mr.doob 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	// 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 已提交
91 92 93 94 95
	// user-defined clipping

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

M
Mr.doob 已提交
96 97
	// physically based shading

98
	this.gammaFactor = 2.0;	// for backwards compatibility
M
Mr.doob 已提交
99 100 101
	this.gammaInput = false;
	this.gammaOutput = false;

102 103
	// physical lights

104
	this.physicallyCorrectLights = false;
105

B
Ben Houston 已提交
106 107
	// tone mapping

R
Rich Harris 已提交
108
	this.toneMapping = LinearToneMapping;
B
Ben Houston 已提交
109 110 111
	this.toneMappingExposure = 1.0;
	this.toneMappingWhitePoint = 1.0;

M
Mr.doob 已提交
112 113 114 115 116 117 118 119 120
	// morphs

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

	// internal properties

	var _this = this,

121 122
		_isContextLost = false,

123
		// internal state cache
M
Mr.doob 已提交
124

125 126
		_framebuffer = null,

127 128 129
		_currentRenderTarget = null,
		_currentFramebuffer = null,
		_currentMaterialId = - 1,
130 131 132 133

		// geometry and program caching

		_currentGeometryProgram = {
M
Mr.doob 已提交
134 135
			geometry: null,
			program: null,
136 137
			wireframe: false
		},
138

139
		_currentCamera = null,
140
		_currentArrayCamera = null,
M
Mr.doob 已提交
141

M
Mr.doob 已提交
142
		_currentViewport = new Vector4(),
143 144
		_currentScissor = new Vector4(),
		_currentScissorTest = null,
145

146
		//
147

148 149
		_width = _canvas.width,
		_height = _canvas.height,
M
Mr.doob 已提交
150

151
		_pixelRatio = 1,
M
Mr.doob 已提交
152

M
Mr.doob 已提交
153
		_viewport = new Vector4( 0, 0, _width, _height ),
154 155
		_scissor = new Vector4( 0, 0, _width, _height ),
		_scissorTest = false,
156

157
		// frustum
M
Mr.doob 已提交
158

159
		_frustum = new Frustum(),
M
Mr.doob 已提交
160

161
		// clipping
T
tschw 已提交
162

163 164 165
		_clipping = new WebGLClipping(),
		_clippingEnabled = false,
		_localClippingEnabled = false,
T
tschw 已提交
166

167
		// camera matrices cache
M
Mr.doob 已提交
168

169
		_projScreenMatrix = new Matrix4(),
M
Mr.doob 已提交
170

M
Mugen87 已提交
171
		_vector3 = new Vector3();
A
Atrahasis 已提交
172

173 174 175 176 177
	function getTargetPixelRatio() {

		return _currentRenderTarget === null ? _pixelRatio : 1;

	}
178

M
Mr.doob 已提交
179 180 181 182
	// initialize

	var _gl;

M
Mr.doob 已提交
183 184
	try {

185
		var contextAttributes = {
M
Mr.doob 已提交
186 187 188 189 190
			alpha: _alpha,
			depth: _depth,
			stencil: _stencil,
			antialias: _antialias,
			premultipliedAlpha: _premultipliedAlpha,
191
			preserveDrawingBuffer: _preserveDrawingBuffer,
192 193
			powerPreference: _powerPreference,
			xrCompatible: true
M
Mr.doob 已提交
194 195
		};

196 197
		// event listeners must be registered before WebGL context is created, see #12753

198
		_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
199
		_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
200

201
		_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
M
Mr.doob 已提交
202 203 204

		if ( _gl === null ) {

G
gero3 已提交
205
			if ( _canvas.getContext( 'webgl' ) !== null ) {
206

D
Daniel Hritzkiv 已提交
207
				throw new Error( 'Error creating WebGL context with your selected attributes.' );
208 209 210

			} else {

D
Daniel Hritzkiv 已提交
211
				throw new Error( 'Error creating WebGL context.' );
212 213

			}
M
Mr.doob 已提交
214 215 216

		}

217 218 219 220 221 222 223 224 225 226 227 228
		// Some experimental-webgl implementations do not have getShaderPrecisionFormat

		if ( _gl.getShaderPrecisionFormat === undefined ) {

			_gl.getShaderPrecisionFormat = function () {

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

			};

		}

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

D
Daniel Hritzkiv 已提交
231
		console.error( 'THREE.WebGLRenderer: ' + error.message );
232
		throw error;
M
Mr.doob 已提交
233 234 235

	}

M
Mugen87 已提交
236
	var extensions, capabilities, state, info;
237
	var properties, textures, attributes, geometries, objects;
238
	var programCache, renderLists, renderStates;
239

240
	var background, morphtargets, bufferRenderer, indexedBufferRenderer;
241

242
	var utils;
243

244
	function initGLContext() {
245

246
		extensions = new WebGLExtensions( _gl );
247

T
Takahiro 已提交
248 249 250
		capabilities = new WebGLCapabilities( _gl, extensions, parameters );

		if ( ! capabilities.isWebGL2 ) {
251 252 253 254 255 256 257 258 259 260 261

			extensions.get( 'WEBGL_depth_texture' );
			extensions.get( 'OES_texture_float' );
			extensions.get( 'OES_texture_half_float' );
			extensions.get( 'OES_texture_half_float_linear' );
			extensions.get( 'OES_standard_derivatives' );
			extensions.get( 'OES_element_index_uint' );
			extensions.get( 'ANGLE_instanced_arrays' );

		}

262
		extensions.get( 'OES_texture_float_linear' );
263

T
Takahiro 已提交
264
		utils = new WebGLUtils( _gl, extensions, capabilities );
M
Mr.doob 已提交
265

T
Takahiro 已提交
266
		state = new WebGLState( _gl, extensions, utils, capabilities );
267 268
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
M
Mr.doob 已提交
269

M
Mugen87 已提交
270
		info = new WebGLInfo( _gl );
271
		properties = new WebGLProperties();
M
Mugen87 已提交
272
		textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
273
		attributes = new WebGLAttributes( _gl );
M
Mugen87 已提交
274 275
		geometries = new WebGLGeometries( _gl, attributes, info );
		objects = new WebGLObjects( geometries, info );
276
		morphtargets = new WebGLMorphtargets( _gl );
277
		programCache = new WebGLPrograms( _this, extensions, capabilities, textures );
278
		renderLists = new WebGLRenderLists();
279
		renderStates = new WebGLRenderStates();
M
Mr.doob 已提交
280

281
		background = new WebGLBackground( _this, state, objects, _premultipliedAlpha );
282

T
Takahiro 已提交
283 284
		bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );
		indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info, capabilities );
285

M
Mugen87 已提交
286
		info.programs = programCache.programs;
287

288 289 290 291 292 293
		_this.context = _gl;
		_this.capabilities = capabilities;
		_this.extensions = extensions;
		_this.properties = properties;
		_this.renderLists = renderLists;
		_this.state = state;
M
Mugen87 已提交
294
		_this.info = info;
M
Mr.doob 已提交
295

296
	}
M
Mr.doob 已提交
297

298
	initGLContext();
M
Mr.doob 已提交
299

300
	// vr
M
Mr.doob 已提交
301

302 303 304 305 306 307 308
	var vr = null;

	if ( typeof navigator !== 'undefined' ) {

		vr = ( 'xr' in navigator ) ? new WebXRManager( _this ) : new WebVRManager( _this );

	}
M
Mr.doob 已提交
309

310
	this.vr = vr;
M
Mr.doob 已提交
311 312

	// shadow map
M
Mr.doob 已提交
313

314
	var shadowMap = new WebGLShadowMap( _this, objects, capabilities.maxTextureSize );
M
Mr.doob 已提交
315

316
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
317

M
Mr.doob 已提交
318 319 320 321 322 323 324 325
	// API

	this.getContext = function () {

		return _gl;

	};

326 327 328 329 330 331
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

332 333
	this.forceContextLoss = function () {

M
Michael Bond 已提交
334 335
		var extension = extensions.get( 'WEBGL_lose_context' );
		if ( extension ) extension.loseContext();
336 337 338

	};

339
	this.forceContextRestore = function () {
M
Mr.doob 已提交
340

341 342
		var extension = extensions.get( 'WEBGL_lose_context' );
		if ( extension ) extension.restoreContext();
M
Mr.doob 已提交
343 344 345

	};

346 347
	this.getPixelRatio = function () {

348
		return _pixelRatio;
349 350 351 352 353

	};

	this.setPixelRatio = function ( value ) {

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

		_pixelRatio = value;

M
Mr.doob 已提交
358
		this.setSize( _width, _height, false );
359 360 361

	};

362
	this.getSize = function ( target ) {
363

364 365 366 367 368 369 370 371 372
		if ( target === undefined ) {

			console.warn( 'WebGLRenderer: .getsize() now requires a Vector2 as an argument' );

			target = new Vector2();

		}

		return target.set( _width, _height );
373 374 375

	};

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

M
Mr.doob 已提交
378
		if ( vr.isPresenting() ) {
379 380 381 382 383 384

			console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' );
			return;

		}

385 386 387
		_width = width;
		_height = height;

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

391
		if ( updateStyle !== false ) {
392

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

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

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

	};

402
	this.getDrawingBufferSize = function ( target ) {
M
Mr.doob 已提交
403

404 405 406 407 408 409 410 411 412
		if ( target === undefined ) {

			console.warn( 'WebGLRenderer: .getdrawingBufferSize() now requires a Vector2 as an argument' );

			target = new Vector2();

		}

		return target.set( _width * _pixelRatio, _height * _pixelRatio );
M
Mr.doob 已提交
413 414 415

	};

416 417 418 419 420 421 422 423 424 425 426 427 428 429
	this.setDrawingBufferSize = function ( width, height, pixelRatio ) {

		_width = width;
		_height = height;

		_pixelRatio = pixelRatio;

		_canvas.width = width * pixelRatio;
		_canvas.height = height * pixelRatio;

		this.setViewport( 0, 0, width, height );

	};

430
	this.getCurrentViewport = function ( target ) {
M
Mugen87 已提交
431

432 433 434 435 436 437 438 439 440
		if ( target === undefined ) {

			console.warn( 'WebGLRenderer: .getCurrentViewport() now requires a Vector4 as an argument' );

			target = new Vector4();

		}

		return target.copy( _currentViewport );
M
Mugen87 已提交
441 442 443

	};

444 445 446
	this.getViewport = function ( target ) {

		return target.copy( _viewport );
M
Mugen87 已提交
447 448 449

	};

M
Mr.doob 已提交
450 451
	this.setViewport = function ( x, y, width, height ) {

452 453 454 455 456 457 458 459 460 461
		if ( x.isVector4 ) {

			_viewport.set( x.x, x.y, x.z, x.w );

		} else {

			_viewport.set( x, y, width, height );

		}

462
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
463

M
Mr.doob 已提交
464 465
	};

466 467 468 469 470 471
	this.getScissor = function ( target ) {

		return target.copy( _scissor );

	};

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

474 475 476 477 478 479 480 481 482 483
		if ( x.isVector4 ) {

			_scissor.set( x.x, x.y, x.z, x.w );

		} else {

			_scissor.set( x, y, width, height );

		}

484
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
485

M
Mr.doob 已提交
486 487
	};

488 489 490 491 492 493
	this.getScissorTest = function () {

		return _scissorTest;

	};

494 495
	this.setScissorTest = function ( boolean ) {

496
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
497 498 499 500 501

	};

	// Clearing

M
Mr.doob 已提交
502
	this.getClearColor = function () {
M
Mr.doob 已提交
503

504
		return background.getClearColor();
M
Mr.doob 已提交
505 506 507

	};

508
	this.setClearColor = function () {
509

510
		background.setClearColor.apply( background, arguments );
M
Mr.doob 已提交
511 512 513

	};

M
Mr.doob 已提交
514
	this.getClearAlpha = function () {
M
Mr.doob 已提交
515

516
		return background.getClearAlpha();
M
Mr.doob 已提交
517 518 519

	};

M
Mugen87 已提交
520
	this.setClearAlpha = function () {
M
Mr.doob 已提交
521

522
		background.setClearAlpha.apply( background, arguments );
M
Mr.doob 已提交
523 524 525 526 527 528 529 530 531 532 533 534

	};

	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 );
535 536 537 538 539

	};

	this.clearColor = function () {

M
Mr.doob 已提交
540
		this.clear( true, false, false );
541 542 543 544 545

	};

	this.clearDepth = function () {

M
Mr.doob 已提交
546
		this.clear( false, true, false );
547 548 549 550 551

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
552
		this.clear( false, false, true );
M
Mr.doob 已提交
553 554 555

	};

M
Mr.doob 已提交
556
	//
M
Mr.doob 已提交
557

M
Mr.doob 已提交
558
	this.dispose = function () {
D
dubejf 已提交
559 560

		_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );
561
		_canvas.removeEventListener( 'webglcontextrestored', onContextRestore, false );
D
dubejf 已提交
562

563
		renderLists.dispose();
564
		renderStates.dispose();
M
Mugen87 已提交
565 566
		properties.dispose();
		objects.dispose();
567

568
		vr.dispose();
569

570
		animation.stop();
B
brunnerh 已提交
571

D
dubejf 已提交
572 573
	};

M
Mr.doob 已提交
574
	// Events
M
Mr.doob 已提交
575

D
dubejf 已提交
576 577 578 579
	function onContextLost( event ) {

		event.preventDefault();

580
		console.log( 'THREE.WebGLRenderer: Context Lost.' );
D
dubejf 已提交
581

582 583 584 585
		_isContextLost = true;

	}

M
Mugen87 已提交
586
	function onContextRestore( /* event */ ) {
D
dubejf 已提交
587

588
		console.log( 'THREE.WebGLRenderer: Context Restored.' );
589 590

		_isContextLost = false;
D
dubejf 已提交
591

592
		initGLContext();
D
dubejf 已提交
593

M
Mr.doob 已提交
594
	}
D
dubejf 已提交
595

596
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
597 598 599 600 601 602 603

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

604
	}
M
Mr.doob 已提交
605 606 607

	// Buffer deallocation

608
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
609

610 611
		releaseMaterialProgramReference( material );

612
		properties.remove( material );
613

614
	}
615 616


617
	function releaseMaterialProgramReference( material ) {
618

619
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
620 621 622

		material.program = undefined;

623
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
624

625
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
626

M
Mr.doob 已提交
627 628
		}

629
	}
M
Mr.doob 已提交
630 631 632

	// Buffer rendering

M
Mr.doob 已提交
633
	function renderObjectImmediate( object, program ) {
M
Mr.doob 已提交
634 635 636

		object.render( function ( object ) {

M
Mr.doob 已提交
637
			_this.renderBufferImmediate( object, program );
M
Mr.doob 已提交
638 639 640 641 642

		} );

	}

M
Mugen87 已提交
643
	this.renderBufferImmediate = function ( object, program ) {
M
Mr.doob 已提交
644

645
		state.initAttributes();
646

647
		var buffers = properties.get( object );
648

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

654
		var programAttributes = program.getAttributes();
655

M
Mr.doob 已提交
656 657
		if ( object.hasPositions ) {

658
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
659
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
660

661 662
			state.enableAttribute( programAttributes.position );
			_gl.vertexAttribPointer( programAttributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
663 664 665 666 667

		}

		if ( object.hasNormals ) {

668
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
M
Mr.doob 已提交
669
			_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
670

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

		}

676
		if ( object.hasUvs ) {
M
Mr.doob 已提交
677

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

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

		}

686
		if ( object.hasColors ) {
M
Mr.doob 已提交
687

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

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

		}

696
		state.disableUnusedAttributes();
697

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

		object.count = 0;

	};

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

706
		var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );
707 708

		state.setMaterial( material, frontFaceCW );
M
Mr.doob 已提交
709

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

M
Mr.doob 已提交
712
		var updateBuffers = false;
M
Mr.doob 已提交
713

714 715 716
		if ( _currentGeometryProgram.geometry !== geometry.id ||
			_currentGeometryProgram.program !== program.id ||
			_currentGeometryProgram.wireframe !== ( material.wireframe === true ) ) {
M
Mr.doob 已提交
717

M
Mr.doob 已提交
718 719
			_currentGeometryProgram.geometry = geometry.id;
			_currentGeometryProgram.program = program.id;
720
			_currentGeometryProgram.wireframe = material.wireframe === true;
M
Mr.doob 已提交
721 722 723 724
			updateBuffers = true;

		}

725
		if ( object.morphTargetInfluences ) {
726

727
			morphtargets.update( object, geometry, material, program );
M
Mr.doob 已提交
728 729 730 731 732

			updateBuffers = true;

		}

733 734
		//

735
		var index = geometry.index;
736
		var position = geometry.attributes.position;
737
		var rangeFactor = 1;
738

739 740
		if ( material.wireframe === true ) {

741
			index = geometries.getWireframeAttribute( geometry );
742
			rangeFactor = 2;
743 744 745

		}

M
Mr.doob 已提交
746
		var attribute;
M
Mr.doob 已提交
747
		var renderer = bufferRenderer;
748

749
		if ( index !== null ) {
750

M
Mr.doob 已提交
751
			attribute = attributes.get( index );
752

753
			renderer = indexedBufferRenderer;
M
Mr.doob 已提交
754
			renderer.setIndex( attribute );
755

756
		}
M
Mr.doob 已提交
757

758
		if ( updateBuffers ) {
M
Mr.doob 已提交
759

760
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
761

762
			if ( index !== null ) {
763

M
Mr.doob 已提交
764
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attribute.buffer );
765 766 767

			}

768
		}
769

770 771
		//

772
		var dataCount = Infinity;
773

M
Mr.doob 已提交
774
		if ( index !== null ) {
775

M
Mr.doob 已提交
776
			dataCount = index.count;
777

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

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

M
Mr.doob 已提交
782
		}
783

M
Mr.doob 已提交
784 785
		var rangeStart = geometry.drawRange.start * rangeFactor;
		var rangeCount = geometry.drawRange.count * rangeFactor;
786

M
Mr.doob 已提交
787 788
		var groupStart = group !== null ? group.start * rangeFactor : 0;
		var groupCount = group !== null ? group.count * rangeFactor : Infinity;
789

M
Mr.doob 已提交
790
		var drawStart = Math.max( rangeStart, groupStart );
791
		var drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;
M
Mr.doob 已提交
792 793 794

		var drawCount = Math.max( 0, drawEnd - drawStart + 1 );

795 796
		if ( drawCount === 0 ) return;

M
Mr.doob 已提交
797
		//
798

799
		if ( object.isMesh ) {
800

801
			if ( material.wireframe === true ) {
802

803
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
804
				renderer.setMode( _gl.LINES );
805

806
			} else {
M
Mr.doob 已提交
807 808

				switch ( object.drawMode ) {
809

R
Rich Harris 已提交
810
					case TrianglesDrawMode:
B
Ben Adams 已提交
811 812 813
						renderer.setMode( _gl.TRIANGLES );
						break;

R
Rich Harris 已提交
814
					case TriangleStripDrawMode:
B
Ben Adams 已提交
815 816 817
						renderer.setMode( _gl.TRIANGLE_STRIP );
						break;

R
Rich Harris 已提交
818
					case TriangleFanDrawMode:
B
Ben Adams 已提交
819 820 821 822
						renderer.setMode( _gl.TRIANGLE_FAN );
						break;

				}
823

824
			}
825

826

827
		} else if ( object.isLine ) {
828

829
			var lineWidth = material.linewidth;
830

831
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
832

833
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
834

835
			if ( object.isLineSegments ) {
836

837
				renderer.setMode( _gl.LINES );
838

839 840 841 842
			} else if ( object.isLineLoop ) {

				renderer.setMode( _gl.LINE_LOOP );

843
			} else {
844

845
				renderer.setMode( _gl.LINE_STRIP );
846 847

			}
M
Mr.doob 已提交
848

849
		} else if ( object.isPoints ) {
850 851

			renderer.setMode( _gl.POINTS );
852

853 854 855 856
		} else if ( object.isSprite ) {

			renderer.setMode( _gl.TRIANGLES );

857
		}
858

T
Takahiro 已提交
859
		if ( geometry && geometry.isInstancedBufferGeometry ) {
M
Mr.doob 已提交
860 861 862

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
865
			}
866 867 868

		} else {

M
Mr.doob 已提交
869
			renderer.render( drawStart, drawCount );
870

M
Mr.doob 已提交
871 872 873 874
		}

	};

875
	function setupVertexAttributes( material, program, geometry ) {
M
Mr.doob 已提交
876

A
Alex Goldring 已提交
877
		if ( geometry && geometry.isInstancedBufferGeometry && ! capabilities.isWebGL2 ) {
B
Ben Adams 已提交
878

879
			if ( extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
B
Ben Adams 已提交
880

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

M
Mr.doob 已提交
884 885 886
			}

		}
B
Ben Adams 已提交
887

888 889
		state.initAttributes();

890
		var geometryAttributes = geometry.attributes;
891

892
		var programAttributes = program.getAttributes();
893

894
		var materialDefaultAttributeValues = material.defaultAttributeValues;
895

896
		for ( var name in programAttributes ) {
897

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

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

902
				var geometryAttribute = geometryAttributes[ name ];
903

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

906
					var normalized = geometryAttribute.normalized;
907
					var size = geometryAttribute.itemSize;
908

M
Mr.doob 已提交
909
					var attribute = attributes.get( geometryAttribute );
910

911 912 913 914
					// TODO Attribute may not be available on context restore

					if ( attribute === undefined ) continue;

M
Mr.doob 已提交
915 916 917
					var buffer = attribute.buffer;
					var type = attribute.type;
					var bytesPerElement = attribute.bytesPerElement;
918

A
aardgoose 已提交
919
					if ( geometryAttribute.isInterleavedBufferAttribute ) {
920

M
Mr.doob 已提交
921 922 923 924
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

T
Takahiro 已提交
925
						if ( data && data.isInstancedInterleavedBuffer ) {
M
Mr.doob 已提交
926

927
							state.enableAttributeAndDivisor( programAttribute, data.meshPerAttribute );
B
Ben Adams 已提交
928

M
Mr.doob 已提交
929
							if ( geometry.maxInstancedCount === undefined ) {
930

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

M
Mr.doob 已提交
933
							}
B
Ben Adams 已提交
934

M
Mr.doob 已提交
935
						} else {
B
Ben Adams 已提交
936

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

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

M
Mr.doob 已提交
941
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
942
						_gl.vertexAttribPointer( programAttribute, size, type, normalized, stride * bytesPerElement, offset * bytesPerElement );
B
Ben Adams 已提交
943

M
Mr.doob 已提交
944
					} else {
B
Ben Adams 已提交
945

A
aardgoose 已提交
946
						if ( geometryAttribute.isInstancedBufferAttribute ) {
B
Ben Adams 已提交
947

948
							state.enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute );
B
Ben Adams 已提交
949

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

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

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

M
Mr.doob 已提交
956 957 958 959
						} else {

							state.enableAttribute( programAttribute );

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

M
Mr.doob 已提交
962
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
963
						_gl.vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 );
M
Mr.doob 已提交
964

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

967 968
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
969
					var value = materialDefaultAttributeValues[ name ];
970

971
					if ( value !== undefined ) {
M
Mr.doob 已提交
972

973
						switch ( value.length ) {
M
Mr.doob 已提交
974

975 976 977
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
978

979 980 981
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
982

983 984 985
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
986

987 988
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
989 990

						}
M
Mr.doob 已提交
991 992 993 994 995 996 997 998

					}

				}

			}

		}
999

1000
		state.disableUnusedAttributes();
1001

M
Mr.doob 已提交
1002 1003
	}

M
Mr.doob 已提交
1004
	// Compile
M
Mr.doob 已提交
1005

M
Mr.doob 已提交
1006
	this.compile = function ( scene, camera ) {
1007

M
Mr.doob 已提交
1008
		currentRenderState = renderStates.get( scene, camera );
1009
		currentRenderState.init();
1010

M
Mr.doob 已提交
1011
		scene.traverse( function ( object ) {
G
gero3 已提交
1012 1013

			if ( object.isLight ) {
M
Mr.doob 已提交
1014

1015
				currentRenderState.pushLight( object );
1016 1017 1018

				if ( object.castShadow ) {

1019
					currentRenderState.pushShadow( object );
1020 1021

				}
M
Mr.doob 已提交
1022

G
gero3 已提交
1023
			}
M
Mr.doob 已提交
1024 1025 1026

		} );

1027
		currentRenderState.setupLights( camera );
M
Mr.doob 已提交
1028 1029 1030 1031 1032

		scene.traverse( function ( object ) {

			if ( object.material ) {

G
gero3 已提交
1033
				if ( Array.isArray( object.material ) ) {
M
Mr.doob 已提交
1034

G
gero3 已提交
1035
					for ( var i = 0; i < object.material.length; i ++ ) {
M
Mr.doob 已提交
1036 1037 1038

						initMaterial( object.material[ i ], scene.fog, object );

G
gero3 已提交
1039
					}
M
Mr.doob 已提交
1040

G
gero3 已提交
1041
				} else {
M
Mr.doob 已提交
1042 1043 1044

					initMaterial( object.material, scene.fog, object );

G
gero3 已提交
1045
				}
M
Mr.doob 已提交
1046

G
gero3 已提交
1047
			}
M
Mr.doob 已提交
1048 1049

		} );
G
gero3 已提交
1050 1051

	};
1052

1053
	// Animation Loop
M
Mr.doob 已提交
1054

M
Mr.doob 已提交
1055
	var onAnimationFrameCallback = null;
1056

1057
	function onAnimationFrame( time ) {
1058

M
Mr.doob 已提交
1059
		if ( vr.isPresenting() ) return;
1060
		if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
1061

1062
	}
1063

M
Mr.doob 已提交
1064 1065
	var animation = new WebGLAnimation();
	animation.setAnimationLoop( onAnimationFrame );
1066 1067

	if ( typeof window !== 'undefined' ) animation.setContext( window );
1068

1069
	this.setAnimationLoop = function ( callback ) {
1070

M
Mr.doob 已提交
1071 1072
		onAnimationFrameCallback = callback;
		vr.setAnimationLoop( callback );
1073

1074 1075
		animation.start();

1076 1077
	};

M
Mr.doob 已提交
1078 1079
	// Rendering

1080 1081
	this.render = function ( scene, camera ) {

M
Mr.doob 已提交
1082
		var renderTarget, forceClear;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

		if ( arguments[ 2 ] !== undefined ) {

			console.warn( 'THREE.WebGLRenderer.render(): the renderTarget argument has been removed. Use .setRenderTarget() instead.' );
			renderTarget = arguments[ 2 ];

		}

		if ( arguments[ 3 ] !== undefined ) {

			console.warn( 'THREE.WebGLRenderer.render(): the forceClear argument has been removed. Use .clear() instead.' );
			forceClear = arguments[ 3 ];

		}
1097

0
06wj 已提交
1098
		if ( ! ( camera && camera.isCamera ) ) {
M
Mr.doob 已提交
1099

1100
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1101 1102 1103 1104
			return;

		}

1105 1106
		if ( _isContextLost ) return;

M
Mr.doob 已提交
1107 1108
		// reset caching for this frame

M
Mr.doob 已提交
1109 1110
		_currentGeometryProgram.geometry = null;
		_currentGeometryProgram.program = null;
1111
		_currentGeometryProgram.wireframe = false;
1112
		_currentMaterialId = - 1;
1113
		_currentCamera = null;
M
Mr.doob 已提交
1114 1115 1116

		// update scene graph

1117
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1118 1119 1120

		// update camera matrices and frustum

1121
		if ( camera.parent === null ) camera.updateMatrixWorld();
M
Mr.doob 已提交
1122

1123 1124 1125 1126 1127 1128
		if ( vr.enabled ) {

			camera = vr.getCamera( camera );

		}

1129 1130
		//

1131 1132
		currentRenderState = renderStates.get( scene, camera );
		currentRenderState.init();
1133

M
Marc-Sefan Cassola 已提交
1134
		scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget );
1135

M
Mr.doob 已提交
1136 1137 1138
		_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
		_frustum.setFromMatrix( _projScreenMatrix );

T
tschw 已提交
1139
		_localClippingEnabled = this.localClippingEnabled;
M
Mr.doob 已提交
1140
		_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1141

1142 1143 1144
		currentRenderList = renderLists.get( scene, camera );
		currentRenderList.init();

1145
		projectObject( scene, camera, 0, _this.sortObjects );
M
Mr.doob 已提交
1146

M
Mr.doob 已提交
1147
		if ( _this.sortObjects === true ) {
1148

1149
			currentRenderList.sort();
M
Mr.doob 已提交
1150

1151 1152
		}

M
Mr.doob 已提交
1153
		//
M
Mr.doob 已提交
1154

T
tschw 已提交
1155
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1156

1157
		var shadowsArray = currentRenderState.state.shadowsArray;
1158

1159
		shadowMap.render( shadowsArray, scene, camera );
1160

1161
		currentRenderState.setupLights( camera );
1162

T
tschw 已提交
1163
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1164

M
Mr.doob 已提交
1165 1166
		//

A
Atrahasis 已提交
1167
		if ( this.info.autoReset ) this.info.reset();
M
Mr.doob 已提交
1168

1169
		if ( renderTarget !== undefined ) {
1170

1171
			this.setRenderTarget( renderTarget );
1172 1173 1174

		}

M
Mr.doob 已提交
1175 1176
		//

1177
		background.render( currentRenderList, scene, camera, forceClear );
M
Mr.doob 已提交
1178

1179
		// render scene
M
Mr.doob 已提交
1180

1181 1182 1183
		var opaqueObjects = currentRenderList.opaque;
		var transparentObjects = currentRenderList.transparent;

M
Mr.doob 已提交
1184 1185
		if ( scene.overrideMaterial ) {

1186
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1187

M
Mr.doob 已提交
1188 1189
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera, overrideMaterial );
			if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera, overrideMaterial );
1190

M
Mr.doob 已提交
1191 1192
		} else {

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

M
Mr.doob 已提交
1195
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera );
1196 1197 1198

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

M
Mr.doob 已提交
1199
			if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera );
M
Mr.doob 已提交
1200

1201
		}
M
Mr.doob 已提交
1202

1203
		//
M
Mr.doob 已提交
1204

1205 1206 1207 1208
		scene.onAfterRender( _this, scene, camera );

		//

M
Marc-Sefan Cassola 已提交
1209
		if ( _currentRenderTarget !== null ) {
M
Mr.doob 已提交
1210

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

1213
			textures.updateRenderTargetMipmap( _currentRenderTarget );
M
Mr.doob 已提交
1214

M
Mugen87 已提交
1215
			// resolve multisample renderbuffers to a single-sample texture if necessary
1216

1217
			textures.updateMultisampleRenderTarget( _currentRenderTarget );
1218

M
Mr.doob 已提交
1219 1220
		}

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

1223 1224 1225
		state.buffers.depth.setTest( true );
		state.buffers.depth.setMask( true );
		state.buffers.color.setMask( true );
M
Mr.doob 已提交
1226

1227
		state.setPolygonOffset( false );
1228

M
Mr.doob 已提交
1229
		if ( vr.enabled ) {
1230

1231
			vr.submitFrame();
1232

M
Mr.doob 已提交
1233
		}
M
Mr.doob 已提交
1234

M
Mr.doob 已提交
1235 1236
		// _gl.finish();

1237
		currentRenderList = null;
1238
		currentRenderState = null;
1239

M
Mr.doob 已提交
1240
	};
M
Mr.doob 已提交
1241

1242
	function projectObject( object, camera, groupOrder, sortObjects ) {
M
Mr.doob 已提交
1243

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

1246
		var visible = object.layers.test( camera.layers );
M
Mr.doob 已提交
1247

1248
		if ( visible ) {
1249

1250 1251 1252 1253 1254
			if ( object.isGroup ) {

				groupOrder = object.renderOrder;

			} else if ( object.isLight ) {
M
Mr.doob 已提交
1255

1256
				currentRenderState.pushLight( object );
1257 1258 1259

				if ( object.castShadow ) {

1260
					currentRenderState.pushShadow( object );
1261 1262

				}
M
Mr.doob 已提交
1263

1264
			} else if ( object.isSprite ) {
M
Mr.doob 已提交
1265

1266
				if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
1267

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
					if ( sortObjects ) {

						_vector3.setFromMatrixPosition( object.matrixWorld )
							.applyMatrix4( _projScreenMatrix );

					}

					var geometry = objects.update( object );
					var material = object.material;

1278 1279 1280 1281 1282
					if ( material.visible ) {

						currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );

					}
M
Mr.doob 已提交
1283

1284
				}
M
Mr.doob 已提交
1285

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

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

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

1293
				}
M
Mr.doob 已提交
1294

1295
				currentRenderList.push( object, null, object.material, groupOrder, _vector3.z, null );
1296

1297
			} else if ( object.isMesh || object.isLine || object.isPoints ) {
1298

1299
				if ( object.isSkinnedMesh ) {
1300

1301
					object.skeleton.update();
1302

1303
				}
1304

1305
				if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
1306

1307 1308 1309 1310 1311 1312
					if ( sortObjects ) {

						_vector3.setFromMatrixPosition( object.matrixWorld )
							.applyMatrix4( _projScreenMatrix );

					}
1313

1314 1315
					var geometry = objects.update( object );
					var material = object.material;
1316

1317
					if ( Array.isArray( material ) ) {
1318

1319
						var groups = geometry.groups;
1320

1321
						for ( var i = 0, l = groups.length; i < l; i ++ ) {
M
Mr.doob 已提交
1322

1323 1324
							var group = groups[ i ];
							var groupMaterial = material[ group.materialIndex ];
1325

1326
							if ( groupMaterial && groupMaterial.visible ) {
1327

1328
								currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
1329 1330

							}
M
Mr.doob 已提交
1331

M
Mr.doob 已提交
1332
						}
M
Mr.doob 已提交
1333

1334
					} else if ( material.visible ) {
1335

1336
						currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
O
OpenShift guest 已提交
1337

1338
					}
M
Mr.doob 已提交
1339

1340
				}
M
Mr.doob 已提交
1341

1342
			}
M
Mr.doob 已提交
1343

M
Mr.doob 已提交
1344
		}
M
Mr.doob 已提交
1345

M
Mr.doob 已提交
1346
		var children = object.children;
M
Mr.doob 已提交
1347

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

1350
			projectObject( children[ i ], camera, groupOrder, sortObjects );
M
Mr.doob 已提交
1351

1352
		}
1353

1354
	}
M
Mr.doob 已提交
1355

1356
	function renderObjects( renderList, scene, camera, overrideMaterial ) {
M
Mr.doob 已提交
1357

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

1360
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1361

1362
			var object = renderItem.object;
M
Mr.doob 已提交
1363 1364 1365
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1366

M
Mr.doob 已提交
1367
			if ( camera.isArrayCamera ) {
M
Mr.doob 已提交
1368

1369 1370
				_currentArrayCamera = camera;

M
Mr.doob 已提交
1371
				var cameras = camera.cameras;
M
Mr.doob 已提交
1372

M
Mr.doob 已提交
1373
				for ( var j = 0, jl = cameras.length; j < jl; j ++ ) {
M
Mr.doob 已提交
1374

M
Mr.doob 已提交
1375
					var camera2 = cameras[ j ];
M
Mr.doob 已提交
1376

1377
					if ( object.layers.test( camera2.layers ) ) {
1378

M
Mr.doob 已提交
1379 1380 1381 1382 1383 1384 1385
						if ( 'viewport' in camera2 ) { // XR

							state.viewport( _currentViewport.copy( camera2.viewport ) );

						} else {

							var bounds = camera2.bounds;
1386

M
Mr.doob 已提交
1387 1388 1389 1390
							var x = bounds.x * _width;
							var y = bounds.y * _height;
							var width = bounds.z * _width;
							var height = bounds.w * _height;
M
Mr.doob 已提交
1391

M
Mr.doob 已提交
1392 1393 1394
							state.viewport( _currentViewport.set( x, y, width, height ).multiplyScalar( _pixelRatio ) );

						}
1395

1396 1397
						currentRenderState.setupLights( camera2 );

1398 1399 1400
						renderObject( object, scene, camera2, geometry, material, group );

					}
M
Mr.doob 已提交
1401

M
Mr.doob 已提交
1402
				}
1403

M
Mr.doob 已提交
1404
			} else {
M
Mr.doob 已提交
1405

1406 1407
				_currentArrayCamera = null;

M
Mr.doob 已提交
1408
				renderObject( object, scene, camera, geometry, material, group );
M
Mr.doob 已提交
1409

M
Mr.doob 已提交
1410
			}
M
Mr.doob 已提交
1411

1412
		}
M
Mr.doob 已提交
1413

1414
	}
G
gero3 已提交
1415

M
Mr.doob 已提交
1416 1417
	function renderObject( object, scene, camera, geometry, material, group ) {

M
Mugen87 已提交
1418
		object.onBeforeRender( _this, scene, camera, geometry, material, group );
1419
		currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
1420

M
Mr.doob 已提交
1421 1422 1423 1424 1425
		object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
		object.normalMatrix.getNormalMatrix( object.modelViewMatrix );

		if ( object.isImmediateRenderObject ) {

1426
			state.setMaterial( material );
M
Mr.doob 已提交
1427 1428 1429

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

M
Mr.doob 已提交
1430 1431
			_currentGeometryProgram.geometry = null;
			_currentGeometryProgram.program = null;
1432
			_currentGeometryProgram.wireframe = false;
M
Mr.doob 已提交
1433

M
Mr.doob 已提交
1434
			renderObjectImmediate( object, program );
M
Mr.doob 已提交
1435 1436 1437

		} else {

M
Mugen87 已提交
1438
			_this.renderBufferDirect( camera, scene.fog, geometry, material, object, group );
M
Mr.doob 已提交
1439 1440 1441

		}

M
Mr.doob 已提交
1442
		object.onAfterRender( _this, scene, camera, geometry, material, group );
1443
		currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
M
Mr.doob 已提交
1444

M
Mr.doob 已提交
1445 1446
	}

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

1449
		var materialProperties = properties.get( material );
G
gero3 已提交
1450

1451 1452
		var lights = currentRenderState.state.lights;
		var shadowsArray = currentRenderState.state.shadowsArray;
1453

M
Mr.doob 已提交
1454 1455 1456
		var lightsHash = materialProperties.lightsHash;
		var lightsStateHash = lights.state.hash;

T
tschw 已提交
1457
		var parameters = programCache.getParameters(
1458
			material, lights.state, shadowsArray, fog, _clipping.numPlanes, _clipping.numIntersection, object );
T
tschw 已提交
1459

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

1462
		var program = materialProperties.program;
T
tschw 已提交
1463
		var programChange = true;
1464

1465
		if ( program === undefined ) {
B
Ben Adams 已提交
1466

M
Mr.doob 已提交
1467 1468
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1469

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

M
Mr.doob 已提交
1472
			// changed glsl or parameters
1473
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1474

M
Mr.doob 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
		} else if ( lightsHash.stateID !== lightsStateHash.stateID ||
			lightsHash.directionalLength !== lightsStateHash.directionalLength ||
			lightsHash.pointLength !== lightsStateHash.pointLength ||
			lightsHash.spotLength !== lightsStateHash.spotLength ||
			lightsHash.rectAreaLength !== lightsStateHash.rectAreaLength ||
			lightsHash.hemiLength !== lightsStateHash.hemiLength ||
			lightsHash.shadowsLength !== lightsStateHash.shadowsLength ) {

			lightsHash.stateID = lightsStateHash.stateID;
			lightsHash.directionalLength = lightsStateHash.directionalLength;
			lightsHash.pointLength = lightsStateHash.pointLength;
			lightsHash.spotLength = lightsStateHash.spotLength;
			lightsHash.rectAreaLength = lightsStateHash.rectAreaLength;
			lightsHash.hemiLength = lightsStateHash.hemiLength;
			lightsHash.shadowsLength = lightsStateHash.shadowsLength;
1490 1491 1492

			programChange = false;

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

T
tschw 已提交
1495
			// same glsl and uniform list
T
tschw 已提交
1496 1497
			return;

T
tschw 已提交
1498
		} else {
B
Ben Adams 已提交
1499

T
tschw 已提交
1500 1501
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1502 1503 1504

		}

1505
		if ( programChange ) {
B
Ben Adams 已提交
1506

1507
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1508

R
Rich Harris 已提交
1509
				var shader = ShaderLib[ parameters.shaderID ];
B
Ben Adams 已提交
1510

1511
				materialProperties.shader = {
1512
					name: material.type,
M
Mr.doob 已提交
1513
					uniforms: cloneUniforms( shader.uniforms ),
1514
					vertexShader: shader.vertexShader,
1515
					fragmentShader: shader.fragmentShader
1516
				};
B
Ben Adams 已提交
1517

1518
			} else {
B
Ben Adams 已提交
1519

1520
				materialProperties.shader = {
1521 1522 1523
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
1524
					fragmentShader: material.fragmentShader
1525
				};
G
gero3 已提交
1526

1527
			}
G
gero3 已提交
1528

1529
			material.onBeforeCompile( materialProperties.shader, _this );
G
gero3 已提交
1530

1531
			// Computing code again as onBeforeCompile may have changed the shaders
1532 1533
			code = programCache.getProgramCode( material, parameters );

1534
			program = programCache.acquireProgram( material, materialProperties.shader, parameters, code );
B
Ben Adams 已提交
1535

1536 1537
			materialProperties.program = program;
			material.program = program;
1538 1539 1540

		}

1541
		var programAttributes = program.getAttributes();
M
Mr.doob 已提交
1542 1543 1544 1545 1546

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

1549
				if ( programAttributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

1565
				if ( programAttributes[ 'morphNormal' + i ] >= 0 ) {
M
Mr.doob 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574

					material.numSupportedMorphNormals ++;

				}

			}

		}

1575
		var uniforms = materialProperties.shader.uniforms;
T
tschw 已提交
1576

1577
		if ( ! material.isShaderMaterial &&
1578 1579
			! material.isRawShaderMaterial ||
			material.clipping === true ) {
T
tschw 已提交
1580

T
tschw 已提交
1581
			materialProperties.numClippingPlanes = _clipping.numPlanes;
1582
			materialProperties.numIntersection = _clipping.numIntersection;
T
tschw 已提交
1583
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1584 1585 1586

		}

1587
		materialProperties.fog = fog;
1588

1589
		// store the light setup it was created for
M
Mr.doob 已提交
1590
		if ( lightsHash === undefined ) {
1591

M
Mr.doob 已提交
1592
			materialProperties.lightsHash = lightsHash = {};
1593 1594 1595

		}

M
Mr.doob 已提交
1596 1597 1598 1599 1600 1601 1602
		lightsHash.stateID = lightsStateHash.stateID;
		lightsHash.directionalLength = lightsStateHash.directionalLength;
		lightsHash.pointLength = lightsStateHash.pointLength;
		lightsHash.spotLength = lightsStateHash.spotLength;
		lightsHash.rectAreaLength = lightsStateHash.rectAreaLength;
		lightsHash.hemiLength = lightsStateHash.hemiLength;
		lightsHash.shadowsLength = lightsStateHash.shadowsLength;
1603

M
Mr.doob 已提交
1604
		if ( material.lights ) {
1605 1606 1607

			// wire up the material to this renderer's lighting state

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
			uniforms.ambientLightColor.value = lights.state.ambient;
			uniforms.directionalLights.value = lights.state.directional;
			uniforms.spotLights.value = lights.state.spot;
			uniforms.rectAreaLights.value = lights.state.rectArea;
			uniforms.pointLights.value = lights.state.point;
			uniforms.hemisphereLights.value = lights.state.hemi;

			uniforms.directionalShadowMap.value = lights.state.directionalShadowMap;
			uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix;
			uniforms.spotShadowMap.value = lights.state.spotShadowMap;
			uniforms.spotShadowMatrix.value = lights.state.spotShadowMatrix;
			uniforms.pointShadowMap.value = lights.state.pointShadowMap;
			uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix;
1621
			// TODO (abelnation): add area lights shadow info to uniforms
1622

1623 1624
		}

T
tschw 已提交
1625 1626
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
1627
				WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1628

T
tschw 已提交
1629
		materialProperties.uniformsList = uniformsList;
A
arose 已提交
1630

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

1633
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1634

1635
		textures.resetTextureUnits();
M
Mr.doob 已提交
1636

1637
		var materialProperties = properties.get( material );
1638
		var lights = currentRenderState.state.lights;
1639

M
Mr.doob 已提交
1640 1641 1642
		var lightsHash = materialProperties.lightsHash;
		var lightsStateHash = lights.state.hash;

T
tschw 已提交
1643 1644 1645 1646 1647
		if ( _clippingEnabled ) {

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

				var useCache =
1648 1649
					camera === _currentCamera &&
					material.id === _currentMaterialId;
T
tschw 已提交
1650 1651 1652 1653

				// we might want to call this function with some ClippingGroup
				// object instead of the material, once it becomes feasible
				// (#8465, #8379)
T
tschw 已提交
1654
				_clipping.setState(
1655 1656
					material.clippingPlanes, material.clipIntersection, material.clipShadows,
					camera, materialProperties, useCache );
T
tschw 已提交
1657 1658 1659 1660 1661

			}

		}

1662
		if ( material.needsUpdate === false ) {
1663

1664
			if ( materialProperties.program === undefined ) {
1665

1666
				material.needsUpdate = true;
1667

1668
			} else if ( material.fog && materialProperties.fog !== fog ) {
1669

M
Mr.doob 已提交
1670
				material.needsUpdate = true;
1671

M
Mr.doob 已提交
1672 1673 1674 1675 1676 1677 1678
			} else if ( material.lights && ( lightsHash.stateID !== lightsStateHash.stateID ||
				lightsHash.directionalLength !== lightsStateHash.directionalLength ||
				lightsHash.pointLength !== lightsStateHash.pointLength ||
				lightsHash.spotLength !== lightsStateHash.spotLength ||
				lightsHash.rectAreaLength !== lightsStateHash.rectAreaLength ||
				lightsHash.hemiLength !== lightsStateHash.hemiLength ||
				lightsHash.shadowsLength !== lightsStateHash.shadowsLength ) ) {
1679

1680
				material.needsUpdate = true;
1681

1682
			} else if ( materialProperties.numClippingPlanes !== undefined &&
1683
				( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
M
Mr.doob 已提交
1684
				materialProperties.numIntersection !== _clipping.numIntersection ) ) {
1685 1686 1687

				material.needsUpdate = true;

1688
			}
1689 1690 1691 1692

		}

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

1694
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1695 1696 1697 1698
			material.needsUpdate = false;

		}

1699
		var refreshProgram = false;
M
Mr.doob 已提交
1700
		var refreshMaterial = false;
1701
		var refreshLights = false;
M
Mr.doob 已提交
1702

1703
		var program = materialProperties.program,
1704
			p_uniforms = program.getUniforms(),
1705
			m_uniforms = materialProperties.shader.uniforms;
M
Mr.doob 已提交
1706

1707
		if ( state.useProgram( program.program ) ) {
M
Mr.doob 已提交
1708

1709
			refreshProgram = true;
M
Mr.doob 已提交
1710
			refreshMaterial = true;
1711
			refreshLights = true;
M
Mr.doob 已提交
1712 1713 1714 1715 1716 1717

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1718

M
Mr.doob 已提交
1719 1720 1721 1722
			refreshMaterial = true;

		}

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

M
Mr.doob 已提交
1725
			p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
M
Mr.doob 已提交
1726

G
gero3 已提交
1727
			if ( capabilities.logarithmicDepthBuffer ) {
1728

T
tschw 已提交
1729
				p_uniforms.setValue( _gl, 'logDepthBufFC',
1730
					2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1731 1732 1733

			}

1734
			if ( _currentCamera !== camera ) {
1735

1736
				_currentCamera = camera;
1737 1738 1739 1740 1741 1742

				// 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 已提交
1743
				refreshLights = true;		// remains set until update done
1744 1745

			}
M
Mr.doob 已提交
1746

1747 1748 1749
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

1750
			if ( material.isShaderMaterial ||
1751 1752 1753
				material.isMeshPhongMaterial ||
				material.isMeshStandardMaterial ||
				material.envMap ) {
1754

T
tschw 已提交
1755 1756 1757
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1758

T
tschw 已提交
1759
					uCamPos.setValue( _gl,
1760
						_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1761 1762 1763 1764 1765

				}

			}

1766
			if ( material.isMeshPhongMaterial ||
1767 1768 1769 1770
				material.isMeshLambertMaterial ||
				material.isMeshBasicMaterial ||
				material.isMeshStandardMaterial ||
				material.isShaderMaterial ||
1771
				material.skinning ) {
1772

T
tschw 已提交
1773
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1774 1775 1776

			}

M
Mr.doob 已提交
1777 1778 1779 1780 1781 1782
		}

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

1783
		if ( material.skinning ) {
M
Mr.doob 已提交
1784

T
tschw 已提交
1785 1786
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1787

T
tschw 已提交
1788
			var skeleton = object.skeleton;
1789

T
tschw 已提交
1790
			if ( skeleton ) {
1791

1792 1793
				var bones = skeleton.bones;

1794
				if ( capabilities.floatVertexTextures ) {
M
Mr.doob 已提交
1795

1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
					if ( skeleton.boneTexture === undefined ) {

						// layout (1 matrix = 4 pixels)
						//      RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
						//  with  8x8  pixel texture max   16 bones * 4 pixels =  (8 * 8)
						//       16x16 pixel texture max   64 bones * 4 pixels = (16 * 16)
						//       32x32 pixel texture max  256 bones * 4 pixels = (32 * 32)
						//       64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)


						var size = Math.sqrt( bones.length * 4 ); // 4 pixels needed for 1 matrix
1807
						size = _Math.ceilPowerOfTwo( size );
1808 1809 1810 1811 1812 1813
						size = Math.max( size, 4 );

						var boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
						boneMatrices.set( skeleton.boneMatrices ); // copy current values

						var boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
1814
						boneTexture.needsUpdate = true;
1815 1816 1817 1818 1819 1820 1821

						skeleton.boneMatrices = boneMatrices;
						skeleton.boneTexture = boneTexture;
						skeleton.boneTextureSize = size;

					}

1822
					p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures );
M
Mr.doob 已提交
1823
					p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
M
Mr.doob 已提交
1824

T
tschw 已提交
1825
				} else {
M
Mr.doob 已提交
1826

T
tschw 已提交
1827
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1828 1829 1830 1831 1832 1833 1834 1835 1836

				}

			}

		}

		if ( refreshMaterial ) {

1837 1838 1839
			p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure );
			p_uniforms.setValue( _gl, 'toneMappingWhitePoint', _this.toneMappingWhitePoint );

M
Mr.doob 已提交
1840
			if ( material.lights ) {
M
Mr.doob 已提交
1841

1842
				// the current material requires lighting info
M
Mr.doob 已提交
1843

T
tschw 已提交
1844 1845 1846 1847 1848 1849
				// 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 已提交
1850

T
tschw 已提交
1851
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1852

T
tschw 已提交
1853
			}
G
gero3 已提交
1854

T
tschw 已提交
1855
			// refresh uniforms common to several materials
G
gero3 已提交
1856

T
tschw 已提交
1857
			if ( fog && material.fog ) {
G
gero3 已提交
1858

T
tschw 已提交
1859
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1860 1861 1862

			}

1863
			if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
1864 1865 1866

				refreshUniformsCommon( m_uniforms, material );

1867
			} else if ( material.isMeshLambertMaterial ) {
M
Mr.doob 已提交
1868

1869 1870
				refreshUniformsCommon( m_uniforms, material );
				refreshUniformsLambert( m_uniforms, material );
M
Mr.doob 已提交
1871

1872
			} else if ( material.isMeshPhongMaterial ) {
M
Mr.doob 已提交
1873

1874
				refreshUniformsCommon( m_uniforms, material );
M
Mr.doob 已提交
1875

1876
				if ( material.isMeshToonMaterial ) {
M
Mr.doob 已提交
1877

1878
					refreshUniformsToon( m_uniforms, material );
M
Mr.doob 已提交
1879

1880
				} else {
1881

1882
					refreshUniformsPhong( m_uniforms, material );
1883

1884
				}
T
Takahiro 已提交
1885

1886
			} else if ( material.isMeshStandardMaterial ) {
T
Takahiro 已提交
1887

1888
				refreshUniformsCommon( m_uniforms, material );
1889

1890
				if ( material.isMeshPhysicalMaterial ) {
1891

1892
					refreshUniformsPhysical( m_uniforms, material );
W
WestLangley 已提交
1893

1894
				} else {
W
WestLangley 已提交
1895

1896
					refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1897

1898
				}
W
WestLangley 已提交
1899

W
WestLangley 已提交
1900 1901 1902 1903 1904 1905
			} else if ( material.isMeshMatcapMaterial ) {

				refreshUniformsCommon( m_uniforms, material );

				refreshUniformsMatcap( m_uniforms, material );

1906
			} else if ( material.isMeshDepthMaterial ) {
M
Mr.doob 已提交
1907

1908
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1909
				refreshUniformsDepth( m_uniforms, material );
1910

W
WestLangley 已提交
1911
			} else if ( material.isMeshDistanceMaterial ) {
1912

1913
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1914
				refreshUniformsDistance( m_uniforms, material );
1915

1916
			} else if ( material.isMeshNormalMaterial ) {
M
Mr.doob 已提交
1917

1918
				refreshUniformsCommon( m_uniforms, material );
1919
				refreshUniformsNormal( m_uniforms, material );
M
Mr.doob 已提交
1920

1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
			} else if ( material.isLineBasicMaterial ) {

				refreshUniformsLine( m_uniforms, material );

				if ( material.isLineDashedMaterial ) {

					refreshUniformsDash( m_uniforms, material );

				}

			} else if ( material.isPointsMaterial ) {

				refreshUniformsPoints( m_uniforms, material );

1935 1936 1937 1938
			} else if ( material.isSpriteMaterial ) {

				refreshUniformsSprites( m_uniforms, material );

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

				m_uniforms.color.value = material.color;
				m_uniforms.opacity.value = material.opacity;

M
Mr.doob 已提交
1944 1945
			}

M
Mr.doob 已提交
1946 1947 1948
			// RectAreaLight Texture
			// TODO (mrdoob): Find a nicer implementation

1949 1950
			if ( m_uniforms.ltc_1 !== undefined ) m_uniforms.ltc_1.value = UniformsLib.LTC_1;
			if ( m_uniforms.ltc_2 !== undefined ) m_uniforms.ltc_2.value = UniformsLib.LTC_2;
M
Mr.doob 已提交
1951

1952
			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
A
arose 已提交
1953 1954 1955

		}

1956 1957
		if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {

1958
			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
1959 1960 1961
			material.uniformsNeedUpdate = false;

		}
M
Mr.doob 已提交
1962

1963 1964 1965 1966 1967 1968
		if ( material.isSpriteMaterial ) {

			p_uniforms.setValue( _gl, 'center', object.center );

		}

T
tschw 已提交
1969
		// common matrices
M
Mr.doob 已提交
1970

M
Mr.doob 已提交
1971 1972
		p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
		p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix );
T
tschw 已提交
1973
		p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
A
arose 已提交
1974

T
tschw 已提交
1975
		return program;
A
arose 已提交
1976 1977 1978

	}

M
Mr.doob 已提交
1979 1980
	// Uniforms (refresh uniforms objects)

M
Mr.doob 已提交
1981
	function refreshUniformsCommon( uniforms, material ) {
M
Mr.doob 已提交
1982 1983 1984

		uniforms.opacity.value = material.opacity;

1985 1986 1987 1988 1989
		if ( material.color ) {

			uniforms.diffuse.value = material.color;

		}
M
Mr.doob 已提交
1990

1991
		if ( material.emissive ) {
M
Mr.doob 已提交
1992

1993
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1994 1995 1996

		}

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
		if ( material.map ) {

			uniforms.map.value = material.map;

		}

		if ( material.alphaMap ) {

			uniforms.alphaMap.value = material.alphaMap;

		}

		if ( material.specularMap ) {

			uniforms.specularMap.value = material.specularMap;

		}

		if ( material.envMap ) {

			uniforms.envMap.value = material.envMap;

			// 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
0
06wj 已提交
2023
			uniforms.flipEnvMap.value = material.envMap.isCubeTexture ? - 1 : 1;
2024 2025 2026 2027

			uniforms.reflectivity.value = material.reflectivity;
			uniforms.refractionRatio.value = material.refractionRatio;

2028
			uniforms.maxMipLevel.value = properties.get( material.envMap ).__maxMipLevel;
2029

2030
		}
M
Mr.doob 已提交
2031

2032 2033 2034 2035 2036 2037 2038
		if ( material.lightMap ) {

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

		}

2039
		if ( material.aoMap ) {
2040

2041 2042
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
2043 2044 2045

		}

M
Mr.doob 已提交
2046
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
2047 2048 2049 2050 2051
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
2052
		// 6. emissive map
M
Mr.doob 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

2064 2065 2066 2067
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
2068 2069 2070 2071 2072 2073 2074 2075
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

2076 2077 2078 2079 2080 2081 2082 2083
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

2084 2085 2086 2087
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

2088 2089 2090 2091
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
2092 2093 2094 2095
		}

		if ( uvScaleMap !== undefined ) {

2096
			// backwards compatibility
2097
			if ( uvScaleMap.isWebGLRenderTarget ) {
M
Mr.doob 已提交
2098 2099 2100 2101 2102

				uvScaleMap = uvScaleMap.texture;

			}

W
WestLangley 已提交
2103
			if ( uvScaleMap.matrixAutoUpdate === true ) {
M
Mr.doob 已提交
2104

W
WestLangley 已提交
2105
				uvScaleMap.updateMatrix();
M
Mr.doob 已提交
2106

W
WestLangley 已提交
2107
			}
2108

2109
			uniforms.uvTransform.value.copy( uvScaleMap.matrix );
M
Mr.doob 已提交
2110 2111 2112

		}

M
Mr.doob 已提交
2113
	}
M
Mr.doob 已提交
2114

M
Mr.doob 已提交
2115
	function refreshUniformsLine( uniforms, material ) {
M
Mr.doob 已提交
2116 2117 2118 2119

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

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

M
Mr.doob 已提交
2122
	function refreshUniformsDash( uniforms, material ) {
M
Mr.doob 已提交
2123 2124 2125 2126 2127

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

M
Mr.doob 已提交
2128
	}
M
Mr.doob 已提交
2129

M
Mr.doob 已提交
2130
	function refreshUniformsPoints( uniforms, material ) {
M
Mr.doob 已提交
2131

2132
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2133
		uniforms.opacity.value = material.opacity;
2134
		uniforms.size.value = material.size * _pixelRatio;
2135
		uniforms.scale.value = _height * 0.5;
M
Mr.doob 已提交
2136 2137 2138

		uniforms.map.value = material.map;

2139 2140
		if ( material.map !== null ) {

W
WestLangley 已提交
2141
			if ( material.map.matrixAutoUpdate === true ) {
2142

W
WestLangley 已提交
2143
				material.map.updateMatrix();
W
WestLangley 已提交
2144 2145

			}
2146

2147
			uniforms.uvTransform.value.copy( material.map.matrix );
2148 2149 2150

		}

2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
	}

	function refreshUniformsSprites( uniforms, material ) {

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

		if ( material.map !== null ) {

			if ( material.map.matrixAutoUpdate === true ) {

				material.map.updateMatrix();

			}

			uniforms.uvTransform.value.copy( material.map.matrix );

		}

M
Mr.doob 已提交
2172
	}
M
Mr.doob 已提交
2173

M
Mr.doob 已提交
2174
	function refreshUniformsFog( uniforms, fog ) {
M
Mr.doob 已提交
2175 2176 2177

		uniforms.fogColor.value = fog.color;

2178
		if ( fog.isFog ) {
M
Mr.doob 已提交
2179 2180 2181 2182

			uniforms.fogNear.value = fog.near;
			uniforms.fogFar.value = fog.far;

2183
		} else if ( fog.isFogExp2 ) {
M
Mr.doob 已提交
2184 2185 2186 2187 2188

			uniforms.fogDensity.value = fog.density;

		}

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

M
Mr.doob 已提交
2191
	function refreshUniformsLambert( uniforms, material ) {
2192 2193 2194 2195 2196 2197 2198 2199 2200

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2201
	function refreshUniformsPhong( uniforms, material ) {
M
Mr.doob 已提交
2202

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

2206
		if ( material.emissiveMap ) {
2207

2208
			uniforms.emissiveMap.value = material.emissiveMap;
2209

2210
		}
M
Mr.doob 已提交
2211

2212 2213 2214 2215
		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
2216
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;
M
Mr.doob 已提交
2217

2218
		}
M
Mr.doob 已提交
2219

2220 2221 2222 2223
		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
2224
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();
2225 2226

		}
M
Mr.doob 已提交
2227

2228 2229 2230 2231 2232
		if ( material.displacementMap ) {

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

2234
		}
2235

T
Takahiro 已提交
2236 2237 2238 2239 2240 2241
	}

	function refreshUniformsToon( uniforms, material ) {

		refreshUniformsPhong( uniforms, material );

T
Takahiro 已提交
2242
		if ( material.gradientMap ) {
T
Takahiro 已提交
2243

T
Takahiro 已提交
2244
			uniforms.gradientMap.value = material.gradientMap;
T
Takahiro 已提交
2245 2246 2247

		}

2248 2249
	}

M
Mr.doob 已提交
2250
	function refreshUniformsStandard( uniforms, material ) {
W
WestLangley 已提交
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

		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.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
2277
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;
W
WestLangley 已提交
2278 2279 2280 2281 2282 2283 2284

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
2285
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();
W
WestLangley 已提交
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305

		}

		if ( material.displacementMap ) {

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

		}

		if ( material.envMap ) {

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

		}

	}

M
Mr.doob 已提交
2306
	function refreshUniformsPhysical( uniforms, material ) {
W
WestLangley 已提交
2307

2308 2309 2310 2311
		refreshUniformsStandard( uniforms, material );

		uniforms.reflectivity.value = material.reflectivity; // also part of uniforms common

2312 2313 2314
		uniforms.clearCoat.value = material.clearCoat;
		uniforms.clearCoatRoughness.value = material.clearCoatRoughness;

W
WestLangley 已提交
2315 2316
	}

W
WestLangley 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
	function refreshUniformsMatcap( uniforms, material ) {

		if ( material.matcap ) {

			uniforms.matcap.value = material.matcap;

		}

		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();

		}

		if ( material.displacementMap ) {

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

		}

	}

W
WestLangley 已提交
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
	function refreshUniformsDepth( uniforms, material ) {

		if ( material.displacementMap ) {

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

		}

	}

	function refreshUniformsDistance( uniforms, material ) {

		if ( material.displacementMap ) {

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

		}

		uniforms.referencePosition.value.copy( material.referencePosition );
		uniforms.nearDistance.value = material.nearDistance;
		uniforms.farDistance.value = material.farDistance;

	}

2379 2380 2381 2382 2383 2384
	function refreshUniformsNormal( uniforms, material ) {

		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
2385
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;
2386 2387 2388 2389 2390 2391 2392

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
2393
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406

		}

		if ( material.displacementMap ) {

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

		}

	}

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

M
Mr.doob 已提交
2409
	function markUniformsLightsNeedsUpdate( uniforms, value ) {
2410

M
Mr.doob 已提交
2411
		uniforms.ambientLightColor.needsUpdate = value;
2412

B
Ben Houston 已提交
2413 2414 2415
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
2416
		uniforms.rectAreaLights.needsUpdate = value;
B
Ben Houston 已提交
2417
		uniforms.hemisphereLights.needsUpdate = value;
2418

M
Mr.doob 已提交
2419
	}
2420

2421 2422 2423
	//
	this.setFramebuffer = function ( value ) {

2424 2425
		if (_framebuffer !== value ) _gl.bindFramebuffer( _gl.FRAMEBUFFER, value );

2426 2427 2428 2429
		_framebuffer = value;

	};

2430
	this.getRenderTarget = function () {
2431 2432 2433

		return _currentRenderTarget;

M
Michael Herzog 已提交
2434
	};
2435

2436
	this.setRenderTarget = function ( renderTarget, activeCubeFace, activeMipMapLevel ) {
M
Mr.doob 已提交
2437

2438 2439
		_currentRenderTarget = renderTarget;

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

2442
			textures.setupRenderTarget( renderTarget );
M
Mr.doob 已提交
2443 2444 2445

		}

2446
		var framebuffer = _framebuffer;
M
Mr.doob 已提交
2447
		var isCube = false;
M
Mr.doob 已提交
2448 2449 2450

		if ( renderTarget ) {

M
Mr.doob 已提交
2451
			var __webglFramebuffer = properties.get( renderTarget ).__webglFramebuffer;
F
Fordy 已提交
2452

M
Mr.doob 已提交
2453
			if ( renderTarget.isWebGLRenderTargetCube ) {
M
Mr.doob 已提交
2454

2455
				framebuffer = __webglFramebuffer[ activeCubeFace || 0 ];
M
Mr.doob 已提交
2456
				isCube = true;
M
Mr.doob 已提交
2457

2458 2459 2460 2461
			} else if ( renderTarget.isWebGLMultisampleRenderTarget ) {

				framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer;

M
Mr.doob 已提交
2462 2463
			} else {

M
Mr.doob 已提交
2464
				framebuffer = __webglFramebuffer;
M
Mr.doob 已提交
2465 2466 2467

			}

M
Mr.doob 已提交
2468
			_currentViewport.copy( renderTarget.viewport );
2469 2470
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
2471

M
Mr.doob 已提交
2472 2473
		} else {

M
Mr.doob 已提交
2474
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
2475
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
2476
			_currentScissorTest = _scissorTest;
2477

M
Mr.doob 已提交
2478 2479
		}

M
Mr.doob 已提交
2480
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
2481 2482 2483 2484 2485 2486

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

		}

M
Mr.doob 已提交
2487
		state.viewport( _currentViewport );
2488 2489
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
2490

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

			var textureProperties = properties.get( renderTarget.texture );
2494
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + ( activeCubeFace || 0 ), textureProperties.__webglTexture, activeMipMapLevel || 0 );
M
Mr.doob 已提交
2495 2496 2497

		}

M
Mr.doob 已提交
2498 2499
	};

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

0
06wj 已提交
2502
		if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
2503

2504
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
2505
			return;
2506

G
gero3 已提交
2507
		}
2508

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

M
Mr.doob 已提交
2511
		if ( framebuffer ) {
2512

G
gero3 已提交
2513
			var restore = false;
2514

M
Mr.doob 已提交
2515
			if ( framebuffer !== _currentFramebuffer ) {
2516

M
Mr.doob 已提交
2517
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
2518

G
gero3 已提交
2519
				restore = true;
2520

G
gero3 已提交
2521
			}
2522

M
Mr.doob 已提交
2523
			try {
2524

M
Mr.doob 已提交
2525
				var texture = renderTarget.texture;
M
Mr.doob 已提交
2526 2527
				var textureFormat = texture.format;
				var textureType = texture.type;
2528

2529
				if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
2530

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

M
Mr.doob 已提交
2534
				}
2535

2536
				if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // IE11, Edge and Chrome Mac < 52 (#9513)
T
Takahiro 已提交
2537 2538
					! ( textureType === FloatType && ( capabilities.isWebGL2 || extensions.get( 'OES_texture_float' ) || extensions.get( 'WEBGL_color_buffer_float' ) ) ) && // Chrome Mac >= 52 and Firefox
					! ( textureType === HalfFloatType && ( capabilities.isWebGL2 ? extensions.get( 'EXT_color_buffer_float' ) : extensions.get( 'EXT_color_buffer_half_float' ) ) ) ) {
2539

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

M
Mr.doob 已提交
2543
				}
2544

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

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

2549
					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
2550

2551
						_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
2552 2553

					}
2554

M
Mr.doob 已提交
2555
				} else {
M
Mr.doob 已提交
2556

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

				}
M
Mr.doob 已提交
2560

M
Mr.doob 已提交
2561
			} finally {
M
Mr.doob 已提交
2562

M
Mr.doob 已提交
2563 2564 2565
				if ( restore ) {

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

M
Mr.doob 已提交
2567 2568 2569
				}

			}
M
Mr.doob 已提交
2570 2571 2572

		}

M
Mr.doob 已提交
2573 2574
	};

2575
	this.copyFramebufferToTexture = function ( position, texture, level ) {
2576 2577 2578

		var width = texture.image.width;
		var height = texture.image.height;
2579
		var glFormat = utils.convert( texture.format );
2580

2581
		textures.setTexture2D( texture, 0 );
2582

2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
		_gl.copyTexImage2D( _gl.TEXTURE_2D, level || 0, glFormat, position.x, position.y, width, height, 0 );

	};

	this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level ) {

		var width = srcTexture.image.width;
		var height = srcTexture.image.height;
		var glFormat = utils.convert( dstTexture.format );
		var glType = utils.convert( dstTexture.type );

2594
		textures.setTexture2D( dstTexture, 0 );
2595

2596 2597 2598 2599 2600 2601 2602 2603 2604
		if ( srcTexture.isDataTexture ) {

			_gl.texSubImage2D( _gl.TEXTURE_2D, level || 0, position.x, position.y, width, height, glFormat, glType, srcTexture.image.data );

		} else {

			_gl.texSubImage2D( _gl.TEXTURE_2D, level || 0, position.x, position.y, glFormat, glType, srcTexture.image );

		}
2605 2606 2607

	};

M
Mr.doob 已提交
2608
}
R
Rich Harris 已提交
2609

T
Tristan VALCKE 已提交
2610

2611
export { WebGLRenderer };