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 30
};

THREE.BufferGeometry.prototype = {

31
	constructor: THREE.BufferGeometry,
32

33 34 35 36 37 38 39 40 41
	getIndex: function () {

		return this.index;

	},

	setIndex: function ( index ) {

		this.index = index;
42 43 44

	},

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

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

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

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

			return;

		}

57
		if ( name === 'index' ) {
M
Mr.doob 已提交
58

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

62 63
			return;

M
Mr.doob 已提交
64 65
		}

M
Mr.doob 已提交
66
		this.attributes[ name ] = attribute;
67

M
Mr.doob 已提交
68 69
		return this;

M
Mr.doob 已提交
70 71 72 73
	},

	getAttribute: function ( name ) {

74 75
		return this.attributes[ name ];

76 77
	},

78 79 80 81
	removeAttribute: function ( name ) {

		delete this.attributes[ name ];

M
Mr.doob 已提交
82 83
		return this;

84 85
	},

86 87 88
	addGroup: function ( start, count, materialIndex ) {

		this.groups.push( {
M
Mr.doob 已提交
89 90

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

		} );

	},

98
	clearGroups: function () {
D
dubejf 已提交
99

100
		this.groups = [];
D
dubejf 已提交
101 102 103

	},

104 105 106 107 108 109 110
	setDrawRange: function ( start, count ) {

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

	},

111 112
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
113
		var position = this.attributes.position;
114

M
Mr.doob 已提交
115
		if ( position !== undefined ) {
116

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

		}

M
Mr.doob 已提交
122
		var normal = this.attributes.normal;
123

M
Mr.doob 已提交
124
		if ( normal !== undefined ) {
125

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

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

		}

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

			this.computeBoundingBox();

		}

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

			this.computeBoundingSphere();

		}

145 146
		return this;

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 265 266
	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 );

		};

	}(),

267 268
	center: function () {

M
Mr.doob 已提交
269 270 271 272
		this.computeBoundingBox();

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

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

		return offset;
276 277 278

	},

279
	setFromObject: function ( object ) {
280

281
		// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
282

283 284
		var geometry = object.geometry;

285
		if ( object instanceof THREE.Points || object instanceof THREE.Line ) {
286

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

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

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

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

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

			}

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

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

			}

			if ( geometry.boundingBox !== null ) {

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

			}
312 313 314

		} else if ( object instanceof THREE.Mesh ) {

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

				this.fromGeometry( geometry );
318

M
Mr.doob 已提交
319
			}
320 321 322 323 324 325 326

		}

		return this;

	},

327 328
	updateFromObject: function ( object ) {

M
Mr.doob 已提交
329
		var geometry = object.geometry;
330

M
Mr.doob 已提交
331
		if ( object instanceof THREE.Mesh ) {
332

M
Mr.doob 已提交
333
			var direct = geometry.__directGeometry;
334

335 336 337 338 339 340
			if ( direct === undefined ) {

				return this.fromGeometry( geometry );

			}

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

			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
351
			geometry.groupsNeedUpdate = false;
352

M
Mr.doob 已提交
353
			geometry = direct;
354 355

		}
356

M
Mr.doob 已提交
357
		if ( geometry.verticesNeedUpdate === true ) {
358

M
Mr.doob 已提交
359
			var attribute = this.attributes.position;
360

M
Mr.doob 已提交
361
			if ( attribute !== undefined ) {
362

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

			}

M
Mr.doob 已提交
368
			geometry.verticesNeedUpdate = false;
369

M
Mr.doob 已提交
370
		}
371

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

M
Mr.doob 已提交
389
			var attribute = this.attributes.color;
390

M
Mr.doob 已提交
391
			if ( attribute !== undefined ) {
392

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

			}

M
Mr.doob 已提交
398 399
			geometry.colorsNeedUpdate = false;

400
		}
401

402 403
		if ( geometry.uvsNeedUpdate ) {

M
Mr.doob 已提交
404
			var attribute = this.attributes.uv;
405

M
Mr.doob 已提交
406
			if ( attribute !== undefined ) {
407

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

M
Mr.doob 已提交
411
			}
412

M
Mr.doob 已提交
413
			geometry.uvsNeedUpdate = false;
414 415 416

		}

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

			var attribute = this.attributes.lineDistance;

			if ( attribute !== undefined ) {

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

			}

			geometry.lineDistancesNeedUpdate = false;

		}

432 433 434 435 436 437 438 439 440
		if ( geometry.groupsNeedUpdate ) {

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

			geometry.groupsNeedUpdate = false;

		}

441
		return this;
442 443 444

	},

445
	fromGeometry: function ( geometry ) {
446

447
		geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
448 449

		return this.fromDirectGeometry( geometry.__directGeometry );
450

451
	},
452

453
	fromDirectGeometry: function ( geometry ) {
454

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

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

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

470 471 472 473 474 475 476 477
		}

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

479 480 481 482 483 484 485
		if ( geometry.uvs2.length > 0 ) {

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

		}

486 487
		if ( geometry.indices.length > 0 ) {

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

		}

494 495 496 497
		// groups

		this.groups = geometry.groups;

498 499
		// morphs

500
		for ( var name in geometry.morphTargets ) {
501

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

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

				var morphTarget = morphTargets[ i ];

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

511
				array.push( attribute.copyVector3sArray( morphTarget ) );
512 513 514

			}

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

		}

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

		}

		//

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

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

		}

		if ( geometry.boundingBox !== null ) {

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

		}
M
Mr.doob 已提交
548 549 550 551 552

		return this;

	},

553 554
	computeBoundingBox: function () {

555
		if ( this.boundingBox === null ) {
556

557
			this.boundingBox = new THREE.Box3();
558

559
		}
560

561
		var positions = this.attributes.position.array;
562

563
		if ( positions !== undefined ) {
564

565
			this.boundingBox.setFromArray( positions );
566

567
		} else {
568

569
			this.boundingBox.makeEmpty();
570

571
		}
572

573
		if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
574

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

577
		}
578

579
	},
580 581 582

	computeBoundingSphere: function () {

583 584
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
585

586
		return function computeBoundingSphere() {
587

588
			if ( this.boundingSphere === null ) {
589

590
				this.boundingSphere = new THREE.Sphere();
591

592
			}
593

594
			var positions = this.attributes.position.array;
595

596
			if ( positions ) {
597

598
				var center = this.boundingSphere.center;
599

600
				box.setFromArray( positions );
601 602
				box.center( center );

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

606 607
				var maxRadiusSq = 0;

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

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

613 614 615 616
				}

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

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

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

				}

623
			}
624

G
Greg French 已提交
625
		};
626

627
	}(),
628

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

		// backwards compatibility

	},

635 636
	computeVertexNormals: function () {

637
		var index = this.index;
638
		var attributes = this.attributes;
639
		var groups = this.groups;
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

M
Mr.doob 已提交
653
				var array = attributes.normal.array;
654

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

M
Mr.doob 已提交
657
					array[ 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 ( index ) {
677

678
				var indices = index.array;
679

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

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

D
dubejf 已提交
684 685
				}

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

688
					var group = groups[ j ];
689

690 691
					var start = group.start;
					var count = group.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
	merge: function ( geometry, offset ) {

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

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

		}

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

		var attributes = this.attributes;

		for ( var key in attributes ) {

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

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

	},

	normalizeNormals: function () {

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

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

		}

	},

M
Mr.doob 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	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 已提交
843
			var index = 0, index2 = 0;
M
Mr.doob 已提交
844

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

				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;

	},

865
	toJSON: function () {
866

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

875
		// standard BufferGeometry serialization
876

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

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

M
Mr.doob 已提交
897
		var index = this.index;
898

M
Mr.doob 已提交
899 900 901 902 903 904 905 906 907 908 909 910
		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;
911 912 913 914 915

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

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

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

		}

M
Mr.doob 已提交
927 928
		var groups = this.groups;

929
		if ( groups.length > 0 ) {
930

931
			data.data.groups = JSON.parse( JSON.stringify( groups ) );
932 933 934

		}

M
Mr.doob 已提交
935 936
		var boundingSphere = this.boundingSphere;

937 938
		if ( boundingSphere !== null ) {

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

		}

946
		return data;
947 948 949

	},

M
Mr.doob 已提交
950
	clone: function () {
O
ohmed 已提交
951

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

		}

973
		return new this.constructor().copy( this );
974 975 976
		*/

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

D
dubejf 已提交
978 979 980
	},

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

982 983 984 985
		var index = source.index;

		if ( index !== null ) {

986
			this.setIndex( index.clone() );
987 988 989

		}

M
Mr.doob 已提交
990
		var attributes = source.attributes;
O
ohmed 已提交
991

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

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

M
Mr.doob 已提交
997
		}
O
ohmed 已提交
998

999 1000
		var groups = source.groups;

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

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

O
ohmed 已提交
1006
		}
O
ohmed 已提交
1007

1008
		return this;
O
ohmed 已提交
1009 1010 1011

	},

1012
	dispose: function () {
1013

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

1016
	}
1017 1018

};
M
Mr.doob 已提交
1019 1020

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

1022
THREE.BufferGeometry.MaxIndex = 65535;