WebGLRenderer.js 60.5 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
		_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
70 71
		_powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default',
		_failIfMajorPerformanceCaveat = parameters.failIfMajorPerformanceCaveat !== undefined ? parameters.failIfMajorPerformanceCaveat : false;
M
Mr.doob 已提交
72

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

M
Mr.doob 已提交
76 77 78 79
	// public properties

	this.domElement = _canvas;

80 81 82 83 84 85 86
	// Debug configuration container
	this.debug = {

		/**
		 * Enables error checking and reporting when shader programs are being compiled
		 * @type {boolean}
		 */
87
		checkShaderErrors: true
88
	};
89

M
Mr.doob 已提交
90 91 92 93 94 95 96 97 98 99 100
	// clearing

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

	// scene graph

	this.sortObjects = true;

T
tschw 已提交
101 102 103 104 105
	// user-defined clipping

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

M
Mr.doob 已提交
106 107
	// physically based shading

108
	this.gammaFactor = 2.0;	// for backwards compatibility
M
Mr.doob 已提交
109 110 111
	this.gammaInput = false;
	this.gammaOutput = false;

112 113
	// physical lights

114
	this.physicallyCorrectLights = false;
115

B
Ben Houston 已提交
116 117
	// tone mapping

R
Rich Harris 已提交
118
	this.toneMapping = LinearToneMapping;
B
Ben Houston 已提交
119 120 121
	this.toneMappingExposure = 1.0;
	this.toneMappingWhitePoint = 1.0;

M
Mr.doob 已提交
122 123 124 125 126 127 128 129 130
	// morphs

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

	// internal properties

	var _this = this,

131 132
		_isContextLost = false,

133
		// internal state cache
M
Mr.doob 已提交
134

135 136
		_framebuffer = null,

137 138
		_currentActiveCubeFace = 0,
		_currentActiveMipmapLevel = 0,
139 140 141
		_currentRenderTarget = null,
		_currentFramebuffer = null,
		_currentMaterialId = - 1,
142 143 144 145

		// geometry and program caching

		_currentGeometryProgram = {
M
Mr.doob 已提交
146 147
			geometry: null,
			program: null,
148 149
			wireframe: false
		},
150

151
		_currentCamera = null,
152
		_currentArrayCamera = null,
M
Mr.doob 已提交
153

M
Mr.doob 已提交
154
		_currentViewport = new Vector4(),
155 156
		_currentScissor = new Vector4(),
		_currentScissorTest = null,
157

A
arobertson0 已提交
158
	//
159

160 161
		_width = _canvas.width,
		_height = _canvas.height,
M
Mr.doob 已提交
162

163
		_pixelRatio = 1,
M
Mr.doob 已提交
164

M
Mr.doob 已提交
165
		_viewport = new Vector4( 0, 0, _width, _height ),
166 167
		_scissor = new Vector4( 0, 0, _width, _height ),
		_scissorTest = false,
168

169
		// frustum
M
Mr.doob 已提交
170

171
		_frustum = new Frustum(),
M
Mr.doob 已提交
172

173
		// clipping
T
tschw 已提交
174

175 176 177
		_clipping = new WebGLClipping(),
		_clippingEnabled = false,
		_localClippingEnabled = false,
T
tschw 已提交
178

179
		// camera matrices cache
M
Mr.doob 已提交
180

181
		_projScreenMatrix = new Matrix4(),
M
Mr.doob 已提交
182

M
Mugen87 已提交
183
		_vector3 = new Vector3();
A
Atrahasis 已提交
184

185 186 187 188 189
	function getTargetPixelRatio() {

		return _currentRenderTarget === null ? _pixelRatio : 1;

	}
190

M
Mr.doob 已提交
191 192 193 194
	// initialize

	var _gl;

M
Mr.doob 已提交
195 196
	try {

197
		var contextAttributes = {
M
Mr.doob 已提交
198 199 200 201 202
			alpha: _alpha,
			depth: _depth,
			stencil: _stencil,
			antialias: _antialias,
			premultipliedAlpha: _premultipliedAlpha,
203
			preserveDrawingBuffer: _preserveDrawingBuffer,
204
			powerPreference: _powerPreference,
M
Mr.doob 已提交
205
			failIfMajorPerformanceCaveat: _failIfMajorPerformanceCaveat,
206
			xrCompatible: true
M
Mr.doob 已提交
207 208
		};

209 210
		// event listeners must be registered before WebGL context is created, see #12753

211
		_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
212
		_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
213

214
		_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
M
Mr.doob 已提交
215 216 217

		if ( _gl === null ) {

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

D
Daniel Hritzkiv 已提交
220
				throw new Error( 'Error creating WebGL context with your selected attributes.' );
221 222 223

			} else {

D
Daniel Hritzkiv 已提交
224
				throw new Error( 'Error creating WebGL context.' );
225 226

			}
M
Mr.doob 已提交
227 228 229

		}

230 231 232 233 234 235 236 237 238 239 240 241
		// 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 已提交
242 243
	} catch ( error ) {

D
Daniel Hritzkiv 已提交
244
		console.error( 'THREE.WebGLRenderer: ' + error.message );
245
		throw error;
M
Mr.doob 已提交
246 247 248

	}

M
Mugen87 已提交
249
	var extensions, capabilities, state, info;
250
	var properties, textures, attributes, geometries, objects;
251
	var programCache, renderLists, renderStates;
252

253
	var background, morphtargets, bufferRenderer, indexedBufferRenderer;
254

255
	var utils;
256

257
	function initGLContext() {
258

A
arobertson0 已提交
259
			extensions = new WebGLExtensions( _gl );
260

A
arobertson0 已提交
261
			capabilities = new WebGLCapabilities( _gl, extensions, parameters );
T
Takahiro 已提交
262

A
arobertson0 已提交
263
			if ( ! capabilities.isWebGL2 ) {
264

A
arobertson0 已提交
265 266 267 268 269 270 271
				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' );
272

A
arobertson0 已提交
273
			}
274

A
arobertson0 已提交
275
			extensions.get( 'OES_texture_float_linear' );
276

A
arobertson0 已提交
277
			utils = new WebGLUtils( _gl, extensions, capabilities );
M
Mr.doob 已提交
278

A
arobertson0 已提交
279 280 281
			state = new WebGLState( _gl, extensions, utils, capabilities );
			state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
			state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
M
Mr.doob 已提交
282

A
arobertson0 已提交
283 284 285 286 287 288 289 290 291 292
			info = new WebGLInfo( _gl );
			properties = new WebGLProperties();
			textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
			attributes = new WebGLAttributes( _gl );
			geometries = new WebGLGeometries( _gl, attributes, info );
			objects = new WebGLObjects( geometries, info );
			morphtargets = new WebGLMorphtargets( _gl );
			programCache = new WebGLPrograms( _this, extensions, capabilities );
			renderLists = new WebGLRenderLists();
			renderStates = new WebGLRenderStates();
M
Mr.doob 已提交
293

A
arobertson0 已提交
294
			background = new WebGLBackground( _this, state, objects, _premultipliedAlpha );
295

A
arobertson0 已提交
296 297
			bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );
			indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info, capabilities );
298

A
arobertson0 已提交
299
			info.programs = programCache.programs;
300

301 302 303 304 305
		_this.capabilities = capabilities;
		_this.extensions = extensions;
		_this.properties = properties;
		_this.renderLists = renderLists;
		_this.state = state;
M
Mugen87 已提交
306
		_this.info = info;
M
Mr.doob 已提交
307

A
arobertson0 已提交
308
		}
M
Mr.doob 已提交
309

A
arobertson0 已提交
310
		initGLContext();
M
Mr.doob 已提交
311

A
arobertson0 已提交
312
		// vr
M
Mr.doob 已提交
313

314
	var vr = ( typeof navigator !== 'undefined' && 'xr' in navigator && 'supportsSession' in navigator.xr ) ? new WebXRManager( _this, _gl ) : new WebVRManager( _this );
M
Mr.doob 已提交
315

A
arobertson0 已提交
316
		this.vr = vr;
M
Mr.doob 已提交
317

A
arobertson0 已提交
318
		// shadow map
M
Mr.doob 已提交
319

A
arobertson0 已提交
320
		var shadowMap = new WebGLShadowMap( _this, objects, capabilities.maxTextureSize );
M
Mr.doob 已提交
321

A
arobertson0 已提交
322
		this.shadowMap = shadowMap;
M
Mr.doob 已提交
323

A
arobertson0 已提交
324
		// API
M
Mr.doob 已提交
325

A
arobertson0 已提交
326
		this.getContext = function () {
M
Mr.doob 已提交
327

A
arobertson0 已提交
328
			return _gl;
M
Mr.doob 已提交
329

A
arobertson0 已提交
330
		};
M
Mr.doob 已提交
331

A
arobertson0 已提交
332
		this.getContextAttributes = function () {
333

A
arobertson0 已提交
334
			return _gl.getContextAttributes();
335

A
arobertson0 已提交
336
		};
337

A
arobertson0 已提交
338
		this.forceContextLoss = function () {
339

A
arobertson0 已提交
340 341
			var extension = extensions.get( 'WEBGL_lose_context' );
			if ( extension ) extension.loseContext();
342

A
arobertson0 已提交
343
		};
344

A
arobertson0 已提交
345
		this.forceContextRestore = function () {
M
Mr.doob 已提交
346

A
arobertson0 已提交
347 348
			var extension = extensions.get( 'WEBGL_lose_context' );
			if ( extension ) extension.restoreContext();
M
Mr.doob 已提交
349

A
arobertson0 已提交
350
		};
M
Mr.doob 已提交
351

A
arobertson0 已提交
352
		this.getPixelRatio = function () {
353

A
arobertson0 已提交
354
			return _pixelRatio;
355

A
arobertson0 已提交
356
		};
357

A
arobertson0 已提交
358
		this.setPixelRatio = function ( value ) {
359

A
arobertson0 已提交
360
			if ( value === undefined ) return;
361

A
arobertson0 已提交
362
			_pixelRatio = value;
363

A
arobertson0 已提交
364
			this.setSize( _width, _height, false );
365

A
arobertson0 已提交
366
		};
367

A
arobertson0 已提交
368
		this.getSize = function ( target ) {
369

A
arobertson0 已提交
370
			if ( target === undefined ) {
371

A
arobertson0 已提交
372
				console.warn( 'WebGLRenderer: .getsize() now requires a Vector2 as an argument' );
373

A
arobertson0 已提交
374
				target = new Vector2();
375

A
arobertson0 已提交
376
			}
377

A
arobertson0 已提交
378
			return target.set( _width, _height );
379

A
arobertson0 已提交
380
		};
381

A
arobertson0 已提交
382
		this.setSize = function ( width, height, updateStyle ) {
M
Mr.doob 已提交
383

A
arobertson0 已提交
384
			if ( vr.isPresenting() ) {
385

A
arobertson0 已提交
386 387
				console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' );
				return;
388

A
arobertson0 已提交
389
			}
390

A
arobertson0 已提交
391 392
			_width = width;
			_height = height;
393

A
arobertson0 已提交
394 395
			_canvas.width = Math.floor( width * _pixelRatio );
			_canvas.height = Math.floor( height * _pixelRatio );
396

A
arobertson0 已提交
397
			if ( updateStyle !== false ) {
398

A
arobertson0 已提交
399 400
				_canvas.style.width = width + 'px';
				_canvas.style.height = height + 'px';
401

A
arobertson0 已提交
402
			}
M
Mr.doob 已提交
403

A
arobertson0 已提交
404
			this.setViewport( 0, 0, width, height );
M
Mr.doob 已提交
405

A
arobertson0 已提交
406
		};
M
Mr.doob 已提交
407

A
arobertson0 已提交
408
		this.getDrawingBufferSize = function ( target ) {
M
Mr.doob 已提交
409

A
arobertson0 已提交
410
			if ( target === undefined ) {
411

A
arobertson0 已提交
412
				console.warn( 'WebGLRenderer: .getdrawingBufferSize() now requires a Vector2 as an argument' );
413

A
arobertson0 已提交
414
				target = new Vector2();
415

A
arobertson0 已提交
416
			}
417

A
arobertson0 已提交
418
			return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor();
M
Mr.doob 已提交
419

A
arobertson0 已提交
420
		};
M
Mr.doob 已提交
421

A
arobertson0 已提交
422
		this.setDrawingBufferSize = function ( width, height, pixelRatio ) {
423

A
arobertson0 已提交
424 425
			_width = width;
			_height = height;
426 427 428

		_pixelRatio = pixelRatio;

429 430
		_canvas.width = Math.floor( width * pixelRatio );
		_canvas.height = Math.floor( height * pixelRatio );
431 432 433 434 435

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

	};

436
	this.getCurrentViewport = function ( target ) {
M
Mugen87 已提交
437

438 439 440 441 442 443 444 445 446
		if ( target === undefined ) {

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

			target = new Vector4();

		}

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

	};

450 451 452
	this.getViewport = function ( target ) {

		return target.copy( _viewport );
M
Mugen87 已提交
453 454 455

	};

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

458 459 460 461 462 463 464 465 466 467
		if ( x.isVector4 ) {

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

		} else {

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

		}

468
		state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
469

M
Mr.doob 已提交
470 471
	};

472 473 474 475 476 477
	this.getScissor = function ( target ) {

		return target.copy( _scissor );

	};

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

480 481 482 483 484 485 486 487 488 489
		if ( x.isVector4 ) {

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

		} else {

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

		}

490
		state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
491

M
Mr.doob 已提交
492 493
	};

494 495 496 497 498 499
	this.getScissorTest = function () {

		return _scissorTest;

	};

500 501
	this.setScissorTest = function ( boolean ) {

502
		state.setScissorTest( _scissorTest = boolean );
M
Mr.doob 已提交
503 504 505 506 507

	};

	// Clearing

M
Mr.doob 已提交
508
	this.getClearColor = function () {
M
Mr.doob 已提交
509

510
		return background.getClearColor();
M
Mr.doob 已提交
511 512 513

	};

514
	this.setClearColor = function () {
515

516
		background.setClearColor.apply( background, arguments );
M
Mr.doob 已提交
517 518 519

	};

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

522
		return background.getClearAlpha();
M
Mr.doob 已提交
523 524 525

	};

M
Mugen87 已提交
526
	this.setClearAlpha = function () {
M
Mr.doob 已提交
527

528
		background.setClearAlpha.apply( background, arguments );
M
Mr.doob 已提交
529 530 531 532 533 534 535 536 537 538 539 540

	};

	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 );
541 542 543 544 545

	};

	this.clearColor = function () {

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

	};

	this.clearDepth = function () {

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

	};

	this.clearStencil = function () {

M
Mr.doob 已提交
558
		this.clear( false, false, true );
M
Mr.doob 已提交
559 560 561

	};

M
Mr.doob 已提交
562
	//
M
Mr.doob 已提交
563

M
Mr.doob 已提交
564
	this.dispose = function () {
D
dubejf 已提交
565 566

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

569
		renderLists.dispose();
570
		renderStates.dispose();
M
Mugen87 已提交
571 572
		properties.dispose();
		objects.dispose();
573

574
		vr.dispose();
575

576
		animation.stop();
B
brunnerh 已提交
577

D
dubejf 已提交
578 579
	};

M
Mr.doob 已提交
580
	// Events
M
Mr.doob 已提交
581

D
dubejf 已提交
582 583 584 585
	function onContextLost( event ) {

		event.preventDefault();

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

588 589 590 591
		_isContextLost = true;

	}

M
Mugen87 已提交
592
	function onContextRestore( /* event */ ) {
D
dubejf 已提交
593

594
		console.log( 'THREE.WebGLRenderer: Context Restored.' );
595 596

		_isContextLost = false;
D
dubejf 已提交
597

598
		initGLContext();
D
dubejf 已提交
599

M
Mr.doob 已提交
600
	}
D
dubejf 已提交
601

602
	function onMaterialDispose( event ) {
M
Mr.doob 已提交
603 604 605 606 607 608 609

		var material = event.target;

		material.removeEventListener( 'dispose', onMaterialDispose );

		deallocateMaterial( material );

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

	// Buffer deallocation

614
	function deallocateMaterial( material ) {
M
Mr.doob 已提交
615

616 617
		releaseMaterialProgramReference( material );

618
		properties.remove( material );
619

620
	}
621 622


623
	function releaseMaterialProgramReference( material ) {
624

625
		var programInfo = properties.get( material ).program;
M
Mr.doob 已提交
626 627 628

		material.program = undefined;

629
		if ( programInfo !== undefined ) {
M
Mr.doob 已提交
630

631
			programCache.releaseProgram( programInfo );
M
Mr.doob 已提交
632

M
Mr.doob 已提交
633 634
		}

635
	}
M
Mr.doob 已提交
636 637 638

	// Buffer rendering

M
Mr.doob 已提交
639
	function renderObjectImmediate( object, program ) {
M
Mr.doob 已提交
640 641 642

		object.render( function ( object ) {

M
Mr.doob 已提交
643
			_this.renderBufferImmediate( object, program );
M
Mr.doob 已提交
644 645 646 647 648

		} );

	}

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

651
		state.initAttributes();
652

653
		var buffers = properties.get( object );
654

655 656 657 658
		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 已提交
659

660
		var programAttributes = program.getAttributes();
661

M
Mr.doob 已提交
662 663
		if ( object.hasPositions ) {

664
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
M
Mr.doob 已提交
665
			_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
666

667 668
			state.enableAttribute( programAttributes.position );
			_gl.vertexAttribPointer( programAttributes.position, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
669 670 671 672 673

		}

		if ( object.hasNormals ) {

674
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
M
Mr.doob 已提交
675
			_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
676

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

		}

682
		if ( object.hasUvs ) {
M
Mr.doob 已提交
683

684
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
M
Mr.doob 已提交
685
			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
686

687
			state.enableAttribute( programAttributes.uv );
688
			_gl.vertexAttribPointer( programAttributes.uv, 2, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
689 690 691

		}

692
		if ( object.hasColors ) {
M
Mr.doob 已提交
693

694
			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
M
Mr.doob 已提交
695
			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
696

697 698
			state.enableAttribute( programAttributes.color );
			_gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 );
M
Mr.doob 已提交
699 700 701

		}

702
		state.disableUnusedAttributes();
703

M
Mr.doob 已提交
704 705 706 707 708 709
		_gl.drawArrays( _gl.TRIANGLES, 0, object.count );

		object.count = 0;

	};

710
	this.renderBufferDirect = function ( camera, fog, geometry, material, object, group ) {
711

712
		var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );
713 714

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

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

M
Mr.doob 已提交
718
		var updateBuffers = false;
M
Mr.doob 已提交
719

720 721 722
		if ( _currentGeometryProgram.geometry !== geometry.id ||
			_currentGeometryProgram.program !== program.id ||
			_currentGeometryProgram.wireframe !== ( material.wireframe === true ) ) {
M
Mr.doob 已提交
723

M
Mr.doob 已提交
724 725
			_currentGeometryProgram.geometry = geometry.id;
			_currentGeometryProgram.program = program.id;
726
			_currentGeometryProgram.wireframe = material.wireframe === true;
M
Mr.doob 已提交
727 728 729 730
			updateBuffers = true;

		}

731
		if ( object.morphTargetInfluences ) {
732

733
			morphtargets.update( object, geometry, material, program );
M
Mr.doob 已提交
734 735 736 737 738

			updateBuffers = true;

		}

739 740
		//

741
		var index = geometry.index;
742
		var position = geometry.attributes.position;
743
		var rangeFactor = 1;
744

745 746
		if ( material.wireframe === true ) {

747
			index = geometries.getWireframeAttribute( geometry );
748
			rangeFactor = 2;
749 750 751

		}

M
Mr.doob 已提交
752
		var attribute;
M
Mr.doob 已提交
753
		var renderer = bufferRenderer;
754

755
		if ( index !== null ) {
756

M
Mr.doob 已提交
757
			attribute = attributes.get( index );
758

759
			renderer = indexedBufferRenderer;
M
Mr.doob 已提交
760
			renderer.setIndex( attribute );
761

762
		}
M
Mr.doob 已提交
763

764
		if ( updateBuffers ) {
M
Mr.doob 已提交
765

766
			setupVertexAttributes( material, program, geometry );
M
Mr.doob 已提交
767

768
			if ( index !== null ) {
769

M
Mr.doob 已提交
770
				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attribute.buffer );
771 772 773

			}

774
		}
775

776 777
		//

778
		var dataCount = Infinity;
779

M
Mr.doob 已提交
780
		if ( index !== null ) {
781

M
Mr.doob 已提交
782
			dataCount = index.count;
783

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

M
Mr.doob 已提交
786
			dataCount = position.count;
787

M
Mr.doob 已提交
788
		}
789

M
Mr.doob 已提交
790 791
		var rangeStart = geometry.drawRange.start * rangeFactor;
		var rangeCount = geometry.drawRange.count * rangeFactor;
792

M
Mr.doob 已提交
793 794
		var groupStart = group !== null ? group.start * rangeFactor : 0;
		var groupCount = group !== null ? group.count * rangeFactor : Infinity;
795

M
Mr.doob 已提交
796
		var drawStart = Math.max( rangeStart, groupStart );
797
		var drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;
M
Mr.doob 已提交
798 799 800

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

801 802
		if ( drawCount === 0 ) return;

M
Mr.doob 已提交
803
		//
804

805
		if ( object.isMesh ) {
806

807
			if ( material.wireframe === true ) {
808

809
				state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
810
				renderer.setMode( _gl.LINES );
811

812
			} else {
M
Mr.doob 已提交
813 814

				switch ( object.drawMode ) {
815

R
Rich Harris 已提交
816
					case TrianglesDrawMode:
B
Ben Adams 已提交
817 818 819
						renderer.setMode( _gl.TRIANGLES );
						break;

R
Rich Harris 已提交
820
					case TriangleStripDrawMode:
B
Ben Adams 已提交
821 822 823
						renderer.setMode( _gl.TRIANGLE_STRIP );
						break;

R
Rich Harris 已提交
824
					case TriangleFanDrawMode:
B
Ben Adams 已提交
825 826 827 828
						renderer.setMode( _gl.TRIANGLE_FAN );
						break;

				}
829

830
			}
831

832

833
		} else if ( object.isLine ) {
834

835
			var lineWidth = material.linewidth;
836

837
			if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material
838

839
			state.setLineWidth( lineWidth * getTargetPixelRatio() );
840

841
			if ( object.isLineSegments ) {
842

843
				renderer.setMode( _gl.LINES );
844

845 846 847 848
			} else if ( object.isLineLoop ) {

				renderer.setMode( _gl.LINE_LOOP );

849
			} else {
850

851
				renderer.setMode( _gl.LINE_STRIP );
852 853

			}
M
Mr.doob 已提交
854

855
		} else if ( object.isPoints ) {
856 857

			renderer.setMode( _gl.POINTS );
858

859 860 861 862
		} else if ( object.isSprite ) {

			renderer.setMode( _gl.TRIANGLES );

863
		}
864

T
Takahiro 已提交
865
		if ( geometry && geometry.isInstancedBufferGeometry ) {
M
Mr.doob 已提交
866 867 868

			if ( geometry.maxInstancedCount > 0 ) {

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

J
jfranc 已提交
871
			}
872 873 874

		} else {

M
Mr.doob 已提交
875
			renderer.render( drawStart, drawCount );
876

M
Mr.doob 已提交
877 878 879 880
		}

	};

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

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

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

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

M
Mr.doob 已提交
890 891 892
			}

		}
B
Ben Adams 已提交
893

894 895
		state.initAttributes();

896
		var geometryAttributes = geometry.attributes;
897

898
		var programAttributes = program.getAttributes();
899

900
		var materialDefaultAttributeValues = material.defaultAttributeValues;
901

902
		for ( var name in programAttributes ) {
903

904
			var programAttribute = programAttributes[ name ];
M
Mr.doob 已提交
905

M
Mr.doob 已提交
906
			if ( programAttribute >= 0 ) {
M
Mr.doob 已提交
907

908
				var geometryAttribute = geometryAttributes[ name ];
909

M
Mr.doob 已提交
910
				if ( geometryAttribute !== undefined ) {
M
Mr.doob 已提交
911

912
					var normalized = geometryAttribute.normalized;
913
					var size = geometryAttribute.itemSize;
914

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

917 918 919 920
					// TODO Attribute may not be available on context restore

					if ( attribute === undefined ) continue;

M
Mr.doob 已提交
921 922 923
					var buffer = attribute.buffer;
					var type = attribute.type;
					var bytesPerElement = attribute.bytesPerElement;
924

A
aardgoose 已提交
925
					if ( geometryAttribute.isInterleavedBufferAttribute ) {
926

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

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

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

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

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

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

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

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

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

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

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

A
aardgoose 已提交
952
						if ( geometryAttribute.isInstancedBufferAttribute ) {
B
Ben Adams 已提交
953

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

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

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

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

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

							state.enableAttribute( programAttribute );

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

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

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

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

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

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

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

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

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

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

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

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

					}

				}

			}

		}
1005

1006
		state.disableUnusedAttributes();
1007

M
Mr.doob 已提交
1008 1009
	}

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

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

M
Mr.doob 已提交
1014
		currentRenderState = renderStates.get( scene, camera );
1015
		currentRenderState.init();
1016

M
Mr.doob 已提交
1017
		scene.traverse( function ( object ) {
G
gero3 已提交
1018 1019

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

1021
				currentRenderState.pushLight( object );
1022 1023 1024

				if ( object.castShadow ) {

1025
					currentRenderState.pushShadow( object );
1026 1027

				}
M
Mr.doob 已提交
1028

G
gero3 已提交
1029
			}
M
Mr.doob 已提交
1030 1031 1032

		} );

1033
		currentRenderState.setupLights( camera );
M
Mr.doob 已提交
1034 1035 1036 1037 1038

		scene.traverse( function ( object ) {

			if ( object.material ) {

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

G
gero3 已提交
1041
					for ( var i = 0; i < object.material.length; i ++ ) {
M
Mr.doob 已提交
1042 1043 1044

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

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

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

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

G
gero3 已提交
1051
				}
M
Mr.doob 已提交
1052

G
gero3 已提交
1053
			}
M
Mr.doob 已提交
1054 1055

		} );
G
gero3 已提交
1056 1057

	};
1058

1059
	// Animation Loop
M
Mr.doob 已提交
1060

M
Mr.doob 已提交
1061
	var onAnimationFrameCallback = null;
1062

1063
	function onAnimationFrame( time ) {
1064

M
Mr.doob 已提交
1065
		if ( vr.isPresenting() ) return;
1066
		if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
1067

1068
	}
1069

M
Mr.doob 已提交
1070 1071
	var animation = new WebGLAnimation();
	animation.setAnimationLoop( onAnimationFrame );
1072 1073

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

1075
	this.setAnimationLoop = function ( callback ) {
1076

M
Mr.doob 已提交
1077 1078
		onAnimationFrameCallback = callback;
		vr.setAnimationLoop( callback );
1079

1080 1081
		animation.start();

1082 1083
	};

M
Mr.doob 已提交
1084 1085
	// Rendering

1086 1087
	this.render = function ( scene, camera ) {

M
Mr.doob 已提交
1088
		var renderTarget, forceClear;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

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

		}
1103

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

1106
			console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
1107 1108 1109 1110
			return;

		}

1111 1112
		if ( _isContextLost ) return;

M
Mr.doob 已提交
1113 1114
		// reset caching for this frame

M
Mr.doob 已提交
1115 1116
		_currentGeometryProgram.geometry = null;
		_currentGeometryProgram.program = null;
1117
		_currentGeometryProgram.wireframe = false;
1118
		_currentMaterialId = - 1;
1119
		_currentCamera = null;
M
Mr.doob 已提交
1120 1121 1122

		// update scene graph

1123
		if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
M
Mr.doob 已提交
1124 1125 1126

		// update camera matrices and frustum

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

1129 1130 1131 1132 1133 1134
		if ( vr.enabled ) {

			camera = vr.getCamera( camera );

		}

1135 1136
		//

1137 1138
		currentRenderState = renderStates.get( scene, camera );
		currentRenderState.init();
1139

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

M
Mr.doob 已提交
1142 1143 1144
		_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
		_frustum.setFromMatrix( _projScreenMatrix );

T
tschw 已提交
1145
		_localClippingEnabled = this.localClippingEnabled;
M
Mr.doob 已提交
1146
		_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
T
tschw 已提交
1147

1148 1149 1150
		currentRenderList = renderLists.get( scene, camera );
		currentRenderList.init();

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

M
Mr.doob 已提交
1153
		if ( _this.sortObjects === true ) {
1154

1155
			currentRenderList.sort();
M
Mr.doob 已提交
1156

1157 1158
		}

M
Mr.doob 已提交
1159
		//
M
Mr.doob 已提交
1160

T
tschw 已提交
1161
		if ( _clippingEnabled ) _clipping.beginShadows();
T
tschw 已提交
1162

1163
		var shadowsArray = currentRenderState.state.shadowsArray;
1164

1165
		shadowMap.render( shadowsArray, scene, camera );
1166

1167
		currentRenderState.setupLights( camera );
1168

T
tschw 已提交
1169
		if ( _clippingEnabled ) _clipping.endShadows();
T
tschw 已提交
1170

M
Mr.doob 已提交
1171 1172
		//

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

1175
		if ( renderTarget !== undefined ) {
1176

1177
			this.setRenderTarget( renderTarget );
1178 1179 1180

		}

M
Mr.doob 已提交
1181 1182
		//

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

1185
		// render scene
M
Mr.doob 已提交
1186

1187 1188 1189
		var opaqueObjects = currentRenderList.opaque;
		var transparentObjects = currentRenderList.transparent;

M
Mr.doob 已提交
1190 1191
		if ( scene.overrideMaterial ) {

1192
			var overrideMaterial = scene.overrideMaterial;
M
Mr.doob 已提交
1193

M
Mr.doob 已提交
1194 1195
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera, overrideMaterial );
			if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera, overrideMaterial );
1196

M
Mr.doob 已提交
1197 1198
		} else {

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

M
Mr.doob 已提交
1201
			if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera );
1202 1203 1204

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

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

1207
		}
M
Mr.doob 已提交
1208

1209
		//
M
Mr.doob 已提交
1210

1211 1212 1213 1214
		scene.onAfterRender( _this, scene, camera );

		//

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

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

1219
			textures.updateRenderTargetMipmap( _currentRenderTarget );
M
Mr.doob 已提交
1220

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

1223
			textures.updateMultisampleRenderTarget( _currentRenderTarget );
1224

M
Mr.doob 已提交
1225 1226
		}

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

1229 1230 1231
		state.buffers.depth.setTest( true );
		state.buffers.depth.setMask( true );
		state.buffers.color.setMask( true );
M
Mr.doob 已提交
1232

1233
		state.setPolygonOffset( false );
1234

M
Mr.doob 已提交
1235
		if ( vr.enabled ) {
1236

1237
			vr.submitFrame();
1238

M
Mr.doob 已提交
1239
		}
M
Mr.doob 已提交
1240

M
Mr.doob 已提交
1241 1242
		// _gl.finish();

1243
		currentRenderList = null;
1244
		currentRenderState = null;
1245

M
Mr.doob 已提交
1246
	};
M
Mr.doob 已提交
1247

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

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

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

1254
		if ( visible ) {
1255

1256 1257 1258 1259
			if ( object.isGroup ) {

				groupOrder = object.renderOrder;

T
Takahiro 已提交
1260 1261
			} else if ( object.isLOD ) {

1262
				if ( object.autoUpdate === true ) object.update( camera );
T
Takahiro 已提交
1263

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

1266
				currentRenderState.pushLight( object );
1267 1268 1269

				if ( object.castShadow ) {

1270
					currentRenderState.pushShadow( object );
1271 1272

				}
M
Mr.doob 已提交
1273

1274
			} else if ( object.isSprite ) {
M
Mr.doob 已提交
1275

1276
				if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
1277

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
					if ( sortObjects ) {

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

					}

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

1288 1289 1290 1291 1292
					if ( material.visible ) {

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

					}
M
Mr.doob 已提交
1293

1294
				}
M
Mr.doob 已提交
1295

1296
			} else if ( object.isImmediateRenderObject ) {
M
Mr.doob 已提交
1297

1298
				if ( sortObjects ) {
M
Mr.doob 已提交
1299

1300 1301
					_vector3.setFromMatrixPosition( object.matrixWorld )
						.applyMatrix4( _projScreenMatrix );
M
Mr.doob 已提交
1302

1303
				}
M
Mr.doob 已提交
1304

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

1307
			} else if ( object.isMesh || object.isLine || object.isPoints ) {
1308

1309
				if ( object.isSkinnedMesh ) {
1310

1311
					object.skeleton.update();
1312

1313
				}
1314

1315
				if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
1316

1317 1318 1319 1320 1321 1322
					if ( sortObjects ) {

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

					}
1323

1324 1325
					var geometry = objects.update( object );
					var material = object.material;
1326

1327
					if ( Array.isArray( material ) ) {
1328

1329
						var groups = geometry.groups;
1330

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

1333 1334
							var group = groups[ i ];
							var groupMaterial = material[ group.materialIndex ];
1335

1336
							if ( groupMaterial && groupMaterial.visible ) {
1337

1338
								currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
1339 1340

							}
M
Mr.doob 已提交
1341

M
Mr.doob 已提交
1342
						}
M
Mr.doob 已提交
1343

1344
					} else if ( material.visible ) {
1345

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

1348
					}
M
Mr.doob 已提交
1349

1350
				}
M
Mr.doob 已提交
1351

1352
			}
M
Mr.doob 已提交
1353

M
Mr.doob 已提交
1354
		}
M
Mr.doob 已提交
1355

M
Mr.doob 已提交
1356
		var children = object.children;
M
Mr.doob 已提交
1357

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

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

1362
		}
1363

1364
	}
M
Mr.doob 已提交
1365

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

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

1370
			var renderItem = renderList[ i ];
M
Mr.doob 已提交
1371

1372
			var object = renderItem.object;
M
Mr.doob 已提交
1373 1374 1375
			var geometry = renderItem.geometry;
			var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
			var group = renderItem.group;
M
Mr.doob 已提交
1376

M
Mr.doob 已提交
1377
			if ( camera.isArrayCamera ) {
M
Mr.doob 已提交
1378

1379 1380
				_currentArrayCamera = camera;

M
Mr.doob 已提交
1381
				var cameras = camera.cameras;
M
Mr.doob 已提交
1382

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

M
Mr.doob 已提交
1385
					var camera2 = cameras[ j ];
M
Mr.doob 已提交
1386

1387
					if ( object.layers.test( camera2.layers ) ) {
1388

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

1391 1392
						currentRenderState.setupLights( camera2 );

1393 1394 1395
						renderObject( object, scene, camera2, geometry, material, group );

					}
M
Mr.doob 已提交
1396

M
Mr.doob 已提交
1397
				}
1398

M
Mr.doob 已提交
1399
			} else {
M
Mr.doob 已提交
1400

1401 1402
				_currentArrayCamera = null;

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

M
Mr.doob 已提交
1405
			}
M
Mr.doob 已提交
1406

1407
		}
M
Mr.doob 已提交
1408

1409
	}
G
gero3 已提交
1410

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

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

M
Mr.doob 已提交
1416 1417 1418 1419 1420
		object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
		object.normalMatrix.getNormalMatrix( object.modelViewMatrix );

		if ( object.isImmediateRenderObject ) {

1421
			state.setMaterial( material );
M
Mr.doob 已提交
1422 1423 1424

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

M
Mr.doob 已提交
1425 1426
			_currentGeometryProgram.geometry = null;
			_currentGeometryProgram.program = null;
1427
			_currentGeometryProgram.wireframe = false;
M
Mr.doob 已提交
1428

M
Mr.doob 已提交
1429
			renderObjectImmediate( object, program );
M
Mr.doob 已提交
1430 1431 1432

		} else {

M
Mugen87 已提交
1433
			_this.renderBufferDirect( camera, scene.fog, geometry, material, object, group );
M
Mr.doob 已提交
1434 1435 1436

		}

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

M
Mr.doob 已提交
1440 1441
	}

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

1444
		var materialProperties = properties.get( material );
G
gero3 已提交
1445

1446 1447
		var lights = currentRenderState.state.lights;
		var shadowsArray = currentRenderState.state.shadowsArray;
1448

A
aardgoose 已提交
1449
		var lightsStateVersion = lights.state.version;
M
Mr.doob 已提交
1450

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

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

1456
		var program = materialProperties.program;
T
tschw 已提交
1457
		var programChange = true;
1458

1459
		if ( program === undefined ) {
B
Ben Adams 已提交
1460

M
Mr.doob 已提交
1461 1462
			// new material
			material.addEventListener( 'dispose', onMaterialDispose );
B
Ben Adams 已提交
1463

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

M
Mr.doob 已提交
1466
			// changed glsl or parameters
1467
			releaseMaterialProgramReference( material );
B
Ben Adams 已提交
1468

A
aardgoose 已提交
1469
		} else if ( materialProperties.lightsStateVersion !== lightsStateVersion ) {
M
Mr.doob 已提交
1470

A
aardgoose 已提交
1471
			materialProperties.lightsStateVersion = lightsStateVersion;
1472 1473 1474

			programChange = false;

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

T
tschw 已提交
1477
			// same glsl and uniform list
T
tschw 已提交
1478 1479
			return;

T
tschw 已提交
1480
		} else {
B
Ben Adams 已提交
1481

T
tschw 已提交
1482 1483
			// only rebuild uniform list
			programChange = false;
B
Ben Adams 已提交
1484 1485 1486

		}

1487
		if ( programChange ) {
B
Ben Adams 已提交
1488

1489
			if ( parameters.shaderID ) {
B
Ben Adams 已提交
1490

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

1493
				materialProperties.shader = {
1494
					name: material.type,
M
Mr.doob 已提交
1495
					uniforms: cloneUniforms( shader.uniforms ),
1496
					vertexShader: shader.vertexShader,
1497
					fragmentShader: shader.fragmentShader
1498
				};
B
Ben Adams 已提交
1499

1500
			} else {
B
Ben Adams 已提交
1501

1502
				materialProperties.shader = {
1503 1504 1505
					name: material.type,
					uniforms: material.uniforms,
					vertexShader: material.vertexShader,
1506
					fragmentShader: material.fragmentShader
1507
				};
G
gero3 已提交
1508

1509
			}
G
gero3 已提交
1510

1511
			material.onBeforeCompile( materialProperties.shader, _this );
G
gero3 已提交
1512

1513
			// Computing code again as onBeforeCompile may have changed the shaders
1514 1515
			code = programCache.getProgramCode( material, parameters );

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

1518 1519
			materialProperties.program = program;
			material.program = program;
1520 1521 1522

		}

1523
		var programAttributes = program.getAttributes();
M
Mr.doob 已提交
1524 1525 1526 1527 1528

		if ( material.morphTargets ) {

			material.numSupportedMorphTargets = 0;

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

1531
				if ( programAttributes[ 'morphTarget' + i ] >= 0 ) {
M
Mr.doob 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544

					material.numSupportedMorphTargets ++;

				}

			}

		}

		if ( material.morphNormals ) {

			material.numSupportedMorphNormals = 0;

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

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

					material.numSupportedMorphNormals ++;

				}

			}

		}

1557
		var uniforms = materialProperties.shader.uniforms;
T
tschw 已提交
1558

1559
		if ( ! material.isShaderMaterial &&
1560 1561
			! material.isRawShaderMaterial ||
			material.clipping === true ) {
T
tschw 已提交
1562

T
tschw 已提交
1563
			materialProperties.numClippingPlanes = _clipping.numPlanes;
1564
			materialProperties.numIntersection = _clipping.numIntersection;
T
tschw 已提交
1565
			uniforms.clippingPlanes = _clipping.uniform;
T
tschw 已提交
1566 1567 1568

		}

1569
		materialProperties.fog = fog;
1570

1571
		// store the light setup it was created for
1572

A
aardgoose 已提交
1573
		materialProperties.lightsStateVersion = lightsStateVersion;
1574

M
Mr.doob 已提交
1575
		if ( material.lights ) {
1576 1577 1578

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

1579
			uniforms.ambientLightColor.value = lights.state.ambient;
W
WestLangley 已提交
1580
			uniforms.lightProbe.value = lights.state.probe;
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
			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;
1593
			// TODO (abelnation): add area lights shadow info to uniforms
1594

1595 1596
		}

T
tschw 已提交
1597 1598
		var progUniforms = materialProperties.program.getUniforms(),
			uniformsList =
1599
				WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
A
arose 已提交
1600

T
tschw 已提交
1601
		materialProperties.uniformsList = uniformsList;
A
arose 已提交
1602

M
Mr.doob 已提交
1603
	}
M
Mr.doob 已提交
1604

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

1607
		textures.resetTextureUnits();
M
Mr.doob 已提交
1608

1609
		var materialProperties = properties.get( material );
1610
		var lights = currentRenderState.state.lights;
1611

T
tschw 已提交
1612 1613 1614 1615 1616
		if ( _clippingEnabled ) {

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

				var useCache =
1617 1618
					camera === _currentCamera &&
					material.id === _currentMaterialId;
T
tschw 已提交
1619 1620 1621 1622

				// we might want to call this function with some ClippingGroup
				// object instead of the material, once it becomes feasible
				// (#8465, #8379)
T
tschw 已提交
1623
				_clipping.setState(
1624 1625
					material.clippingPlanes, material.clipIntersection, material.clipShadows,
					camera, materialProperties, useCache );
T
tschw 已提交
1626 1627 1628 1629 1630

			}

		}

1631
		if ( material.needsUpdate === false ) {
1632

1633
			if ( materialProperties.program === undefined ) {
1634

1635
				material.needsUpdate = true;
1636

1637
			} else if ( material.fog && materialProperties.fog !== fog ) {
1638

M
Mr.doob 已提交
1639
				material.needsUpdate = true;
1640

A
aardgoose 已提交
1641
			} else if ( material.lights && materialProperties.lightsStateVersion !== lights.state.version ) {
1642

1643
				material.needsUpdate = true;
1644

1645
			} else if ( materialProperties.numClippingPlanes !== undefined &&
1646
				( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
M
Mr.doob 已提交
1647
				materialProperties.numIntersection !== _clipping.numIntersection ) ) {
1648 1649 1650

				material.needsUpdate = true;

1651
			}
1652 1653 1654 1655

		}

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

1657
			initMaterial( material, fog, object );
M
Mr.doob 已提交
1658 1659 1660 1661
			material.needsUpdate = false;

		}

1662
		var refreshProgram = false;
M
Mr.doob 已提交
1663
		var refreshMaterial = false;
1664
		var refreshLights = false;
M
Mr.doob 已提交
1665

1666
		var program = materialProperties.program,
1667
			p_uniforms = program.getUniforms(),
1668
			m_uniforms = materialProperties.shader.uniforms;
M
Mr.doob 已提交
1669

1670
		if ( state.useProgram( program.program ) ) {
M
Mr.doob 已提交
1671

1672
			refreshProgram = true;
M
Mr.doob 已提交
1673
			refreshMaterial = true;
1674
			refreshLights = true;
M
Mr.doob 已提交
1675 1676 1677 1678 1679 1680

		}

		if ( material.id !== _currentMaterialId ) {

			_currentMaterialId = material.id;
1681

M
Mr.doob 已提交
1682 1683 1684 1685
			refreshMaterial = true;

		}

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

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

G
gero3 已提交
1690
			if ( capabilities.logarithmicDepthBuffer ) {
1691

T
tschw 已提交
1692
				p_uniforms.setValue( _gl, 'logDepthBufFC',
1693
					2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
1694 1695 1696

			}

1697
			if ( _currentCamera !== camera ) {
1698

1699
				_currentCamera = camera;
1700 1701 1702 1703 1704 1705

				// 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 已提交
1706
				refreshLights = true;		// remains set until update done
1707 1708

			}
M
Mr.doob 已提交
1709

1710 1711 1712
			// load material specific uniforms
			// (shader material also gets them for the sake of genericity)

1713
			if ( material.isShaderMaterial ||
1714 1715 1716
				material.isMeshPhongMaterial ||
				material.isMeshStandardMaterial ||
				material.envMap ) {
1717

T
tschw 已提交
1718 1719 1720
				var uCamPos = p_uniforms.map.cameraPosition;

				if ( uCamPos !== undefined ) {
1721

T
tschw 已提交
1722
					uCamPos.setValue( _gl,
1723
						_vector3.setFromMatrixPosition( camera.matrixWorld ) );
1724 1725 1726 1727 1728

				}

			}

1729
			if ( material.isMeshPhongMaterial ||
1730 1731 1732 1733
				material.isMeshLambertMaterial ||
				material.isMeshBasicMaterial ||
				material.isMeshStandardMaterial ||
				material.isShaderMaterial ||
1734
				material.skinning ) {
1735

T
tschw 已提交
1736
				p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
1737 1738 1739

			}

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

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

1746
		if ( material.skinning ) {
M
Mr.doob 已提交
1747

T
tschw 已提交
1748 1749
			p_uniforms.setOptional( _gl, object, 'bindMatrix' );
			p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
1750

T
tschw 已提交
1751
			var skeleton = object.skeleton;
1752

T
tschw 已提交
1753
			if ( skeleton ) {
1754

1755 1756
				var bones = skeleton.bones;

1757
				if ( capabilities.floatVertexTextures ) {
M
Mr.doob 已提交
1758

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
					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
1770
						size = _Math.ceilPowerOfTwo( size );
1771 1772 1773 1774 1775 1776
						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 );
1777
						boneTexture.needsUpdate = true;
1778 1779 1780 1781 1782 1783 1784

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

					}

1785
					p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures );
M
Mr.doob 已提交
1786
					p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
M
Mr.doob 已提交
1787

T
tschw 已提交
1788
				} else {
M
Mr.doob 已提交
1789

T
tschw 已提交
1790
					p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
M
Mr.doob 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799

				}

			}

		}

		if ( refreshMaterial ) {

1800 1801 1802
			p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure );
			p_uniforms.setValue( _gl, 'toneMappingWhitePoint', _this.toneMappingWhitePoint );

M
Mr.doob 已提交
1803
			if ( material.lights ) {
M
Mr.doob 已提交
1804

1805
				// the current material requires lighting info
M
Mr.doob 已提交
1806

T
tschw 已提交
1807 1808 1809 1810 1811 1812
				// 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 已提交
1813

T
tschw 已提交
1814
				markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
G
gero3 已提交
1815

T
tschw 已提交
1816
			}
G
gero3 已提交
1817

T
tschw 已提交
1818
			// refresh uniforms common to several materials
G
gero3 已提交
1819

T
tschw 已提交
1820
			if ( fog && material.fog ) {
G
gero3 已提交
1821

T
tschw 已提交
1822
				refreshUniformsFog( m_uniforms, fog );
M
Mr.doob 已提交
1823 1824 1825

			}

1826
			if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
1827 1828 1829

				refreshUniformsCommon( m_uniforms, material );

1830
			} else if ( material.isMeshLambertMaterial ) {
M
Mr.doob 已提交
1831

1832 1833
				refreshUniformsCommon( m_uniforms, material );
				refreshUniformsLambert( m_uniforms, material );
M
Mr.doob 已提交
1834

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

1837
				refreshUniformsCommon( m_uniforms, material );
M
Mr.doob 已提交
1838

1839
				if ( material.isMeshToonMaterial ) {
M
Mr.doob 已提交
1840

1841
					refreshUniformsToon( m_uniforms, material );
M
Mr.doob 已提交
1842

1843
				} else {
1844

1845
					refreshUniformsPhong( m_uniforms, material );
1846

1847
				}
T
Takahiro 已提交
1848

1849
			} else if ( material.isMeshStandardMaterial ) {
T
Takahiro 已提交
1850

1851
				refreshUniformsCommon( m_uniforms, material );
1852

1853
				if ( material.isMeshPhysicalMaterial ) {
1854

1855
					refreshUniformsPhysical( m_uniforms, material );
W
WestLangley 已提交
1856

1857
				} else {
W
WestLangley 已提交
1858

1859
					refreshUniformsStandard( m_uniforms, material );
W
WestLangley 已提交
1860

1861
				}
W
WestLangley 已提交
1862

W
WestLangley 已提交
1863 1864 1865 1866 1867 1868
			} else if ( material.isMeshMatcapMaterial ) {

				refreshUniformsCommon( m_uniforms, material );

				refreshUniformsMatcap( m_uniforms, material );

1869
			} else if ( material.isMeshDepthMaterial ) {
M
Mr.doob 已提交
1870

1871
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1872
				refreshUniformsDepth( m_uniforms, material );
1873

W
WestLangley 已提交
1874
			} else if ( material.isMeshDistanceMaterial ) {
1875

1876
				refreshUniformsCommon( m_uniforms, material );
W
WestLangley 已提交
1877
				refreshUniformsDistance( m_uniforms, material );
1878

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

1881
				refreshUniformsCommon( m_uniforms, material );
1882
				refreshUniformsNormal( m_uniforms, material );
M
Mr.doob 已提交
1883

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
			} else if ( material.isLineBasicMaterial ) {

				refreshUniformsLine( m_uniforms, material );

				if ( material.isLineDashedMaterial ) {

					refreshUniformsDash( m_uniforms, material );

				}

			} else if ( material.isPointsMaterial ) {

				refreshUniformsPoints( m_uniforms, material );

1898 1899 1900 1901
			} else if ( material.isSpriteMaterial ) {

				refreshUniformsSprites( m_uniforms, material );

1902 1903
			} else if ( material.isShadowMaterial ) {

T
Takahiro 已提交
1904
				m_uniforms.color.value.copy( material.color );
1905 1906
				m_uniforms.opacity.value = material.opacity;

M
Mr.doob 已提交
1907 1908
			}

M
Mr.doob 已提交
1909 1910 1911
			// RectAreaLight Texture
			// TODO (mrdoob): Find a nicer implementation

1912 1913
			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 已提交
1914

1915
			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
A
arose 已提交
1916 1917 1918

		}

1919 1920
		if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {

1921
			WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
1922 1923 1924
			material.uniformsNeedUpdate = false;

		}
M
Mr.doob 已提交
1925

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

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

		}

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
		if ( material.color ) {

T
Takahiro 已提交
1950
			uniforms.diffuse.value.copy( material.color );
1951 1952

		}
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
		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 已提交
1986
			uniforms.flipEnvMap.value = material.envMap.isCubeTexture ? - 1 : 1;
1987 1988 1989 1990

			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

W
WestLangley 已提交
2068
				uvScaleMap.updateMatrix();
M
Mr.doob 已提交
2069

W
WestLangley 已提交
2070
			}
2071

2072
			uniforms.uvTransform.value.copy( uvScaleMap.matrix );
M
Mr.doob 已提交
2073 2074 2075

		}

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

M
Mr.doob 已提交
2078
	function refreshUniformsLine( uniforms, material ) {
M
Mr.doob 已提交
2079

T
Takahiro 已提交
2080
		uniforms.diffuse.value.copy( material.color );
M
Mr.doob 已提交
2081 2082
		uniforms.opacity.value = material.opacity;

M
Mr.doob 已提交
2083
	}
M
Mr.doob 已提交
2084

M
Mr.doob 已提交
2085
	function refreshUniformsDash( uniforms, material ) {
M
Mr.doob 已提交
2086 2087 2088 2089 2090

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

M
Mr.doob 已提交
2091
	}
M
Mr.doob 已提交
2092

M
Mr.doob 已提交
2093
	function refreshUniformsPoints( uniforms, material ) {
M
Mr.doob 已提交
2094

T
Takahiro 已提交
2095
		uniforms.diffuse.value.copy( material.color );
M
Mr.doob 已提交
2096
		uniforms.opacity.value = material.opacity;
2097
		uniforms.size.value = material.size * _pixelRatio;
2098
		uniforms.scale.value = _height * 0.5;
M
Mr.doob 已提交
2099 2100 2101

		uniforms.map.value = material.map;

2102 2103
		if ( material.map !== null ) {

W
WestLangley 已提交
2104
			if ( material.map.matrixAutoUpdate === true ) {
2105

W
WestLangley 已提交
2106
				material.map.updateMatrix();
W
WestLangley 已提交
2107 2108

			}
2109

2110
			uniforms.uvTransform.value.copy( material.map.matrix );
2111 2112 2113

		}

2114 2115 2116 2117
	}

	function refreshUniformsSprites( uniforms, material ) {

T
Takahiro 已提交
2118
		uniforms.diffuse.value.copy( material.color );
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
		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 已提交
2135
	}
M
Mr.doob 已提交
2136

M
Mr.doob 已提交
2137
	function refreshUniformsFog( uniforms, fog ) {
M
Mr.doob 已提交
2138

T
Takahiro 已提交
2139
		uniforms.fogColor.value.copy( fog.color );
M
Mr.doob 已提交
2140

2141
		if ( fog.isFog ) {
M
Mr.doob 已提交
2142 2143 2144 2145

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

2146
		} else if ( fog.isFogExp2 ) {
M
Mr.doob 已提交
2147 2148 2149 2150 2151

			uniforms.fogDensity.value = fog.density;

		}

M
Mr.doob 已提交
2152
	}
M
Mr.doob 已提交
2153

M
Mr.doob 已提交
2154
	function refreshUniformsLambert( uniforms, material ) {
2155 2156 2157 2158 2159 2160 2161 2162 2163

		if ( material.emissiveMap ) {

			uniforms.emissiveMap.value = material.emissiveMap;

		}

	}

M
Mr.doob 已提交
2164
	function refreshUniformsPhong( uniforms, material ) {
M
Mr.doob 已提交
2165

T
Takahiro 已提交
2166
		uniforms.specular.value.copy( material.specular );
M
Mr.doob 已提交
2167
		uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
M
Mr.doob 已提交
2168

2169
		if ( material.emissiveMap ) {
2170

2171
			uniforms.emissiveMap.value = material.emissiveMap;
2172

2173
		}
M
Mr.doob 已提交
2174

2175 2176 2177 2178
		if ( material.bumpMap ) {

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

2181
		}
M
Mr.doob 已提交
2182

2183 2184 2185 2186
		if ( material.normalMap ) {

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

		}
M
Mr.doob 已提交
2190

2191 2192 2193 2194 2195
		if ( material.displacementMap ) {

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

2197
		}
2198

T
Takahiro 已提交
2199 2200 2201 2202 2203 2204
	}

	function refreshUniformsToon( uniforms, material ) {

		refreshUniformsPhong( uniforms, material );

T
Takahiro 已提交
2205
		if ( material.gradientMap ) {
T
Takahiro 已提交
2206

T
Takahiro 已提交
2207
			uniforms.gradientMap.value = material.gradientMap;
T
Takahiro 已提交
2208 2209 2210

		}

2211 2212
	}

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

		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;
2240
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;
W
WestLangley 已提交
2241 2242 2243 2244 2245 2246 2247

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
2248
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();
W
WestLangley 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268

		}

		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 已提交
2269
	function refreshUniformsPhysical( uniforms, material ) {
W
WestLangley 已提交
2270

2271 2272 2273 2274
		refreshUniformsStandard( uniforms, material );

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

2275 2276
		uniforms.clearCoat.value = material.clearCoat;
		uniforms.clearCoatRoughness.value = material.clearCoatRoughness;
A
arobertson0 已提交
2277

A
arobertson0 已提交
2278 2279 2280 2281 2282
		if ( material.normalMap || material.clearCoatNormalMap) {

			if(material.clearCoatNormalMap){
				uniforms.clearCoatNormalMap.value = material.clearCoatNormalMap;
			}
2283

A
arobertson0 已提交
2284
			uniforms.clearCoatNormalScale.value.copy( material.clearCoatNormalScale );
A
arobertson0 已提交
2285 2286 2287
			if ( material.side === BackSide ){
				uniforms.clearCoatNormalScale.value.negate();
			}
A
arobertson0 已提交
2288 2289

		}
W
WestLangley 已提交
2290 2291
	}

W
WestLangley 已提交
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 2317 2318 2319 2320 2321 2322 2323 2324 2325
	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 已提交
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 2351 2352 2353
	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;

	}

2354 2355 2356 2357 2358 2359
	function refreshUniformsNormal( uniforms, material ) {

		if ( material.bumpMap ) {

			uniforms.bumpMap.value = material.bumpMap;
			uniforms.bumpScale.value = material.bumpScale;
2360
			if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1;
2361 2362 2363 2364 2365 2366 2367

		}

		if ( material.normalMap ) {

			uniforms.normalMap.value = material.normalMap;
			uniforms.normalScale.value.copy( material.normalScale );
2368
			if ( material.side === BackSide ) uniforms.normalScale.value.negate();
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381

		}

		if ( material.displacementMap ) {

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

		}

	}

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

M
Mr.doob 已提交
2384
	function markUniformsLightsNeedsUpdate( uniforms, value ) {
2385

M
Mr.doob 已提交
2386
		uniforms.ambientLightColor.needsUpdate = value;
W
WestLangley 已提交
2387
		uniforms.lightProbe.needsUpdate = value;
2388

B
Ben Houston 已提交
2389 2390 2391
		uniforms.directionalLights.needsUpdate = value;
		uniforms.pointLights.needsUpdate = value;
		uniforms.spotLights.needsUpdate = value;
2392
		uniforms.rectAreaLights.needsUpdate = value;
B
Ben Houston 已提交
2393
		uniforms.hemisphereLights.needsUpdate = value;
2394

M
Mr.doob 已提交
2395
	}
2396

2397 2398 2399
	//
	this.setFramebuffer = function ( value ) {

M
Mr.doob 已提交
2400
		if ( _framebuffer !== value ) _gl.bindFramebuffer( _gl.FRAMEBUFFER, value );
2401

2402 2403 2404 2405
		_framebuffer = value;

	};

2406 2407 2408 2409 2410 2411
	this.getActiveCubeFace = function () {

		return _currentActiveCubeFace;

	};

2412
	this.getActiveMipmapLevel = function () {
2413 2414 2415 2416 2417

		return _currentActiveMipmapLevel;

	};

2418
	this.getRenderTarget = function () {
2419 2420 2421

		return _currentRenderTarget;

M
Michael Herzog 已提交
2422
	};
2423

2424
	this.setRenderTarget = function ( renderTarget, activeCubeFace, activeMipmapLevel ) {
M
Mr.doob 已提交
2425

2426
		_currentRenderTarget = renderTarget;
2427
		_currentActiveCubeFace = activeCubeFace;
2428
		_currentActiveMipmapLevel = activeMipmapLevel;
2429

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

2432
			textures.setupRenderTarget( renderTarget );
M
Mr.doob 已提交
2433 2434 2435

		}

2436
		var framebuffer = _framebuffer;
M
Mr.doob 已提交
2437
		var isCube = false;
M
Mr.doob 已提交
2438 2439 2440

		if ( renderTarget ) {

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

M
Mr.doob 已提交
2443
			if ( renderTarget.isWebGLRenderTargetCube ) {
M
Mr.doob 已提交
2444

2445
				framebuffer = __webglFramebuffer[ activeCubeFace || 0 ];
M
Mr.doob 已提交
2446
				isCube = true;
M
Mr.doob 已提交
2447

2448 2449 2450 2451
			} else if ( renderTarget.isWebGLMultisampleRenderTarget ) {

				framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer;

M
Mr.doob 已提交
2452 2453
			} else {

M
Mr.doob 已提交
2454
				framebuffer = __webglFramebuffer;
M
Mr.doob 已提交
2455 2456 2457

			}

M
Mr.doob 已提交
2458
			_currentViewport.copy( renderTarget.viewport );
2459 2460
			_currentScissor.copy( renderTarget.scissor );
			_currentScissorTest = renderTarget.scissorTest;
2461

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

2464 2465
			_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor();
			_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor();
2466
			_currentScissorTest = _scissorTest;
2467

M
Mr.doob 已提交
2468 2469
		}

M
Mr.doob 已提交
2470
		if ( _currentFramebuffer !== framebuffer ) {
M
Mr.doob 已提交
2471 2472 2473 2474 2475 2476

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

		}

M
Mr.doob 已提交
2477
		state.viewport( _currentViewport );
2478 2479
		state.scissor( _currentScissor );
		state.setScissorTest( _currentScissorTest );
2480

M
Mr.doob 已提交
2481 2482 2483
		if ( isCube ) {

			var textureProperties = properties.get( renderTarget.texture );
2484
			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + ( activeCubeFace || 0 ), textureProperties.__webglTexture, activeMipmapLevel || 0 );
M
Mr.doob 已提交
2485 2486 2487

		}

M
Mr.doob 已提交
2488 2489
	};

2490
	this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
2491

0
06wj 已提交
2492
		if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
2493

2494
			console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
G
gero3 已提交
2495
			return;
2496

G
gero3 已提交
2497
		}
2498

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

2501
		if ( renderTarget.isWebGLRenderTargetCube && activeCubeFaceIndex !== undefined ) {
2502 2503 2504 2505 2506

			framebuffer = framebuffer[ activeCubeFaceIndex ];

		}

M
Mr.doob 已提交
2507
		if ( framebuffer ) {
2508

G
gero3 已提交
2509
			var restore = false;
2510

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

M
Mr.doob 已提交
2513
				_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
2514

G
gero3 已提交
2515
				restore = true;
2516

G
gero3 已提交
2517
			}
2518

M
Mr.doob 已提交
2519
			try {
2520

M
Mr.doob 已提交
2521
				var texture = renderTarget.texture;
M
Mr.doob 已提交
2522 2523
				var textureFormat = texture.format;
				var textureType = texture.type;
2524

2525
				if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {
2526

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

M
Mr.doob 已提交
2530
				}
2531

2532
				if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // IE11, Edge and Chrome Mac < 52 (#9513)
T
Takahiro 已提交
2533 2534
					! ( 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' ) ) ) ) {
2535

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

M
Mr.doob 已提交
2539
				}
2540

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

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

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

2547
						_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
2548 2549

					}
2550

M
Mr.doob 已提交
2551
				} else {
M
Mr.doob 已提交
2552

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

				}
M
Mr.doob 已提交
2556

M
Mr.doob 已提交
2557
			} finally {
M
Mr.doob 已提交
2558

M
Mr.doob 已提交
2559 2560 2561
				if ( restore ) {

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

M
Mr.doob 已提交
2563 2564 2565
				}

			}
M
Mr.doob 已提交
2566 2567 2568

		}

M
Mr.doob 已提交
2569 2570
	};

2571
	this.copyFramebufferToTexture = function ( position, texture, level ) {
2572 2573 2574

		var width = texture.image.width;
		var height = texture.image.height;
2575
		var glFormat = utils.convert( texture.format );
2576

2577
		textures.setTexture2D( texture, 0 );
2578

2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
		_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 );

2590
		textures.setTexture2D( dstTexture, 0 );
2591

2592 2593 2594 2595 2596 2597 2598 2599 2600
		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 );

		}
2601 2602 2603

	};

M
Mr.doob 已提交
2604
	if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
R
Rich Harris 已提交
2605

M
Mugen87 已提交
2606 2607
		__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef

2608
	}
R
Rich Harris 已提交
2609

M
Mr.doob 已提交
2610
}
T
Tristan VALCKE 已提交
2611

2612
export { WebGLRenderer };