Geometry.js 24.2 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5 6 7 8 9 10 11 12
import { EventDispatcher } from './EventDispatcher';
import { Face3 } from './Face3';
import { Matrix3 } from '../math/Matrix3';
import { Sphere } from '../math/Sphere';
import { Box3 } from '../math/Box3';
import { Vector3 } from '../math/Vector3';
import { Matrix4 } from '../math/Matrix4';
import { Vector2 } from '../math/Vector2';
import { Color } from '../math/Color';
import { Object3D } from './Object3D';
import { _Math } from '../math/Math';

M
Mr.doob 已提交
13 14 15 16 17 18
/**
 * @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
19
 * @author bhouston / http://clara.io
M
Mr.doob 已提交
20 21
 */

M
Mr.doob 已提交
22
function Geometry() {
M
Mr.doob 已提交
23

R
Rich Harris 已提交
24
	Object.defineProperty( this, 'id', { value: GeometryIdCount() } );
M
Mr.doob 已提交
25

R
Rich Harris 已提交
26
	this.uuid = _Math.generateUUID();
M
Mr.doob 已提交
27 28

	this.name = '';
29
	this.type = 'Geometry';
M
Mr.doob 已提交
30

M
Mr.doob 已提交
31 32
	this.vertices = [];
	this.colors = [];
M
Mr.doob 已提交
33
	this.faces = [];
34
	this.faceVertexUvs = [ [] ];
M
Mr.doob 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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

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

	this.lineDistances = [];

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

	// update flags

	this.elementsNeedUpdate = false;
M
Mr.doob 已提交
50
	this.verticesNeedUpdate = false;
M
Mr.doob 已提交
51 52 53 54
	this.uvsNeedUpdate = false;
	this.normalsNeedUpdate = false;
	this.colorsNeedUpdate = false;
	this.lineDistancesNeedUpdate = false;
55
	this.groupsNeedUpdate = false;
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57
}
M
Mr.doob 已提交
58

R
Rich Harris 已提交
59
Object.assign( Geometry.prototype, EventDispatcher.prototype, {
M
Mr.doob 已提交
60

61 62
	isGeometry: true,

M
Mr.doob 已提交
63 64
	applyMatrix: function ( matrix ) {

R
Rich Harris 已提交
65
		var normalMatrix = new Matrix3().getNormalMatrix( matrix );
M
Mr.doob 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

		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 已提交
87
		if ( this.boundingBox !== null ) {
M
Mr.doob 已提交
88 89 90 91 92

			this.computeBoundingBox();

		}

M
Mr.doob 已提交
93
		if ( this.boundingSphere !== null ) {
M
Mr.doob 已提交
94 95 96 97 98

			this.computeBoundingSphere();

		}

M
Mr.doob 已提交
99 100 101
		this.verticesNeedUpdate = true;
		this.normalsNeedUpdate = true;

102 103
		return this;

M
Mr.doob 已提交
104 105
	},

106 107 108 109 110 111 112 113
	rotateX: function () {

		// rotate geometry around world x-axis

		var m1;

		return function rotateX( angle ) {

R
Rich Harris 已提交
114
			if ( m1 === undefined ) m1 = new Matrix4();
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

			m1.makeRotationX( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	rotateY: function () {

		// rotate geometry around world y-axis

		var m1;

		return function rotateY( angle ) {

R
Rich Harris 已提交
134
			if ( m1 === undefined ) m1 = new Matrix4();
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

			m1.makeRotationY( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	rotateZ: function () {

		// rotate geometry around world z-axis

		var m1;

		return function rotateZ( angle ) {

R
Rich Harris 已提交
154
			if ( m1 === undefined ) m1 = new Matrix4();
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

			m1.makeRotationZ( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	translate: function () {

		// translate geometry

		var m1;

		return function translate( x, y, z ) {

R
Rich Harris 已提交
174
			if ( m1 === undefined ) m1 = new Matrix4();
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

			m1.makeTranslation( x, y, z );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	scale: function () {

		// scale geometry

		var m1;

		return function scale( x, y, z ) {

R
Rich Harris 已提交
194
			if ( m1 === undefined ) m1 = new Matrix4();
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

			m1.makeScale( x, y, z );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	lookAt: function () {

		var obj;

		return function lookAt( vector ) {

R
Rich Harris 已提交
212
			if ( obj === undefined ) obj = new Object3D();
213 214 215 216 217 218 219 220 221 222 223

			obj.lookAt( vector );

			obj.updateMatrix();

			this.applyMatrix( obj.matrix );

		};

	}(),

224 225 226 227
	fromBufferGeometry: function ( geometry ) {

		var scope = this;

228
		var indices = geometry.index !== null ? geometry.index.array : undefined;
229 230
		var attributes = geometry.attributes;

231
		var positions = attributes.position.array;
M
Mr.doob 已提交
232 233 234
		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;
235 236 237
		var uvs2 = attributes.uv2 !== undefined ? attributes.uv2.array : undefined;

		if ( uvs2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
238 239 240

		var tempNormals = [];
		var tempUVs = [];
241
		var tempUVs2 = [];
242

243
		for ( var i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
244

R
Rich Harris 已提交
245
			scope.vertices.push( new Vector3( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ) );
246 247 248

			if ( normals !== undefined ) {

R
Rich Harris 已提交
249
				tempNormals.push( new Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );
250 251 252 253 254

			}

			if ( colors !== undefined ) {

R
Rich Harris 已提交
255
				scope.colors.push( new Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
256 257 258 259 260

			}

			if ( uvs !== undefined ) {

R
Rich Harris 已提交
261
				tempUVs.push( new Vector2( uvs[ j ], uvs[ j + 1 ] ) );
262 263 264

			}

265 266
			if ( uvs2 !== undefined ) {

R
Rich Harris 已提交
267
				tempUVs2.push( new Vector2( uvs2[ j ], uvs2[ j + 1 ] ) );
268 269 270

			}

271 272
		}

273
		function addFace( a, b, c, materialIndex ) {
274

275
			var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
276
			var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
277

R
Rich Harris 已提交
278
			var face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
279 280

			scope.faces.push( face );
281 282 283

			if ( uvs !== undefined ) {

284
				scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
285 286

			}
287

288 289 290 291 292 293
			if ( uvs2 !== undefined ) {

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

			}

294
		}
295 296 297

		if ( indices !== undefined ) {

298
			var groups = geometry.groups;
299

300
			if ( groups.length > 0 ) {
301

302
				for ( var i = 0; i < groups.length; i ++ ) {
303

304
					var group = groups[ i ];
305

306 307
					var start = group.start;
					var count = group.count;
308 309 310

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

311
						addFace( indices[ j ], indices[ j + 1 ], indices[ j + 2 ], group.materialIndex  );
312 313 314 315 316 317 318 319 320 321 322 323

					}

				}

			} else {

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

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

				}
324 325 326 327 328

			}

		} else {

329
			for ( var i = 0; i < positions.length / 3; i += 3 ) {
330 331 332 333 334 335

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

			}

		}
336

M
Mr.doob 已提交
337
		this.computeFaceNormals();
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

		if ( geometry.boundingBox !== null ) {

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

		}

		if ( geometry.boundingSphere !== null ) {

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

		}

		return this;

	},

355 356 357 358
	center: function () {

		this.computeBoundingBox();

359
		var offset = this.boundingBox.getCenter().negate();
360

361
		this.translate( offset.x, offset.y, offset.z );
362 363 364 365 366

		return offset;

	},

M
Mr.doob 已提交
367
	normalize: function () {
Z
update  
Zhonghua Xi 已提交
368 369 370

		this.computeBoundingSphere();

M
Mr.doob 已提交
371 372
		var center = this.boundingSphere.center;
		var radius = this.boundingSphere.radius;
Z
Zhonghua Xi 已提交
373

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

R
Rich Harris 已提交
376
		var matrix = new Matrix4();
M
Mr.doob 已提交
377
		matrix.set(
G
gero3 已提交
378 379 380
			s, 0, 0, - s * center.x,
			0, s, 0, - s * center.y,
			0, 0, s, - s * center.z,
M
Mr.doob 已提交
381 382
			0, 0, 0, 1
		);
Z
Zhonghua Xi 已提交
383

M
Mr.doob 已提交
384
		this.applyMatrix( matrix );
Z
Zhonghua Xi 已提交
385 386

		return this;
G
gero3 已提交
387

Z
Zhonghua Xi 已提交
388 389
	},

M
Mr.doob 已提交
390 391
	computeFaceNormals: function () {

R
Rich Harris 已提交
392
		var cb = new Vector3(), ab = new Vector3();
M
Mr.doob 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

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

416 417
		if ( areaWeighted === undefined ) areaWeighted = true;

M
Mr.doob 已提交
418 419 420 421 422 423
		var v, vl, f, fl, face, vertices;

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

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

R
Rich Harris 已提交
424
			vertices[ v ] = new Vector3();
M
Mr.doob 已提交
425 426 427 428 429 430 431 432

		}

		if ( areaWeighted ) {

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

B
brason 已提交
433
			var vA, vB, vC;
R
Rich Harris 已提交
434
			var cb = new Vector3(), ab = new Vector3();
M
Mr.doob 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

			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 {

456 457
			this.computeFaceNormals();

M
Mr.doob 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
			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 ];

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
			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 已提交
495 496 497

		}

498 499 500 501 502 503
		if ( this.faces.length > 0 ) {

			this.normalsNeedUpdate = true;

		}

M
Mr.doob 已提交
504 505
	},

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
	computeFlatVertexNormals: function () {

		var f, fl, face;

		this.computeFaceNormals();

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

			face = this.faces[ f ];

			var vertexNormals = face.vertexNormals;

			if ( vertexNormals.length === 3 ) {

				vertexNormals[ 0 ].copy( face.normal );
				vertexNormals[ 1 ].copy( face.normal );
				vertexNormals[ 2 ].copy( face.normal );

			} else {

				vertexNormals[ 0 ] = face.normal.clone();
				vertexNormals[ 1 ] = face.normal.clone();
				vertexNormals[ 2 ] = face.normal.clone();

			}

		}

		if ( this.faces.length > 0 ) {

			this.normalsNeedUpdate = true;

		}

	},

M
Mr.doob 已提交
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 576 577 578 579 580 581 582 583
	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

R
Rich Harris 已提交
584
		var tmpGeo = new Geometry();
M
Mr.doob 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
		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 ++ ) {

R
Rich Harris 已提交
604 605
					faceNormal = new Vector3();
					vertexNormals = { a: new Vector3(), b: new Vector3(), c: new Vector3() };
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 648 649 650 651 652 653 654 655 656 657 658 659 660

					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 () {

M
Mr.doob 已提交
661
		console.warn( 'THREE.Geometry: .computeTangents() has been removed.' );
M
Mr.doob 已提交
662 663 664

	},

665
	computeLineDistances: function () {
M
Mr.doob 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

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

R
Rich Harris 已提交
688
			this.boundingBox = new Box3();
M
Mr.doob 已提交
689 690 691 692 693 694 695 696 697 698 699

		}

		this.boundingBox.setFromPoints( this.vertices );

	},

	computeBoundingSphere: function () {

		if ( this.boundingSphere === null ) {

R
Rich Harris 已提交
700
			this.boundingSphere = new Sphere();
M
Mr.doob 已提交
701 702 703 704 705 706 707

		}

		this.boundingSphere.setFromPoints( this.vertices );

	},

M
Mr.doob 已提交
708
	merge: function ( geometry, matrix, materialIndexOffset ) {
709

R
Rich Harris 已提交
710
		if ( (geometry && geometry.isGeometry) === false ) {
711

712
			console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
713 714 715 716
			return;

		}

M
Mr.doob 已提交
717
		var normalMatrix,
718 719
		vertexOffset = this.vertices.length,
		vertices1 = this.vertices,
M
Mr.doob 已提交
720
		vertices2 = geometry.vertices,
721
		faces1 = this.faces,
M
Mr.doob 已提交
722
		faces2 = geometry.faces,
723
		uvs1 = this.faceVertexUvs[ 0 ],
724 725 726
		uvs2 = geometry.faceVertexUvs[ 0 ],
		colors1 = this.colors,
		colors2 = geometry.colors;
727

M
Mr.doob 已提交
728 729
		if ( materialIndexOffset === undefined ) materialIndexOffset = 0;

M
Mr.doob 已提交
730
		if ( matrix !== undefined ) {
731

R
Rich Harris 已提交
732
			normalMatrix = new Matrix3().getNormalMatrix( matrix );
733 734 735 736 737 738 739 740 741 742 743

		}

		// vertices

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

			var vertex = vertices2[ i ];

			var vertexCopy = vertex.clone();

M
Mr.doob 已提交
744
			if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
745 746 747 748 749

			vertices1.push( vertexCopy );

		}

750 751 752 753 754 755 756 757
		// colors

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

			colors1.push( colors2[ i ].clone() );

		}

758 759 760 761 762 763 764 765
		// faces

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

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

R
Rich Harris 已提交
766
			faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
767 768
			faceCopy.normal.copy( face.normal );

M
Mr.doob 已提交
769
			if ( normalMatrix !== undefined ) {
770 771 772 773 774 775 776 777 778

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

			}

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

				normal = faceVertexNormals[ j ].clone();

M
Mr.doob 已提交
779
				if ( normalMatrix !== undefined ) {
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

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

			}

M
Mr.doob 已提交
798 799
			faceCopy.materialIndex = face.materialIndex + materialIndexOffset;

800 801 802 803 804 805 806 807 808
			faces1.push( faceCopy );

		}

		// uvs

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

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

810
			if ( uv === undefined ) {
811

812
				continue;
813

814 815 816 817
			}

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

M
Mr.doob 已提交
818
				uvCopy.push( uv[ j ].clone() );
819 820 821 822 823 824 825 826 827

			}

			uvs1.push( uvCopy );

		}

	},

828 829
	mergeMesh: function ( mesh ) {

R
Rich Harris 已提交
830
		if ( (mesh && mesh.isMesh) === false ) {
831

832
			console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
833 834 835 836 837 838 839 840 841 842
			return;

		}

		mesh.matrixAutoUpdate && mesh.updateMatrix();

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

	},

M
Mr.doob 已提交
843 844 845 846 847 848 849 850
	/*
	 * Checks for duplicate vertices with hashmap.
	 * Duplicated vertices are removed
	 * and faces' vertices are updated.
	 */

	mergeVertices: function () {

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

		var v, key;
D
dubejf 已提交
855
		var precisionPoints = 4; // number of decimal points, e.g. 4 for epsilon of 0.0001
M
Mr.doob 已提交
856
		var precision = Math.pow( 10, precisionPoints );
B
brason 已提交
857 858
		var i, il, face;
		var indices, j, jl;
M
Mr.doob 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872

		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 {

873
				//console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
M
Mr.doob 已提交
874 875 876 877
				changes[ i ] = changes[ verticesMap[ key ] ];

			}

B
brason 已提交
878
		}
M
Mr.doob 已提交
879 880 881 882 883 884


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

885
		for ( i = 0, il = this.faces.length; i < il; i ++ ) {
M
Mr.doob 已提交
886 887 888 889 890 891 892 893 894

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

895
			var dupIndex = - 1;
M
Mr.doob 已提交
896 897 898 899

			// 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 已提交
900

F
Fabian Lange 已提交
901
				if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
M
Mr.doob 已提交
902 903 904 905 906 907

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

				}
G
gero3 已提交
908

M
Mr.doob 已提交
909 910 911 912 913
			}

		}

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

M
Mr.doob 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
			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;

	},

935
	sortFacesByMaterialIndex: function () {
936

937 938 939
		var faces = this.faces;
		var length = faces.length;

M
Mr.doob 已提交
940 941
		// tag faces

942 943 944 945 946 947 948 949
		for ( var i = 0; i < length; i ++ ) {

			faces[ i ]._id = i;

		}

		// sort faces

950
		function materialIndexSort( a, b ) {
951 952 953 954 955

			return a.materialIndex - b.materialIndex;

		}

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
		faces.sort( materialIndexSort );

		// sort uvs

		var uvs1 = this.faceVertexUvs[ 0 ];
		var uvs2 = this.faceVertexUvs[ 1 ];

		var newUvs1, newUvs2;

		if ( uvs1 && uvs1.length === length ) newUvs1 = [];
		if ( uvs2 && uvs2.length === length ) newUvs2 = [];

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

			var id = faces[ i ]._id;

			if ( newUvs1 ) newUvs1.push( uvs1[ id ] );
			if ( newUvs2 ) newUvs2.push( uvs2[ id ] );

		}

		if ( newUvs1 ) this.faceVertexUvs[ 0 ] = newUvs1;
		if ( newUvs2 ) this.faceVertexUvs[ 1 ] = newUvs2;
979 980 981

	},

982
	toJSON: function () {
983

984 985 986 987 988 989
		var data = {
			metadata: {
				version: 4.4,
				type: 'Geometry',
				generator: 'Geometry.toJSON'
			}
990 991
		};

992
		// standard Geometry serialization
993

994
		data.uuid = this.uuid;
M
Mr.doob 已提交
995
		data.type = this.type;
996
		if ( this.name !== '' ) data.name = this.name;
997

998
		if ( this.parameters !== undefined ) {
999 1000 1001 1002 1003

			var parameters = this.parameters;

			for ( var key in parameters ) {

1004
				if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
1005 1006 1007

			}

1008
			return data;
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

		}

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

1033
			var hasMaterial = true;
1034 1035 1036 1037 1038 1039 1040 1041 1042
			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;

1043
			faceType = setBit( faceType, 0, 0 ); // isQuad
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			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 );
1054
			faces.push( face.materialIndex );
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

			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 已提交
1108
			return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

		}

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

		}

1163
		data.data = {};
1164

1165 1166 1167 1168 1169
		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;
1170

1171
		return data;
1172 1173 1174

	},

1175
	clone: function () {
M
Mr.doob 已提交
1176

1177
		/*
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
		// Handle primitives

		var parameters = this.parameters;

		if ( parameters !== undefined ) {

			var values = [];

			for ( var key in parameters ) {

				values.push( parameters[ key ] );

			}

			var geometry = Object.create( this.constructor.prototype );
			this.constructor.apply( geometry, values );
			return geometry;

		}

1198
		return new this.constructor().copy( this );
1199 1200
		*/

R
Rich Harris 已提交
1201
		return new Geometry().copy( this );
D
dubejf 已提交
1202

1203
	},
D
dubejf 已提交
1204

D
dubejf 已提交
1205
	copy: function ( source ) {
D
dubejf 已提交
1206

1207 1208 1209
		this.vertices = [];
		this.faces = [];
		this.faceVertexUvs = [ [] ];
1210
		this.colors = [];
M
Mr.doob 已提交
1211

1212
		var vertices = source.vertices;
M
Mr.doob 已提交
1213 1214 1215

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

1216
			this.vertices.push( vertices[ i ].clone() );
M
Mr.doob 已提交
1217 1218 1219

		}

1220 1221 1222 1223 1224 1225 1226 1227
		var colors = source.colors;

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

			this.colors.push( colors[ i ].clone() );

		}

1228
		var faces = source.faces;
M
Mr.doob 已提交
1229 1230 1231

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

1232
			this.faces.push( faces[ i ].clone() );
M
Mr.doob 已提交
1233 1234 1235

		}

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

1238
			var faceVertexUvs = source.faceVertexUvs[ i ];
M
Mr.doob 已提交
1239

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

1242
				this.faceVertexUvs[ i ] = [];
M
Mr.doob 已提交
1243

1244
			}
M
Mr.doob 已提交
1245

M
Mr.doob 已提交
1246 1247 1248
			for ( var j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {

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

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

M
Mr.doob 已提交
1252
					var uv = uvs[ k ];
1253

M
Mr.doob 已提交
1254
					uvsCopy.push( uv.clone() );
M
Mr.doob 已提交
1255

1256 1257
				}

1258
				this.faceVertexUvs[ i ].push( uvsCopy );
1259 1260

			}
M
Mr.doob 已提交
1261 1262 1263

		}

1264
		return this;
M
Mr.doob 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273

	},

	dispose: function () {

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

	}

1274
} );
M
Mr.doob 已提交
1275

R
Rich Harris 已提交
1276
var count = 0;
M
Mr.doob 已提交
1277
function GeometryIdCount() { return count++; };
R
Rich Harris 已提交
1278 1279


1280
export { GeometryIdCount, Geometry };