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

THREE.BufferGeometry = function () {

7
	this.id = THREE.Math.generateUUID();
8

9
	// attributes
10

11
	this.attributes = {};
12

13
	// attributes typed arrays are kept only if dynamic flag is set
14 15 16

	this.dynamic = false;

17 18 19 20
	// offsets for chunks when using indexed elements

	this.offsets = [];

21 22 23 24 25
	// boundings

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

26 27
	this.hasTangents = false;

28 29 30 31 32 33 34 35
	// for compatibility

	this.morphTargets = [];

};

THREE.BufferGeometry.prototype = {

36
	constructor: THREE.BufferGeometry,
37

M
Mr.doob 已提交
38 39 40 41 42
	addEventListener: THREE.EventDispatcher.prototype.addEventListener,
	hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
	removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
	dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent,

43 44
	applyMatrix: function ( matrix ) {

45 46 47 48 49 50 51
		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 ) {
52

53
			matrix.multiplyVector3Array( positionArray );
54 55 56 57
			this.verticesNeedUpdate = true;

		}

58
		if ( normalArray !== undefined ) {
59

60
			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
61 62 63 64

			normalMatrix.multiplyVector3Array( normalArray );

			this.normalizeNormals();
65 66 67 68 69 70 71

			this.normalsNeedUpdate = true;

		}

	},

72 73
	computeBoundingBox: function () {

74
		if ( this.boundingBox === null ) {
75

B
Ben Houston 已提交
76
			this.boundingBox = new THREE.Box3();
77 78 79

		}

80
		var positions = this.attributes[ "position" ].array;
81

82
		if ( positions ) {
83 84 85 86

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

87 88 89 90 91 92 93
			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 ) {
94

95 96 97
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
98 99 100 101 102 103 104 105 106 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

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

				}

			}

		}

135
		if ( positions === undefined || positions.length === 0 ) {
136 137 138 139 140 141 142

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

		}

	},
143 144 145

	computeBoundingSphere: function () {

146 147
		if ( this.boundingSphere === null ) {

B
Ben Houston 已提交
148
			this.boundingSphere = new THREE.Sphere();
149

B
Ben Houston 已提交
150
		}
151

152
		var positions = this.attributes[ "position" ].array;
153

154
		if ( positions ) {
155

156
			var radiusSq, maxRadiusSq = 0;
157 158
			var x, y, z;

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

161 162 163
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
164

165 166
				radiusSq =  x * x + y * y + z * z;
				if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
167 168 169

			}

170
			this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
171 172 173

		}

174 175 176 177
	},

	computeVertexNormals: function () {

178
		if ( this.attributes[ "position" ] ) {
179 180 181 182

			var i, il;
			var j, jl;

183
			var nVertexElements = this.attributes[ "position" ].array.length;
184 185

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

187
				this.attributes[ "normal" ] = {
188

189
					itemSize: 3,
190
					array: new Float32Array( nVertexElements )
191 192

				};
193 194 195 196 197

			} else {

				// reset existing normals to zero

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

200
					this.attributes[ "normal" ].array[ i ] = 0;
201 202 203 204 205

				}

			}

206 207
			var positions = this.attributes[ "position" ].array;
			var normals = this.attributes[ "normal" ].array;
208 209 210 211 212 213 214 215 216 217

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

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

248 249 250 251
						x = positions[ vC * 3 ];
						y = positions[ vC * 3 + 1 ];
						z = positions[ vC * 3 + 2 ];
						pC.set( x, y, z );
252

253 254 255
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
256

257
						normals[ vA * 3 ]     += cb.x;
258 259 260
						normals[ vA * 3 + 1 ] += cb.y;
						normals[ vA * 3 + 2 ] += cb.z;

261
						normals[ vB * 3 ]     += cb.x;
262 263 264
						normals[ vB * 3 + 1 ] += cb.y;
						normals[ vB * 3 + 2 ] += cb.z;

265
						normals[ vC * 3 ]     += cb.x;
266 267 268 269 270 271 272 273 274 275
						normals[ vC * 3 + 1 ] += cb.y;
						normals[ vC * 3 + 2 ] += cb.z;

					}

				}

			// non-indexed elements (unconnected triangle soup)

			} else {
276

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

279 280 281
					x = positions[ i ];
					y = positions[ i + 1 ];
					z = positions[ i + 2 ];
282 283
					pA.set( x, y, z );

284 285 286
					x = positions[ i + 3 ];
					y = positions[ i + 4 ];
					z = positions[ i + 5 ];
287 288
					pB.set( x, y, z );

289 290 291
					x = positions[ i + 6 ];
					y = positions[ i + 7 ];
					z = positions[ i + 8 ];
292 293
					pC.set( x, y, z );

294 295 296
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
297

298 299 300
					normals[ i ] 	 = cb.x;
					normals[ i + 1 ] = cb.y;
					normals[ i + 2 ] = cb.z;
301

302 303 304
					normals[ i + 3 ] = cb.x;
					normals[ i + 4 ] = cb.y;
					normals[ i + 5 ] = cb.z;
305

306 307 308
					normals[ i + 6 ] = cb.x;
					normals[ i + 7 ] = cb.y;
					normals[ i + 8 ] = cb.z;
309 310 311 312 313

				}

			}

314
			this.normalizeNormals();
315

316
			this.normalsNeedUpdate = true;
317

318
		}
319

320
	},
321

322
	normalizeNormals: function () {
323

324
		var normals = this.attributes[ "normal" ].array;
325

326 327 328 329 330 331 332 333 334 335 336 337 338
		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;
339 340 341

		}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	},

	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,
373
				array: new Float32Array( nTangentElements )
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 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

			};

		}

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

443 444 445 446 447 448 449 450 451 452 453
			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
			);
454

455 456 457
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
458

459 460 461
			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

		}

		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 );
506
			tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
507 508 509

			// Calculate handedness

510
			tmp2.crossVectors( n2, t );
511 512 513
			test = tmp2.dot( tan2[ v ] );
			w = ( test < 0.0 ) ? -1.0 : 1.0;

514
			tangents[ v * 4 ]     = tmp.x;
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 540 541 542 543
			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;

544 545
	},

546
	dispose: function () {
547

548
		this.dispatchEvent( { type: 'dispose' } );
549

550
	}
551 552

};