BufferGeometry.js 11.5 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
	// attributes
13

14
	this.attributes = {};
15

16
	// attributes typed arrays are kept only if dynamic flag is set
17

18
	this.dynamic = true;
19

20 21 22 23
	// offsets for chunks when using indexed elements

	this.offsets = [];

24 25 26 27 28
	// boundings

	this.boundingBox = null;
	this.boundingSphere = null;

29 30
	this.hasTangents = false;

31 32 33 34 35 36 37 38
	// for compatibility

	this.morphTargets = [];

};

THREE.BufferGeometry.prototype = {

39
	constructor: THREE.BufferGeometry,
40

O
ohmed 已提交
41
	addAttribute: function( name, type, numItems, itemSize ) {
42 43 44

		this.attributes[ name ] = {

M
Mr.doob 已提交
45 46
			itemSize: itemSize,
			array: new type( numItems * itemSize )
47 48 49 50 51

		};

	},

52 53
	applyMatrix: function ( matrix ) {

54 55 56 57 58 59 60
		var positionArray;
		var normalArray;

		if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array;
		if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array;

		if ( positionArray !== undefined ) {
61

62
			matrix.multiplyVector3Array( positionArray );
63 64 65 66
			this.verticesNeedUpdate = true;

		}

67
		if ( normalArray !== undefined ) {
68

69
			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
70 71 72 73

			normalMatrix.multiplyVector3Array( normalArray );

			this.normalizeNormals();
74 75 76 77 78 79 80

			this.normalsNeedUpdate = true;

		}

	},

81 82
	computeBoundingBox: function () {

83
		if ( this.boundingBox === null ) {
84

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

		}

89
		var positions = this.attributes[ "position" ].array;
90

91
		if ( positions ) {
92 93 94 95

			var bb = this.boundingBox;
			var x, y, z;

96 97 98 99 100 101 102
			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 ) {
103

104 105 106
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
107 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

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

				}

			}

		}

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

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

		}

	},
152 153 154

	computeBoundingSphere: function () {

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

158
		return function () {
159

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

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

164
			}
165

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

168
			if ( positions ) {
169

170
				var center = this.boundingSphere.center;
171

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

174 175 176 177 178 179 180 181 182 183 184 185 186
					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
					box.addPoint( vector );

				}

				box.center( center );

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

188 189 190 191 192
				}

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

			}
193 194 195

		}

196
	}(),
197 198 199

	computeVertexNormals: function () {

200
		if ( this.attributes[ "position" ] ) {
201 202 203 204

			var i, il;
			var j, jl;

205
			var nVertexElements = this.attributes[ "position" ].array.length;
206 207

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

209
				this.attributes[ "normal" ] = {
210

211
					itemSize: 3,
212
					array: new Float32Array( nVertexElements )
213 214

				};
215 216 217 218 219

			} else {

				// reset existing normals to zero

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

222
					this.attributes[ "normal" ].array[ i ] = 0;
223 224 225 226 227

				}

			}

228 229
			var positions = this.attributes[ "position" ].array;
			var normals = this.attributes[ "normal" ].array;
230 231 232 233 234 235 236 237 238 239

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

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
			// indexed elements

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

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

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

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

270 271 272 273
						x = positions[ vC * 3 ];
						y = positions[ vC * 3 + 1 ];
						z = positions[ vC * 3 + 2 ];
						pC.set( x, y, z );
274

275 276 277
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
278

279
						normals[ vA * 3 ]     += cb.x;
280 281 282
						normals[ vA * 3 + 1 ] += cb.y;
						normals[ vA * 3 + 2 ] += cb.z;

283
						normals[ vB * 3 ]     += cb.x;
284 285 286
						normals[ vB * 3 + 1 ] += cb.y;
						normals[ vB * 3 + 2 ] += cb.z;

287
						normals[ vC * 3 ]     += cb.x;
288 289 290 291 292 293 294 295 296 297
						normals[ vC * 3 + 1 ] += cb.y;
						normals[ vC * 3 + 2 ] += cb.z;

					}

				}

			// non-indexed elements (unconnected triangle soup)

			} else {
298

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

301 302 303
					x = positions[ i ];
					y = positions[ i + 1 ];
					z = positions[ i + 2 ];
304 305
					pA.set( x, y, z );

306 307 308
					x = positions[ i + 3 ];
					y = positions[ i + 4 ];
					z = positions[ i + 5 ];
309 310
					pB.set( x, y, z );

311 312 313
					x = positions[ i + 6 ];
					y = positions[ i + 7 ];
					z = positions[ i + 8 ];
314 315
					pC.set( x, y, z );

316 317 318
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
319

320 321 322
					normals[ i ] 	 = cb.x;
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
323

324 325 326
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
327

328 329 330
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
331 332 333 334 335

				}

			}

336
			this.normalizeNormals();
337

338
			this.normalsNeedUpdate = true;
339

340
		}
341

342
	},
343

344
	normalizeNormals: function () {
345

346
		var normals = this.attributes[ "normal" ].array;
347

348 349 350 351 352 353 354 355 356 357 358 359 360
		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 );

			normals[ i ] 	 *= n;
			normals[ i + 1 ] *= n;
			normals[ i + 2 ] *= n;
361 362 363

		}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	},

	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,
395
				array: new Float32Array( nTangentElements )
396 397 398 399 400 401 402 403 404 405 406 407 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

			};

		}

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

465 466 467 468 469 470 471 472 473 474 475
			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
			);
476

477 478 479
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
480

481 482 483
			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );
484 485 486 487 488 489 490 491 492 493 494 495 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

		}

		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 );
528
			tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
529 530 531

			// Calculate handedness

532
			tmp2.crossVectors( n2, t );
533 534 535
			test = tmp2.dot( tan2[ v ] );
			w = ( test < 0.0 ) ? -1.0 : 1.0;

536
			tangents[ v * 4 ]     = tmp.x;
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
			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 );

			}

		}

		this.hasTangents = true;
		this.tangentsNeedUpdate = true;

566 567
	},

M
Mr.doob 已提交
568
	clone: function () {
O
ohmed 已提交
569 570 571

		var geometry = new THREE.BufferGeometry();

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

O
ohmed 已提交
574 575
		for ( var attr in this.attributes ) {

M
Mr.doob 已提交
576 577
			var sourceAttr = this.attributes[ attr ];
			var sourceArray = sourceAttr.array;
O
ohmed 已提交
578

M
Mr.doob 已提交
579
			var attribute = {
O
ohmed 已提交
580 581 582

				itemSize: sourceAttr.itemSize,
				numItems: sourceAttr.numItems,
O
ohmed 已提交
583
				array: null
O
ohmed 已提交
584

O
ohmed 已提交
585 586
			};

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

O
ohmed 已提交
589
				var type = types[ i ];
O
ohmed 已提交
590

O
ohmed 已提交
591
				if ( sourceArray instanceof type ) {
O
ohmed 已提交
592

M
Mr.doob 已提交
593
					attribute.array = new type( sourceArray );
O
ohmed 已提交
594
					break;
O
ohmed 已提交
595

O
ohmed 已提交
596
				}
O
ohmed 已提交
597 598 599 600 601 602 603

			}

			geometry.attributes[ attr ] = attribute;

		}

O
ohmed 已提交
604
		for ( var i = 0, il = this.offsets.length; i < il; i ++ ) {
O
ohmed 已提交
605 606 607

			var offset = this.offsets[ i ];

M
Mr.doob 已提交
608
			geometry.offsets.push( {
O
ohmed 已提交
609

O
ohmed 已提交
610 611 612
				start: offset.start,
				index: offset.index,
				count: offset.count
O
ohmed 已提交
613

M
Mr.doob 已提交
614
			} );
O
ohmed 已提交
615

O
ohmed 已提交
616
		}
O
ohmed 已提交
617 618 619 620 621

		return geometry;

	},

622
	dispose: function () {
623

624
		this.dispatchEvent( { type: 'dispose' } );
625

626
	}
627 628

};
M
Mr.doob 已提交
629 630

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