BufferGeometry.js 18.7 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.index = null;
16
	this.attributes = {};
17

18
	this.morphAttributes = {};
19

20
	this.groups = [];
21

22 23 24
	this.boundingBox = null;
	this.boundingSphere = null;

25 26
	this.drawRange = { start: 0, count: Infinity };

27 28
};

29
Object.assign( THREE.BufferGeometry.prototype, THREE.EventDispatcher.prototype, {
30

31 32 33 34 35 36 37 38 39
	getIndex: function () {

		return this.index;

	},

	setIndex: function ( index ) {

		this.index = index;
40 41 42

	},

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

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

47
			console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
48

49
			this.addAttribute( name, new THREE.BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
50 51 52 53 54

			return;

		}

55
		if ( name === 'index' ) {
M
Mr.doob 已提交
56

57
			console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' );
58
			this.setIndex( attribute );
M
Mr.doob 已提交
59

60 61
			return;

M
Mr.doob 已提交
62 63
		}

M
Mr.doob 已提交
64
		this.attributes[ name ] = attribute;
65

M
Mr.doob 已提交
66 67
		return this;

M
Mr.doob 已提交
68 69 70 71
	},

	getAttribute: function ( name ) {

72 73
		return this.attributes[ name ];

74 75
	},

76 77 78 79
	removeAttribute: function ( name ) {

		delete this.attributes[ name ];

M
Mr.doob 已提交
80 81
		return this;

82 83
	},

84 85 86
	addGroup: function ( start, count, materialIndex ) {

		this.groups.push( {
M
Mr.doob 已提交
87 88

			start: start,
89 90
			count: count,
			materialIndex: materialIndex !== undefined ? materialIndex : 0
M
Mr.doob 已提交
91 92 93 94 95

		} );

	},

96
	clearGroups: function () {
D
dubejf 已提交
97

98
		this.groups = [];
D
dubejf 已提交
99 100 101

	},

102 103 104 105 106 107 108
	setDrawRange: function ( start, count ) {

		this.drawRange.start = start;
		this.drawRange.count = count;

	},

109 110
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
111
		var position = this.attributes.position;
112

M
Mr.doob 已提交
113
		if ( position !== undefined ) {
114

115
			matrix.applyToVector3Array( position.array );
M
Mr.doob 已提交
116
			position.needsUpdate = true;
117 118 119

		}

M
Mr.doob 已提交
120
		var normal = this.attributes.normal;
121

M
Mr.doob 已提交
122
		if ( normal !== undefined ) {
123

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

126
			normalMatrix.applyToVector3Array( normal.array );
M
Mr.doob 已提交
127
			normal.needsUpdate = true;
128 129 130

		}

M
Mr.doob 已提交
131
		if ( this.boundingBox !== null ) {
132 133 134 135 136

			this.computeBoundingBox();

		}

M
Mr.doob 已提交
137
		if ( this.boundingSphere !== null ) {
138 139 140 141 142

			this.computeBoundingSphere();

		}

143 144
		return this;

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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	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 );

		};

	}(),

265 266
	center: function () {

M
Mr.doob 已提交
267 268 269 270
		this.computeBoundingBox();

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

271
		this.translate( offset.x, offset.y, offset.z );
M
Mr.doob 已提交
272 273

		return offset;
274 275 276

	},

277
	setFromObject: function ( object ) {
278

279
		// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
280

281 282
		var geometry = object.geometry;

283
		if ( object instanceof THREE.Points || object instanceof THREE.Line ) {
284

285 286
			var positions = new THREE.Float32Attribute( geometry.vertices.length * 3, 3 );
			var colors = new THREE.Float32Attribute( geometry.colors.length * 3, 3 );
287

288 289
			this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
			this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
290

C
Chen Pang 已提交
291 292 293 294
			if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {

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

295
				this.addAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
C
Chen Pang 已提交
296 297 298

			}

299 300 301 302 303 304 305 306 307 308 309
			if ( geometry.boundingSphere !== null ) {

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

			}

			if ( geometry.boundingBox !== null ) {

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

			}
310 311 312

		} else if ( object instanceof THREE.Mesh ) {

M
Mr.doob 已提交
313
			if ( geometry instanceof THREE.Geometry ) {
314 315

				this.fromGeometry( geometry );
316

M
Mr.doob 已提交
317
			}
318 319 320 321 322 323 324

		}

		return this;

	},

325 326
	updateFromObject: function ( object ) {

M
Mr.doob 已提交
327
		var geometry = object.geometry;
328

M
Mr.doob 已提交
329
		if ( object instanceof THREE.Mesh ) {
330

M
Mr.doob 已提交
331
			var direct = geometry.__directGeometry;
332

333 334 335 336 337 338
			if ( direct === undefined ) {

				return this.fromGeometry( geometry );

			}

M
Mr.doob 已提交
339 340 341 342
			direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
			direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
			direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
			direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
343
			direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
M
Mr.doob 已提交
344 345 346 347 348

			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
349
			geometry.groupsNeedUpdate = false;
350

M
Mr.doob 已提交
351
			geometry = direct;
352 353

		}
354

M
Mr.doob 已提交
355
		if ( geometry.verticesNeedUpdate === true ) {
356

M
Mr.doob 已提交
357
			var attribute = this.attributes.position;
358

M
Mr.doob 已提交
359
			if ( attribute !== undefined ) {
360

M
Mr.doob 已提交
361 362
				attribute.copyVector3sArray( geometry.vertices );
				attribute.needsUpdate = true;
363 364 365

			}

M
Mr.doob 已提交
366
			geometry.verticesNeedUpdate = false;
367

M
Mr.doob 已提交
368
		}
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
		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 已提交
385
		if ( geometry.colorsNeedUpdate === true ) {
386

M
Mr.doob 已提交
387
			var attribute = this.attributes.color;
388

M
Mr.doob 已提交
389
			if ( attribute !== undefined ) {
390

M
Mr.doob 已提交
391 392
				attribute.copyColorsArray( geometry.colors );
				attribute.needsUpdate = true;
393 394 395

			}

M
Mr.doob 已提交
396 397
			geometry.colorsNeedUpdate = false;

398
		}
399

400 401
		if ( geometry.uvsNeedUpdate ) {

M
Mr.doob 已提交
402
			var attribute = this.attributes.uv;
403

M
Mr.doob 已提交
404
			if ( attribute !== undefined ) {
405

M
Mr.doob 已提交
406 407
				attribute.copyVector2sArray( geometry.uvs );
				attribute.needsUpdate = true;
408

M
Mr.doob 已提交
409
			}
410

M
Mr.doob 已提交
411
			geometry.uvsNeedUpdate = false;
412 413 414

		}

C
Chen Pang 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		if ( geometry.lineDistancesNeedUpdate ) {

			var attribute = this.attributes.lineDistance;

			if ( attribute !== undefined ) {

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

			}

			geometry.lineDistancesNeedUpdate = false;

		}

430 431 432 433 434 435 436 437 438
		if ( geometry.groupsNeedUpdate ) {

			geometry.computeGroups( object.geometry );
			this.groups = geometry.groups;

			geometry.groupsNeedUpdate = false;

		}

439
		return this;
440 441 442

	},

443
	fromGeometry: function ( geometry ) {
444

445
		geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
446 447

		return this.fromDirectGeometry( geometry.__directGeometry );
448

449
	},
450

451
	fromDirectGeometry: function ( geometry ) {
452

453 454
		var positions = new Float32Array( geometry.vertices.length * 3 );
		this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
455

456 457 458 459 460 461 462 463
		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 已提交
464

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

468 469 470 471 472 473 474 475
		}

		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 已提交
476

477 478 479 480 481 482 483
		if ( geometry.uvs2.length > 0 ) {

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

		}

484 485
		if ( geometry.indices.length > 0 ) {

M
Mr.doob 已提交
486 487
			var TypeArray = geometry.vertices.length > 65535 ? Uint32Array : Uint16Array;
			var indices = new TypeArray( geometry.indices.length * 3 );
488
			this.setIndex( new THREE.BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
489 490 491

		}

492 493 494 495
		// groups

		this.groups = geometry.groups;

496 497
		// morphs

498
		for ( var name in geometry.morphTargets ) {
499

500 501
			var array = [];
			var morphTargets = geometry.morphTargets[ name ];
502 503 504 505 506 507 508

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

				var morphTarget = morphTargets[ i ];

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

509
				array.push( attribute.copyVector3sArray( morphTarget ) );
510 511 512

			}

513
			this.morphAttributes[ name ] = array;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

		}

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

		}

		//

535 536 537 538 539 540 541 542 543 544 545
		if ( geometry.boundingSphere !== null ) {

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

		}

		if ( geometry.boundingBox !== null ) {

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

		}
M
Mr.doob 已提交
546 547 548 549 550

		return this;

	},

551 552
	computeBoundingBox: function () {

553
		if ( this.boundingBox === null ) {
554

555
			this.boundingBox = new THREE.Box3();
556

557
		}
558

559
		var positions = this.attributes.position.array;
560

561
		if ( positions !== undefined ) {
562

563
			this.boundingBox.setFromArray( positions );
564

565
		} else {
566

567
			this.boundingBox.makeEmpty();
568

569
		}
570

571
		if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
572

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

575
		}
576

577
	},
578 579 580

	computeBoundingSphere: function () {

581 582
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
583

584
		return function computeBoundingSphere() {
585

586
			if ( this.boundingSphere === null ) {
587

588
				this.boundingSphere = new THREE.Sphere();
589

590
			}
591

592
			var positions = this.attributes.position.array;
593

594
			if ( positions ) {
595

596
				var center = this.boundingSphere.center;
597

598
				box.setFromArray( positions );
599 600
				box.center( center );

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

604 605
				var maxRadiusSq = 0;

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

608
					vector.fromArray( positions, i );
609
					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
610

611 612 613 614
				}

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

615 616
				if ( isNaN( this.boundingSphere.radius ) ) {

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

				}

621
			}
622

G
Greg French 已提交
623
		};
624

625
	}(),
626

627 628 629 630 631 632
	computeFaceNormals: function () {

		// backwards compatibility

	},

633 634
	computeVertexNormals: function () {

635
		var index = this.index;
636
		var attributes = this.attributes;
637
		var groups = this.groups;
638

639
		if ( attributes.position ) {
640

641
			var positions = attributes.position.array;
642

643
			if ( attributes.normal === undefined ) {
644

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

			} else {

				// reset existing normals to zero

M
Mr.doob 已提交
651
				var array = attributes.normal.array;
652

M
Mr.doob 已提交
653
				for ( var i = 0, il = array.length; i < il; i ++ ) {
654

M
Mr.doob 已提交
655
					array[ i ] = 0;
656 657 658 659 660

				}

			}

661
			var normals = attributes.normal.array;
662

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

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

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

672 673
			// indexed elements

674
			if ( index ) {
675

676
				var indices = index.array;
677

678
				if ( groups.length === 0 ) {
D
dubejf 已提交
679

680
					this.addGroup( 0, indices.length );
681

D
dubejf 已提交
682 683
				}

684
				for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
685

686
					var group = groups[ j ];
687

688 689
					var start = group.start;
					var count = group.count;
690

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

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

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

701 702 703
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
704

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

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

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

					}

				}

			} else {
722

723 724 725
				// non-indexed elements (unconnected triangle soup)

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

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

731 732 733
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
734

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

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

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

				}

			}

751
			this.normalizeNormals();
752

753
			attributes.normal.needsUpdate = true;
754

755
		}
756

757
	},
758

759 760 761 762
	merge: function ( geometry, offset ) {

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

763
			console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
764 765 766 767 768 769 770 771 772 773 774
			return;

		}

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

		var attributes = this.attributes;

		for ( var key in attributes ) {

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

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
			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;
793 794 795 796 797

	},

	normalizeNormals: function () {

798
		var normals = this.attributes.normal.array;
799 800 801 802 803 804 805 806 807 808 809

		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 已提交
810
			normals[ i ] *= n;
811 812 813 814 815 816 817
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;

		}

	},

M
Mr.doob 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	toNonIndexed: function () {

		if ( this.index === null ) {

			console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
			return this;

		}

		var geometry2 = new THREE.BufferGeometry();

		var indices = this.index.array;
		var attributes = this.attributes;

		for ( var name in attributes ) {

			var attribute = attributes[ name ];

			var array = attribute.array;
			var itemSize = attribute.itemSize;

			var array2 = new array.constructor( indices.length * itemSize );

M
Mr.doob 已提交
841
			var index = 0, index2 = 0;
M
Mr.doob 已提交
842

M
Mr.doob 已提交
843
			for ( var i = 0, l = indices.length; i < l; i ++ ) {
M
Mr.doob 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

				index = indices[ i ] * itemSize;

				for ( var j = 0; j < itemSize; j ++ ) {

					array2[ index2 ++ ] = array[ index ++ ];

				}

			}

			geometry2.addAttribute( name, new THREE.BufferAttribute( array2, itemSize ) );

		}

		return geometry2;

	},

863
	toJSON: function () {
864

865 866 867 868 869 870
		var data = {
			metadata: {
				version: 4.4,
				type: 'BufferGeometry',
				generator: 'BufferGeometry.toJSON'
			}
B
brason 已提交
871
		};
872

873
		// standard BufferGeometry serialization
874

875
		data.uuid = this.uuid;
M
Mr.doob 已提交
876
		data.type = this.type;
877
		if ( this.name !== '' ) data.name = this.name;
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

		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: {} };
894

M
Mr.doob 已提交
895
		var index = this.index;
896

M
Mr.doob 已提交
897 898 899 900 901 902 903 904 905 906 907 908
		if ( index !== null ) {

			var array = Array.prototype.slice.call( index.array );

			data.data.index = {
				type: index.array.constructor.name,
				array: array
			};

		}

		var attributes = this.attributes;
909 910 911 912 913

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

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

916
			data.data.attributes[ key ] = {
917 918
				itemSize: attribute.itemSize,
				type: attribute.array.constructor.name,
919 920
				array: array,
				normalized: attribute.normalized
921
			};
922 923 924

		}

M
Mr.doob 已提交
925 926
		var groups = this.groups;

927
		if ( groups.length > 0 ) {
928

929
			data.data.groups = JSON.parse( JSON.stringify( groups ) );
930 931 932

		}

M
Mr.doob 已提交
933 934
		var boundingSphere = this.boundingSphere;

935 936
		if ( boundingSphere !== null ) {

937
			data.data.boundingSphere = {
938 939
				center: boundingSphere.center.toArray(),
				radius: boundingSphere.radius
940
			};
941 942 943

		}

944
		return data;
945 946 947

	},

M
Mr.doob 已提交
948
	clone: function () {
O
ohmed 已提交
949

950
		/*
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
		// 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;

		}

971
		return new this.constructor().copy( this );
972 973 974
		*/

		return new THREE.BufferGeometry().copy( this );
O
ohmed 已提交
975

D
dubejf 已提交
976 977 978
	},

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

980 981 982 983
		var index = source.index;

		if ( index !== null ) {

984
			this.setIndex( index.clone() );
985 986 987

		}

M
Mr.doob 已提交
988
		var attributes = source.attributes;
O
ohmed 已提交
989

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

M
Mr.doob 已提交
992 993
			var attribute = attributes[ name ];
			this.addAttribute( name, attribute.clone() );
O
ohmed 已提交
994

M
Mr.doob 已提交
995
		}
O
ohmed 已提交
996

997 998
		var groups = source.groups;

999 1000 1001
		for ( var i = 0, l = groups.length; i < l; i ++ ) {

			var group = groups[ i ];
1002
			this.addGroup( group.start, group.count, group.materialIndex );
O
ohmed 已提交
1003

O
ohmed 已提交
1004
		}
O
ohmed 已提交
1005

1006
		return this;
O
ohmed 已提交
1007 1008 1009

	},

1010
	dispose: function () {
1011

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

1014
	}
1015

1016
} );
1017

1018
THREE.BufferGeometry.MaxIndex = 65535;