BufferGeometry.js 17.2 KB
Newer Older
1 2 3 4 5 6
/**
 * @author alteredq / http://alteredqualia.com/
 */

THREE.BufferGeometry = function () {

7 8
	this.id = THREE.GeometryIdCount ++;
	this.uuid = THREE.Math.generateUUID();
9

M
Mr.doob 已提交
10 11
	this.name = '';

12
	this.attributes = {};
13 14
	this.drawcalls = [];
	this.offsets = this.drawcalls; // backwards compatibility
15

16 17 18 19 20 21 22
	this.boundingBox = null;
	this.boundingSphere = null;

};

THREE.BufferGeometry.prototype = {

23
	constructor: THREE.BufferGeometry,
24

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

27 28 29 30 31 32 33 34 35 36
		if ( attribute instanceof THREE.BufferAttribute === false ) {

			console.warn( 'DEPRECATED: BufferGeometry\'s addAttribute() now expects ( name, attribute ).' );

			this.attributes[ name ] = { array: arguments[ 1 ], itemSize: arguments[ 2 ] };

			return;

		}

M
Mr.doob 已提交
37
		this.attributes[ name ] = attribute;
38

M
Mr.doob 已提交
39 40 41 42
	},

	getAttribute: function ( name ) {

43 44
		return this.attributes[ name ];

45 46
	},

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

49
		this.drawcalls.push( {
M
Mr.doob 已提交
50 51 52

			start: start,
			count: count,
M
Mr.doob 已提交
53
			index: indexOffset !== undefined ? indexOffset : 0
M
Mr.doob 已提交
54 55 56 57 58

		} );

	},

59 60
	applyMatrix: function ( matrix ) {

M
Mr.doob 已提交
61
		var position = this.attributes.position;
62

M
Mr.doob 已提交
63
		if ( position !== undefined ) {
64

65
			matrix.applyToVector3Array( position.array );
M
Mr.doob 已提交
66
			position.needsUpdate = true;
67 68 69

		}

M
Mr.doob 已提交
70
		var normal = this.attributes.normal;
71

M
Mr.doob 已提交
72
		if ( normal !== undefined ) {
73

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

76
			normalMatrix.applyToVector3Array( normal.array );
M
Mr.doob 已提交
77
			normal.needsUpdate = true;
78 79 80 81 82

		}

	},

83 84
	computeBoundingBox: function () {

85
		if ( this.boundingBox === null ) {
86

B
Ben Houston 已提交
87
			this.boundingBox = new THREE.Box3();
88 89 90

		}

91
		var positions = this.attributes[ "position" ].array;
92

93
		if ( positions ) {
94 95 96

			var bb = this.boundingBox;

97 98 99 100 101 102 103
			if( positions.length >= 3 ) {
				bb.min.x = bb.max.x = positions[ 0 ];
				bb.min.y = bb.max.y = positions[ 1 ];
				bb.min.z = bb.max.z = positions[ 2 ];
			}

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

M
Mr.doob 已提交
105 106 107
				var x = positions[ i ];
				var y = positions[ i + 1 ];
				var z = positions[ i + 2 ];
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

				// bounding box

				if ( x < bb.min.x ) {

					bb.min.x = x;

				} else if ( x > bb.max.x ) {

					bb.max.x = x;

				}

				if ( y < bb.min.y ) {

					bb.min.y = y;

				} else if ( y > bb.max.y ) {

					bb.max.y = y;

				}

				if ( z < bb.min.z ) {

					bb.min.z = z;

				} else if ( z > bb.max.z ) {

					bb.max.z = z;

				}

			}

		}

145
		if ( positions === undefined || positions.length === 0 ) {
146 147 148 149 150 151 152

			this.boundingBox.min.set( 0, 0, 0 );
			this.boundingBox.max.set( 0, 0, 0 );

		}

	},
153 154 155

	computeBoundingSphere: function () {

156 157
		var box = new THREE.Box3();
		var vector = new THREE.Vector3();
158

159
		return function () {
160

161
			if ( this.boundingSphere === null ) {
162

163
				this.boundingSphere = new THREE.Sphere();
164

165
			}
166

167
			var positions = this.attributes[ "position" ].array;
168

169
			if ( positions ) {
170

171 172
				box.makeEmpty();

173
				var center = this.boundingSphere.center;
174

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

177 178 179 180 181 182 183
					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
					box.addPoint( vector );

				}

				box.center( center );

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

187 188 189 190 191 192
				var maxRadiusSq = 0;

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

					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
193

194 195 196 197 198
				}

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

			}
199 200 201

		}

202
	}(),
203

204 205 206 207 208 209
	computeFaceNormals: function () {

		// backwards compatibility

	},

210 211
	computeVertexNormals: function () {

212
		if ( this.attributes[ "position" ] ) {
213 214 215 216

			var i, il;
			var j, jl;

217
			var nVertexElements = this.attributes[ "position" ].array.length;
218 219

			if ( this.attributes[ "normal" ] === undefined ) {
220

221
				this.attributes[ "normal" ] = {
222

223
					itemSize: 3,
224
					array: new Float32Array( nVertexElements )
225 226

				};
227 228 229 230 231

			} else {

				// reset existing normals to zero

232
				for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
233

234
					this.attributes[ "normal" ].array[ i ] = 0;
235 236 237 238 239

				}

			}

240 241
			var positions = this.attributes[ "position" ].array;
			var normals = this.attributes[ "normal" ].array;
242 243 244 245 246 247 248 249 250 251

			var vA, vB, vC, x, y, z,

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

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

252 253 254 255 256 257
			// indexed elements

			if ( this.attributes[ "index" ] ) {

				var indices = this.attributes[ "index" ].array;

258
				var offsets = (this.offsets.length > 0 ? this.offsets : [ { start: 0, count: indices.length, index: 0 } ]);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

				for ( j = 0, jl = offsets.length; j < jl; ++ j ) {

					var start = offsets[ j ].start;
					var count = offsets[ j ].count;
					var index = offsets[ j ].index;

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

						vA = index + indices[ i ];
						vB = index + indices[ i + 1 ];
						vC = index + indices[ i + 2 ];

						x = positions[ vA * 3 ];
						y = positions[ vA * 3 + 1 ];
						z = positions[ vA * 3 + 2 ];
						pA.set( x, y, z );

						x = positions[ vB * 3 ];
						y = positions[ vB * 3 + 1 ];
						z = positions[ vB * 3 + 2 ];
						pB.set( x, y, z );
281

282 283 284 285
						x = positions[ vC * 3 ];
						y = positions[ vC * 3 + 1 ];
						z = positions[ vC * 3 + 2 ];
						pC.set( x, y, z );
286

287 288 289
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
290

M
Mr.doob 已提交
291
						normals[ vA * 3     ] += cb.x;
292 293 294
						normals[ vA * 3 + 1 ] += cb.y;
						normals[ vA * 3 + 2 ] += cb.z;

M
Mr.doob 已提交
295
						normals[ vB * 3     ] += cb.x;
296 297 298
						normals[ vB * 3 + 1 ] += cb.y;
						normals[ vB * 3 + 2 ] += cb.z;

M
Mr.doob 已提交
299
						normals[ vC * 3     ] += cb.x;
300 301 302 303 304 305 306 307 308 309
						normals[ vC * 3 + 1 ] += cb.y;
						normals[ vC * 3 + 2 ] += cb.z;

					}

				}

			// non-indexed elements (unconnected triangle soup)

			} else {
310

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

313 314 315
					x = positions[ i ];
					y = positions[ i + 1 ];
					z = positions[ i + 2 ];
316 317
					pA.set( x, y, z );

318 319 320
					x = positions[ i + 3 ];
					y = positions[ i + 4 ];
					z = positions[ i + 5 ];
321 322
					pB.set( x, y, z );

323 324 325
					x = positions[ i + 6 ];
					y = positions[ i + 7 ];
					z = positions[ i + 8 ];
326 327
					pC.set( x, y, z );

328 329 330
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
331

M
Mr.doob 已提交
332
					normals[ i     ] = cb.x;
333 334
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
335

336 337 338
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
339

340 341 342
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
343 344 345 346 347

				}

			}

348
			this.normalizeNormals();
349

350
			this.normalsNeedUpdate = true;
351

352
		}
353

354
	},
355

356
	normalizeNormals: function () {
357

358
		var normals = this.attributes[ "normal" ].array;
359

360 361 362 363 364 365 366 367 368 369
		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 );

M
Mr.doob 已提交
370
			normals[ i     ] *= n;
371 372
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;
373 374 375

		}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	},

	computeTangents: function () {

		// based on http://www.terathon.com/code/tangent.html
		// (per vertex tangents)

		if ( this.attributes[ "index" ] === undefined ||
			 this.attributes[ "position" ] === undefined ||
			 this.attributes[ "normal" ] === undefined ||
			 this.attributes[ "uv" ] === undefined ) {

			console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
			return;

		}

		var indices = this.attributes[ "index" ].array;
		var positions = this.attributes[ "position" ].array;
		var normals = this.attributes[ "normal" ].array;
		var uvs = this.attributes[ "uv" ].array;

		var nVertices = positions.length / 3;

		if ( this.attributes[ "tangent" ] === undefined ) {

			var nTangentElements = 4 * nVertices;

			this.attributes[ "tangent" ] = {

				itemSize: 4,
407
				array: new Float32Array( nTangentElements )
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

			};

		}

		var tangents = this.attributes[ "tangent" ].array;

		var tan1 = [], tan2 = [];

		for ( var k = 0; k < nVertices; k ++ ) {

			tan1[ k ] = new THREE.Vector3();
			tan2[ k ] = new THREE.Vector3();

		}

		var xA, yA, zA,
			xB, yB, zB,
			xC, yC, zC,

			uA, vA,
			uB, vB,
			uC, vC,

			x1, x2, y1, y2, z1, z2,
			s1, s2, t1, t2, r;

		var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();

		function handleTriangle( a, b, c ) {

			xA = positions[ a * 3 ];
			yA = positions[ a * 3 + 1 ];
			zA = positions[ a * 3 + 2 ];

			xB = positions[ b * 3 ];
			yB = positions[ b * 3 + 1 ];
			zB = positions[ b * 3 + 2 ];

			xC = positions[ c * 3 ];
			yC = positions[ c * 3 + 1 ];
			zC = positions[ c * 3 + 2 ];

			uA = uvs[ a * 2 ];
			vA = uvs[ a * 2 + 1 ];

			uB = uvs[ b * 2 ];
			vB = uvs[ b * 2 + 1 ];

			uC = uvs[ c * 2 ];
			vC = uvs[ c * 2 + 1 ];

			x1 = xB - xA;
			x2 = xC - xA;

			y1 = yB - yA;
			y2 = yC - yA;

			z1 = zB - zA;
			z2 = zC - zA;

			s1 = uB - uA;
			s2 = uC - uA;

			t1 = vB - vA;
			t2 = vC - vA;

			r = 1.0 / ( s1 * t2 - s2 * t1 );

477 478 479 480 481 482 483 484 485 486 487
			sdir.set(
				( t2 * x1 - t1 * x2 ) * r,
				( t2 * y1 - t1 * y2 ) * r,
				( t2 * z1 - t1 * z2 ) * r
			);

			tdir.set(
				( s1 * x2 - s2 * x1 ) * r,
				( s1 * y2 - s2 * y1 ) * r,
				( s1 * z2 - s2 * z1 ) * r
			);
488

489 490 491
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
492

493 494 495
			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

		}

		var i, il;
		var j, jl;
		var iA, iB, iC;

		var offsets = this.offsets;

		for ( j = 0, jl = offsets.length; j < jl; ++ j ) {

			var start = offsets[ j ].start;
			var count = offsets[ j ].count;
			var index = offsets[ j ].index;

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

				iA = index + indices[ i ];
				iB = index + indices[ i + 1 ];
				iC = index + indices[ i + 2 ];

				handleTriangle( iA, iB, iC );

			}

		}

		var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
		var n = new THREE.Vector3(), n2 = new THREE.Vector3();
		var w, t, test;

		function handleVertex( v ) {

			n.x = normals[ v * 3 ];
			n.y = normals[ v * 3 + 1 ];
			n.z = normals[ v * 3 + 2 ];

			n2.copy( n );

			t = tan1[ v ];

			// Gram-Schmidt orthogonalize

			tmp.copy( t );
540
			tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
541 542 543

			// Calculate handedness

544
			tmp2.crossVectors( n2, t );
545 546 547
			test = tmp2.dot( tan2[ v ] );
			w = ( test < 0.0 ) ? -1.0 : 1.0;

M
Mr.doob 已提交
548
			tangents[ v * 4     ] = tmp.x;
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			tangents[ v * 4 + 1 ] = tmp.y;
			tangents[ v * 4 + 2 ] = tmp.z;
			tangents[ v * 4 + 3 ] = w;

		}

		for ( j = 0, jl = offsets.length; j < jl; ++ j ) {

			var start = offsets[ j ].start;
			var count = offsets[ j ].count;
			var index = offsets[ j ].index;

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

				iA = index + indices[ i ];
				iB = index + indices[ i + 1 ];
				iC = index + indices[ i + 2 ];

				handleVertex( iA );
				handleVertex( iB );
				handleVertex( iC );

			}

		}

575 576
	},

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	/*
		computeOffsets
		Compute the draw offset for large models by chunking the index buffer into chunks of 65k addressable vertices.
		This method will effectively rewrite the index buffer and remap all attributes to match the new indices.
		WARNING: This method will also expand the vertex count to prevent sprawled triangles across draw offsets.
		indexBufferSize - Defaults to 65535, but allows for larger or smaller chunks.
	*/
	computeOffsets: function(indexBufferSize) {

		var size = indexBufferSize;
		if(indexBufferSize === undefined)
			size = 65535; //WebGL limits type of index buffer values to 16-bit.

		var s = Date.now();

		var indices = this.attributes['index'].array;
		var vertices = this.attributes['position'].array;

		var verticesCount = (vertices.length/3);
		var facesCount = (indices.length/3);

		/*
		console.log("Computing buffers in offsets of "+size+" -> indices:"+indices.length+" vertices:"+vertices.length);
		console.log("Faces to process: "+(indices.length/3));
		console.log("Reordering "+verticesCount+" vertices.");
		*/

		var sortedIndices = new Uint16Array( indices.length ); //16-bit buffers
		var indexPtr = 0;
		var vertexPtr = 0;

		var offsets = [ { start:0, count:0, index:0 } ];
		var offset = offsets[0];

		var duplicatedVertices = 0;
		var newVerticeMaps = 0;
		var faceVertices = new Int32Array(6);
		var vertexMap = new Int32Array( vertices.length );
		var revVertexMap = new Int32Array( vertices.length );
		for(var j = 0; j < vertices.length; j++) { vertexMap[j] = -1; revVertexMap[j] = -1; }

		/*
			Traverse every face and reorder vertices in the proper offsets of 65k.
			We can have more than 65k entries in the index buffer per offset, but only reference 65k values.
		*/
		for(var findex = 0; findex < facesCount; findex++) {
			newVerticeMaps = 0;

			for(var vo = 0; vo < 3; vo++) {
				var vid = indices[ findex*3 + vo ];
				if(vertexMap[vid] == -1) {
					//Unmapped vertice
					faceVertices[vo*2] = vid;
					faceVertices[vo*2+1] = -1;
					newVerticeMaps++;
				} else if(vertexMap[vid] < offset.index) {
					//Reused vertices from previous block (duplicate)
					faceVertices[vo*2] = vid;
					faceVertices[vo*2+1] = -1;
					duplicatedVertices++;
				} else {
					//Reused vertice in the current block
					faceVertices[vo*2] = vid;
					faceVertices[vo*2+1] = vertexMap[vid];
				}
			}

			var faceMax = vertexPtr + newVerticeMaps;
			if(faceMax > (offset.index + size)) {
				var new_offset = { start:indexPtr, count:0, index:vertexPtr };
				offsets.push(new_offset);
				offset = new_offset;

				//Re-evaluate reused vertices in light of new offset.
				for(var v = 0; v < 6; v+=2) {
					var new_vid = faceVertices[v+1];
					if(new_vid > -1 && new_vid < offset.index)
						faceVertices[v+1] = -1;
				}
			}

			//Reindex the face.
			for(var v = 0; v < 6; v+=2) {
				var vid = faceVertices[v];
				var new_vid = faceVertices[v+1];

				if(new_vid === -1)
					new_vid = vertexPtr++;

				vertexMap[vid] = new_vid;
				revVertexMap[new_vid] = vid;
				sortedIndices[indexPtr++] = new_vid - offset.index; //XXX overflows at 16bit
				offset.count++;
			}
		}

		/* Move all attribute values to map to the new computed indices , also expand the vertice stack to match our new vertexPtr. */
		this.reorderBuffers(sortedIndices, revVertexMap, vertexPtr);
		this.offsets = offsets;

		/*
		var orderTime = Date.now();
		console.log("Reorder time: "+(orderTime-s)+"ms");
		console.log("Duplicated "+duplicatedVertices+" vertices.");
		console.log("Compute Buffers time: "+(Date.now()-s)+"ms");
		console.log("Draw offsets: "+offsets.length);
		*/

		return offsets;
	},

	/*
		reoderBuffers:
		Reorder attributes based on a new indexBuffer and indexMap.
		indexBuffer - Uint16Array of the new ordered indices.
		indexMap - Int32Array where the position is the new vertex ID and the value the old vertex ID for each vertex.
		vertexCount - Amount of total vertices considered in this reordering (in case you want to grow the vertice stack).
	*/
	reorderBuffers: function(indexBuffer, indexMap, vertexCount) {

		/* Create a copy of all attributes for reordering. */
		var sortedAttributes = {};
		var types = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ];
		for( var attr in this.attributes ) {
			if(attr == 'index')
				continue;
			var sourceArray = this.attributes[attr].array;
			for ( var i = 0, il = types.length; i < il; i++ ) {
				var type = types[i];
				if (sourceArray instanceof type) {
					sortedAttributes[attr] = new type( this.attributes[attr].itemSize * vertexCount );
					break;
				}
			}
		}

		/* Move attribute positions based on the new index map */
		for(var new_vid = 0; new_vid < vertexCount; new_vid++) {
			var vid = indexMap[new_vid];
			for ( var attr in this.attributes ) {
				if(attr == 'index')
					continue;
				var attrArray = this.attributes[attr].array;
				var attrSize = this.attributes[attr].itemSize;
				var sortedAttr = sortedAttributes[attr];
				for(var k = 0; k < attrSize; k++)
					sortedAttr[ new_vid * attrSize + k ] = attrArray[ vid * attrSize + k ];
			}
		}

		/* Carry the new sorted buffers locally */
		this.attributes['index'].array = indexBuffer;
		for ( var attr in this.attributes ) {
			if(attr == 'index')
				continue;
			this.attributes[attr].array = sortedAttributes[attr];
			this.attributes[attr].numItems = this.attributes[attr].itemSize * vertexCount;
		}
	},

M
Mr.doob 已提交
737
	clone: function () {
O
ohmed 已提交
738 739 740

		var geometry = new THREE.BufferGeometry();

O
ohmed 已提交
741 742
		var types = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ];

O
ohmed 已提交
743 744
		for ( var attr in this.attributes ) {

M
Mr.doob 已提交
745 746
			var sourceAttr = this.attributes[ attr ];
			var sourceArray = sourceAttr.array;
O
ohmed 已提交
747

M
Mr.doob 已提交
748
			var attribute = {
O
ohmed 已提交
749 750

				itemSize: sourceAttr.itemSize,
O
ohmed 已提交
751
				array: null
O
ohmed 已提交
752

O
ohmed 已提交
753 754
			};

O
ohmed 已提交
755
			for ( var i = 0, il = types.length; i < il; i ++ ) {
O
ohmed 已提交
756

O
ohmed 已提交
757
				var type = types[ i ];
O
ohmed 已提交
758

O
ohmed 已提交
759
				if ( sourceArray instanceof type ) {
O
ohmed 已提交
760

M
Mr.doob 已提交
761
					attribute.array = new type( sourceArray );
O
ohmed 已提交
762
					break;
O
ohmed 已提交
763

O
ohmed 已提交
764
				}
O
ohmed 已提交
765 766 767 768 769 770 771

			}

			geometry.attributes[ attr ] = attribute;

		}

O
ohmed 已提交
772
		for ( var i = 0, il = this.offsets.length; i < il; i ++ ) {
O
ohmed 已提交
773 774 775

			var offset = this.offsets[ i ];

M
Mr.doob 已提交
776
			geometry.offsets.push( {
O
ohmed 已提交
777

O
ohmed 已提交
778 779 780
				start: offset.start,
				index: offset.index,
				count: offset.count
O
ohmed 已提交
781

M
Mr.doob 已提交
782
			} );
O
ohmed 已提交
783

O
ohmed 已提交
784
		}
O
ohmed 已提交
785 786 787 788 789

		return geometry;

	},

790
	dispose: function () {
791

792
		this.dispatchEvent( { type: 'dispose' } );
793

794
	}
795 796

};
M
Mr.doob 已提交
797 798

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