Geometry.js 22.9 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9 10 11
/**
 * @author mrdoob / http://mrdoob.com/
 * @author kile / http://kile.stravaganza.org/
 * @author alteredq / http://alteredqualia.com/
 * @author mikael emtinger / http://gomo.se/
 * @author zz85 / http://www.lab4games.net/zz85/blog
 * @author bhouston / http://exocortex.com
 */

THREE.Geometry = function () {

M
Mr.doob 已提交
12 13
	Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );

M
Mr.doob 已提交
14 15 16
	this.uuid = THREE.Math.generateUUID();

	this.name = '';
17
	this.type = 'Geometry';
M
Mr.doob 已提交
18

M
Mr.doob 已提交
19 20
	this.vertices = [];
	this.colors = [];
M
Mr.doob 已提交
21
	this.faces = [];
22
	this.faceVertexUvs = [ [] ];
M
Mr.doob 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

	this.morphTargets = [];
	this.morphColors = [];
	this.morphNormals = [];

	this.skinWeights = [];
	this.skinIndices = [];

	this.lineDistances = [];

	this.boundingBox = null;
	this.boundingSphere = null;

	this.hasTangents = false;

	// update flags

	this.verticesNeedUpdate = false;
	this.elementsNeedUpdate = false;
	this.uvsNeedUpdate = false;
	this.normalsNeedUpdate = false;
	this.tangentsNeedUpdate = false;
	this.colorsNeedUpdate = false;
	this.lineDistancesNeedUpdate = false;

};

THREE.Geometry.prototype = {

	constructor: THREE.Geometry,

	applyMatrix: function ( matrix ) {

		var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );

		for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {

			var vertex = this.vertices[ i ];
			vertex.applyMatrix4( matrix );

		}

		for ( var i = 0, il = this.faces.length; i < il; i ++ ) {

			var face = this.faces[ i ];
			face.normal.applyMatrix3( normalMatrix ).normalize();

			for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {

				face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();

			}

		}

M
Mr.doob 已提交
78
		if ( this.boundingBox !== null ) {
M
Mr.doob 已提交
79 80 81 82 83

			this.computeBoundingBox();

		}

M
Mr.doob 已提交
84
		if ( this.boundingSphere !== null ) {
M
Mr.doob 已提交
85 86 87 88 89

			this.computeBoundingSphere();

		}

M
Mr.doob 已提交
90 91 92
		this.verticesNeedUpdate = true;
		this.normalsNeedUpdate = true;

M
Mr.doob 已提交
93 94
	},

95 96 97 98 99 100 101
	fromBufferGeometry: function ( geometry ) {

		var scope = this;

		var attributes = geometry.attributes;

		var vertices = attributes.position.array;
M
Mr.doob 已提交
102 103 104 105
		var indices = attributes.index !== undefined ? attributes.index.array : undefined;
		var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
		var colors = attributes.color !== undefined ? attributes.color.array : undefined;
		var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
106 107 108 109 110
		var uvs2 = attributes.uv2 !== undefined ? attributes.uv2.array : undefined;
		var tangents = attributes.tangent !== undefined ? attributes.tangent.array : undefined;

		if ( uvs2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
		if ( tangents !== undefined ) this.hasTangents = true;
111 112 113

		var tempNormals = [];
		var tempUVs = [];
114 115
		var tempUVs2 = [];
		var tempTangents = [];
116

117
		for ( var i = 0, j = 0, k = 0; i < vertices.length; i += 3, j += 2, k += 4 ) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

			scope.vertices.push( new THREE.Vector3( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ) );

			if ( normals !== undefined ) {

				tempNormals.push( new THREE.Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );

			}

			if ( colors !== undefined ) {

				scope.colors.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );

			}

			if ( uvs !== undefined ) {

				tempUVs.push( new THREE.Vector2( uvs[ j ], uvs[ j + 1 ] ) );

			}

139 140 141 142 143 144 145 146 147 148 149 150
			if ( uvs2 !== undefined ) {

				tempUVs2.push( new THREE.Vector2( uvs2[ j ], uvs2[ j + 1 ] ) );

			}

			if ( tangents !== undefined ) {

				tempTangents.push( new THREE.Vector4( tangents[ k ], tangents[ k + 1 ], tangents[ k + 2 ], tangents[ k + 3 ] ) );

			}

151 152 153 154
		}

		var addFace = function ( a, b, c ) {

155
			var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
156
			var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
157

158 159 160
			var face = new THREE.Face3( a, b, c, vertexNormals, vertexColors );

			scope.faces.push( face );
161 162 163

			if ( uvs !== undefined ) {

164
				scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
165 166

			}
167

168 169 170 171 172 173 174 175 176 177 178 179
			if ( uvs2 !== undefined ) {

				scope.faceVertexUvs[ 1 ].push( [ tempUVs2[ a ].clone(), tempUVs2[ b ].clone(), tempUVs2[ c ].clone() ] );

			}

			if ( tangents !== undefined ) {

				face.vertexTangents.push( tempTangents[ a ].clone(), tempTangents[ b ].clone(), tempTangents[ c ].clone() );

			}

180 181 182 183
		};

		if ( indices !== undefined ) {

184
			var drawcalls = geometry.drawcalls;
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
			if ( drawcalls.length > 0 ) {

				for ( var i = 0; i < drawcalls.length; i ++ ) {

					var drawcall = drawcalls[ i ];

					var start = drawcall.start;
					var count = drawcall.count;
					var index = drawcall.index;

					for ( var j = start, jl = start + count; j < jl; j += 3 ) {

						addFace( index + indices[ j ], index + indices[ j + 1 ], index + indices[ j + 2 ] );

					}

				}

			} else {

				for ( var i = 0; i < indices.length; i += 3 ) {

					addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );

				}
211 212 213 214 215 216 217 218 219 220 221 222

			}

		} else {

			for ( var i = 0; i < vertices.length / 3; i += 3 ) {

				addFace( i, i + 1, i + 2 );

			}

		}
223

M
Mr.doob 已提交
224
		this.computeFaceNormals();
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

		if ( geometry.boundingBox !== null ) {

			this.boundingBox = geometry.boundingBox.clone();

		}

		if ( geometry.boundingSphere !== null ) {

			this.boundingSphere = geometry.boundingSphere.clone();

		}

		return this;

	},

242 243 244 245
	center: function () {

		this.computeBoundingBox();

M
Mr.doob 已提交
246
		var offset = this.boundingBox.center().negate();
247

M
Mr.doob 已提交
248
		this.applyMatrix( new THREE.Matrix4().setPosition( offset ) );
249 250 251 252 253

		return offset;

	},

M
Mr.doob 已提交
254
	normalize: function () {
Z
update  
Zhonghua Xi 已提交
255 256 257

		this.computeBoundingSphere();

M
Mr.doob 已提交
258 259
		var center = this.boundingSphere.center;
		var radius = this.boundingSphere.radius;
Z
Zhonghua Xi 已提交
260

M
Mr.doob 已提交
261
		var s = radius === 0 ? 1 : 1.0 / radius;
Z
Zhonghua Xi 已提交
262

M
Mr.doob 已提交
263 264
		var matrix = new THREE.Matrix4();
		matrix.set(
G
gero3 已提交
265 266 267
			s, 0, 0, - s * center.x,
			0, s, 0, - s * center.y,
			0, 0, s, - s * center.z,
M
Mr.doob 已提交
268 269
			0, 0, 0, 1
		);
Z
Zhonghua Xi 已提交
270

M
Mr.doob 已提交
271
		this.applyMatrix( matrix );
Z
Zhonghua Xi 已提交
272 273

		return this;
G
gero3 已提交
274

Z
Zhonghua Xi 已提交
275 276
	},

M
Mr.doob 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	computeFaceNormals: function () {

		var cb = new THREE.Vector3(), ab = new THREE.Vector3();

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

			var face = this.faces[ f ];

			var vA = this.vertices[ face.a ];
			var vB = this.vertices[ face.b ];
			var vC = this.vertices[ face.c ];

			cb.subVectors( vC, vB );
			ab.subVectors( vA, vB );
			cb.cross( ab );

			cb.normalize();

			face.normal.copy( cb );

		}

	},

	computeVertexNormals: function ( areaWeighted ) {

		var v, vl, f, fl, face, vertices;

		vertices = new Array( this.vertices.length );

		for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

			vertices[ v ] = new THREE.Vector3();

		}

		if ( areaWeighted ) {

			// vertex normals weighted by triangle areas
			// http://www.iquilezles.org/www/articles/normals/normals.htm

B
brason 已提交
318 319
			var vA, vB, vC;
			var cb = new THREE.Vector3(), ab = new THREE.Vector3();
M
Mr.doob 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

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

				face = this.faces[ f ];

				vA = this.vertices[ face.a ];
				vB = this.vertices[ face.b ];
				vC = this.vertices[ face.c ];

				cb.subVectors( vC, vB );
				ab.subVectors( vA, vB );
				cb.cross( ab );

				vertices[ face.a ].add( cb );
				vertices[ face.b ].add( cb );
				vertices[ face.c ].add( cb );

			}

		} else {

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

				face = this.faces[ f ];

				vertices[ face.a ].add( face.normal );
				vertices[ face.b ].add( face.normal );
				vertices[ face.c ].add( face.normal );

			}

		}

		for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

			vertices[ v ].normalize();

		}

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

			face = this.faces[ f ];

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
			var vertexNormals = face.vertexNormals;

			if ( vertexNormals.length === 3 ) {

				vertexNormals[ 0 ].copy( vertices[ face.a ] );
				vertexNormals[ 1 ].copy( vertices[ face.b ] );
				vertexNormals[ 2 ].copy( vertices[ face.c ] );

			} else {

				vertexNormals[ 0 ] = vertices[ face.a ].clone();
				vertexNormals[ 1 ] = vertices[ face.b ].clone();
				vertexNormals[ 2 ] = vertices[ face.c ].clone();

			}
M
Mr.doob 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

		}

	},

	computeMorphNormals: function () {

		var i, il, f, fl, face;

		// save original normals
		// - create temp variables on first access
		//   otherwise just copy (for faster repeated calls)

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

			face = this.faces[ f ];

			if ( ! face.__originalFaceNormal ) {

				face.__originalFaceNormal = face.normal.clone();

			} else {

				face.__originalFaceNormal.copy( face.normal );

			}

			if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];

			for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {

				if ( ! face.__originalVertexNormals[ i ] ) {

					face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();

				} else {

					face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );

				}

			}

		}

		// use temp geometry to compute face and vertex normals for each morph

		var tmpGeo = new THREE.Geometry();
		tmpGeo.faces = this.faces;

		for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {

			// create on first access

			if ( ! this.morphNormals[ i ] ) {

				this.morphNormals[ i ] = {};
				this.morphNormals[ i ].faceNormals = [];
				this.morphNormals[ i ].vertexNormals = [];

				var dstNormalsFace = this.morphNormals[ i ].faceNormals;
				var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;

				var faceNormal, vertexNormals;

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

					faceNormal = new THREE.Vector3();
					vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };

					dstNormalsFace.push( faceNormal );
					dstNormalsVertex.push( vertexNormals );

				}

			}

			var morphNormals = this.morphNormals[ i ];

			// set vertices to morph target

			tmpGeo.vertices = this.morphTargets[ i ].vertices;

			// compute morph normals

			tmpGeo.computeFaceNormals();
			tmpGeo.computeVertexNormals();

			// store morph normals

			var faceNormal, vertexNormals;

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

				face = this.faces[ f ];

				faceNormal = morphNormals.faceNormals[ f ];
				vertexNormals = morphNormals.vertexNormals[ f ];

				faceNormal.copy( face.normal );

				vertexNormals.a.copy( face.vertexNormals[ 0 ] );
				vertexNormals.b.copy( face.vertexNormals[ 1 ] );
				vertexNormals.c.copy( face.vertexNormals[ 2 ] );

			}

		}

		// restore original normals

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

			face = this.faces[ f ];

			face.normal = face.__originalFaceNormal;
			face.vertexNormals = face.__originalVertexNormals;

		}

	},

	computeTangents: function () {

		// based on http://www.terathon.com/code/tangent.html
		// tangents go to vertices

B
brason 已提交
505
		var f, fl, v, vl, i, vertexIndex,
M
Mr.doob 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 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
			face, uv, vA, vB, vC, uvA, uvB, uvC,
			x1, x2, y1, y2, z1, z2,
			s1, s2, t1, t2, r, t, test,
			tan1 = [], tan2 = [],
			sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
			tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
			n = new THREE.Vector3(), w;

		for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

			tan1[ v ] = new THREE.Vector3();
			tan2[ v ] = new THREE.Vector3();

		}

		function handleTriangle( context, a, b, c, ua, ub, uc ) {

			vA = context.vertices[ a ];
			vB = context.vertices[ b ];
			vC = context.vertices[ c ];

			uvA = uv[ ua ];
			uvB = uv[ ub ];
			uvC = uv[ uc ];

			x1 = vB.x - vA.x;
			x2 = vC.x - vA.x;
			y1 = vB.y - vA.y;
			y2 = vC.y - vA.y;
			z1 = vB.z - vA.z;
			z2 = vC.z - vA.z;

			s1 = uvB.x - uvA.x;
			s2 = uvC.x - uvA.x;
			t1 = uvB.y - uvA.y;
			t2 = uvC.y - uvA.y;

			r = 1.0 / ( s1 * t2 - s2 * t1 );
			sdir.set( ( t2 * x1 - t1 * x2 ) * r,
					  ( t2 * y1 - t1 * y2 ) * r,
					  ( t2 * z1 - t1 * z2 ) * r );
			tdir.set( ( s1 * x2 - s2 * x1 ) * r,
					  ( s1 * y2 - s2 * y1 ) * r,
					  ( s1 * z2 - s2 * z1 ) * r );

			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );

			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );

		}

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

			face = this.faces[ f ];
			uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents

			handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );

		}

		var faceIndex = [ 'a', 'b', 'c', 'd' ];

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

			face = this.faces[ f ];

576
			for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i ++ ) {
M
Mr.doob 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

				n.copy( face.vertexNormals[ i ] );

				vertexIndex = face[ faceIndex[ i ] ];

				t = tan1[ vertexIndex ];

				// Gram-Schmidt orthogonalize

				tmp.copy( t );
				tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();

				// Calculate handedness

				tmp2.crossVectors( face.vertexNormals[ i ], t );
				test = tmp2.dot( tan2[ vertexIndex ] );
593
				w = ( test < 0.0 ) ? - 1.0 : 1.0;
M
Mr.doob 已提交
594 595 596 597 598 599 600 601 602 603 604

				face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );

			}

		}

		this.hasTangents = true;

	},

605
	computeLineDistances: function () {
M
Mr.doob 已提交
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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

		var d = 0;
		var vertices = this.vertices;

		for ( var i = 0, il = vertices.length; i < il; i ++ ) {

			if ( i > 0 ) {

				d += vertices[ i ].distanceTo( vertices[ i - 1 ] );

			}

			this.lineDistances[ i ] = d;

		}

	},

	computeBoundingBox: function () {

		if ( this.boundingBox === null ) {

			this.boundingBox = new THREE.Box3();

		}

		this.boundingBox.setFromPoints( this.vertices );

	},

	computeBoundingSphere: function () {

		if ( this.boundingSphere === null ) {

			this.boundingSphere = new THREE.Sphere();

		}

		this.boundingSphere.setFromPoints( this.vertices );

	},

M
Mr.doob 已提交
648
	merge: function ( geometry, matrix ) {
649

650 651
		if ( geometry instanceof THREE.Geometry === false ) {

652
			console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
653 654 655 656
			return;

		}

M
Mr.doob 已提交
657
		var normalMatrix,
658 659
		vertexOffset = this.vertices.length,
		vertices1 = this.vertices,
M
Mr.doob 已提交
660
		vertices2 = geometry.vertices,
661
		faces1 = this.faces,
M
Mr.doob 已提交
662
		faces2 = geometry.faces,
663
		uvs1 = this.faceVertexUvs[ 0 ],
M
Mr.doob 已提交
664
		uvs2 = geometry.faceVertexUvs[ 0 ];
665

M
Mr.doob 已提交
666
		if ( matrix !== undefined ) {
667 668 669 670 671 672 673 674 675 676 677 678 679

			normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );

		}

		// vertices

		for ( var i = 0, il = vertices2.length; i < il; i ++ ) {

			var vertex = vertices2[ i ];

			var vertexCopy = vertex.clone();

M
Mr.doob 已提交
680
			if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

			vertices1.push( vertexCopy );

		}

		// faces

		for ( i = 0, il = faces2.length; i < il; i ++ ) {

			var face = faces2[ i ], faceCopy, normal, color,
			faceVertexNormals = face.vertexNormals,
			faceVertexColors = face.vertexColors;

			faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
			faceCopy.normal.copy( face.normal );

M
Mr.doob 已提交
697
			if ( normalMatrix !== undefined ) {
698 699 700 701 702 703 704 705 706

				faceCopy.normal.applyMatrix3( normalMatrix ).normalize();

			}

			for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {

				normal = faceVertexNormals[ j ].clone();

M
Mr.doob 已提交
707
				if ( normalMatrix !== undefined ) {
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

					normal.applyMatrix3( normalMatrix ).normalize();

				}

				faceCopy.vertexNormals.push( normal );

			}

			faceCopy.color.copy( face.color );

			for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {

				color = faceVertexColors[ j ];
				faceCopy.vertexColors.push( color.clone() );

			}

			faces1.push( faceCopy );

		}

		// uvs

		for ( i = 0, il = uvs2.length; i < il; i ++ ) {

			var uv = uvs2[ i ], uvCopy = [];
735

736
			if ( uv === undefined ) {
737

738
				continue;
739

740 741 742 743
			}

			for ( var j = 0, jl = uv.length; j < jl; j ++ ) {

M
Mr.doob 已提交
744
				uvCopy.push( uv[ j ].clone() );
745 746 747 748 749 750 751 752 753

			}

			uvs1.push( uvCopy );

		}

	},

754 755 756 757
	mergeMesh: function ( mesh ) {

		if ( mesh instanceof THREE.Mesh === false ) {

758
			console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
759 760 761 762 763 764 765 766 767 768
			return;

		}

		mesh.matrixAutoUpdate && mesh.updateMatrix();

		this.merge( mesh.geometry, mesh.matrix );

	},

M
Mr.doob 已提交
769 770 771 772 773 774 775 776
	/*
	 * Checks for duplicate vertices with hashmap.
	 * Duplicated vertices are removed
	 * and faces' vertices are updated.
	 */

	mergeVertices: function () {

D
dubejf 已提交
777
		var verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
M
Mr.doob 已提交
778 779 780
		var unique = [], changes = [];

		var v, key;
D
dubejf 已提交
781
		var precisionPoints = 4; // number of decimal points, e.g. 4 for epsilon of 0.0001
M
Mr.doob 已提交
782
		var precision = Math.pow( 10, precisionPoints );
B
brason 已提交
783 784
		var i, il, face;
		var indices, j, jl;
M
Mr.doob 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798

		for ( i = 0, il = this.vertices.length; i < il; i ++ ) {

			v = this.vertices[ i ];
			key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );

			if ( verticesMap[ key ] === undefined ) {

				verticesMap[ key ] = i;
				unique.push( this.vertices[ i ] );
				changes[ i ] = unique.length - 1;

			} else {

799
				//console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
M
Mr.doob 已提交
800 801 802 803
				changes[ i ] = changes[ verticesMap[ key ] ];

			}

B
brason 已提交
804
		}
M
Mr.doob 已提交
805 806 807 808 809 810


		// if faces are completely degenerate after merging vertices, we
		// have to remove them from the geometry.
		var faceIndicesToRemove = [];

811
		for ( i = 0, il = this.faces.length; i < il; i ++ ) {
M
Mr.doob 已提交
812 813 814 815 816 817 818 819 820

			face = this.faces[ i ];

			face.a = changes[ face.a ];
			face.b = changes[ face.b ];
			face.c = changes[ face.c ];

			indices = [ face.a, face.b, face.c ];

821
			var dupIndex = - 1;
M
Mr.doob 已提交
822 823 824 825

			// if any duplicate vertices are found in a Face3
			// we have to remove the face as nothing can be saved
			for ( var n = 0; n < 3; n ++ ) {
G
gero3 已提交
826

F
Fabian Lange 已提交
827
				if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
M
Mr.doob 已提交
828 829 830 831 832 833

					dupIndex = n;
					faceIndicesToRemove.push( i );
					break;

				}
G
gero3 已提交
834

M
Mr.doob 已提交
835 836 837 838 839
			}

		}

		for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
G
gero3 已提交
840

M
Mr.doob 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
			var idx = faceIndicesToRemove[ i ];

			this.faces.splice( idx, 1 );

			for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {

				this.faceVertexUvs[ j ].splice( idx, 1 );

			}

		}

		// Use unique set of vertices

		var diff = this.vertices.length - unique.length;
		this.vertices = unique;
		return diff;

	},

861
	toJSON: function () {
862

863 864 865 866 867 868
		var data = {
			metadata: {
				version: 4.4,
				type: 'Geometry',
				generator: 'Geometry.toJSON'
			}
869 870
		};

871
		// standard Geometry serialization
872

873
		data.uuid = this.uuid;
M
Mr.doob 已提交
874
		data.type = this.type;
875
		if ( this.name !== '' ) data.name = this.name;
876

877
		if ( this.parameters !== undefined ) {
878 879 880 881 882

			var parameters = this.parameters;

			for ( var key in parameters ) {

883
				if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
884 885 886

			}

887
			return data;
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

		}

		var vertices = [];

		for ( var i = 0; i < this.vertices.length; i ++ ) {

			var vertex = this.vertices[ i ];
			vertices.push( vertex.x, vertex.y, vertex.z );

		}

		var faces = [];
		var normals = [];
		var normalsHash = {};
		var colors = [];
		var colorsHash = {};
		var uvs = [];
		var uvsHash = {};

		for ( var i = 0; i < this.faces.length; i ++ ) {

			var face = this.faces[ i ];

			var hasMaterial = false; // face.materialIndex !== undefined;
			var hasFaceUv = false; // deprecated
			var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
			var hasFaceNormal = face.normal.length() > 0;
			var hasFaceVertexNormal = face.vertexNormals.length > 0;
			var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
			var hasFaceVertexColor = face.vertexColors.length > 0;

			var faceType = 0;

			faceType = setBit( faceType, 0, 0 );
			faceType = setBit( faceType, 1, hasMaterial );
			faceType = setBit( faceType, 2, hasFaceUv );
			faceType = setBit( faceType, 3, hasFaceVertexUv );
			faceType = setBit( faceType, 4, hasFaceNormal );
			faceType = setBit( faceType, 5, hasFaceVertexNormal );
			faceType = setBit( faceType, 6, hasFaceColor );
			faceType = setBit( faceType, 7, hasFaceVertexColor );

			faces.push( faceType );
			faces.push( face.a, face.b, face.c );

			if ( hasFaceVertexUv ) {

				var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];

				faces.push(
					getUvIndex( faceVertexUvs[ 0 ] ),
					getUvIndex( faceVertexUvs[ 1 ] ),
					getUvIndex( faceVertexUvs[ 2 ] )
				);

			}

			if ( hasFaceNormal ) {

				faces.push( getNormalIndex( face.normal ) );

			}

			if ( hasFaceVertexNormal ) {

				var vertexNormals = face.vertexNormals;

				faces.push(
					getNormalIndex( vertexNormals[ 0 ] ),
					getNormalIndex( vertexNormals[ 1 ] ),
					getNormalIndex( vertexNormals[ 2 ] )
				);

			}

			if ( hasFaceColor ) {

				faces.push( getColorIndex( face.color ) );

			}

			if ( hasFaceVertexColor ) {

				var vertexColors = face.vertexColors;

				faces.push(
					getColorIndex( vertexColors[ 0 ] ),
					getColorIndex( vertexColors[ 1 ] ),
					getColorIndex( vertexColors[ 2 ] )
				);

			}

		}

		function setBit( value, position, enabled ) {

G
gero3 已提交
986
			return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

		}

		function getNormalIndex( normal ) {

			var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();

			if ( normalsHash[ hash ] !== undefined ) {

				return normalsHash[ hash ];

			}

			normalsHash[ hash ] = normals.length / 3;
			normals.push( normal.x, normal.y, normal.z );

			return normalsHash[ hash ];

		}

		function getColorIndex( color ) {

			var hash = color.r.toString() + color.g.toString() + color.b.toString();

			if ( colorsHash[ hash ] !== undefined ) {

				return colorsHash[ hash ];

			}

			colorsHash[ hash ] = colors.length;
			colors.push( color.getHex() );

			return colorsHash[ hash ];

		}

		function getUvIndex( uv ) {

			var hash = uv.x.toString() + uv.y.toString();

			if ( uvsHash[ hash ] !== undefined ) {

				return uvsHash[ hash ];

			}

			uvsHash[ hash ] = uvs.length / 2;
			uvs.push( uv.x, uv.y );

			return uvsHash[ hash ];

		}

1041
		data.data = {};
1042

1043 1044 1045 1046 1047
		data.data.vertices = vertices;
		data.data.normals = normals;
		if ( colors.length > 0 ) data.data.colors = colors;
		if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
		data.data.faces = faces;
1048

1049
		return data;
1050 1051 1052

	},

1053
	clone: function () {
M
Mr.doob 已提交
1054

M
Mr.doob 已提交
1055
		return new THREE.Geometry().copy( this );
D
dubejf 已提交
1056

1057
	},
D
dubejf 已提交
1058

D
dubejf 已提交
1059
	copy: function ( source ) {
D
dubejf 已提交
1060

1061 1062 1063
		this.vertices = [];
		this.faces = [];
		this.faceVertexUvs = [ [] ];
M
Mr.doob 已提交
1064

1065
		var vertices = source.vertices;
M
Mr.doob 已提交
1066 1067 1068

		for ( var i = 0, il = vertices.length; i < il; i ++ ) {

1069
			this.vertices.push( vertices[ i ].clone() );
M
Mr.doob 已提交
1070 1071 1072

		}

1073
		var faces = source.faces;
M
Mr.doob 已提交
1074 1075 1076

		for ( var i = 0, il = faces.length; i < il; i ++ ) {

1077
			this.faces.push( faces[ i ].clone() );
M
Mr.doob 已提交
1078 1079 1080

		}

1081
		for ( var i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
M
Mr.doob 已提交
1082

1083
			var faceVertexUvs = source.faceVertexUvs[ i ];
M
Mr.doob 已提交
1084

1085
			if ( this.faceVertexUvs[ i ] === undefined ) {
M
Mr.doob 已提交
1086

1087
				this.faceVertexUvs[ i ] = [];
M
Mr.doob 已提交
1088

1089
			}
M
Mr.doob 已提交
1090

M
Mr.doob 已提交
1091 1092 1093
			for ( var j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {

				var uvs = faceVertexUvs[ j ], uvsCopy = [];
M
Mr.doob 已提交
1094

M
Mr.doob 已提交
1095
				for ( var k = 0, kl = uvs.length; k < kl; k ++ ) {
M
Mr.doob 已提交
1096

M
Mr.doob 已提交
1097
					var uv = uvs[ k ];
1098

M
Mr.doob 已提交
1099
					uvsCopy.push( uv.clone() );
M
Mr.doob 已提交
1100

1101 1102
				}

1103
				this.faceVertexUvs[ i ].push( uvsCopy );
1104 1105

			}
M
Mr.doob 已提交
1106 1107 1108

		}

1109
		return this;
M
Mr.doob 已提交
1110 1111 1112 1113 1114 1115 1116

	},

	dispose: function () {

		this.dispatchEvent( { type: 'dispose' } );

1117 1118 1119 1120 1121 1122 1123 1124
	},

	// Backwards compatibility

	set groupsNeedUpdate ( value ) {

		if ( value === true ) this.dispose();

M
Mr.doob 已提交
1125 1126 1127 1128 1129 1130 1131
	}

};

THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );

THREE.GeometryIdCount = 0;