Projector.js 15.2 KB
Newer Older
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3 4 5 6 7 8
 * @author supereggbert / http://www.paulbrunt.co.uk/
 * @author julianwa / https://github.com/julianwa
 */

THREE.Projector = function() {

M
Mr.doob 已提交
9 10 11 12 13 14
	var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
	_vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
	_face, _face3Count, _face3Pool = [], _face3PoolLength = 0,
	_face4Count, _face4Pool = [], _face4PoolLength = 0,
	_line, _lineCount, _linePool = [], _linePoolLength = 0,
	_particle, _particleCount, _particlePool = [], _particlePoolLength = 0,
15

M
Mr.doob 已提交
16
	_renderData = { objects: [], sprites: [], lights: [], elements: [] },
17

18
	_vector3 = new THREE.Vector3(),
19
	_vector4 = new THREE.Vector4(),
20

21 22
	_viewProjectionMatrix = new THREE.Matrix4(),
	_modelViewProjectionMatrix = new THREE.Matrix4(),
23

A
alteredq 已提交
24
	_frustum = new THREE.Frustum(),
25 26 27 28 29 30 31

	_clippedVertex1PositionScreen = new THREE.Vector4(),
	_clippedVertex2PositionScreen = new THREE.Vector4(),

	_face3VertexNormals;

	this.projectVector = function ( vector, camera ) {
32

33
		camera.matrixWorldInverse.getInverse( camera.matrixWorld );
34

35 36
		_viewProjectionMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
		_viewProjectionMatrix.multiplyVector3( vector );
37 38

		return vector;
39

40 41
	};

J
Justin Sermeno 已提交
42
	this.unprojectVector = function ( vector, camera ) {
43

44 45
		camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );

46 47
		_viewProjectionMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
		_viewProjectionMatrix.multiplyVector3( vector );
48

J
Justin Sermeno 已提交
49
		return vector;
50

J
Justin Sermeno 已提交
51
	};
J
Justin Sermeno 已提交
52

J
Justin Sermeno 已提交
53
	this.pickingRay = function ( vector, camera ) {
J
Justin Sermeno 已提交
54

55 56 57
		var end, ray, t;

		// set two vectors with opposing z values
J
Justin Sermeno 已提交
58 59
		vector.z = -1.0;
		end = new THREE.Vector3( vector.x, vector.y, 1.0 );
J
Justin Sermeno 已提交
60

J
Justin Sermeno 已提交
61 62 63
		this.unprojectVector( vector, camera );
		this.unprojectVector( end, camera );

64 65
		// find direction from vector to end
		end.subSelf( vector ).normalize();
66

67
		return new THREE.Ray( vector, end );
68

69 70
	};

71
	var projectGraph = function ( root, sortObjects ) {
M
Mr.doob 已提交
72 73

		_objectCount = 0;
74

75
		_renderData.objects.length = 0;
M
Mr.doob 已提交
76
		_renderData.sprites.length = 0;
77
		_renderData.lights.length = 0;
78

79
		var projectObject = function ( parent ) {
80

81
			for ( var c = 0, cl = parent.children.length; c < cl; c ++ ) {
82

83
				var object = parent.children[ c ];
84

85
				if ( object.visible === false ) continue;
86

87
				if ( object instanceof THREE.Light ) {
88

89
					_renderData.lights.push( object );
90

91
				} else if ( object instanceof THREE.Mesh || object instanceof THREE.Line ) {
92

93
					if ( object.frustumCulled === false || _frustum.contains( object ) === true ) {
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
						_object = getNextObjectInPool();
						_object.object = object;

						if ( object.renderDepth !== null ) {

							_object.z = object.renderDepth;

						} else {

							_vector3.copy( object.matrixWorld.getPosition() );
							_viewProjectionMatrix.multiplyVector3( _vector3 );
							_object.z = _vector3.z;

						}

						_renderData.objects.push( _object );
M
Mr.doob 已提交
111

112
					}
M
Mr.doob 已提交
113

114
				} else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
M
Mr.doob 已提交
115

116 117
					_object = getNextObjectInPool();
					_object.object = object;
118

119
					// TODO: Find an elegant and performant solution and remove this dupe code.
120

121
					if ( object.renderDepth !== null ) {
122

123 124 125 126 127 128 129 130 131 132 133
						_object.z = object.renderDepth;

					} else {

						_vector3.copy( object.matrixWorld.getPosition() );
						_viewProjectionMatrix.multiplyVector3( _vector3 );
						_object.z = _vector3.z;

					}

					_renderData.sprites.push( _object );
134 135 136

				} else {

137 138
					_object = getNextObjectInPool();
					_object.object = object;
139

140
					if ( object.renderDepth !== null ) {
M
Mr.doob 已提交
141

142
						_object.z = object.renderDepth;
M
Mr.doob 已提交
143

144
					} else {
145

146 147 148
						_vector3.copy( object.matrixWorld.getPosition() );
						_viewProjectionMatrix.multiplyVector3( _vector3 );
						_object.z = _vector3.z;
149

150
					}
151

152 153 154
					_renderData.objects.push( _object );

				}
M
Mr.doob 已提交
155

156
				projectObject( object );
M
Mr.doob 已提交
157 158 159 160 161

			}

		};

M
Mr.doob 已提交
162 163
		projectObject( root );

164
		if ( sortObjects === true ) _renderData.objects.sort( painterSort );
165

166
		return _renderData;
167 168 169

	};

170
	this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
171

172
		var near = camera.near, far = camera.far, visible = false,
173
		o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
174
		modelMatrix, rotationMatrix,
175
		geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
176
		faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
177
		v1, v2, v3, v4, isFaceMaterial, material, side;
178 179 180 181 182 183

		_face3Count = 0;
		_face4Count = 0;
		_lineCount = 0;
		_particleCount = 0;

184 185
		_renderData.elements.length = 0;

M
Mr.doob 已提交
186
		scene.updateMatrixWorld();
187

188 189
		if ( camera.parent === undefined ) camera.updateMatrixWorld();

190
		camera.matrixWorldInverse.getInverse( camera.matrixWorld );
191

192
		_viewProjectionMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
193

194
		_frustum.setFromMatrix( _viewProjectionMatrix );
195

196
		_renderData = projectGraph( scene, sortObjects );
197

198
		for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
199

M
Mr.doob 已提交
200
			object = _renderData.objects[ o ].object;
201

202
			modelMatrix = object.matrixWorld;
203 204 205 206 207 208

			_vertexCount = 0;

			if ( object instanceof THREE.Mesh ) {

				geometry = object.geometry;
209
				geometryMaterials = object.geometry.materials;
210 211 212 213
				vertices = geometry.vertices;
				faces = geometry.faces;
				faceVertexUvs = geometry.faceVertexUvs;

214 215
				rotationMatrix = object.matrixRotationWorld.extractRotation( modelMatrix );

216 217
				isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
				side = object.material.side;
M
Mr.doob 已提交
218

219 220 221
				for ( v = 0, vl = vertices.length; v < vl; v ++ ) {

					_vertex = getNextVertexInPool();
222
					_vertex.positionWorld.copy( vertices[ v ] );
223

224
					modelMatrix.multiplyVector3( _vertex.positionWorld );
225 226

					_vertex.positionScreen.copy( _vertex.positionWorld );
227
					_viewProjectionMatrix.multiplyVector4( _vertex.positionScreen );
228 229 230 231 232 233 234 235 236 237 238 239

					_vertex.positionScreen.x /= _vertex.positionScreen.w;
					_vertex.positionScreen.y /= _vertex.positionScreen.w;

					_vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;

				}

				for ( f = 0, fl = faces.length; f < fl; f ++ ) {

					face = faces[ f ];

240
					material = isFaceMaterial === true ? geometryMaterials[ face.materialIndex ] : object.material;
241 242 243

					if ( material === undefined ) continue;

244 245
					side = material.side;

246 247 248 249 250 251
					if ( face instanceof THREE.Face3 ) {

						v1 = _vertexPool[ face.a ];
						v2 = _vertexPool[ face.b ];
						v3 = _vertexPool[ face.c ];

M
Mr.doob 已提交
252
						if ( v1.visible === true && v2.visible === true && v3.visible === true ) {
253

254 255
							visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
								( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
256

257
							if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
258 259 260 261 262 263 264 265 266 267 268 269

								_face = getNextFace3InPool();

								_face.v1.copy( v1 );
								_face.v2.copy( v2 );
								_face.v3.copy( v3 );

							} else {

								continue;

							}
270 271 272 273 274 275 276 277 278 279 280 281 282 283

						} else {

							continue;

						}

					} else if ( face instanceof THREE.Face4 ) {

						v1 = _vertexPool[ face.a ];
						v2 = _vertexPool[ face.b ];
						v3 = _vertexPool[ face.c ];
						v4 = _vertexPool[ face.d ];

M
Mr.doob 已提交
284
						if ( v1.visible === true && v2.visible === true && v3.visible === true && v4.visible === true ) {
285 286 287 288 289 290 291

							visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
								( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
								( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
								( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;


292
							if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
293 294 295 296 297 298 299 300 301

								_face = getNextFace4InPool();

								_face.v1.copy( v1 );
								_face.v2.copy( v2 );
								_face.v3.copy( v3 );
								_face.v4.copy( v4 );

							} else {
302

303
								continue;
304

305
							}
306 307 308 309 310 311 312 313 314 315

						} else {

							continue;

						}

					}

					_face.normalWorld.copy( face.normal );
316

317
					if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) _face.normalWorld.negate();
318
					rotationMatrix.multiplyVector3( _face.normalWorld );
319 320

					_face.centroidWorld.copy( face.centroid );
321
					modelMatrix.multiplyVector3( _face.centroidWorld );
322 323

					_face.centroidScreen.copy( _face.centroidWorld );
324
					_viewProjectionMatrix.multiplyVector3( _face.centroidScreen );
325 326 327 328 329 330 331

					faceVertexNormals = face.vertexNormals;

					for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {

						normal = _face.vertexNormalsWorld[ n ];
						normal.copy( faceVertexNormals[ n ] );
M
Mr.doob 已提交
332

333
						if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) normal.negate();
M
Mr.doob 已提交
334

335
						rotationMatrix.multiplyVector3( normal );
336 337 338

					}

339
					_face.vertexNormalsLength = faceVertexNormals.length;
340

341 342 343 344
					for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {

						uvs = faceVertexUvs[ c ][ f ];

M
Mr.doob 已提交
345
						if ( uvs === undefined ) continue;
346 347 348 349 350 351 352 353 354

						for ( u = 0, ul = uvs.length; u < ul; u ++ ) {

							_face.uvs[ c ][ u ] = uvs[ u ];

						}

					}

355
					_face.material = material;
356 357 358

					_face.z = _face.centroidScreen.z;

359
					_renderData.elements.push( _face );
360 361 362

				}

M
Mr.doob 已提交
363
			} else if ( object instanceof THREE.Line ) {
M
Mr.doob 已提交
364

365
				_modelViewProjectionMatrix.multiply( _viewProjectionMatrix, modelMatrix );
366 367

				vertices = object.geometry.vertices;
M
Mr.doob 已提交
368

369
				v1 = getNextVertexInPool();
370
				v1.positionScreen.copy( vertices[ 0 ] );
371
				_modelViewProjectionMatrix.multiplyVector4( v1.positionScreen );
372

M
Mr.doob 已提交
373 374 375 376
				// Handle LineStrip and LinePieces
				var step = object.type === THREE.LinePieces ? 2 : 1;

				for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
377 378

					v1 = getNextVertexInPool();
379
					v1.positionScreen.copy( vertices[ v ] );
380
					_modelViewProjectionMatrix.multiplyVector4( v1.positionScreen );
381

M
Mr.doob 已提交
382
					if ( ( v + 1 ) % step > 0 ) continue;
M
Mr.doob 已提交
383

384 385 386 387 388
					v2 = _vertexPool[ _vertexCount - 2 ];

					_clippedVertex1PositionScreen.copy( v1.positionScreen );
					_clippedVertex2PositionScreen.copy( v2.positionScreen );

M
Mr.doob 已提交
389
					if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
390 391 392 393 394 395 396 397 398 399 400

						// Perform the perspective divide
						_clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
						_clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );

						_line = getNextLineInPool();
						_line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
						_line.v2.positionScreen.copy( _clippedVertex2PositionScreen );

						_line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );

401
						_line.material = object.material;
402

403
						_renderData.elements.push( _line );
404 405

					}
M
Mr.doob 已提交
406

407 408
				}

M
Mr.doob 已提交
409
			}
M
Mr.doob 已提交
410

M
Mr.doob 已提交
411 412 413 414
		}

		for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {

M
Mr.doob 已提交
415
			object = _renderData.sprites[ o ].object;
M
Mr.doob 已提交
416

417
			modelMatrix = object.matrixWorld;
M
Mr.doob 已提交
418 419

			if ( object instanceof THREE.Particle ) {
420

421 422
				_vector4.set( modelMatrix.elements[12], modelMatrix.elements[13], modelMatrix.elements[14], 1 );
				_viewProjectionMatrix.multiplyVector4( _vector4 );
423 424 425 426 427 428

				_vector4.z /= _vector4.w;

				if ( _vector4.z > 0 && _vector4.z < 1 ) {

					_particle = getNextParticleInPool();
429
					_particle.object = object;
430 431 432 433 434 435
					_particle.x = _vector4.x / _vector4.w;
					_particle.y = _vector4.y / _vector4.w;
					_particle.z = _vector4.z;

					_particle.rotation = object.rotation.z;

436 437
					_particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
					_particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
438

439
					_particle.material = object.material;
440

441
					_renderData.elements.push( _particle );
442 443 444 445 446 447 448

				}

			}

		}

449
		if ( sortElements === true ) _renderData.elements.sort( painterSort );
450

451
		return _renderData;
452 453 454 455 456

	};

	// Pools

M
Mr.doob 已提交
457 458
	function getNextObjectInPool() {

M
Mr.doob 已提交
459
		if ( _objectCount === _objectPoolLength ) {
M
Mr.doob 已提交
460

M
Mr.doob 已提交
461
			var object = new THREE.RenderableObject();
M
Mr.doob 已提交
462
			_objectPool.push( object );
M
Mr.doob 已提交
463 464 465
			_objectPoolLength ++;
			_objectCount ++;
			return object;
M
Mr.doob 已提交
466 467

		}
M
Mr.doob 已提交
468

M
Mr.doob 已提交
469
		return _objectPool[ _objectCount ++ ];
M
Mr.doob 已提交
470 471 472

	}

473 474
	function getNextVertexInPool() {

M
Mr.doob 已提交
475
		if ( _vertexCount === _vertexPoolLength ) {
M
Mr.doob 已提交
476

M
Mr.doob 已提交
477
			var vertex = new THREE.RenderableVertex();
M
Mr.doob 已提交
478
			_vertexPool.push( vertex );
M
Mr.doob 已提交
479 480 481
			_vertexPoolLength ++;
			_vertexCount ++;
			return vertex;
M
Mr.doob 已提交
482 483

		}
484

M
Mr.doob 已提交
485
		return _vertexPool[ _vertexCount ++ ];
486 487 488 489 490

	}

	function getNextFace3InPool() {

M
Mr.doob 已提交
491
		if ( _face3Count === _face3PoolLength ) {
M
Mr.doob 已提交
492

M
Mr.doob 已提交
493
			var face = new THREE.RenderableFace3();
M
Mr.doob 已提交
494
			_face3Pool.push( face );
M
Mr.doob 已提交
495 496 497
			_face3PoolLength ++;
			_face3Count ++;
			return face;
M
Mr.doob 已提交
498 499

		}
500

M
Mr.doob 已提交
501
		return _face3Pool[ _face3Count ++ ];
502

M
Mr.doob 已提交
503

504 505 506 507
	}

	function getNextFace4InPool() {

M
Mr.doob 已提交
508
		if ( _face4Count === _face4PoolLength ) {
M
Mr.doob 已提交
509

M
Mr.doob 已提交
510
			var face = new THREE.RenderableFace4();
M
Mr.doob 已提交
511
			_face4Pool.push( face );
M
Mr.doob 已提交
512 513 514
			_face4PoolLength ++;
			_face4Count ++;
			return face;
M
Mr.doob 已提交
515 516

		}
517

M
Mr.doob 已提交
518
		return _face4Pool[ _face4Count ++ ];
519 520 521 522 523

	}

	function getNextLineInPool() {

M
Mr.doob 已提交
524
		if ( _lineCount === _linePoolLength ) {
M
Mr.doob 已提交
525

M
Mr.doob 已提交
526
			var line = new THREE.RenderableLine();
M
Mr.doob 已提交
527
			_linePool.push( line );
M
Mr.doob 已提交
528 529 530
			_linePoolLength ++;
			_lineCount ++
			return line;
M
Mr.doob 已提交
531 532

		}
533

M
Mr.doob 已提交
534
		return _linePool[ _lineCount ++ ];
535 536 537 538 539

	}

	function getNextParticleInPool() {

M
Mr.doob 已提交
540
		if ( _particleCount === _particlePoolLength ) {
M
Mr.doob 已提交
541

M
Mr.doob 已提交
542
			var particle = new THREE.RenderableParticle();
M
Mr.doob 已提交
543
			_particlePool.push( particle );
M
Mr.doob 已提交
544 545 546
			_particlePoolLength ++;
			_particleCount ++
			return particle;
M
Mr.doob 已提交
547 548 549

		}

M
Mr.doob 已提交
550
		return _particlePool[ _particleCount ++ ];
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	}

	//

	function painterSort( a, b ) {

		return b.z - a.z;

	}

	function clipLine( s1, s2 ) {

		var alpha1 = 0, alpha2 = 1,

		// Calculate the boundary coordinate of each vertex for the near and far clip planes,
		// Z = -1 and Z = +1, respectively.
		bc1near =  s1.z + s1.w,
		bc2near =  s2.z + s2.w,
		bc1far =  - s1.z + s1.w,
		bc2far =  - s2.z + s2.w;

		if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {

			// Both vertices lie entirely within all clip planes.
			return true;

		} else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {

			// Both vertices lie entirely outside one of the clip planes.
			return false;

		} else {

			// The line segment spans at least one clip plane.

			if ( bc1near < 0 ) {

				// v1 lies outside the near plane, v2 inside
				alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );

			} else if ( bc2near < 0 ) {

				// v2 lies outside the near plane, v1 inside
				alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );

			}

			if ( bc1far < 0 ) {

				// v1 lies outside the far plane, v2 inside
				alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );

			} else if ( bc2far < 0 ) {

				// v2 lies outside the far plane, v2 inside
				alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );

			}

			if ( alpha2 < alpha1 ) {

				// The line segment spans two boundaries, but is outside both of them.
				// (This can't happen when we're only clipping against just near/far but good
				//  to leave the check here for future usage if other clip planes are added.)
				return false;

			} else {

				// Update the s1 and s2 vertices to match the clipped line segment.
				s1.lerpSelf( s2, alpha1 );
				s2.lerpSelf( s1, 1 - alpha2 );

				return true;

			}

		}

	}

};