WebGLRenderer.js 59.4 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9 10 11 12
import {
	REVISION,
	RGBAFormat,
	HalfFloatType,
	FloatType,
	UnsignedByteType,
	TriangleFanDrawMode,
	TriangleStripDrawMode,
	TrianglesDrawMode,
	NoColors,
	LinearToneMapping
} 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 19
import { UniformsLib } from './shaders/UniformsLib.js';
import { UniformsUtils } from './shaders/UniformsUtils.js';
M
Mr.doob 已提交
20 21
import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js';
B
bentok 已提交
22 23 24
import { WebGLAttributes } from './webgl/WebGLAttributes.js';
import { WebGLBackground } from './webgl/WebGLBackground.js';
import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer.js';
M
Mr.doob 已提交
25 26 27
import { WebGLCapabilities } from './webgl/WebGLCapabilities.js';
import { WebGLClipping } from './webgl/WebGLClipping.js';
import { WebGLExtensions } from './webgl/WebGLExtensions.js';
B
bentok 已提交
28
import { WebGLGeometries } from './webgl/WebGLGeometries.js';
M
Mr.doob 已提交
29 30 31
import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer.js';
import { WebGLInfo } from './webgl/WebGLInfo.js';
import { WebGLMorphtargets } from './webgl/WebGLMorphtargets.js';
B
bentok 已提交
32 33 34
import { WebGLObjects } from './webgl/WebGLObjects.js';
import { WebGLPrograms } from './webgl/WebGLPrograms.js';
import { WebGLProperties } from './webgl/WebGLProperties.js';
M
Mr.doob 已提交
35 36 37 38
import { WebGLRenderLists } from './webgl/WebGLRenderLists.js';
import { WebGLRenderStates } from './webgl/WebGLRenderStates.js';
import { WebGLShadowMap } from './webgl/WebGLShadowMap.js';
import { WebGLSpriteRenderer } from './webgl/WebGLSpriteRenderer.js';
B
bentok 已提交
39
import { WebGLState } from './webgl/WebGLState.js';
M
Mr.doob 已提交
40 41
import { WebGLTextures } from './webgl/WebGLTextures.js';
import { WebGLUniforms } from './webgl/WebGLUniforms.js';
B
bentok 已提交
42
import { WebGLUtils } from './webgl/WebGLUtils.js';
M
Mr.doob 已提交
43
import { WebVRManager } from './webvr/WebVRManager.js';
R
Rich Harris 已提交
44

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

M
Mr.doob 已提交
53
function WebGLRenderer( parameters ) {
M
Mr.doob 已提交
54

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

	parameters = parameters || {};

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

62 63 64 65 66
		_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,
67
		_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
T
Takahiro 已提交
68
		_powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default',
69
		_webglVersion = parameters.webglVersion !== undefined ? parameters.webglVersion : 'webgl';
M
Mr.doob 已提交
70

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

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

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

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

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

101 102
	// physical lights

103
	this.physicallyCorrectLights = false;
104

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

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

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

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

	// internal properties

	var _this = this,

120 121
		_isContextLost = false,

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

124 125 126 127
		_currentRenderTarget = null,
		_currentFramebuffer = null,
		_currentMaterialId = - 1,
		_currentGeometryProgram = '',
128

129
		_currentCamera = null,
130
		_currentArrayCamera = null,
M
Mr.doob 已提交
131

M
Mr.doob 已提交
132
		_currentViewport = new Vector4(),
133 134
		_currentScissor = new Vector4(),
		_currentScissorTest = null,
135

136
		//
137

138
		_usedTextureUnits = 0,
M
Mr.doob 已提交
139

140
		//
M
Mr.doob 已提交
141

142 143
		_width = _canvas.width,
		_height = _canvas.height,
M
Mr.doob 已提交
144

145
		_pixelRatio = 1,
M
Mr.doob 已提交
146

M
Mr.doob 已提交
147
		_viewport = new Vector4( 0, 0, _width, _height ),
148 149
		_scissor = new Vector4( 0, 0, _width, _height ),
		_scissorTest = false,
150

151
		// frustum
M
Mr.doob 已提交
152

153
		_frustum = new Frustum(),
M
Mr.doob 已提交
154

155
		// clipping
T
tschw 已提交
156

157 158 159
		_clipping = new WebGLClipping(),
		_clippingEnabled = false,
		_localClippingEnabled = false,
T
tschw 已提交
160

161
		// camera matrices cache
M
Mr.doob 已提交
162

163
		_projScreenMatrix = new Matrix4(),
M
Mr.doob 已提交
164

M
Mugen87 已提交
165
		_vector3 = new Vector3();
A
Atrahasis 已提交
166

167 168 169 170 171
	function getTargetPixelRatio() {

		return _currentRenderTarget === null ? _pixelRatio : 1;

	}
172

M
Mr.doob 已提交
173 174 175 176
	// initialize

	var _gl;

M
Mr.doob 已提交
177 178
	try {

179
		var contextAttributes = {
M
Mr.doob 已提交
180 181 182 183 184
			alpha: _alpha,
			depth: _depth,
			stencil: _stencil,
			antialias: _antialias,
			premultipliedAlpha: _premultipliedAlpha,
185
			preserveDrawingBuffer: _preserveDrawingBuffer,
L
Luigi De Rosa 已提交
186
			powerPreference: _powerPreference
M
Mr.doob 已提交
187 188
		};

189 190
		// event listeners must be registered before WebGL context is created, see #12753

191
		_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
192
		_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
193

194
		var webglVersion = ( _webglVersion === 'webgl2' || _webglVersion === 'auto' ) ? 'webgl2' : 'webgl';
T
Takahiro 已提交
195 196 197

		_gl = _context || _canvas.getContext( webglVersion, contextAttributes );

198 199 200
		if ( _gl === null && _webglVersion === 'auto' ) _gl = _canvas.getContext( 'webgl', contextAttributes );

		if ( _gl === null && ( webglVersion === 'webgl' || _webglVersion === 'auto' ) ) _gl = _canvas.getContext( 'experimental-webgl', contextAttributes );
M
Mr.doob 已提交
201 202 203

		if ( _gl === null ) {

T
Takahiro 已提交
204
			if ( _canvas.getContext( webglVersion ) !== null ) {
205

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

			} else {

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

			}
M
Mr.doob 已提交
213 214 215

		}

T
Takahiro 已提交
216 217
		_gl.isWebGL2 = typeof WebGL2RenderingContext !== 'undefined' && _gl instanceof WebGL2RenderingContext;

218 219 220 221 222 223 224 225 226 227 228 229
		// 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 已提交
230 231
	} catch ( error ) {

D
Daniel Hritzkiv 已提交
232
		console.error( 'THREE.WebGLRenderer: ' + error.message );
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;
M
Mugen87 已提交
241
	var spriteRenderer;
242

243
	var utils;
244

245
	function initGLContext() {
246

247
		extensions = new WebGLExtensions( _gl );
248 249 250 251 252 253 254 255 256 257 258 259 260

		if ( ! _gl.isWebGL2 ) {

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

		}

261
		extensions.get( 'OES_texture_float_linear' );
262

263
		utils = new WebGLUtils( _gl, extensions );
M
Mr.doob 已提交
264

265
		capabilities = new WebGLCapabilities( _gl, extensions, parameters );
M
Mr.doob 已提交
266

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

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

M
Mr.doob 已提交
282
		background = new WebGLBackground( _this, state, geometries, _premultipliedAlpha );
283

M
Mugen87 已提交
284 285
		bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info );
		indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info );
286

287
		spriteRenderer = new WebGLSpriteRenderer( _this, _gl, state, textures, capabilities );
288

M
Mugen87 已提交
289
		info.programs = programCache.programs;
290

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

299
	}
M
Mr.doob 已提交
300

301
	initGLContext();
M
Mr.doob 已提交
302

303
	// vr
M
Mr.doob 已提交
304

305
	var vr = new WebVRManager( _this );
M
Mr.doob 已提交
306

307
	this.vr = vr;
M
Mr.doob 已提交
308 309

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

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

313
	this.shadowMap = shadowMap;
M
Mr.doob 已提交
314

M
Mr.doob 已提交
315 316 317 318 319 320 321 322
	// API

	this.getContext = function () {

		return _gl;

	};

323 324 325 326 327 328
	this.getContextAttributes = function () {

		return _gl.getContextAttributes();

	};

329 330
	this.forceContextLoss = function () {

M
Michael Bond 已提交
331 332
		var extension = extensions.get( 'WEBGL_lose_context' );
		if ( extension ) extension.loseContext();
333 334 335

	};

336
	this.forceContextRestore = function () {
M
Mr.doob 已提交
337

338 339
		var extension = extensions.get( 'WEBGL_lose_context' );
		if ( extension ) extension.restoreContext();
M
Mr.doob 已提交
340 341 342

	};

343 344
	this.getPixelRatio = function () {

345
		return _pixelRatio;
346 347 348 349 350

	};

	this.setPixelRatio = function ( value ) {

351 352 353 354
		if ( value === undefined ) return;

		_pixelRatio = value;

M
Mr.doob 已提交
355
		this.setSize( _width, _height, false );
356 357 358

	};

359 360 361
	this.getSize = function () {

		return {
362 363
			width: _width,
			height: _height
364 365 366 367
		};

	};

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

370 371 372 373 374 375 376 377 378
		var device = vr.getDevice();

		if ( device && device.isPresenting ) {

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

		}

379 380 381
		_width = width;
		_height = height;

382 383
		_canvas.width = width * _pixelRatio;
		_canvas.height = height * _pixelRatio;
384

385
		if ( updateStyle !== false ) {
386

G
gero3 已提交
387 388
			_canvas.style.width = width + 'px';
			_canvas.style.height = height + 'px';
389

G
gero3 已提交
390
		}
M
Mr.doob 已提交
391

392
		this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
393 394 395

	};

M
Mr.doob 已提交
396 397 398 399 400 401 402 403 404
	this.getDrawingBufferSize = function () {

		return {
			width: _width * _pixelRatio,
			height: _height * _pixelRatio
		};

	};

405 406 407 408 409 410 411 412 413 414 415 416 417 418
	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 );

	};

419
	this.getCurrentViewport = function () {
M
Mugen87 已提交
420

421
		return _currentViewport;
M
Mugen87 已提交
422 423 424

	};

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

M
Mugen87 已提交
427
		_viewport.set( x, _height - y - height, width, height );
428
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ) );
429

M
Mr.doob 已提交
430 431
	};

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

M
Mugen87 已提交
434
		_scissor.set( x, _height - y - height, width, height );
435
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ) );
436

M
Mr.doob 已提交
437 438
	};

439 440
	this.setScissorTest = function ( boolean ) {

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

	};

	// Clearing

M
Mr.doob 已提交
447
	this.getClearColor = function () {
M
Mr.doob 已提交
448

449
		return background.getClearColor();
M
Mr.doob 已提交
450 451 452

	};

453
	this.setClearColor = function () {
454

455
		background.setClearColor.apply( background, arguments );
M
Mr.doob 已提交
456 457 458

	};

M
Mr.doob 已提交
459
	this.getClearAlpha = function () {
M
Mr.doob 已提交
460

461
		return background.getClearAlpha();
M
Mr.doob 已提交
462 463 464

	};

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

467
		background.setClearAlpha.apply( background, arguments );
M
Mr.doob 已提交
468 469 470 471 472 473 474 475 476 477 478 479

	};

	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 );
480 481 482 483 484

	};

	this.clearColor = function () {

M
Mr.doob 已提交
485
		this.clear( true, false, false );
486 487 488 489 490

	};

	this.clearDepth = function () {

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

	};

	this.clearStencil = function () {

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

	};

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

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

	};

M
Mr.doob 已提交
508
	//
M
Mr.doob 已提交
509

M
Mr.doob 已提交
510
	this.dispose = function () {
D
dubejf 已提交
511 512

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

515
		renderLists.dispose();
516
		renderStates.dispose();
M
Mugen87 已提交
517 518
		properties.dispose();
		objects.dispose();
519

520
		vr.dispose();
521

B
Brunner 已提交
522
		stopAnimation();
B
brunnerh 已提交
523

D
dubejf 已提交
524 525
	};

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

D
dubejf 已提交
528 529 530 531
	function onContextLost( event ) {

		event.preventDefault();

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

534 535 536 537
		_isContextLost = true;

	}

M
Mugen87 已提交
538
	function onContextRestore( /* event */ ) {
D
dubejf 已提交
539

540
		console.log( 'THREE.WebGLRenderer: Context Restored.' );
541 542

		_isContextLost = false;
D
dubejf 已提交
543

544
		initGLContext();
D
dubejf 已提交
545

M
Mr.doob 已提交
546
	}
D
dubejf 已提交
547

548
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
549 550 551 552 553 554 555

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

556
	}
M
Mr.doob 已提交
557 558 559

	// Buffer deallocation

560
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
561

562 563
		releaseMaterialProgramReference( material );

564
		properties.remove( material );
565

566
	}
567 568


569
	function releaseMaterialProgramReference( material ) {
570

571
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
572 573 574

		material.program = undefined;

575
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
576

577
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
578

M
Mr.doob 已提交
579 580
		}

581
	}
M
Mr.doob 已提交
582 583 584

	// Buffer rendering

M
Mr.doob 已提交
585 586 587 588 589 590 591 592 593 594
	function renderObjectImmediate( object, program, material ) {

		object.render( function ( object ) {

			_this.renderBufferImmediate( object, program, material );

		} );

	}

M
Mr.doob 已提交
595 596
	this.renderBufferImmediate = function ( object, program, material ) {

597
		state.initAttributes();
598

599
		var buffers = properties.get( object );
600

601 602 603 604
		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 已提交
605

606
		var programAttributes = program.getAttributes();
607

M
Mr.doob 已提交
608 609
		if ( object.hasPositions ) {

610
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
611
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
612

613 614
			state.enableAttribute( programAttributes.position );
			_gl.vertexAttribPointer( programAttributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
615 616 617 618 619

		}

		if ( object.hasNormals ) {

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

622
			if ( ! material.isMeshPhongMaterial &&
623
				! material.isMeshStandardMaterial &&
624
				! material.isMeshNormalMaterial &&
625
				material.flatShading === true ) {
M
Mr.doob 已提交
626

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

629
					var array = object.normalArray;
M
Mr.doob 已提交
630

631 632 633
					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 已提交
634

635 636 637
					array[ i + 0 ] = nx;
					array[ i + 1 ] = ny;
					array[ i + 2 ] = nz;
M
Mr.doob 已提交
638

639 640 641
					array[ i + 3 ] = nx;
					array[ i + 4 ] = ny;
					array[ i + 5 ] = nz;
M
Mr.doob 已提交
642

643 644 645
					array[ i + 6 ] = nx;
					array[ i + 7 ] = ny;
					array[ i + 8 ] = nz;
M
Mr.doob 已提交
646 647 648 649 650 651

				}

			}

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

653
			state.enableAttribute( programAttributes.normal );
654

655
			_gl.vertexAttribPointer( programAttributes.normal, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
656 657 658 659 660

		}

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

661
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
662
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
663

664
			state.enableAttribute( programAttributes.uv );
665

666
			_gl.vertexAttribPointer( programAttributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
667 668 669

		}

R
Rich Harris 已提交
670
		if ( object.hasColors && material.vertexColors !== NoColors ) {
M
Mr.doob 已提交
671

672
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
673
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
674

675
			state.enableAttribute( programAttributes.color );
676

677
			_gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
678 679 680

		}

681
		state.disableUnusedAttributes();
682

M
Mr.doob 已提交
683 684 685 686 687 688
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

689
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
690

691 692 693
		var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );

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

M
Mr.doob 已提交
695
		var program = setProgram( camera, fog, material, object );
M
Mr.doob 已提交
696
		var geometryProgram = geometry.id + '_' + program.id + '_' + ( material.wireframe === true );
M
Mr.doob 已提交
697

M
Mr.doob 已提交
698
		var updateBuffers = false;
M
Mr.doob 已提交
699 700 701 702 703 704 705 706

		if ( geometryProgram !== _currentGeometryProgram ) {

			_currentGeometryProgram = geometryProgram;
			updateBuffers = true;

		}

707
		if ( object.morphTargetInfluences ) {
708

709
			morphtargets.update( object, geometry, material, program );
M
Mr.doob 已提交
710 711 712 713 714

			updateBuffers = true;

		}

715 716
		//

717
		var index = geometry.index;
718
		var position = geometry.attributes.position;
719
		var rangeFactor = 1;
720

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

723
			index = geometries.getWireframeAttribute( geometry );
724
			rangeFactor = 2;
725 726 727

		}

M
Mr.doob 已提交
728
		var attribute;
M
Mr.doob 已提交
729
		var renderer = bufferRenderer;
730

731
		if ( index !== null ) {
732

M
Mr.doob 已提交
733
			attribute = attributes.get( index );
734

735
			renderer = indexedBufferRenderer;
M
Mr.doob 已提交
736
			renderer.setIndex( attribute );
737

738
		}
M
Mr.doob 已提交
739

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

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

744
			if ( index !== null ) {
745

M
Mr.doob 已提交
746
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attribute.buffer );
747 748 749

			}

750
		}
751

752 753
		//

754
		var dataCount = Infinity;
755

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

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

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

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

M
Mr.doob 已提交
764
		}
765

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

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

M
Mr.doob 已提交
772
		var drawStart = Math.max( rangeStart, groupStart );
773
		var drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;
M
Mr.doob 已提交
774 775 776

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

777 778
		if ( drawCount === 0 ) return;

M
Mr.doob 已提交
779
		//
780

781
		if ( object.isMesh ) {
782

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

785
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
786
				renderer.setMode( _gl.LINES );
787

788
			} else {
M
Mr.doob 已提交
789 790

				switch ( object.drawMode ) {
791

R
Rich Harris 已提交
792
					case TrianglesDrawMode:
B
Ben Adams 已提交
793 794 795
						renderer.setMode( _gl.TRIANGLES );
						break;

R
Rich Harris 已提交
796
					case TriangleStripDrawMode:
B
Ben Adams 已提交
797 798 799
						renderer.setMode( _gl.TRIANGLE_STRIP );
						break;

R
Rich Harris 已提交
800
					case TriangleFanDrawMode:
B
Ben Adams 已提交
801 802 803 804
						renderer.setMode( _gl.TRIANGLE_FAN );
						break;

				}
805

806
			}
807

808

809
		} else if ( object.isLine ) {
810

811
			var lineWidth = material.linewidth;
812

813
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
814

815
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
816

817
			if ( object.isLineSegments ) {
818

819
				renderer.setMode( _gl.LINES );
820

821 822 823 824
			} else if ( object.isLineLoop ) {

				renderer.setMode( _gl.LINE_LOOP );

825
			} else {
826

827
				renderer.setMode( _gl.LINE_STRIP );
828 829

			}
M
Mr.doob 已提交
830

831
		} else if ( object.isPoints ) {
832 833

			renderer.setMode( _gl.POINTS );
834 835

		}
836

T
Takahiro 已提交
837
		if ( geometry && geometry.isInstancedBufferGeometry ) {
M
Mr.doob 已提交
838 839 840

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
843
			}
844 845 846

		} else {

M
Mr.doob 已提交
847
			renderer.render( drawStart, drawCount );
848

M
Mr.doob 已提交
849 850 851 852
		}

	};

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

855
		if ( geometry && geometry.isInstancedBufferGeometry & ! _gl.isWebGL2 ) {
B
Ben Adams 已提交
856

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

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

M
Mr.doob 已提交
862 863 864
			}

		}
B
Ben Adams 已提交
865

866 867
		state.initAttributes();

868
		var geometryAttributes = geometry.attributes;
869

870
		var programAttributes = program.getAttributes();
871

872
		var materialDefaultAttributeValues = material.defaultAttributeValues;
873

874
		for ( var name in programAttributes ) {
875

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

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

880
				var geometryAttribute = geometryAttributes[ name ];
881

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

884
					var normalized = geometryAttribute.normalized;
885
					var size = geometryAttribute.itemSize;
886

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

889 890 891 892
					// TODO Attribute may not be available on context restore

					if ( attribute === undefined ) continue;

M
Mr.doob 已提交
893 894 895
					var buffer = attribute.buffer;
					var type = attribute.type;
					var bytesPerElement = attribute.bytesPerElement;
896

A
aardgoose 已提交
897
					if ( geometryAttribute.isInterleavedBufferAttribute ) {
898

M
Mr.doob 已提交
899 900 901 902
						var data = geometryAttribute.data;
						var stride = data.stride;
						var offset = geometryAttribute.offset;

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

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

M
Mr.doob 已提交
907
							if ( geometry.maxInstancedCount === undefined ) {
908

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

M
Mr.doob 已提交
911
							}
B
Ben Adams 已提交
912

M
Mr.doob 已提交
913
						} else {
B
Ben Adams 已提交
914

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

M
Mr.doob 已提交
917
						}
B
Ben Adams 已提交
918

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

M
Mr.doob 已提交
922
					} else {
B
Ben Adams 已提交
923

A
aardgoose 已提交
924
						if ( geometryAttribute.isInstancedBufferAttribute ) {
B
Ben Adams 已提交
925

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

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

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

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

M
Mr.doob 已提交
934 935 936 937
						} else {

							state.enableAttribute( programAttribute );

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

M
Mr.doob 已提交
940
						_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
941
						_gl.vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 );
M
Mr.doob 已提交
942

B
Ben Adams 已提交
943
					}
M
Mr.doob 已提交
944

945 946
				} else if ( materialDefaultAttributeValues !== undefined ) {

T
tschw 已提交
947
					var value = materialDefaultAttributeValues[ name ];
948

949
					if ( value !== undefined ) {
M
Mr.doob 已提交
950

951
						switch ( value.length ) {
M
Mr.doob 已提交
952

953 954 955
							case 2:
								_gl.vertexAttrib2fv( programAttribute, value );
								break;
M
Mr.doob 已提交
956

957 958 959
							case 3:
								_gl.vertexAttrib3fv( programAttribute, value );
								break;
M
Mr.doob 已提交
960

961 962 963
							case 4:
								_gl.vertexAttrib4fv( programAttribute, value );
								break;
964

965 966
							default:
								_gl.vertexAttrib1fv( programAttribute, value );
967 968

						}
M
Mr.doob 已提交
969 970 971 972 973 974 975 976

					}

				}

			}

		}
977

978
		state.disableUnusedAttributes();
979

M
Mr.doob 已提交
980 981
	}

M
Mr.doob 已提交
982
	// Compile
M
Mr.doob 已提交
983

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

M
Mr.doob 已提交
986
		currentRenderState = renderStates.get( scene, camera );
987
		currentRenderState.init();
988

M
Mr.doob 已提交
989
		scene.traverse( function ( object ) {
G
gero3 已提交
990 991

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

993
				currentRenderState.pushLight( object );
994 995 996

				if ( object.castShadow ) {

997
					currentRenderState.pushShadow( object );
998 999

				}
M
Mr.doob 已提交
1000

G
gero3 已提交
1001
			}
M
Mr.doob 已提交
1002 1003 1004

		} );

1005
		currentRenderState.setupLights( camera );
M
Mr.doob 已提交
1006 1007 1008 1009 1010

		scene.traverse( function ( object ) {

			if ( object.material ) {

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

G
gero3 已提交
1013
					for ( var i = 0; i < object.material.length; i ++ ) {
M
Mr.doob 已提交
1014 1015 1016

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

G
gero3 已提交
1017
					}
M
Mr.doob 已提交
1018

G
gero3 已提交
1019
				} else {
M
Mr.doob 已提交
1020 1021 1022

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

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

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

		} );
G
gero3 已提交
1028 1029

	};
1030

1031
	// Animation Loop
M
Mr.doob 已提交
1032

1033 1034
	var isAnimating = false;
	var onAnimationFrame = null;
1035

B
Brunner 已提交
1036
	function startAnimation() {
1037

1038
		if ( isAnimating ) return;
1039

B
Brunner 已提交
1040
		requestAnimationLoopFrame();
M
Mugen87 已提交
1041

1042
		isAnimating = true;
1043

1044
	}
1045

B
Brunner 已提交
1046
	function stopAnimation() {
1047

1048
		isAnimating = false;
1049

1050
	}
1051

B
Brunner 已提交
1052
	function requestAnimationLoopFrame() {
1053 1054

		var device = vr.getDevice();
M
Mugen87 已提交
1055

1056
		if ( device && device.isPresenting ) {
1057

B
Brunner 已提交
1058
			device.requestAnimationFrame( animationLoop );
1059 1060 1061

		} else {

B
Brunner 已提交
1062
			window.requestAnimationFrame( animationLoop );
1063 1064

		}
1065

1066 1067
	}

B
Brunner 已提交
1068
	function animationLoop( time ) {
1069

B
Brunner 已提交
1070
		if ( isAnimating === false ) return;
1071

B
brunnerh 已提交
1072
		onAnimationFrame( time );
1073

B
Brunner 已提交
1074
		requestAnimationLoopFrame();
1075

1076 1077 1078
	}

	this.animate = function ( callback ) {
1079

1080
		onAnimationFrame = callback;
B
Brunner 已提交
1081
		onAnimationFrame !== null ? startAnimation() : stopAnimation();
1082 1083 1084

	};

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

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

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

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

		}

1096 1097
		if ( _isContextLost ) return;

M
Mr.doob 已提交
1098 1099
		// reset caching for this frame

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

		// update scene graph

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

		// update camera matrices and frustum

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

1112 1113 1114 1115 1116 1117
		if ( vr.enabled ) {

			camera = vr.getCamera( camera );

		}

1118 1119
		//

1120 1121
		currentRenderState = renderStates.get( scene, camera );
		currentRenderState.init();
1122

1123 1124
		scene.onBeforeRender( _this, scene, camera, renderTarget );

M
Mr.doob 已提交
1125 1126 1127
		_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
		_frustum.setFromMatrix( _projScreenMatrix );

T
tschw 已提交
1128
		_localClippingEnabled = this.localClippingEnabled;
M
Mr.doob 已提交
1129
		_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1130

1131 1132 1133
		currentRenderList = renderLists.get( scene, camera );
		currentRenderList.init();

M
Mr.doob 已提交
1134
		projectObject( scene, camera, _this.sortObjects );
M
Mr.doob 已提交
1135

M
Mr.doob 已提交
1136
		if ( _this.sortObjects === true ) {
1137

1138
			currentRenderList.sort();
M
Mr.doob 已提交
1139

1140 1141
		}

M
Mr.doob 已提交
1142
		//
M
Mr.doob 已提交
1143

T
tschw 已提交
1144
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1145

1146
		var shadowsArray = currentRenderState.state.shadowsArray;
1147

1148
		shadowMap.render( shadowsArray, scene, camera );
1149

1150
		currentRenderState.setupLights( camera );
1151

T
tschw 已提交
1152
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1153

M
Mr.doob 已提交
1154 1155
		//

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

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

			renderTarget = null;

		}

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

M
Mr.doob 已提交
1166 1167
		//

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

1170
		// render scene
M
Mr.doob 已提交
1171

1172 1173 1174
		var opaqueObjects = currentRenderList.opaque;
		var transparentObjects = currentRenderList.transparent;

M
Mr.doob 已提交
1175 1176
		if ( scene.overrideMaterial ) {

1177
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1178

M
Mr.doob 已提交
1179 1180
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera, overrideMaterial );
			if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera, overrideMaterial );
1181

M
Mr.doob 已提交
1182 1183 1184 1185
		} else {

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

M
Mr.doob 已提交
1186
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera );
M
Mr.doob 已提交
1187 1188 1189

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

M
Mr.doob 已提交
1190
			if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera );
M
Mr.doob 已提交
1191 1192 1193

		}

1194
		// custom renderers
M
Mr.doob 已提交
1195

1196
		var spritesArray = currentRenderState.state.spritesArray;
1197

1198
		spriteRenderer.render( spritesArray, scene, camera );
M
Mr.doob 已提交
1199 1200 1201

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

M
Mr.doob 已提交
1202 1203
		if ( renderTarget ) {

1204
			textures.updateRenderTargetMipmap( renderTarget );
M
Mr.doob 已提交
1205 1206 1207

		}

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

1210 1211 1212
		state.buffers.depth.setTest( true );
		state.buffers.depth.setMask( true );
		state.buffers.color.setMask( true );
M
Mr.doob 已提交
1213

1214
		state.setPolygonOffset( false );
1215

1216
		scene.onAfterRender( _this, scene, camera );
1217

M
Mr.doob 已提交
1218
		if ( vr.enabled ) {
1219

M
Mr.doob 已提交
1220
			vr.submitFrame();
1221

M
Mr.doob 已提交
1222
		}
M
Mr.doob 已提交
1223

M
Mr.doob 已提交
1224 1225
		// _gl.finish();

1226
		currentRenderList = null;
1227
		currentRenderState = null;
1228

M
Mr.doob 已提交
1229
	};
M
Mr.doob 已提交
1230

1231
	/*
M
Mr.doob 已提交
1232 1233
	// TODO Duplicated code (Frustum)

1234 1235
	var _sphere = new Sphere();

T
tschw 已提交
1236 1237 1238 1239 1240 1241 1242
	function isObjectViewable( object ) {

		var geometry = object.geometry;

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

M
Mr.doob 已提交
1243
		_sphere.copy( geometry.boundingSphere ).
1244
		applyMatrix4( object.matrixWorld );
M
Mr.doob 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260

		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 已提交
1261 1262

		if ( ! _frustum.intersectsSphere( sphere ) ) return false;
T
tschw 已提交
1263 1264 1265 1266

		var numPlanes = _clipping.numPlanes;

		if ( numPlanes === 0 ) return true;
T
tschw 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

		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 已提交
1279
		} while ( ++ i !== numPlanes );
T
tschw 已提交
1280 1281 1282 1283

		return true;

	}
1284
	*/
T
tschw 已提交
1285

M
Mr.doob 已提交
1286
	function projectObject( object, camera, sortObjects ) {
M
Mr.doob 已提交
1287

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

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

1292
		if ( visible ) {
1293

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

1296
				currentRenderState.pushLight( object );
1297 1298 1299

				if ( object.castShadow ) {

1300
					currentRenderState.pushShadow( object );
1301 1302

				}
M
Mr.doob 已提交
1303

1304
			} else if ( object.isSprite ) {
M
Mr.doob 已提交
1305

1306
				if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
1307

1308
					currentRenderState.pushSprite( object );
M
Mr.doob 已提交
1309

1310
				}
M
Mr.doob 已提交
1311

1312
			} else if ( object.isImmediateRenderObject ) {
M
Mr.doob 已提交
1313

1314
				if ( sortObjects ) {
M
Mr.doob 已提交
1315

1316 1317
					_vector3.setFromMatrixPosition( object.matrixWorld )
						.applyMatrix4( _projScreenMatrix );
M
Mr.doob 已提交
1318

1319
				}
M
Mr.doob 已提交
1320

1321
				currentRenderList.push( object, null, object.material, _vector3.z, null );
1322

1323
			} else if ( object.isMesh || object.isLine || object.isPoints ) {
1324

1325
				if ( object.isSkinnedMesh ) {
1326

1327
					object.skeleton.update();
1328

1329
				}
1330

1331
				if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
1332

1333 1334 1335 1336 1337 1338
					if ( sortObjects ) {

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

					}
1339

1340 1341
					var geometry = objects.update( object );
					var material = object.material;
1342

1343
					if ( Array.isArray( material ) ) {
1344

1345
						var groups = geometry.groups;
1346

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

1349 1350
							var group = groups[ i ];
							var groupMaterial = material[ group.materialIndex ];
1351

1352
							if ( groupMaterial && groupMaterial.visible ) {
1353

1354 1355 1356
								currentRenderList.push( object, geometry, groupMaterial, _vector3.z, group );

							}
M
Mr.doob 已提交
1357

M
Mr.doob 已提交
1358
						}
M
Mr.doob 已提交
1359

1360
					} else if ( material.visible ) {
1361

1362
						currentRenderList.push( object, geometry, material, _vector3.z, null );
O
OpenShift guest 已提交
1363

1364
					}
M
Mr.doob 已提交
1365

1366
				}
M
Mr.doob 已提交
1367

1368
			}
M
Mr.doob 已提交
1369

M
Mr.doob 已提交
1370
		}
M
Mr.doob 已提交
1371

M
Mr.doob 已提交
1372
		var children = object.children;
M
Mr.doob 已提交
1373

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

M
Mr.doob 已提交
1376
			projectObject( children[ i ], camera, sortObjects );
M
Mr.doob 已提交
1377

1378
		}
1379

1380
	}
M
Mr.doob 已提交
1381

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

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

1386
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1387

1388
			var object = renderItem.object;
M
Mr.doob 已提交
1389 1390 1391
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1392

M
Mr.doob 已提交
1393
			if ( camera.isArrayCamera ) {
M
Mr.doob 已提交
1394

1395 1396
				_currentArrayCamera = camera;

M
Mr.doob 已提交
1397
				var cameras = camera.cameras;
M
Mr.doob 已提交
1398

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

M
Mr.doob 已提交
1401
					var camera2 = cameras[ j ];
M
Mr.doob 已提交
1402

1403
					if ( object.layers.test( camera2.layers ) ) {
1404

1405
						var bounds = camera2.bounds;
1406

1407 1408 1409 1410
						var x = bounds.x * _width;
						var y = bounds.y * _height;
						var width = bounds.z * _width;
						var height = bounds.w * _height;
M
Mr.doob 已提交
1411

1412
						state.viewport( _currentViewport.set( x, y, width, height ).multiplyScalar( _pixelRatio ) );
1413 1414 1415 1416

						renderObject( object, scene, camera2, geometry, material, group );

					}
M
Mr.doob 已提交
1417

M
Mr.doob 已提交
1418
				}
1419

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

1422 1423
				_currentArrayCamera = null;

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

M
Mr.doob 已提交
1426
			}
M
Mr.doob 已提交
1427

1428
		}
M
Mr.doob 已提交
1429

1430
	}
G
gero3 已提交
1431

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

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

M
Mr.doob 已提交
1437 1438 1439 1440 1441
		object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
		object.normalMatrix.getNormalMatrix( object.modelViewMatrix );

		if ( object.isImmediateRenderObject ) {

1442 1443 1444
			var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );

			state.setMaterial( material, frontFaceCW );
M
Mr.doob 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453

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

			_currentGeometryProgram = '';

			renderObjectImmediate( object, program, material );

		} else {

M
Mugen87 已提交
1454
			_this.renderBufferDirect( camera, scene.fog, geometry, material, object, group );
M
Mr.doob 已提交
1455 1456 1457

		}

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

M
Mr.doob 已提交
1461 1462
	}

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

1465
		var materialProperties = properties.get( material );
G
gero3 已提交
1466

1467 1468
		var lights = currentRenderState.state.lights;
		var shadowsArray = currentRenderState.state.shadowsArray;
1469

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

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

1475
		var program = materialProperties.program;
T
tschw 已提交
1476
		var programChange = true;
1477

1478
		if ( program === undefined ) {
B
Ben Adams 已提交
1479

M
Mr.doob 已提交
1480 1481
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1482

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

M
Mr.doob 已提交
1485
			// changed glsl or parameters
1486
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1487

1488 1489 1490 1491 1492
		} else if ( materialProperties.lightsHash !== lights.state.hash ) {

			properties.update( material, 'lightsHash', lights.state.hash );
			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,
1513
					uniforms: UniformsUtils.clone( 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
			program = programCache.acquireProgram( material, materialProperties.shader, parameters, code );
B
Ben Adams 已提交
1532

1533 1534
			materialProperties.program = program;
			material.program = program;
1535 1536 1537

		}

1538
		var programAttributes = program.getAttributes();
M
Mr.doob 已提交
1539 1540 1541 1542 1543

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

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

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

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

					material.numSupportedMorphNormals ++;

				}

			}

		}

1572
		var uniforms = materialProperties.shader.uniforms;
T
tschw 已提交
1573

1574
		if ( ! material.isShaderMaterial &&
1575 1576
			! material.isRawShaderMaterial ||
			material.clipping === true ) {
T
tschw 已提交
1577

T
tschw 已提交
1578
			materialProperties.numClippingPlanes = _clipping.numPlanes;
1579
			materialProperties.numIntersection = _clipping.numIntersection;
T
tschw 已提交
1580
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1581 1582 1583

		}

1584
		materialProperties.fog = fog;
1585

1586
		// store the light setup it was created for
1587

1588
		materialProperties.lightsHash = lights.state.hash;
1589

M
Mr.doob 已提交
1590
		if ( material.lights ) {
1591 1592 1593

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

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
			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;
1607
			// TODO (abelnation): add area lights shadow info to uniforms
1608

1609 1610
		}

T
tschw 已提交
1611 1612
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
1613
				WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1614

T
tschw 已提交
1615
		materialProperties.uniformsList = uniformsList;
A
arose 已提交
1616

M
Mr.doob 已提交
1617
	}
M
Mr.doob 已提交
1618

1619
	function setProgram( camera, fog, material, object ) {
M
Mr.doob 已提交
1620 1621 1622

		_usedTextureUnits = 0;

1623
		var materialProperties = properties.get( material );
1624
		var lights = currentRenderState.state.lights;
1625

T
tschw 已提交
1626 1627 1628 1629 1630
		if ( _clippingEnabled ) {

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

				var useCache =
1631 1632
					camera === _currentCamera &&
					material.id === _currentMaterialId;
T
tschw 已提交
1633 1634 1635 1636

				// we might want to call this function with some ClippingGroup
				// object instead of the material, once it becomes feasible
				// (#8465, #8379)
T
tschw 已提交
1637
				_clipping.setState(
1638 1639
					material.clippingPlanes, material.clipIntersection, material.clipShadows,
					camera, materialProperties, useCache );
T
tschw 已提交
1640 1641 1642 1643 1644

			}

		}

1645
		if ( material.needsUpdate === false ) {
1646

1647
			if ( materialProperties.program === undefined ) {
1648

1649
				material.needsUpdate = true;
1650

1651
			} else if ( material.fog && materialProperties.fog !== fog ) {
1652

M
Mr.doob 已提交
1653
				material.needsUpdate = true;
1654

1655
			} else if ( material.lights && materialProperties.lightsHash !== lights.state.hash ) {
1656

1657
				material.needsUpdate = true;
1658

1659
			} else if ( materialProperties.numClippingPlanes !== undefined &&
1660
				( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
M
Mr.doob 已提交
1661
				materialProperties.numIntersection !== _clipping.numIntersection ) ) {
1662 1663 1664

				material.needsUpdate = true;

1665
			}
1666 1667 1668 1669

		}

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

1671
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1672 1673 1674 1675
			material.needsUpdate = false;

		}

1676
		var refreshProgram = false;
M
Mr.doob 已提交
1677
		var refreshMaterial = false;
1678
		var refreshLights = false;
M
Mr.doob 已提交
1679

1680
		var program = materialProperties.program,
1681
			p_uniforms = program.getUniforms(),
1682
			m_uniforms = materialProperties.shader.uniforms;
M
Mr.doob 已提交
1683

1684
		if ( state.useProgram( program.program ) ) {
M
Mr.doob 已提交
1685

1686
			refreshProgram = true;
M
Mr.doob 已提交
1687
			refreshMaterial = true;
1688
			refreshLights = true;
M
Mr.doob 已提交
1689 1690 1691 1692 1693 1694

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1695

M
Mr.doob 已提交
1696 1697 1698 1699
			refreshMaterial = true;

		}

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

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

G
gero3 已提交
1704
			if ( capabilities.logarithmicDepthBuffer ) {
1705

T
tschw 已提交
1706
				p_uniforms.setValue( _gl, 'logDepthBufFC',
1707
					2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1708 1709 1710

			}

1711
			// Avoid unneeded uniform updates per ArrayCamera's sub-camera
1712

1713
			if ( _currentCamera !== ( _currentArrayCamera || camera ) ) {
1714

1715
				_currentCamera = ( _currentArrayCamera || camera );
1716 1717 1718 1719 1720 1721

				// 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 已提交
1722
				refreshLights = true;		// remains set until update done
1723 1724

			}
M
Mr.doob 已提交
1725

1726 1727 1728
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

1729
			if ( material.isShaderMaterial ||
1730 1731 1732
				material.isMeshPhongMaterial ||
				material.isMeshStandardMaterial ||
				material.envMap ) {
1733

T
tschw 已提交
1734 1735 1736
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1737

T
tschw 已提交
1738
					uCamPos.setValue( _gl,
1739
						_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1740 1741 1742 1743 1744

				}

			}

1745
			if ( material.isMeshPhongMaterial ||
1746 1747 1748 1749
				material.isMeshLambertMaterial ||
				material.isMeshBasicMaterial ||
				material.isMeshStandardMaterial ||
				material.isShaderMaterial ||
1750
				material.skinning ) {
1751

T
tschw 已提交
1752
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1753 1754 1755

			}

M
Mr.doob 已提交
1756 1757 1758 1759 1760 1761
		}

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

1762
		if ( material.skinning ) {
M
Mr.doob 已提交
1763

T
tschw 已提交
1764 1765
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1766

T
tschw 已提交
1767
			var skeleton = object.skeleton;
1768

T
tschw 已提交
1769
			if ( skeleton ) {
1770

1771 1772
				var bones = skeleton.bones;

1773
				if ( capabilities.floatVertexTextures ) {
M
Mr.doob 已提交
1774

1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
					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
1786
						size = _Math.ceilPowerOfTwo( size );
1787 1788 1789 1790 1791 1792
						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 );
1793
						boneTexture.needsUpdate = true;
1794 1795 1796 1797 1798 1799 1800

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

					}

M
Mr.doob 已提交
1801 1802
					p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture );
					p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
M
Mr.doob 已提交
1803

T
tschw 已提交
1804
				} else {
M
Mr.doob 已提交
1805

T
tschw 已提交
1806
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815

				}

			}

		}

		if ( refreshMaterial ) {

1816 1817 1818
			p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure );
			p_uniforms.setValue( _gl, 'toneMappingWhitePoint', _this.toneMappingWhitePoint );

M
Mr.doob 已提交
1819
			if ( material.lights ) {
M
Mr.doob 已提交
1820

1821
				// the current material requires lighting info
M
Mr.doob 已提交
1822

T
tschw 已提交
1823 1824 1825 1826 1827 1828
				// 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 已提交
1829

T
tschw 已提交
1830
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1831

T
tschw 已提交
1832
			}
G
gero3 已提交
1833

T
tschw 已提交
1834
			// refresh uniforms common to several materials
G
gero3 已提交
1835

T
tschw 已提交
1836
			if ( fog && material.fog ) {
G
gero3 已提交
1837

T
tschw 已提交
1838
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1839 1840 1841

			}

1842
			if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
1843 1844 1845

				refreshUniformsCommon( m_uniforms, material );

1846
			} else if ( material.isMeshLambertMaterial ) {
M
Mr.doob 已提交
1847

1848 1849
				refreshUniformsCommon( m_uniforms, material );
				refreshUniformsLambert( m_uniforms, material );
M
Mr.doob 已提交
1850

1851
			} else if ( material.isMeshPhongMaterial ) {
M
Mr.doob 已提交
1852

1853
				refreshUniformsCommon( m_uniforms, material );
M
Mr.doob 已提交
1854

1855
				if ( material.isMeshToonMaterial ) {
M
Mr.doob 已提交
1856

1857
					refreshUniformsToon( m_uniforms, material );
M
Mr.doob 已提交
1858

1859
				} else {
1860

1861
					refreshUniformsPhong( m_uniforms, material );
1862

1863
				}
T
Takahiro 已提交
1864

1865
			} else if ( material.isMeshStandardMaterial ) {
T
Takahiro 已提交
1866

1867
				refreshUniformsCommon( m_uniforms, material );
1868

1869
				if ( material.isMeshPhysicalMaterial ) {
1870

1871
					refreshUniformsPhysical( m_uniforms, material );
W
WestLangley 已提交
1872

1873
				} else {
W
WestLangley 已提交
1874

1875
					refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1876

1877
				}
W
WestLangley 已提交
1878

1879
			} else if ( material.isMeshDepthMaterial ) {
M
Mr.doob 已提交
1880

1881
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1882
				refreshUniformsDepth( m_uniforms, material );
1883

W
WestLangley 已提交
1884
			} else if ( material.isMeshDistanceMaterial ) {
1885

1886
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1887
				refreshUniformsDistance( m_uniforms, material );
1888

1889
			} else if ( material.isMeshNormalMaterial ) {
M
Mr.doob 已提交
1890

1891
				refreshUniformsCommon( m_uniforms, material );
1892
				refreshUniformsNormal( m_uniforms, material );
M
Mr.doob 已提交
1893

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
			} else if ( material.isLineBasicMaterial ) {

				refreshUniformsLine( m_uniforms, material );

				if ( material.isLineDashedMaterial ) {

					refreshUniformsDash( m_uniforms, material );

				}

			} else if ( material.isPointsMaterial ) {

				refreshUniformsPoints( m_uniforms, material );

1908 1909 1910 1911 1912
			} else if ( material.isShadowMaterial ) {

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

M
Mr.doob 已提交
1913 1914
			}

M
Mr.doob 已提交
1915 1916 1917
			// RectAreaLight Texture
			// TODO (mrdoob): Find a nicer implementation

1918 1919
			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 已提交
1920

1921
			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, _this );
A
arose 已提交
1922 1923 1924

		}

1925 1926 1927 1928 1929 1930
		if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {

			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, _this );
			material.uniformsNeedUpdate = false;

		}
M
Mr.doob 已提交
1931

T
tschw 已提交
1932
		// common matrices
M
Mr.doob 已提交
1933

M
Mr.doob 已提交
1934 1935
		p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
		p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix );
T
tschw 已提交
1936
		p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
A
arose 已提交
1937

T
tschw 已提交
1938
		return program;
A
arose 已提交
1939 1940 1941

	}

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

M
Mr.doob 已提交
1944
	function refreshUniformsCommon( uniforms, material ) {
M
Mr.doob 已提交
1945 1946 1947

		uniforms.opacity.value = material.opacity;

1948 1949 1950 1951 1952
		if ( material.color ) {

			uniforms.diffuse.value = material.color;

		}
M
Mr.doob 已提交
1953

1954
		if ( material.emissive ) {
M
Mr.doob 已提交
1955

1956
			uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
M
Mr.doob 已提交
1957 1958 1959

		}

1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
		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
			uniforms.flipEnvMap.value = ( ! ( material.envMap && material.envMap.isCubeTexture ) ) ? 1 : - 1;

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

1991
			uniforms.maxMipLevel.value = properties.get( material.envMap ).__maxMipLevel;
1992

1993
		}
M
Mr.doob 已提交
1994

1995 1996 1997 1998 1999 2000 2001
		if ( material.lightMap ) {

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

		}

2002
		if ( material.aoMap ) {
2003

2004 2005
			uniforms.aoMap.value = material.aoMap;
			uniforms.aoMapIntensity.value = material.aoMapIntensity;
2006 2007 2008

		}

M
Mr.doob 已提交
2009
		// uv repeat and offset setting priorities
M
Mr.doob 已提交
2010 2011 2012 2013 2014
		// 1. color map
		// 2. specular map
		// 3. normal map
		// 4. bump map
		// 5. alpha map
2015
		// 6. emissive map
M
Mr.doob 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

		var uvScaleMap;

		if ( material.map ) {

			uvScaleMap = material.map;

		} else if ( material.specularMap ) {

			uvScaleMap = material.specularMap;

2027 2028 2029 2030
		} else if ( material.displacementMap ) {

			uvScaleMap = material.displacementMap;

M
Mr.doob 已提交
2031 2032 2033 2034 2035 2036 2037 2038
		} else if ( material.normalMap ) {

			uvScaleMap = material.normalMap;

		} else if ( material.bumpMap ) {

			uvScaleMap = material.bumpMap;

2039 2040 2041 2042 2043 2044 2045 2046
		} else if ( material.roughnessMap ) {

			uvScaleMap = material.roughnessMap;

		} else if ( material.metalnessMap ) {

			uvScaleMap = material.metalnessMap;

2047 2048 2049 2050
		} else if ( material.alphaMap ) {

			uvScaleMap = material.alphaMap;

2051 2052 2053 2054
		} else if ( material.emissiveMap ) {

			uvScaleMap = material.emissiveMap;

M
Mr.doob 已提交
2055 2056 2057 2058
		}

		if ( uvScaleMap !== undefined ) {

2059
			// backwards compatibility
2060
			if ( uvScaleMap.isWebGLRenderTarget ) {
M
Mr.doob 已提交
2061 2062 2063 2064 2065

				uvScaleMap = uvScaleMap.texture;

			}

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

T
Tentone 已提交
2068 2069 2070 2071
				var offset = uvScaleMap.offset;
				var repeat = uvScaleMap.repeat;
				var rotation = uvScaleMap.rotation;
				var center = uvScaleMap.center;
M
Mr.doob 已提交
2072

T
Tentone 已提交
2073
				uvScaleMap.matrix.setUvTransform( offset.x, offset.y, repeat.x, repeat.y, rotation, center.x, center.y );
M
Mr.doob 已提交
2074

W
WestLangley 已提交
2075
			}
2076

2077
			uniforms.uvTransform.value.copy( uvScaleMap.matrix );
M
Mr.doob 已提交
2078 2079 2080

		}

M
Mr.doob 已提交
2081
	}
M
Mr.doob 已提交
2082

M
Mr.doob 已提交
2083
	function refreshUniformsLine( uniforms, material ) {
M
Mr.doob 已提交
2084 2085 2086 2087

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

M
Mr.doob 已提交
2088
	}
M
Mr.doob 已提交
2089

M
Mr.doob 已提交
2090
	function refreshUniformsDash( uniforms, material ) {
M
Mr.doob 已提交
2091 2092 2093 2094 2095

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

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

M
Mr.doob 已提交
2098
	function refreshUniformsPoints( uniforms, material ) {
M
Mr.doob 已提交
2099

2100
		uniforms.diffuse.value = material.color;
M
Mr.doob 已提交
2101
		uniforms.opacity.value = material.opacity;
2102
		uniforms.size.value = material.size * _pixelRatio;
2103
		uniforms.scale.value = _height * 0.5;
M
Mr.doob 已提交
2104 2105 2106

		uniforms.map.value = material.map;

2107 2108
		if ( material.map !== null ) {

W
WestLangley 已提交
2109
			if ( material.map.matrixAutoUpdate === true ) {
2110

T
Tentone 已提交
2111 2112 2113 2114
				var offset = material.map.offset;
				var repeat = material.map.repeat;
				var rotation = material.map.rotation;
				var center = material.map.center;
2115

T
Tentone 已提交
2116
				material.map.matrix.setUvTransform( offset.x, offset.y, repeat.x, repeat.y, rotation, center.x, center.y );
W
WestLangley 已提交
2117 2118

			}
2119

2120
			uniforms.uvTransform.value.copy( material.map.matrix );
2121 2122 2123

		}

M
Mr.doob 已提交
2124
	}
M
Mr.doob 已提交
2125

M
Mr.doob 已提交
2126
	function refreshUniformsFog( uniforms, fog ) {
M
Mr.doob 已提交
2127 2128 2129

		uniforms.fogColor.value = fog.color;

2130
		if ( fog.isFog ) {
M
Mr.doob 已提交
2131 2132 2133 2134

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

2135
		} else if ( fog.isFogExp2 ) {
M
Mr.doob 已提交
2136 2137 2138 2139 2140

			uniforms.fogDensity.value = fog.density;

		}

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

M
Mr.doob 已提交
2143
	function refreshUniformsLambert( uniforms, material ) {
2144 2145 2146 2147 2148 2149 2150 2151 2152

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2153
	function refreshUniformsPhong( uniforms, material ) {
M
Mr.doob 已提交
2154

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

2158
		if ( material.emissiveMap ) {
2159

2160
			uniforms.emissiveMap.value = material.emissiveMap;
2161

2162
		}
M
Mr.doob 已提交
2163

2164 2165 2166 2167
		if ( material.bumpMap ) {

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

2169
		}
M
Mr.doob 已提交
2170

2171 2172 2173 2174 2175 2176
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2177

2178 2179 2180 2181 2182
		if ( material.displacementMap ) {

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

2184
		}
2185

T
Takahiro 已提交
2186 2187 2188 2189 2190 2191
	}

	function refreshUniformsToon( uniforms, material ) {

		refreshUniformsPhong( uniforms, material );

T
Takahiro 已提交
2192
		if ( material.gradientMap ) {
T
Takahiro 已提交
2193

T
Takahiro 已提交
2194
			uniforms.gradientMap.value = material.gradientMap;
T
Takahiro 已提交
2195 2196 2197

		}

2198 2199
	}

M
Mr.doob 已提交
2200
	function refreshUniformsStandard( uniforms, material ) {
W
WestLangley 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 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

		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;

		}

		if ( material.normalMap ) {

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

		}

		if ( material.displacementMap ) {

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

		}

		if ( material.envMap ) {

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

		}

	}

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

2256 2257 2258
		uniforms.clearCoat.value = material.clearCoat;
		uniforms.clearCoatRoughness.value = material.clearCoatRoughness;

W
WestLangley 已提交
2259 2260 2261 2262
		refreshUniformsStandard( uniforms, material );

	}

W
WestLangley 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
	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;

	}

2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
	function refreshUniformsNormal( uniforms, material ) {

		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;

		}

	}

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

M
Mr.doob 已提交
2319
	function markUniformsLightsNeedsUpdate( uniforms, value ) {
2320

M
Mr.doob 已提交
2321
		uniforms.ambientLightColor.needsUpdate = value;
2322

B
Ben Houston 已提交
2323 2324 2325
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
2326
		uniforms.rectAreaLights.needsUpdate = value;
B
Ben Houston 已提交
2327
		uniforms.hemisphereLights.needsUpdate = value;
2328

M
Mr.doob 已提交
2329
	}
2330

M
Mr.doob 已提交
2331 2332
	// Textures

T
tschw 已提交
2333 2334 2335 2336 2337 2338
	function allocTextureUnit() {

		var textureUnit = _usedTextureUnits;

		if ( textureUnit >= capabilities.maxTextures ) {

M
Mugen87 已提交
2339
			console.warn( 'THREE.WebGLRenderer: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
T
tschw 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348

		}

		_usedTextureUnits += 1;

		return textureUnit;

	}

2349
	this.allocTextureUnit = allocTextureUnit;
T
tschw 已提交
2350

2351
	// this.setTexture2D = setTexture2D;
M
Mr.doob 已提交
2352
	this.setTexture2D = ( function () {
T
tschw 已提交
2353

2354
		var warned = false;
T
tschw 已提交
2355

2356
		// backwards compatibility: peel texture.texture
W
WestLangley 已提交
2357
		return function setTexture2D( texture, slot ) {
T
tschw 已提交
2358

T
Takahiro 已提交
2359
			if ( texture && texture.isWebGLRenderTarget ) {
T
tschw 已提交
2360

2361
				if ( ! warned ) {
T
tschw 已提交
2362

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

2366
				}
T
tschw 已提交
2367

2368
				texture = texture.texture;
T
tschw 已提交
2369

2370
			}
T
tschw 已提交
2371

2372
			textures.setTexture2D( texture, slot );
T
tschw 已提交
2373

2374
		};
T
tschw 已提交
2375

2376
	}() );
T
tschw 已提交
2377

M
Mr.doob 已提交
2378
	this.setTexture = ( function () {
2379 2380 2381

		var warned = false;

W
WestLangley 已提交
2382
		return function setTexture( texture, slot ) {
2383 2384 2385 2386 2387 2388 2389 2390

			if ( ! warned ) {

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

			}

2391
			textures.setTexture2D( texture, slot );
2392 2393 2394 2395 2396

		};

	}() );

M
Mr.doob 已提交
2397
	this.setTextureCube = ( function () {
2398 2399 2400

		var warned = false;

W
WestLangley 已提交
2401
		return function setTextureCube( texture, slot ) {
2402 2403

			// backwards compatibility: peel texture.texture
T
Takahiro 已提交
2404
			if ( texture && texture.isWebGLRenderTargetCube ) {
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418

				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
T
Takahiro 已提交
2419
			if ( ( texture && texture.isCubeTexture ) ||
2420
				( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
2421 2422 2423 2424

				// CompressedTexture can have Array in image :/

				// this function alone should take care of cube textures
2425
				textures.setTextureCube( texture, slot );
2426 2427 2428 2429 2430

			} else {

				// assumed: texture property of THREE.WebGLRenderTargetCube

2431
				textures.setTextureCubeDynamic( texture, slot );
2432 2433 2434 2435 2436 2437

			}

		};

	}() );
T
tschw 已提交
2438

2439
	this.getRenderTarget = function () {
2440 2441 2442

		return _currentRenderTarget;

M
Michael Herzog 已提交
2443
	};
2444

2445
	this.setRenderTarget = function ( renderTarget ) {
M
Mr.doob 已提交
2446

2447 2448
		_currentRenderTarget = renderTarget;

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

2451
			textures.setupRenderTarget( renderTarget );
M
Mr.doob 已提交
2452 2453 2454

		}

M
Mr.doob 已提交
2455
		var framebuffer = null;
M
Mr.doob 已提交
2456
		var isCube = false;
M
Mr.doob 已提交
2457 2458 2459

		if ( renderTarget ) {

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

M
Mr.doob 已提交
2462
			if ( renderTarget.isWebGLRenderTargetCube ) {
M
Mr.doob 已提交
2463

M
Mr.doob 已提交
2464
				framebuffer = __webglFramebuffer[ renderTarget.activeCubeFace ];
M
Mr.doob 已提交
2465
				isCube = true;
M
Mr.doob 已提交
2466 2467 2468

			} else {

M
Mr.doob 已提交
2469
				framebuffer = __webglFramebuffer;
M
Mr.doob 已提交
2470 2471 2472

			}

M
Mr.doob 已提交
2473
			_currentViewport.copy( renderTarget.viewport );
2474 2475
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
2476

M
Mr.doob 已提交
2477 2478
		} else {

M
Mr.doob 已提交
2479
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio );
2480
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio );
2481
			_currentScissorTest = _scissorTest;
2482

M
Mr.doob 已提交
2483 2484
		}

M
Mr.doob 已提交
2485
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
2486

2487
			_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
M
Mr.doob 已提交
2488 2489 2490 2491
			_currentFramebuffer = framebuffer;

		}

M
Mr.doob 已提交
2492
		state.viewport( _currentViewport );
2493 2494
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
2495

M
Mr.doob 已提交
2496 2497 2498
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
2499
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + renderTarget.activeCubeFace, textureProperties.__webglTexture, renderTarget.activeMipMapLevel );
M
Mr.doob 已提交
2500 2501 2502

		}

M
Mr.doob 已提交
2503 2504
	};

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

0
06wj 已提交
2507
		if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
2508

2509
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
2510
			return;
2511

G
gero3 已提交
2512
		}
2513

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

M
Mr.doob 已提交
2516
		if ( framebuffer ) {
2517

G
gero3 已提交
2518
			var restore = false;
2519

M
Mr.doob 已提交
2520
			if ( framebuffer !== _currentFramebuffer ) {
2521

M
Mr.doob 已提交
2522
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
2523

G
gero3 已提交
2524
				restore = true;
2525

G
gero3 已提交
2526
			}
2527

M
Mr.doob 已提交
2528
			try {
2529

M
Mr.doob 已提交
2530
				var texture = renderTarget.texture;
M
Mr.doob 已提交
2531 2532
				var textureFormat = texture.format;
				var textureType = texture.type;
2533

2534
				if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
2535

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

M
Mr.doob 已提交
2539
				}
2540

2541
				if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // IE11, Edge and Chrome Mac < 52 (#9513)
2542 2543
					! ( textureType === FloatType && ( _gl.isWebGL2 || extensions.get( 'OES_texture_float' ) || extensions.get( 'WEBGL_color_buffer_float' ) ) ) && // Chrome Mac >= 52 and Firefox
					! ( textureType === HalfFloatType && ( _gl.isWebGL2 ? extensions.get( 'EXT_color_buffer_float' ) : extensions.get( 'EXT_color_buffer_half_float' ) ) ) ) {
2544

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

M
Mr.doob 已提交
2548
				}
2549

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

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

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

2556
						_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
2557 2558

					}
2559

M
Mr.doob 已提交
2560
				} else {
M
Mr.doob 已提交
2561

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

				}
M
Mr.doob 已提交
2565

M
Mr.doob 已提交
2566
			} finally {
M
Mr.doob 已提交
2567

M
Mr.doob 已提交
2568 2569 2570
				if ( restore ) {

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

M
Mr.doob 已提交
2572 2573 2574
				}

			}
M
Mr.doob 已提交
2575 2576 2577

		}

M
Mr.doob 已提交
2578 2579
	};

2580
	this.copyFramebufferToTexture = function ( position, texture, level ) {
2581 2582 2583

		var width = texture.image.width;
		var height = texture.image.height;
2584
		var glFormat = utils.convert( texture.format );
2585 2586 2587

		this.setTexture2D( texture, 0 );

2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
		_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 );
		var pixels = srcTexture.isDataTexture ? srcTexture.image.data : srcTexture.image;

		this.setTexture2D( dstTexture, 0 );

		_gl.texSubImage2D( _gl.TEXTURE_2D, level || 0, position.x, position.y, width, height, glFormat, glType, pixels );
2603 2604 2605

	};

M
Mr.doob 已提交
2606
}
R
Rich Harris 已提交
2607

T
Tristan VALCKE 已提交
2608

2609
export { WebGLRenderer };