BufferGeometry.js 18.9 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
			if ( direct === undefined || geometry.elementsNeedUpdate === true ) {
334 335 336 337 338

				return this.fromGeometry( geometry );

			}

339 340 341 342 343
			direct.verticesNeedUpdate = geometry.verticesNeedUpdate || geometry.elementsNeedUpdate;
			direct.normalsNeedUpdate = geometry.normalsNeedUpdate || geometry.elementsNeedUpdate;
			direct.colorsNeedUpdate = geometry.colorsNeedUpdate || geometry.elementsNeedUpdate;
			direct.uvsNeedUpdate = geometry.uvsNeedUpdate || geometry.elementsNeedUpdate;
			direct.groupsNeedUpdate = geometry.groupsNeedUpdate || geometry.elementsNeedUpdate;
M
Mr.doob 已提交
344

345
			geometry.elementsNeedUpdate = false;
M
Mr.doob 已提交
346 347 348 349
			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
350
			geometry.groupsNeedUpdate = false;
351

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

		}
355

M
Mr.doob 已提交
356 357
		var attribute;

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

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

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

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

			}

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

M
Mr.doob 已提交
371
		}
372

373 374
		if ( geometry.normalsNeedUpdate === true ) {

M
Mr.doob 已提交
375
			attribute = this.attributes.normal;
376 377 378 379 380 381 382 383 384 385 386 387

			if ( attribute !== undefined ) {

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

			}

			geometry.normalsNeedUpdate = false;

		}

M
Mr.doob 已提交
388
		if ( geometry.colorsNeedUpdate === true ) {
389

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

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

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

			}

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

401
		}
402

403 404
		if ( geometry.uvsNeedUpdate ) {

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

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

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

M
Mr.doob 已提交
412
			}
413

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

		}

C
Chen Pang 已提交
418 419
		if ( geometry.lineDistancesNeedUpdate ) {

M
Mr.doob 已提交
420
			attribute = this.attributes.lineDistance;
C
Chen Pang 已提交
421 422 423 424 425 426 427 428 429 430 431 432

			if ( attribute !== undefined ) {

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

			}

			geometry.lineDistancesNeedUpdate = false;

		}

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

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

			geometry.groupsNeedUpdate = false;

		}

442
		return this;
443 444 445

	},

446
	fromGeometry: function ( geometry ) {
447

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

		return this.fromDirectGeometry( geometry.__directGeometry );
451

452
	},
453

454
	fromDirectGeometry: function ( geometry ) {
455

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

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

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

471 472 473 474 475 476 477 478
		}

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

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

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

		}

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

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

		}

495 496 497 498
		// groups

		this.groups = geometry.groups;

499 500
		// morphs

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

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

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

				var morphTarget = morphTargets[ i ];

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

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

			}

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

		}

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

		}

		//

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

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

		}

		if ( geometry.boundingBox !== null ) {

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

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

		return this;

	},

554 555
	computeBoundingBox: function () {

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

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

560
		}
561

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

564
		if ( positions !== undefined ) {
565

566
			this.boundingBox.setFromArray( positions );
567

568
		} else {
569

570
			this.boundingBox.makeEmpty();
571

572
		}
573

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

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

578
		}
579

580
	},
581 582 583

	computeBoundingSphere: function () {

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

587
		return function computeBoundingSphere() {
588

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

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

593
			}
594

595
			var positions = this.attributes.position;
596

597
			if ( positions ) {
598

599
				var array = positions.array;
600
				var center = this.boundingSphere.center;
601

602
				box.setFromArray( array );
603 604
				box.center( center );

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

608 609
				var maxRadiusSq = 0;

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

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

615 616 617 618
				}

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

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

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

				}

625
			}
626

G
Greg French 已提交
627
		};
628

629
	}(),
630

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

		// backwards compatibility

	},

637 638
	computeVertexNormals: function () {

639
		var index = this.index;
640
		var attributes = this.attributes;
641
		var groups = this.groups;
642

643
		if ( attributes.position ) {
644

645
			var positions = attributes.position.array;
646

647
			if ( attributes.normal === undefined ) {
648

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

			} else {

				// reset existing normals to zero

M
Mr.doob 已提交
655
				var array = attributes.normal.array;
656

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

M
Mr.doob 已提交
659
					array[ i ] = 0;
660 661 662 663 664

				}

			}

665
			var normals = attributes.normal.array;
666

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

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

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

676 677
			// indexed elements

678
			if ( index ) {
679

680
				var indices = index.array;
681

682
				if ( groups.length === 0 ) {
D
dubejf 已提交
683

684
					this.addGroup( 0, indices.length );
685

D
dubejf 已提交
686 687
				}

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

690
					var group = groups[ j ];
691

692 693
					var start = group.start;
					var count = group.count;
694

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

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

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

705 706 707
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
708

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

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

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

					}

				}

			} else {
726

727 728 729
				// non-indexed elements (unconnected triangle soup)

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

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

735 736 737
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
738

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

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

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

				}

			}

755
			this.normalizeNormals();
756

757
			attributes.normal.needsUpdate = true;
758

759
		}
760

761
	},
762

763 764 765 766
	merge: function ( geometry, offset ) {

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

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

		}

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

		var attributes = this.attributes;

		for ( var key in attributes ) {

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

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

	},

	normalizeNormals: function () {

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

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

		}

	},

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

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

				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;

	},

867
	toJSON: function () {
868

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

877
		// standard BufferGeometry serialization
878

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

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

M
Mr.doob 已提交
899
		var index = this.index;
900

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

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

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

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

		}

M
Mr.doob 已提交
929 930
		var groups = this.groups;

931
		if ( groups.length > 0 ) {
932

933
			data.data.groups = JSON.parse( JSON.stringify( groups ) );
934 935 936

		}

M
Mr.doob 已提交
937 938
		var boundingSphere = this.boundingSphere;

939 940
		if ( boundingSphere !== null ) {

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

		}

948
		return data;
949 950 951

	},

M
Mr.doob 已提交
952
	clone: function () {
O
ohmed 已提交
953

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

		}

975
		return new this.constructor().copy( this );
976 977 978
		*/

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

D
dubejf 已提交
980 981 982
	},

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

984 985 986 987
		var index = source.index;

		if ( index !== null ) {

988
			this.setIndex( index.clone() );
989 990 991

		}

M
Mr.doob 已提交
992
		var attributes = source.attributes;
O
ohmed 已提交
993

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

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

M
Mr.doob 已提交
999
		}
O
ohmed 已提交
1000

1001 1002
		var groups = source.groups;

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

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

O
ohmed 已提交
1008
		}
O
ohmed 已提交
1009

1010
		return this;
O
ohmed 已提交
1011 1012 1013

	},

1014
	dispose: function () {
1015

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

1018
	}
1019

1020
} );
1021

1022
THREE.BufferGeometry.MaxIndex = 65535;