BufferGeometry.js 21.4 KB
Newer Older
1 2
/**
 * @author alteredq / http://alteredqualia.com/
3
 * @author mrdoob / http://mrdoob.com/
4 5 6 7
 */

THREE.BufferGeometry = function () {

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

10
	this.uuid = THREE.Math.generateUUID();
11

M
Mr.doob 已提交
12
	this.name = '';
13
	this.type = 'BufferGeometry';
M
Mr.doob 已提交
14

15
	this.attributes = {};
16

17
	this.morphAttributes = {};
18

19
	this.drawcalls = [];
20

21 22 23 24 25 26 27
	this.boundingBox = null;
	this.boundingSphere = null;

};

THREE.BufferGeometry.prototype = {

28
	constructor: THREE.BufferGeometry,
29

M
Mr.doob 已提交
30
	addAttribute: function ( name, attribute ) {
31

M
Mr.doob 已提交
32
		if ( attribute instanceof THREE.BufferAttribute === false && attribute instanceof THREE.InterleavedBufferAttribute === false ) {
33

34
			console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
35 36 37 38 39 40 41

			this.attributes[ name ] = { array: arguments[ 1 ], itemSize: arguments[ 2 ] };

			return;

		}

M
Mr.doob 已提交
42 43 44 45 46 47 48
		if ( name === 'index' && attribute instanceof THREE.IndexBufferAttribute === false ) {

			console.warn( 'THREE.BufferGeometry.addAttribute: Use THREE.IndexBufferAttribute for index attribute.' );
			attribute = new THREE.IndexBufferAttribute( attribute.array, attribute.itemSize );

		}

M
Mr.doob 已提交
49
		this.attributes[ name ] = attribute;
50

M
Mr.doob 已提交
51 52 53 54
	},

	getAttribute: function ( name ) {

55 56
		return this.attributes[ name ];

57 58
	},

59 60 61 62 63 64
	removeAttribute: function ( name ) {

		delete this.attributes[ name ];

	},

65
	get offsets() {
66 67

		console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .drawcalls.' );
68
		return this.drawcalls;
69 70 71

	},

M
Mr.doob 已提交
72 73
	addDrawCall: function ( start, count, indexOffset ) {

74 75 76 77 78 79
		if ( indexOffset !== undefined ) {

			console.warn( 'THREE.BufferGeometry: .addDrawCall() no longer supports indexOffset.' );

		}

80
		this.drawcalls.push( {
M
Mr.doob 已提交
81 82

			start: start,
83
			count: count
M
Mr.doob 已提交
84 85 86 87 88

		} );

	},

D
dubejf 已提交
89 90 91 92 93 94
	clearDrawCalls: function () {

		this.drawcalls = [];

	},

95 96
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
97
		var position = this.attributes.position;
98

M
Mr.doob 已提交
99
		if ( position !== undefined ) {
100

101
			matrix.applyToVector3Array( position.array );
M
Mr.doob 已提交
102
			position.needsUpdate = true;
103 104 105

		}

M
Mr.doob 已提交
106
		var normal = this.attributes.normal;
107

M
Mr.doob 已提交
108
		if ( normal !== undefined ) {
109

M
Mr.doob 已提交
110
			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
111

112
			normalMatrix.applyToVector3Array( normal.array );
M
Mr.doob 已提交
113
			normal.needsUpdate = true;
114 115 116

		}

M
Mr.doob 已提交
117
		if ( this.boundingBox !== null ) {
118 119 120 121 122

			this.computeBoundingBox();

		}

M
Mr.doob 已提交
123
		if ( this.boundingSphere !== null ) {
124 125 126 127 128

			this.computeBoundingSphere();

		}

129 130
	},

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	rotateX: function () {

		// rotate geometry around world x-axis

		var m1;

		return function rotateX( angle ) {

			if ( m1 === undefined ) m1 = new THREE.Matrix4();

			m1.makeRotationX( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	rotateY: function () {

		// rotate geometry around world y-axis

		var m1;

		return function rotateY( angle ) {

			if ( m1 === undefined ) m1 = new THREE.Matrix4();

			m1.makeRotationY( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	rotateZ: function () {

		// rotate geometry around world z-axis

		var m1;

		return function rotateZ( angle ) {

			if ( m1 === undefined ) m1 = new THREE.Matrix4();

			m1.makeRotationZ( angle );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	translate: function () {

		// translate geometry

		var m1;

		return function translate( x, y, z ) {

			if ( m1 === undefined ) m1 = new THREE.Matrix4();

			m1.makeTranslation( x, y, z );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	scale: function () {

		// scale geometry

		var m1;

		return function scale( x, y, z ) {

			if ( m1 === undefined ) m1 = new THREE.Matrix4();

			m1.makeScale( x, y, z );

			this.applyMatrix( m1 );

			return this;

		};

	}(),

	lookAt: function () {

		var obj;

		return function lookAt( vector ) {

			if ( obj === undefined ) obj = new THREE.Object3D();

			obj.lookAt( vector );

			obj.updateMatrix();

			this.applyMatrix( obj.matrix );

		};

	}(),

249 250
	center: function () {

M
Mr.doob 已提交
251 252 253 254
		this.computeBoundingBox();

		var offset = this.boundingBox.center().negate();

255
		this.translate( offset.x, offset.y, offset.z );
M
Mr.doob 已提交
256 257

		return offset;
258 259 260

	},

261
	setFromObject: function ( object ) {
262

263
		// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
264

265 266 267 268
		var geometry = object.geometry;

		if ( object instanceof THREE.PointCloud || object instanceof THREE.Line ) {

269 270
			var positions = new THREE.Float32Attribute( geometry.vertices.length * 3, 3 );
			var colors = new THREE.Float32Attribute( geometry.colors.length * 3, 3 );
271

272 273
			this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
			this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
274

C
Chen Pang 已提交
275 276 277 278 279 280 281 282
			if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {

				var lineDistances = new THREE.Float32Attribute( geometry.lineDistances.length, 1 );

				this.addAttribute( 'lineDistance',  lineDistances.copyArray( geometry.lineDistances ) );

			}

283 284 285 286 287 288 289 290 291 292 293
			if ( geometry.boundingSphere !== null ) {

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

			}

			if ( geometry.boundingBox !== null ) {

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

			}
294 295 296

		} else if ( object instanceof THREE.Mesh ) {

M
Mr.doob 已提交
297
			if ( geometry instanceof THREE.Geometry ) {
298 299

				this.fromGeometry( geometry );
300

M
Mr.doob 已提交
301
			}
302 303 304 305 306 307 308

		}

		return this;

	},

309 310
	updateFromObject: function ( object ) {

M
Mr.doob 已提交
311
		var geometry = object.geometry;
312

M
Mr.doob 已提交
313
		if ( object instanceof THREE.Mesh ) {
314

M
Mr.doob 已提交
315
			var direct = geometry.__directGeometry;
316

317 318 319 320 321 322
			if ( direct === undefined ) {

				return this.fromGeometry( geometry );

			}

M
Mr.doob 已提交
323 324 325 326
			direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
			direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
			direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
			direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
327
			direct.tangentsNeedUpdate = geometry.tangentsNeedUpdate;
M
Mr.doob 已提交
328 329 330 331 332

			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
333
			geometry.tangentsNeedUpdate = false;
334

M
Mr.doob 已提交
335
			geometry = direct;
336 337

		}
338

M
Mr.doob 已提交
339
		if ( geometry.verticesNeedUpdate === true ) {
340

M
Mr.doob 已提交
341
			var attribute = this.attributes.position;
342

M
Mr.doob 已提交
343
			if ( attribute !== undefined ) {
344

M
Mr.doob 已提交
345 346
				attribute.copyVector3sArray( geometry.vertices );
				attribute.needsUpdate = true;
347 348 349

			}

M
Mr.doob 已提交
350
			geometry.verticesNeedUpdate = false;
351

M
Mr.doob 已提交
352
		}
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
		if ( geometry.normalsNeedUpdate === true ) {

			var attribute = this.attributes.normal;

			if ( attribute !== undefined ) {

				attribute.copyVector3sArray( geometry.normals );
				attribute.needsUpdate = true;

			}

			geometry.normalsNeedUpdate = false;

		}

M
Mr.doob 已提交
369
		if ( geometry.colorsNeedUpdate === true ) {
370

M
Mr.doob 已提交
371
			var attribute = this.attributes.color;
372

M
Mr.doob 已提交
373
			if ( attribute !== undefined ) {
374

M
Mr.doob 已提交
375 376
				attribute.copyColorsArray( geometry.colors );
				attribute.needsUpdate = true;
377 378 379

			}

M
Mr.doob 已提交
380 381
			geometry.colorsNeedUpdate = false;

382
		}
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		if ( geometry.tangentsNeedUpdate === true ) {

			var attribute = this.attributes.tangent;

			if ( attribute !== undefined ) {

				attribute.copyVector4sArray( geometry.tangents );
				attribute.needsUpdate = true;

			}

			geometry.tangentsNeedUpdate = false;

		}

C
Chen Pang 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		if ( geometry.lineDistancesNeedUpdate ) {

			var attribute = this.attributes.lineDistance;

			if ( attribute !== undefined ) {

				attribute.copyArray( geometry.lineDistances );
				attribute.needsUpdate = true;

			}

			geometry.lineDistancesNeedUpdate = false;

		}

414
		return this;
415 416 417

	},

418
	fromGeometry: function ( geometry ) {
419

420
		geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
421 422

		return this.fromDirectGeometry( geometry.__directGeometry );
423

424
	},
425

426
	fromDirectGeometry: function ( geometry ) {
427

428 429
		var positions = new Float32Array( geometry.vertices.length * 3 );
		this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
430

431 432 433 434 435 436 437 438
		if ( geometry.normals.length > 0 ) {

			var normals = new Float32Array( geometry.normals.length * 3 );
			this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );

		}

		if ( geometry.colors.length > 0 ) {
M
Mr.doob 已提交
439

440
			var colors = new Float32Array( geometry.colors.length * 3 );
441
			this.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
M
Mr.doob 已提交
442

443 444 445 446 447 448 449 450
		}

		if ( geometry.uvs.length > 0 ) {

			var uvs = new Float32Array( geometry.uvs.length * 2 );
			this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );

		}
M
Mr.doob 已提交
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465
		if ( geometry.uvs2.length > 0 ) {

			var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
			this.addAttribute( 'uv2', new THREE.BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );

		}

		if ( geometry.tangents.length > 0 ) {

			var tangents = new Float32Array( geometry.tangents.length * 4 );
			this.addAttribute( 'tangent', new THREE.BufferAttribute( tangents, 4 ).copyVector4sArray( geometry.tangents ) );

		}

466 467
		if ( geometry.indices.length > 0 ) {

M
Mr.doob 已提交
468 469 470
			var TypeArray = geometry.vertices.length > 65535 ? Uint32Array : Uint16Array;
			var indices = new TypeArray( geometry.indices.length * 3 );
			this.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
471 472 473

		}

474 475
		// morphs

476
		for ( var name in geometry.morphTargets ) {
477

478 479
			var array = [];
			var morphTargets = geometry.morphTargets[ name ];
480 481 482 483 484 485 486

			for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {

				var morphTarget = morphTargets[ i ];

				var attribute = new THREE.Float32Attribute( morphTarget.length * 3, 3 );

487
				array.push( attribute.copyVector3sArray( morphTarget ) );
488 489 490

			}

491
			this.morphAttributes[ name ] = array;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

		}

		// skinning

		if ( geometry.skinIndices.length > 0 ) {

			var skinIndices = new THREE.Float32Attribute( geometry.skinIndices.length * 4, 4 );
			this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );

		}

		if ( geometry.skinWeights.length > 0 ) {

			var skinWeights = new THREE.Float32Attribute( geometry.skinWeights.length * 4, 4 );
			this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );

		}

		//

513 514 515 516 517 518 519 520 521 522 523
		if ( geometry.boundingSphere !== null ) {

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

		}

		if ( geometry.boundingBox !== null ) {

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

		}
M
Mr.doob 已提交
524 525 526 527 528

		return this;

	},

529 530
	computeBoundingBox: function () {

531
		var vector = new THREE.Vector3();
532

533
		return function () {
534

535
			if ( this.boundingBox === null ) {
536

537
				this.boundingBox = new THREE.Box3();
538

539 540
			}

541
			var positions = this.attributes.position.array;
542

543
			if ( positions ) {
544

545 546
				var bb = this.boundingBox;
				bb.makeEmpty();
547

548
				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
549

550
					vector.fromArray( positions, i );
551
					bb.expandByPoint( vector );
552 553 554 555 556

				}

			}

557
			if ( positions === undefined || positions.length === 0 ) {
558

559 560
				this.boundingBox.min.set( 0, 0, 0 );
				this.boundingBox.max.set( 0, 0, 0 );
561

562
			}
563

564
			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
565

566
				console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
567

568
			}
569

G
Greg French 已提交
570
		};
571

572
	}(),
573 574 575

	computeBoundingSphere: function () {

576 577
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
578

579
		return function () {
580

581
			if ( this.boundingSphere === null ) {
582

583
				this.boundingSphere = new THREE.Sphere();
584

585
			}
586

587
			var positions = this.attributes.position.array;
588

589
			if ( positions ) {
590

591 592
				box.makeEmpty();

593
				var center = this.boundingSphere.center;
594

595
				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
596

597
					vector.fromArray( positions, i );
J
Jan Wrobel 已提交
598
					box.expandByPoint( vector );
599 600 601 602 603

				}

				box.center( center );

604
				// hoping to find a boundingSphere with a radius smaller than the
M
Mr.doob 已提交
605
				// boundingSphere of the boundingBox: sqrt(3) smaller in the best case
606

607 608
				var maxRadiusSq = 0;

609
				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
610

611
					vector.fromArray( positions, i );
612
					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
613

614 615 616 617
				}

				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );

618 619
				if ( isNaN( this.boundingSphere.radius ) ) {

620
					console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
621 622 623

				}

624
			}
625

G
Greg French 已提交
626
		};
627

628
	}(),
629

630 631 632 633 634 635
	computeFaceNormals: function () {

		// backwards compatibility

	},

636 637
	computeVertexNormals: function () {

638
		var attributes = this.attributes;
639
		var drawcalls = this.drawcalls;
640

641
		if ( attributes.position ) {
642

643
			var positions = attributes.position.array;
644

645
			if ( attributes.normal === undefined ) {
646

647
				this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( positions.length ), 3 ) );
648 649 650 651 652

			} else {

				// reset existing normals to zero

653 654 655
				var normals = attributes.normal.array;

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

657
					normals[ i ] = 0;
658 659 660 661 662

				}

			}

663
			var normals = attributes.normal.array;
664

M
Mr.doob 已提交
665
			var vA, vB, vC,
666 667 668 669 670 671 672 673

			pA = new THREE.Vector3(),
			pB = new THREE.Vector3(),
			pC = new THREE.Vector3(),

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

674 675
			// indexed elements

676
			if ( attributes.index ) {
677

678
				var indices = attributes.index.array;
679

680
				if ( drawcalls.length === 0 ) {
D
dubejf 已提交
681

682
					this.addDrawCall( 0, indices.length );
683

D
dubejf 已提交
684 685
				}

686 687 688
				for ( var j = 0, jl = drawcalls.length; j < jl; ++ j ) {

					var drawcall = drawcalls[ j ];
689

690 691
					var start = drawcall.start;
					var count = drawcall.count;
692

693
					for ( var i = start, il = start + count; i < il; i += 3 ) {
694

695 696 697
						vA = indices[ i + 0 ] * 3;
						vB = indices[ i + 1 ] * 3;
						vC = indices[ i + 2 ] * 3;
698

M
Mr.doob 已提交
699 700 701
						pA.fromArray( positions, vA );
						pB.fromArray( positions, vB );
						pC.fromArray( positions, vC );
702

703 704 705
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
706

G
gero3 已提交
707
						normals[ vA ] += cb.x;
M
Mr.doob 已提交
708 709
						normals[ vA + 1 ] += cb.y;
						normals[ vA + 2 ] += cb.z;
710

G
gero3 已提交
711
						normals[ vB ] += cb.x;
M
Mr.doob 已提交
712 713
						normals[ vB + 1 ] += cb.y;
						normals[ vB + 2 ] += cb.z;
714

G
gero3 已提交
715
						normals[ vC ] += cb.x;
M
Mr.doob 已提交
716 717
						normals[ vC + 1 ] += cb.y;
						normals[ vC + 2 ] += cb.z;
718 719 720 721 722 723

					}

				}

			} else {
724

725 726 727
				// non-indexed elements (unconnected triangle soup)

				for ( var i = 0, il = positions.length; i < il; i += 9 ) {
728

M
Mr.doob 已提交
729 730 731
					pA.fromArray( positions, i );
					pB.fromArray( positions, i + 3 );
					pC.fromArray( positions, i + 6 );
732

733 734 735
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
736

G
gero3 已提交
737
					normals[ i ] = cb.x;
738 739
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
740

741 742 743
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
744

745 746 747
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
748 749 750 751 752

				}

			}

753
			this.normalizeNormals();
754

755
			attributes.normal.needsUpdate = true;
756

757
		}
758

759
	},
760

761 762 763 764 765
	computeTangents: function () {

		// based on http://www.terathon.com/code/tangent.html
		// (per vertex tangents)

766 767 768 769
		if ( this.attributes.index === undefined ||
			 this.attributes.position === undefined ||
			 this.attributes.normal === undefined ||
			 this.attributes.uv === undefined ) {
770

771
			console.warn( 'THREE.BufferGeometry: Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()' );
772 773 774 775
			return;

		}

776 777 778 779
		var indices = this.attributes.index.array;
		var positions = this.attributes.position.array;
		var normals = this.attributes.normal.array;
		var uvs = this.attributes.uv.array;
780 781 782

		var nVertices = positions.length / 3;

783
		if ( this.attributes.tangent === undefined ) {
784

785
			this.addAttribute( 'tangent', new THREE.BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
786 787 788

		}

789
		var tangents = this.attributes.tangent.array;
790 791 792 793 794 795 796 797 798 799

		var tan1 = [], tan2 = [];

		for ( var k = 0; k < nVertices; k ++ ) {

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

		}

M
Mr.doob 已提交
800 801 802
		var vA = new THREE.Vector3(),
			vB = new THREE.Vector3(),
			vC = new THREE.Vector3(),
803

M
Mr.doob 已提交
804 805 806
			uvA = new THREE.Vector2(),
			uvB = new THREE.Vector2(),
			uvC = new THREE.Vector2(),
807 808 809 810 811 812 813 814

			x1, x2, y1, y2, z1, z2,
			s1, s2, t1, t2, r;

		var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();

		function handleTriangle( a, b, c ) {

M
Mr.doob 已提交
815 816 817
			vA.fromArray( positions, a * 3 );
			vB.fromArray( positions, b * 3 );
			vC.fromArray( positions, c * 3 );
818

M
Mr.doob 已提交
819 820 821
			uvA.fromArray( uvs, a * 2 );
			uvB.fromArray( uvs, b * 2 );
			uvC.fromArray( uvs, c * 2 );
822

M
Mr.doob 已提交
823 824
			x1 = vB.x - vA.x;
			x2 = vC.x - vA.x;
825

M
Mr.doob 已提交
826 827
			y1 = vB.y - vA.y;
			y2 = vC.y - vA.y;
828

M
Mr.doob 已提交
829 830
			z1 = vB.z - vA.z;
			z2 = vC.z - vA.z;
831

M
Mr.doob 已提交
832 833
			s1 = uvB.x - uvA.x;
			s2 = uvC.x - uvA.x;
834

M
Mr.doob 已提交
835 836
			t1 = uvB.y - uvA.y;
			t2 = uvC.y - uvA.y;
837 838 839

			r = 1.0 / ( s1 * t2 - s2 * t1 );

840 841 842 843 844 845 846 847 848 849 850
			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
			);
851

852 853 854
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
855

856 857 858
			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );
859 860 861 862 863 864 865

		}

		var i, il;
		var j, jl;
		var iA, iB, iC;

866
		if ( this.drawcalls.length === 0 ) {
867

868
			this.addDrawCall( 0, indices.length );
869

870 871 872 873 874 875
		}

		var drawcalls = this.drawcalls;

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

876 877 878 879
			var drawcall = drawcalls[ j ];

			var start = drawcall.start;
			var count = drawcall.count;
880 881 882

			for ( i = start, il = start + count; i < il; i += 3 ) {

883 884 885
				iA = indices[ i + 0 ];
				iB = indices[ i + 1 ];
				iC = indices[ i + 2 ];
886 887 888 889 890 891 892 893 894 895 896 897 898

				handleTriangle( iA, iB, iC );

			}

		}

		var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
		var n = new THREE.Vector3(), n2 = new THREE.Vector3();
		var w, t, test;

		function handleVertex( v ) {

M
Mr.doob 已提交
899
			n.fromArray( normals, v * 3 );
900 901 902 903 904 905 906
			n2.copy( n );

			t = tan1[ v ];

			// Gram-Schmidt orthogonalize

			tmp.copy( t );
907
			tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
908 909 910

			// Calculate handedness

911
			tmp2.crossVectors( n2, t );
912
			test = tmp2.dot( tan2[ v ] );
913
			w = ( test < 0.0 ) ? - 1.0 : 1.0;
914

G
gero3 已提交
915
			tangents[ v * 4 ] = tmp.x;
916 917 918 919 920 921
			tangents[ v * 4 + 1 ] = tmp.y;
			tangents[ v * 4 + 2 ] = tmp.z;
			tangents[ v * 4 + 3 ] = w;

		}

922
		for ( j = 0, jl = drawcalls.length; j < jl; ++ j ) {
923

924 925 926 927
			var drawcall = drawcalls[ j ];

			var start = drawcall.start;
			var count = drawcall.count;
928 929 930

			for ( i = start, il = start + count; i < il; i += 3 ) {

931 932 933
				iA = indices[ i + 0 ];
				iB = indices[ i + 1 ];
				iC = indices[ i + 2 ];
934 935 936 937 938 939 940 941 942

				handleVertex( iA );
				handleVertex( iB );
				handleVertex( iC );

			}

		}

943 944
	},

M
Mr.doob 已提交
945
	computeOffsets: function ( size ) {
946

947
		console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.')
948 949 950

	},

951 952 953 954
	merge: function ( geometry, offset ) {

		if ( geometry instanceof THREE.BufferGeometry === false ) {

955
			console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
956 957 958 959 960 961 962 963 964 965 966
			return;

		}

		if ( offset === undefined ) offset = 0;

		var attributes = this.attributes;

		for ( var key in attributes ) {

			if ( geometry.attributes[ key ] === undefined ) continue;
967

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
			var attribute1 = attributes[ key ];
			var attributeArray1 = attribute1.array;

			var attribute2 = geometry.attributes[ key ];
			var attributeArray2 = attribute2.array;

			var attributeSize = attribute2.itemSize;

			for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {

				attributeArray1[ j ] = attributeArray2[ i ];

			}

		}

		return this;
985 986 987 988 989

	},

	normalizeNormals: function () {

990
		var normals = this.attributes.normal.array;
991 992 993 994 995 996 997 998 999 1000 1001

		var x, y, z, n;

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

			x = normals[ i ];
			y = normals[ i + 1 ];
			z = normals[ i + 2 ];

			n = 1.0 / Math.sqrt( x * x + y * y + z * z );

G
gero3 已提交
1002
			normals[ i ] *= n;
1003 1004 1005 1006 1007 1008 1009
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;

		}

	},

1010
	toJSON: function () {
1011

1012 1013 1014 1015 1016 1017
		var data = {
			metadata: {
				version: 4.4,
				type: 'BufferGeometry',
				generator: 'BufferGeometry.toJSON'
			}
B
brason 已提交
1018
		};
1019

1020
		// standard BufferGeometry serialization
1021

1022
		data.uuid = this.uuid;
M
Mr.doob 已提交
1023
		data.type = this.type;
1024
		if ( this.name !== '' ) data.name = this.name;
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

		if ( this.parameters !== undefined ) {

			var parameters = this.parameters;

			for ( var key in parameters ) {

				if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];

			}

			return data;

		}

		data.data = { attributes: {} };
1041

1042
		var attributes = this.attributes;
1043
		var drawcalls = this.drawcalls;
1044 1045 1046 1047 1048 1049
		var boundingSphere = this.boundingSphere;

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

M
makc 已提交
1050
			var array = Array.prototype.slice.call( attribute.array );
1051

1052
			data.data.attributes[ key ] = {
1053 1054 1055
				itemSize: attribute.itemSize,
				type: attribute.array.constructor.name,
				array: array
1056
			};
1057 1058 1059

		}

1060
		if ( drawcalls.length > 0 ) {
1061

1062
			data.data.drawcalls = JSON.parse( JSON.stringify( drawcalls ) );
1063 1064 1065 1066 1067

		}

		if ( boundingSphere !== null ) {

1068
			data.data.boundingSphere = {
1069 1070
				center: boundingSphere.center.toArray(),
				radius: boundingSphere.radius
1071
			};
1072 1073 1074

		}

1075
		return data;
1076 1077 1078

	},

M
Mr.doob 已提交
1079
	clone: function () {
O
ohmed 已提交
1080

1081
		return new this.constructor().copy( this );
O
ohmed 已提交
1082

D
dubejf 已提交
1083 1084 1085
	},

	copy: function ( source ) {
O
ohmed 已提交
1086

M
Mr.doob 已提交
1087 1088
		var attributes = source.attributes;
		var drawcalls = source.drawcalls;
O
ohmed 已提交
1089

M
Mr.doob 已提交
1090
		for ( var name in attributes ) {
O
ohmed 已提交
1091

M
Mr.doob 已提交
1092 1093
			var attribute = attributes[ name ];
			this.addAttribute( name, attribute.clone() );
O
ohmed 已提交
1094

M
Mr.doob 已提交
1095
		}
O
ohmed 已提交
1096

M
Mr.doob 已提交
1097
		for ( var i = 0, l = drawcalls.length; i < l; i ++ ) {
O
ohmed 已提交
1098

M
Mr.doob 已提交
1099 1100
			var drawcall = drawcalls[ i ];
			this.addDrawCall( drawcall.start, drawcall.count );
O
ohmed 已提交
1101

O
ohmed 已提交
1102
		}
O
ohmed 已提交
1103

1104
		return this;
O
ohmed 已提交
1105 1106 1107

	},

1108
	dispose: function () {
1109

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

1112
	}
1113 1114

};
M
Mr.doob 已提交
1115 1116

THREE.EventDispatcher.prototype.apply( THREE.BufferGeometry.prototype );
1117

1118
THREE.BufferGeometry.MaxIndex = 65535;