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

M
Mr.doob 已提交
9
THREE.Geometry = function () {
10

11
	this.id = THREE.GeometryCount ++;
M
Mr.doob 已提交
12

M
Mr.doob 已提交
13
	this.vertices = [];
14
	this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
15

16 17
	this.materials = [];

M
Mr.doob 已提交
18
	this.faces = [];
19

20 21
	this.faceUvs = [[]];
	this.faceVertexUvs = [[]];
22

23
	this.morphTargets = [];
M
Mr.doob 已提交
24
	this.morphColors = [];
A
alteredq 已提交
25
	this.morphNormals = [];
M
Mr.doob 已提交
26

27 28
	this.skinWeights = [];
	this.skinIndices = [];
M
Mr.doob 已提交
29

30 31 32
	this.boundingBox = null;
	this.boundingSphere = null;

33
	this.hasTangents = false;
34

35 36
	this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.

M
Mr.doob 已提交
37 38 39 40
};

THREE.Geometry.prototype = {

41 42
	constructor : THREE.Geometry,

43 44 45
	applyMatrix: function ( matrix ) {

		var matrixRotation = new THREE.Matrix4();
A
alteredq 已提交
46
		matrixRotation.extractRotation( matrix );
47 48 49 50 51

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

			var vertex = this.vertices[ i ];

52
			matrix.multiplyVector3( vertex );
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

		}

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

			var face = this.faces[ i ];

			matrixRotation.multiplyVector3( face.normal );

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

				matrixRotation.multiplyVector3( face.vertexNormals[ j ] );

			}

			matrix.multiplyVector3( face.centroid );

		}

	},

M
Mr.doob 已提交
74 75 76 77
	computeCentroids: function () {

		var f, fl, face;

M
Mr.doob 已提交
78
		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
M
Mr.doob 已提交
79 80 81 82 83 84

			face = this.faces[ f ];
			face.centroid.set( 0, 0, 0 );

			if ( face instanceof THREE.Face3 ) {

85 86 87
				face.centroid.addSelf( this.vertices[ face.a ] );
				face.centroid.addSelf( this.vertices[ face.b ] );
				face.centroid.addSelf( this.vertices[ face.c ] );
M
Mr.doob 已提交
88 89 90 91
				face.centroid.divideScalar( 3 );

			} else if ( face instanceof THREE.Face4 ) {

92 93 94 95
				face.centroid.addSelf( this.vertices[ face.a ] );
				face.centroid.addSelf( this.vertices[ face.b ] );
				face.centroid.addSelf( this.vertices[ face.c ] );
				face.centroid.addSelf( this.vertices[ face.d ] );
M
Mr.doob 已提交
96 97 98 99 100 101 102 103
				face.centroid.divideScalar( 4 );

			}

		}

	},

104
	computeFaceNormals: function () {
105

106 107
		var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
		cb = new THREE.Vector3(), ab = new THREE.Vector3();
108

M
Mr.doob 已提交
109
		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
110

M
Mr.doob 已提交
111
			face = this.faces[ f ];
112

113 114 115
			vA = this.vertices[ face.a ];
			vB = this.vertices[ face.b ];
			vC = this.vertices[ face.c ];
U
unknown 已提交
116

117 118
			cb.sub( vC, vB );
			ab.sub( vA, vB );
119
			cb.crossSelf( ab );
U
unknown 已提交
120

121
			if ( !cb.isZero() ) {
U
unknown 已提交
122

123
				cb.normalize();
124 125 126

			}

127 128
			face.normal.copy( cb );

129 130 131 132
		}

	},

133 134
	computeVertexNormals: function () {

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

137 138
		// create internal buffers for reuse when calling this method repeatedly
		// (otherwise memory allocation / deallocation every frame is big resource hog)
M
Mr.doob 已提交
139

140
		if ( this.__tmpVertices === undefined ) {
M
Mr.doob 已提交
141

142 143
			this.__tmpVertices = new Array( this.vertices.length );
			vertices = this.__tmpVertices;
M
Mr.doob 已提交
144

145 146 147 148 149
			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

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

			}
M
Mr.doob 已提交
150

151 152 153 154 155 156 157
			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {

				face = this.faces[ f ];

				if ( face instanceof THREE.Face3 ) {

					face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
M
Mr.doob 已提交
158

159
				} else if ( face instanceof THREE.Face4 ) {
M
Mr.doob 已提交
160

161 162 163 164 165
					face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];

				}

			}
M
Mr.doob 已提交
166

167
		} else {
M
Mr.doob 已提交
168

169
			vertices = this.__tmpVertices;
M
Mr.doob 已提交
170

171 172 173 174 175
			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

				vertices[ v ].set( 0, 0, 0 );

			}
M
Mr.doob 已提交
176

M
Mr.doob 已提交
177 178 179 180 181 182 183
		}

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

			face = this.faces[ f ];

			if ( face instanceof THREE.Face3 ) {
184

M
Mr.doob 已提交
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 211
				vertices[ face.a ].addSelf( face.normal );
				vertices[ face.b ].addSelf( face.normal );
				vertices[ face.c ].addSelf( face.normal );

			} else if ( face instanceof THREE.Face4 ) {

				vertices[ face.a ].addSelf( face.normal );
				vertices[ face.b ].addSelf( face.normal );
				vertices[ face.c ].addSelf( face.normal );
				vertices[ face.d ].addSelf( 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 ];

			if ( face instanceof THREE.Face3 ) {

212 213 214
				face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
				face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
				face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
M
Mr.doob 已提交
215 216 217

			} else if ( face instanceof THREE.Face4 ) {

218 219 220 221
				face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
				face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
				face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
				face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
M
Mr.doob 已提交
222 223

			}
224 225 226 227 228

		}

	},

229 230 231 232
	computeMorphNormals: function () {

		var i, il, f, fl, face;

233 234 235
		// save original normals
		// - create temp variables on first access
		//   otherwise just copy (for faster repeated calls)
236 237 238 239

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

			face = this.faces[ f ];
240 241 242 243 244 245 246 247 248 249 250 251

			if ( ! face.__originalFaceNormal ) {

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

			} else {

				face.__originalFaceNormal.copy( face.normal );

			}

			if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
252 253 254

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

255 256 257 258 259 260 261 262 263
				if ( ! face.__originalVertexNormals[ i ] ) {

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

				} else {

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

				}
264 265 266 267 268 269 270 271 272 273 274 275

			}

		}

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

276 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
			// 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 ++ ) {

					face = this.faces[ f ];

					faceNormal = new THREE.Vector3();

					if ( face instanceof THREE.Face3 ) {

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

					} else {

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

					}

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

				}

			}

312 313 314 315 316 317 318 319 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
			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 );

				if ( face instanceof THREE.Face3 ) {

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

				} else {

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

				}

			}

		}

355
		// restore original normals
356 357 358 359

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

			face = this.faces[ f ];
360 361

			face.normal = face.__originalFaceNormal;
362 363 364 365 366 367
			face.vertexNormals = face.__originalVertexNormals;

		}

	},

368
	computeTangents: function () {
369

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

373 374
		var f, fl, v, vl, i, il, vertexIndex,
			face, uv, vA, vB, vC, uvA, uvB, uvC,
375
			x1, x2, y1, y2, z1, z2,
376
			s1, s2, t1, t2, r, t, test,
377 378
			tan1 = [], tan2 = [],
			sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
379
			tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
380
			n = new THREE.Vector3(), w;
381

382 383 384 385 386 387
		for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {

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

		}
388

M
Mr.doob 已提交
389
		function handleTriangle( context, a, b, c, ua, ub, uc ) {
390

391 392 393
			vA = context.vertices[ a ];
			vB = context.vertices[ b ];
			vC = context.vertices[ c ];
394

M
Mr.doob 已提交
395 396 397
			uvA = uv[ ua ];
			uvB = uv[ ub ];
			uvC = uv[ uc ];
398

399 400 401 402 403 404 405 406 407 408 409 410 411
			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.u - uvA.u;
			s2 = uvC.u - uvA.u;
			t1 = uvB.v - uvA.v;
			t2 = uvC.v - uvA.v;

			r = 1.0 / ( s1 * t2 - s2 * t1 );
412
			sdir.set( ( t2 * x1 - t1 * x2 ) * r,
413 414
					  ( t2 * y1 - t1 * y2 ) * r,
					  ( t2 * z1 - t1 * z2 ) * r );
415
			tdir.set( ( s1 * x2 - s2 * x1 ) * r,
416 417
					  ( s1 * y2 - s2 * y1 ) * r,
					  ( s1 * z2 - s2 * z1 ) * r );
418

419 420 421
			tan1[ a ].addSelf( sdir );
			tan1[ b ].addSelf( sdir );
			tan1[ c ].addSelf( sdir );
422

423 424 425
			tan2[ a ].addSelf( tdir );
			tan2[ b ].addSelf( tdir );
			tan2[ c ].addSelf( tdir );
426

427
		}
428

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

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

434
			if ( face instanceof THREE.Face3 ) {
435

M
Mr.doob 已提交
436
				handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
437

438
			} else if ( face instanceof THREE.Face4 ) {
439

M
Mr.doob 已提交
440
				handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
441
				handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
442 443

			}
444

445
		}
446

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

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

451
			face = this.faces[ f ];
452

453
			for ( i = 0; i < face.vertexNormals.length; i++ ) {
454

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

457
				vertexIndex = face[ faceIndex[ i ] ];
458

459
				t = tan1[ vertexIndex ];
460

461
				// Gram-Schmidt orthogonalize
462

463 464
				tmp.copy( t );
				tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
465

466
				// Calculate handedness
467

468 469 470
				tmp2.cross( face.vertexNormals[ i ], t );
				test = tmp2.dot( tan2[ vertexIndex ] );
				w = (test < 0.0) ? -1.0 : 1.0;
471

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

474
			}
475

476
		}
477

478
		this.hasTangents = true;
479

480
	},
481

482
	computeBoundingBox: function () {
483

484
		if ( ! this.boundingBox ) {
485

486
			this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
487

488
		}
489

490
		if ( this.vertices.length > 0 ) {
491

492
			var position, firstPosition = this.vertices[ 0 ];
493

494 495
			this.boundingBox.min.copy( firstPosition );
			this.boundingBox.max.copy( firstPosition );
496 497 498

			var min = this.boundingBox.min,
				max = this.boundingBox.max;
499

M
Mr.doob 已提交
500
			for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
501

502
				position = this.vertices[ v ];
503

504
				if ( position.x < min.x ) {
505

506
					min.x = position.x;
507

508
				} else if ( position.x > max.x ) {
509

510
					max.x = position.x;
511 512 513

				}

514
				if ( position.y < min.y ) {
515

516
					min.y = position.y;
517

518
				} else if ( position.y > max.y ) {
519

520
					max.y = position.y;
521 522 523

				}

524
				if ( position.z < min.z ) {
525

526
					min.z = position.z;
527

528
				} else if ( position.z > max.z ) {
529

530
					max.z = position.z;
531 532 533 534

				}

			}
535

536 537 538 539 540
		} else {

			this.boundingBox.min.set( 0, 0, 0 );
			this.boundingBox.max.set( 0, 0, 0 );

541 542
		}

M
Mr.doob 已提交
543 544
	},

545 546
	computeBoundingSphere: function () {

547 548
		if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };

549
		var radius, maxRadius = 0;
550 551 552

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

553
			radius = this.vertices[ v ].length();
554
			if ( radius > maxRadius ) maxRadius = radius;
555 556 557

		}

558
		this.boundingSphere.radius = maxRadius;
559

560
	},
561 562 563

	/*
	 * Checks for duplicate vertices with hashmap.
564
	 * Duplicated vertices are removed
565
	 * and faces' vertices are updated.
566
	 */
567

568
	mergeVertices: function() {
569

570
		var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
571
		var unique = [], changes = [];
572

573
		var v, key;
574 575
		var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
		var precision = Math.pow( 10, precisionPoints );
576
		var i,il, face;
Z
zz85 已提交
577
		var abcd = 'abcd', o, k, j, jl, u;
578 579 580

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

581
			v = this.vertices[ i ];
582 583 584 585 586 587 588 589
			key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );

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

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

590
			} else {
591

592
				//console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
593 594
				changes[ i ] = changes[ verticesMap[ key ] ];

595
			}
596

597
		};
598 599 600 601


		// Start to patch face indices

602 603 604 605 606
		for( i = 0, il = this.faces.length; i < il; i ++ ) {

			face = this.faces[ i ];

			if ( face instanceof THREE.Face3 ) {
607 608 609 610 611 612 613 614 615 616 617 618

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

			} else if ( face instanceof THREE.Face4 ) {

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

619 620 621 622 623 624 625
				// check dups in (a, b, c, d) and convert to -> face3
				o = [face.a, face.b, face.c, face.d];
				for (k=3;k>0;k--) {
					if ( o.indexOf(face[abcd[k]]) != k ) {
						// console.log('faces', face.a, face.b, face.c, face.d, 'dup at', k);
						o.splice(k, 1);
						this.faces[ i ] = new THREE.Face3(o[0], o[1], o[2]);
Z
zz85 已提交
626 627 628 629
						for (j=0,jl=this.faceVertexUvs.length;j<jl;j++) {
							u = this.faceVertexUvs[j][i];
							if (u) u.splice(k, 1);
						}
630
						
Z
zz85 已提交
631
						break;
632 633 634 635
					}
				}


636
			}
637

638
		}
639

640
		// Use unique set of vertices
Z
zz85 已提交
641
		var diff = this.vertices.length - unique.length;
642
		this.vertices = unique;
Z
zz85 已提交
643
		return diff;
644

645
	}
646

M
Mr.doob 已提交
647
};
648

649
THREE.GeometryCount = 0;