BufferGeometry.js 18.3 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 25 26 27 28
	this.boundingBox = null;
	this.boundingSphere = null;

};

THREE.BufferGeometry.prototype = {

29
	constructor: THREE.BufferGeometry,
30

31 32 33 34 35 36
	addIndex: function ( attribute ) {

		this.index = attribute;

	},

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

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

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

43
			this.addAttribute( name, new THREE.BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
44 45 46 47 48

			return;

		}

49
		if ( name === 'index' ) {
M
Mr.doob 已提交
50

51 52
			console.warn( 'THREE.BufferGeometry.addAttribute: Use .addIndex() for index attribute.' );
			this.addIndex( attribute );
M
Mr.doob 已提交
53 54 55

		}

M
Mr.doob 已提交
56
		this.attributes[ name ] = attribute;
57

M
Mr.doob 已提交
58 59 60 61
	},

	getAttribute: function ( name ) {

62 63
		return this.attributes[ name ];

64 65
	},

66 67 68 69 70 71
	removeAttribute: function ( name ) {

		delete this.attributes[ name ];

	},

72 73 74 75 76 77 78
	get drawcalls() {

		console.error( 'THREE.BufferGeometry: .drawcalls has been renamed to .groups.' );
		return this.groups;

	},

79
	get offsets() {
80

81 82
		console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .groups.' );
		return this.groups;
83 84 85

	},

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

88 89 90 91 92 93
		if ( indexOffset !== undefined ) {

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

		}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		console.warn( 'THREE.BufferGeometry: .addDrawCall() is now .addGroup().' );
		this.addGroup( start, count );

	},

	clearDrawCalls: function () {

		console.warn( 'THREE.BufferGeometry: .clearDrawCalls() is now .clearGroups().' );
		this.clearGroups();

	},

	addGroup: function ( start, count, materialIndex ) {

		this.groups.push( {
M
Mr.doob 已提交
109 110

			start: start,
111 112
			count: count,
			materialIndex: materialIndex !== undefined ? materialIndex : 0
M
Mr.doob 已提交
113 114 115 116 117

		} );

	},

118
	clearGroups: function () {
D
dubejf 已提交
119

120
		this.groups = [];
D
dubejf 已提交
121 122 123

	},

124 125
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
126
		var position = this.attributes.position;
127

M
Mr.doob 已提交
128
		if ( position !== undefined ) {
129

130
			matrix.applyToVector3Array( position.array );
M
Mr.doob 已提交
131
			position.needsUpdate = true;
132 133 134

		}

M
Mr.doob 已提交
135
		var normal = this.attributes.normal;
136

M
Mr.doob 已提交
137
		if ( normal !== undefined ) {
138

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

141
			normalMatrix.applyToVector3Array( normal.array );
M
Mr.doob 已提交
142
			normal.needsUpdate = true;
143 144 145

		}

M
Mr.doob 已提交
146
		if ( this.boundingBox !== null ) {
147 148 149 150 151

			this.computeBoundingBox();

		}

M
Mr.doob 已提交
152
		if ( this.boundingSphere !== null ) {
153 154 155 156 157

			this.computeBoundingSphere();

		}

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 267 268 269 270 271 272 273 274 275 276 277
	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 );

		};

	}(),

278 279
	center: function () {

M
Mr.doob 已提交
280 281 282 283
		this.computeBoundingBox();

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

284
		this.translate( offset.x, offset.y, offset.z );
M
Mr.doob 已提交
285 286

		return offset;
287 288 289

	},

290
	setFromObject: function ( object ) {
291

292
		// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
293

294 295 296 297
		var geometry = object.geometry;

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

298 299
			var positions = new THREE.Float32Attribute( geometry.vertices.length * 3, 3 );
			var colors = new THREE.Float32Attribute( geometry.colors.length * 3, 3 );
300

301 302
			this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
			this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
303

C
Chen Pang 已提交
304 305 306 307 308 309 310 311
			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 ) );

			}

312 313 314 315 316 317 318 319 320 321 322
			if ( geometry.boundingSphere !== null ) {

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

			}

			if ( geometry.boundingBox !== null ) {

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

			}
323 324 325

		} else if ( object instanceof THREE.Mesh ) {

M
Mr.doob 已提交
326
			if ( geometry instanceof THREE.Geometry ) {
327 328

				this.fromGeometry( geometry );
329

M
Mr.doob 已提交
330
			}
331 332 333 334 335 336 337

		}

		return this;

	},

338 339
	updateFromObject: function ( object ) {

M
Mr.doob 已提交
340
		var geometry = object.geometry;
341

M
Mr.doob 已提交
342
		if ( object instanceof THREE.Mesh ) {
343

M
Mr.doob 已提交
344
			var direct = geometry.__directGeometry;
345

346 347 348 349 350 351
			if ( direct === undefined ) {

				return this.fromGeometry( geometry );

			}

M
Mr.doob 已提交
352 353 354 355
			direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
			direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
			direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
			direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
356
			direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
M
Mr.doob 已提交
357 358 359 360 361

			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
362
			geometry.groupsNeedUpdate = false;
363

M
Mr.doob 已提交
364
			geometry = direct;
365 366

		}
367

M
Mr.doob 已提交
368
		if ( geometry.verticesNeedUpdate === true ) {
369

M
Mr.doob 已提交
370
			var attribute = this.attributes.position;
371

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

M
Mr.doob 已提交
374 375
				attribute.copyVector3sArray( geometry.vertices );
				attribute.needsUpdate = true;
376 377 378

			}

M
Mr.doob 已提交
379
			geometry.verticesNeedUpdate = false;
380

M
Mr.doob 已提交
381
		}
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		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 已提交
398
		if ( geometry.colorsNeedUpdate === true ) {
399

M
Mr.doob 已提交
400
			var attribute = this.attributes.color;
401

M
Mr.doob 已提交
402
			if ( attribute !== undefined ) {
403

M
Mr.doob 已提交
404 405
				attribute.copyColorsArray( geometry.colors );
				attribute.needsUpdate = true;
406 407 408

			}

M
Mr.doob 已提交
409 410
			geometry.colorsNeedUpdate = false;

411
		}
412

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

			var attribute = this.attributes.lineDistance;

			if ( attribute !== undefined ) {

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

			}

			geometry.lineDistancesNeedUpdate = false;

		}

428 429 430 431 432 433 434 435 436
		if ( geometry.groupsNeedUpdate ) {

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

			geometry.groupsNeedUpdate = false;

		}

437
		return this;
438 439 440

	},

441
	fromGeometry: function ( geometry ) {
442

443
		geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
444 445

		return this.fromDirectGeometry( geometry.__directGeometry );
446

447
	},
448

449
	fromDirectGeometry: function ( geometry ) {
450

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

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

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

466 467 468 469 470 471 472 473
		}

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

475 476 477 478 479 480 481
		if ( geometry.uvs2.length > 0 ) {

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

		}

482 483
		if ( geometry.indices.length > 0 ) {

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

		}

490 491 492 493
		// groups

		this.groups = geometry.groups;

494 495
		// morphs

496
		for ( var name in geometry.morphTargets ) {
497

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

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

				var morphTarget = morphTargets[ i ];

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

507
				array.push( attribute.copyVector3sArray( morphTarget ) );
508 509 510

			}

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

		}

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

		}

		//

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

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

		}

		if ( geometry.boundingBox !== null ) {

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

		}
M
Mr.doob 已提交
544 545 546 547 548

		return this;

	},

549 550
	computeBoundingBox: function () {

551
		var vector = new THREE.Vector3();
552

553
		return function () {
554

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 ) {
564

565 566
				var bb = this.boundingBox;
				bb.makeEmpty();
567

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

570
					vector.fromArray( positions, i );
571
					bb.expandByPoint( vector );
572 573 574 575 576

				}

			}

577
			if ( positions === undefined || positions.length === 0 ) {
578

579 580
				this.boundingBox.min.set( 0, 0, 0 );
				this.boundingBox.max.set( 0, 0, 0 );
581

582
			}
583

584
			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
585

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

588
			}
589

G
Greg French 已提交
590
		};
591

592
	}(),
593 594 595

	computeBoundingSphere: function () {

596 597
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
598

599
		return function () {
600

601
			if ( this.boundingSphere === null ) {
602

603
				this.boundingSphere = new THREE.Sphere();
604

605
			}
606

607
			var positions = this.attributes.position.array;
608

609
			if ( positions ) {
610

611 612
				box.makeEmpty();

613
				var center = this.boundingSphere.center;
614

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

617
					vector.fromArray( positions, i );
J
Jan Wrobel 已提交
618
					box.expandByPoint( vector );
619 620 621 622 623

				}

				box.center( center );

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

627 628
				var maxRadiusSq = 0;

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

631
					vector.fromArray( positions, i );
632
					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
633

634 635 636 637
				}

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

638 639
				if ( isNaN( this.boundingSphere.radius ) ) {

640
					console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
641 642 643

				}

644
			}
645

G
Greg French 已提交
646
		};
647

648
	}(),
649

650 651 652 653 654 655
	computeFaceNormals: function () {

		// backwards compatibility

	},

656 657
	computeVertexNormals: function () {

658
		var index = this.index;
659
		var attributes = this.attributes;
660
		var groups = this.groups;
661

662
		if ( attributes.position ) {
663

664
			var positions = attributes.position.array;
665

666
			if ( attributes.normal === undefined ) {
667

668
				this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( positions.length ), 3 ) );
669 670 671 672 673

			} else {

				// reset existing normals to zero

674 675 676
				var normals = attributes.normal.array;

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

678
					normals[ i ] = 0;
679 680 681 682 683

				}

			}

684
			var normals = attributes.normal.array;
685

M
Mr.doob 已提交
686
			var vA, vB, vC,
687 688 689 690 691 692 693 694

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

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

695 696
			// indexed elements

697
			if ( index ) {
698

699
				var indices = index.array;
700

701
				if ( groups.length === 0 ) {
D
dubejf 已提交
702

703
					this.addGroup( 0, indices.length );
704

D
dubejf 已提交
705 706
				}

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

709
					var group = groups[ j ];
710

711 712
					var start = group.start;
					var count = group.count;
713

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

716 717 718
						vA = indices[ i + 0 ] * 3;
						vB = indices[ i + 1 ] * 3;
						vC = indices[ i + 2 ] * 3;
719

M
Mr.doob 已提交
720 721 722
						pA.fromArray( positions, vA );
						pB.fromArray( positions, vB );
						pC.fromArray( positions, vC );
723

724 725 726
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
727

G
gero3 已提交
728
						normals[ vA ] += cb.x;
M
Mr.doob 已提交
729 730
						normals[ vA + 1 ] += cb.y;
						normals[ vA + 2 ] += cb.z;
731

G
gero3 已提交
732
						normals[ vB ] += cb.x;
M
Mr.doob 已提交
733 734
						normals[ vB + 1 ] += cb.y;
						normals[ vB + 2 ] += cb.z;
735

G
gero3 已提交
736
						normals[ vC ] += cb.x;
M
Mr.doob 已提交
737 738
						normals[ vC + 1 ] += cb.y;
						normals[ vC + 2 ] += cb.z;
739 740 741 742 743 744

					}

				}

			} else {
745

746 747 748
				// non-indexed elements (unconnected triangle soup)

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

M
Mr.doob 已提交
750 751 752
					pA.fromArray( positions, i );
					pB.fromArray( positions, i + 3 );
					pC.fromArray( positions, i + 6 );
753

754 755 756
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
757

G
gero3 已提交
758
					normals[ i ] = cb.x;
759 760
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
761

762 763 764
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
765

766 767 768
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
769 770 771 772 773

				}

			}

774
			this.normalizeNormals();
775

776
			attributes.normal.needsUpdate = true;
777

778
		}
779

780
	},
781

782 783
	computeTangents: function () {

M
Mr.doob 已提交
784
		console.warn( 'THREE.BufferGeometry: .computeTangents() has been removed.' );
785

786 787
	},

M
Mr.doob 已提交
788
	computeOffsets: function ( size ) {
789

790
		console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.')
791 792 793

	},

794 795 796 797
	merge: function ( geometry, offset ) {

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

798
			console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
799 800 801 802 803 804 805 806 807 808 809
			return;

		}

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

		var attributes = this.attributes;

		for ( var key in attributes ) {

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

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
			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;
828 829 830 831 832

	},

	normalizeNormals: function () {

833
		var normals = this.attributes.normal.array;
834 835 836 837 838 839 840 841 842 843 844

		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 已提交
845
			normals[ i ] *= n;
846 847 848 849 850 851 852
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;

		}

	},

853
	toJSON: function () {
854

855 856 857 858 859 860
		var data = {
			metadata: {
				version: 4.4,
				type: 'BufferGeometry',
				generator: 'BufferGeometry.toJSON'
			}
B
brason 已提交
861
		};
862

863
		// standard BufferGeometry serialization
864

865
		data.uuid = this.uuid;
M
Mr.doob 已提交
866
		data.type = this.type;
867
		if ( this.name !== '' ) data.name = this.name;
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

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

M
Mr.doob 已提交
885
		var index = this.index;
886

M
Mr.doob 已提交
887 888 889 890 891 892 893 894 895 896 897 898
		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;
899 900 901 902 903

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

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

906
			data.data.attributes[ key ] = {
907 908 909
				itemSize: attribute.itemSize,
				type: attribute.array.constructor.name,
				array: array
910
			};
911 912 913

		}

M
Mr.doob 已提交
914 915
		var groups = this.groups;

916
		if ( groups.length > 0 ) {
917

918
			data.data.groups = JSON.parse( JSON.stringify( groups ) );
919 920 921

		}

M
Mr.doob 已提交
922 923
		var boundingSphere = this.boundingSphere;

924 925
		if ( boundingSphere !== null ) {

926
			data.data.boundingSphere = {
927 928
				center: boundingSphere.center.toArray(),
				radius: boundingSphere.radius
929
			};
930 931 932

		}

933
		return data;
934 935 936

	},

M
Mr.doob 已提交
937
	clone: function () {
O
ohmed 已提交
938

939
		return new this.constructor().copy( this );
O
ohmed 已提交
940

D
dubejf 已提交
941 942 943
	},

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

945 946 947 948 949 950 951 952
		var index = source.index;

		if ( index !== null ) {

			this.addIndex( index.clone() );

		}

M
Mr.doob 已提交
953
		var attributes = source.attributes;
O
ohmed 已提交
954

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

M
Mr.doob 已提交
957 958
			var attribute = attributes[ name ];
			this.addAttribute( name, attribute.clone() );
O
ohmed 已提交
959

M
Mr.doob 已提交
960
		}
O
ohmed 已提交
961

962 963
		var groups = source.groups;

964 965 966
		for ( var i = 0, l = groups.length; i < l; i ++ ) {

			var group = groups[ i ];
M
Mr.doob 已提交
967
			this.addGroup( group.start, group.count );
O
ohmed 已提交
968

O
ohmed 已提交
969
		}
O
ohmed 已提交
970

971
		return this;
O
ohmed 已提交
972 973 974

	},

975
	dispose: function () {
976

977
		this.dispatchEvent( { type: 'dispose' } );
978

979
	}
980 981

};
M
Mr.doob 已提交
982 983

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

985
THREE.BufferGeometry.MaxIndex = 65535;