BufferGeometry.js 18.1 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 70 71
	},

	getAttribute: function ( name ) {

72 73
		return this.attributes[ name ];

74 75
	},

76 77 78 79 80 81
	removeAttribute: function ( name ) {

		delete this.attributes[ name ];

	},

82 83 84
	addGroup: function ( start, count, materialIndex ) {

		this.groups.push( {
M
Mr.doob 已提交
85 86

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

		} );

	},

94
	clearGroups: function () {
D
dubejf 已提交
95

96
		this.groups = [];
D
dubejf 已提交
97 98 99

	},

100 101 102 103 104 105 106
	setDrawRange: function ( start, count ) {

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

	},

107 108
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
109
		var position = this.attributes.position;
110

M
Mr.doob 已提交
111
		if ( position !== undefined ) {
112

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

		}

M
Mr.doob 已提交
118
		var normal = this.attributes.normal;
119

M
Mr.doob 已提交
120
		if ( normal !== undefined ) {
121

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

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

		}

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

			this.computeBoundingBox();

		}

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

			this.computeBoundingSphere();

		}

141 142
	},

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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 );

		};

	}(),

261 262
	center: function () {

M
Mr.doob 已提交
263 264 265 266
		this.computeBoundingBox();

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

267
		this.translate( offset.x, offset.y, offset.z );
M
Mr.doob 已提交
268 269

		return offset;
270 271 272

	},

273
	setFromObject: function ( object ) {
274

275
		// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
276

277 278
		var geometry = object.geometry;

279
		if ( object instanceof THREE.Points || object instanceof THREE.Line ) {
280

281 282
			var positions = new THREE.Float32Attribute( geometry.vertices.length * 3, 3 );
			var colors = new THREE.Float32Attribute( geometry.colors.length * 3, 3 );
283

284 285
			this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
			this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
286

C
Chen Pang 已提交
287 288 289 290
			if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {

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

291
				this.addAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
C
Chen Pang 已提交
292 293 294

			}

295 296 297 298 299 300 301 302 303 304 305
			if ( geometry.boundingSphere !== null ) {

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

			}

			if ( geometry.boundingBox !== null ) {

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

			}
306 307 308

		} else if ( object instanceof THREE.Mesh ) {

M
Mr.doob 已提交
309
			if ( geometry instanceof THREE.Geometry ) {
310 311

				this.fromGeometry( geometry );
312

M
Mr.doob 已提交
313
			}
314 315 316 317 318 319 320

		}

		return this;

	},

321 322
	updateFromObject: function ( object ) {

M
Mr.doob 已提交
323
		var geometry = object.geometry;
324

M
Mr.doob 已提交
325
		if ( object instanceof THREE.Mesh ) {
326

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

329 330 331 332 333 334
			if ( direct === undefined ) {

				return this.fromGeometry( geometry );

			}

M
Mr.doob 已提交
335 336 337 338
			direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
			direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
			direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
			direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
339
			direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
M
Mr.doob 已提交
340 341 342 343 344

			geometry.verticesNeedUpdate = false;
			geometry.normalsNeedUpdate = false;
			geometry.colorsNeedUpdate = false;
			geometry.uvsNeedUpdate = false;
345
			geometry.groupsNeedUpdate = false;
346

M
Mr.doob 已提交
347
			geometry = direct;
348 349

		}
350

M
Mr.doob 已提交
351
		if ( geometry.verticesNeedUpdate === true ) {
352

M
Mr.doob 已提交
353
			var attribute = this.attributes.position;
354

M
Mr.doob 已提交
355
			if ( attribute !== undefined ) {
356

M
Mr.doob 已提交
357 358
				attribute.copyVector3sArray( geometry.vertices );
				attribute.needsUpdate = true;
359 360 361

			}

M
Mr.doob 已提交
362
			geometry.verticesNeedUpdate = false;
363

M
Mr.doob 已提交
364
		}
365

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

M
Mr.doob 已提交
383
			var attribute = this.attributes.color;
384

M
Mr.doob 已提交
385
			if ( attribute !== undefined ) {
386

M
Mr.doob 已提交
387 388
				attribute.copyColorsArray( geometry.colors );
				attribute.needsUpdate = true;
389 390 391

			}

M
Mr.doob 已提交
392 393
			geometry.colorsNeedUpdate = false;

394
		}
395

396 397
		if ( geometry.uvsNeedUpdate ) {

M
Mr.doob 已提交
398
			var attribute = this.attributes.uv;
399

M
Mr.doob 已提交
400
			if ( attribute !== undefined ) {
401

M
Mr.doob 已提交
402 403
				attribute.copyVector2sArray( geometry.uvs );
				attribute.needsUpdate = true;
404

M
Mr.doob 已提交
405
			}
406

M
Mr.doob 已提交
407
			geometry.uvsNeedUpdate = false;
408 409 410

		}

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

			var attribute = this.attributes.lineDistance;

			if ( attribute !== undefined ) {

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

			}

			geometry.lineDistancesNeedUpdate = false;

		}

426 427 428 429 430 431 432 433 434
		if ( geometry.groupsNeedUpdate ) {

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

			geometry.groupsNeedUpdate = false;

		}

435
		return this;
436 437 438

	},

439
	fromGeometry: function ( geometry ) {
440

441
		geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );
442 443

		return this.fromDirectGeometry( geometry.__directGeometry );
444

445
	},
446

447
	fromDirectGeometry: function ( geometry ) {
448

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

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

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

464 465 466 467 468 469 470 471
		}

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

473 474 475 476 477 478 479
		if ( geometry.uvs2.length > 0 ) {

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

		}

480 481
		if ( geometry.indices.length > 0 ) {

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

		}

488 489 490 491
		// groups

		this.groups = geometry.groups;

492 493
		// morphs

494
		for ( var name in geometry.morphTargets ) {
495

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

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

				var morphTarget = morphTargets[ i ];

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

505
				array.push( attribute.copyVector3sArray( morphTarget ) );
506 507 508

			}

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

		}

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

		}

		//

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

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

		}

		if ( geometry.boundingBox !== null ) {

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

		}
M
Mr.doob 已提交
542 543 544 545 546

		return this;

	},

547 548
	computeBoundingBox: function () {

549
		var vector = new THREE.Vector3();
550

551
		return function () {
552

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

M
Mr.doob 已提交
563
				this.boundingBox.setFromArray( positions );
564

565 566
			}

567
			if ( positions === undefined || positions.length === 0 ) {
568

569 570
				this.boundingBox.min.set( 0, 0, 0 );
				this.boundingBox.max.set( 0, 0, 0 );
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

G
Greg French 已提交
580
		};
581

582
	}(),
583 584 585

	computeBoundingSphere: function () {

586 587
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
588

589
		return function () {
590

591
			if ( this.boundingSphere === null ) {
592

593
				this.boundingSphere = new THREE.Sphere();
594

595
			}
596

597
			var positions = this.attributes.position.array;
598

599
			if ( positions ) {
600

601 602
				box.makeEmpty();

603
				var center = this.boundingSphere.center;
604

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

607
					vector.fromArray( positions, i );
J
Jan Wrobel 已提交
608
					box.expandByPoint( vector );
609 610 611 612 613

				}

				box.center( center );

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

617 618
				var maxRadiusSq = 0;

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

621
					vector.fromArray( positions, i );
622
					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
623

624 625 626 627
				}

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

628 629
				if ( isNaN( this.boundingSphere.radius ) ) {

630
					console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
631 632 633

				}

634
			}
635

G
Greg French 已提交
636
		};
637

638
	}(),
639

640 641 642 643 644 645
	computeFaceNormals: function () {

		// backwards compatibility

	},

646 647
	computeVertexNormals: function () {

648
		var index = this.index;
649
		var attributes = this.attributes;
650
		var groups = this.groups;
651

652
		if ( attributes.position ) {
653

654
			var positions = attributes.position.array;
655

656
			if ( attributes.normal === undefined ) {
657

658
				this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( positions.length ), 3 ) );
659 660 661 662 663

			} else {

				// reset existing normals to zero

M
Mr.doob 已提交
664
				var array = attributes.normal.array;
665

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

M
Mr.doob 已提交
668
					array[ i ] = 0;
669 670 671 672 673

				}

			}

674
			var normals = attributes.normal.array;
675

M
Mr.doob 已提交
676
			var vA, vB, vC,
677 678 679 680 681 682 683 684

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

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

685 686
			// indexed elements

687
			if ( index ) {
688

689
				var indices = index.array;
690

691
				if ( groups.length === 0 ) {
D
dubejf 已提交
692

693
					this.addGroup( 0, indices.length );
694

D
dubejf 已提交
695 696
				}

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

699
					var group = groups[ j ];
700

701 702
					var start = group.start;
					var count = group.count;
703

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

706 707 708
						vA = indices[ i + 0 ] * 3;
						vB = indices[ i + 1 ] * 3;
						vC = indices[ i + 2 ] * 3;
709

M
Mr.doob 已提交
710 711 712
						pA.fromArray( positions, vA );
						pB.fromArray( positions, vB );
						pC.fromArray( positions, vC );
713

714 715 716
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
717

G
gero3 已提交
718
						normals[ vA ] += cb.x;
M
Mr.doob 已提交
719 720
						normals[ vA + 1 ] += cb.y;
						normals[ vA + 2 ] += cb.z;
721

G
gero3 已提交
722
						normals[ vB ] += cb.x;
M
Mr.doob 已提交
723 724
						normals[ vB + 1 ] += cb.y;
						normals[ vB + 2 ] += cb.z;
725

G
gero3 已提交
726
						normals[ vC ] += cb.x;
M
Mr.doob 已提交
727 728
						normals[ vC + 1 ] += cb.y;
						normals[ vC + 2 ] += cb.z;
729 730 731 732 733 734

					}

				}

			} else {
735

736 737 738
				// non-indexed elements (unconnected triangle soup)

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

M
Mr.doob 已提交
740 741 742
					pA.fromArray( positions, i );
					pB.fromArray( positions, i + 3 );
					pC.fromArray( positions, i + 6 );
743

744 745 746
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
747

G
gero3 已提交
748
					normals[ i ] = cb.x;
749 750
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
751

752 753 754
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
755

756 757 758
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
759 760 761 762 763

				}

			}

764
			this.normalizeNormals();
765

766
			attributes.normal.needsUpdate = true;
767

768
		}
769

770
	},
771

772 773 774 775
	merge: function ( geometry, offset ) {

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

776
			console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
777 778 779 780 781 782 783 784 785 786 787
			return;

		}

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

		var attributes = this.attributes;

		for ( var key in attributes ) {

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

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
			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;
806 807 808 809 810

	},

	normalizeNormals: function () {

811
		var normals = this.attributes.normal.array;
812 813 814 815 816 817 818 819 820 821 822

		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 已提交
823
			normals[ i ] *= n;
824 825 826 827 828 829 830
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;

		}

	},

831
	toJSON: function () {
832

833 834 835 836 837 838
		var data = {
			metadata: {
				version: 4.4,
				type: 'BufferGeometry',
				generator: 'BufferGeometry.toJSON'
			}
B
brason 已提交
839
		};
840

841
		// standard BufferGeometry serialization
842

843
		data.uuid = this.uuid;
M
Mr.doob 已提交
844
		data.type = this.type;
845
		if ( this.name !== '' ) data.name = this.name;
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

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

M
Mr.doob 已提交
863
		var index = this.index;
864

M
Mr.doob 已提交
865 866 867 868 869 870 871 872 873 874 875 876
		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;
877 878 879 880 881

		for ( var key in attributes ) {

			var attribute = attributes[ key ];

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

884
			data.data.attributes[ key ] = {
885 886 887
				itemSize: attribute.itemSize,
				type: attribute.array.constructor.name,
				array: array
888
			};
889 890 891

		}

M
Mr.doob 已提交
892 893
		var groups = this.groups;

894
		if ( groups.length > 0 ) {
895

896
			data.data.groups = JSON.parse( JSON.stringify( groups ) );
897 898 899

		}

M
Mr.doob 已提交
900 901
		var boundingSphere = this.boundingSphere;

902 903
		if ( boundingSphere !== null ) {

904
			data.data.boundingSphere = {
905 906
				center: boundingSphere.center.toArray(),
				radius: boundingSphere.radius
907
			};
908 909 910

		}

911
		return data;
912 913 914

	},

M
Mr.doob 已提交
915
	clone: function () {
O
ohmed 已提交
916

917
		/*
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
		// 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;

		}

938
		return new this.constructor().copy( this );
939 940 941
		*/

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

D
dubejf 已提交
943 944 945
	},

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

947 948 949 950
		var index = source.index;

		if ( index !== null ) {

951
			this.setIndex( index.clone() );
952 953 954

		}

M
Mr.doob 已提交
955
		var attributes = source.attributes;
O
ohmed 已提交
956

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

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

M
Mr.doob 已提交
962
		}
O
ohmed 已提交
963

964 965
		var groups = source.groups;

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

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

O
ohmed 已提交
971
		}
O
ohmed 已提交
972

973
		return this;
O
ohmed 已提交
974 975 976

	},

977
	dispose: function () {
978

979
		this.dispatchEvent( { type: 'dispose' } );
980

981
	}
982 983

};
M
Mr.doob 已提交
984 985

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

987
THREE.BufferGeometry.MaxIndex = 65535;