BufferGeometry.js 10.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
	// 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

41 42
	applyMatrix: function ( matrix ) {

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

51
			matrix.multiplyVector3Array( positionArray );
52 53 54 55
			this.verticesNeedUpdate = true;

		}

56
		if ( normalArray !== undefined ) {
57

58
			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
59 60 61 62

			normalMatrix.multiplyVector3Array( normalArray );

			this.normalizeNormals();
63 64 65 66 67 68 69

			this.normalsNeedUpdate = true;

		}

	},

70 71
	computeBoundingBox: function () {

72
		if ( this.boundingBox === null ) {
73

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

		}

78
		var positions = this.attributes[ "position" ].array;
79

80
		if ( positions ) {
81 82 83 84

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

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

93 94 95
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
96 97 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

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

				}

			}

		}

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

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

		}

	},
141 142 143

	computeBoundingSphere: function () {

144 145
		if ( this.boundingSphere === null ) {

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

B
Ben Houston 已提交
148
		}
149

150
		var positions = this.attributes[ "position" ].array;
151

152
		if ( positions ) {
153

154
			var radiusSq, maxRadiusSq = 0;
155 156
			var x, y, z;

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

159 160 161
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
162

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

			}

168
			this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
169 170 171

		}

172 173 174 175
	},

	computeVertexNormals: function () {

176
		if ( this.attributes[ "position" ] ) {
177 178 179 180

			var i, il;
			var j, jl;

181
			var nVertexElements = this.attributes[ "position" ].array.length;
182 183

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

185
				this.attributes[ "normal" ] = {
186

187
					itemSize: 3,
188
					array: new Float32Array( nVertexElements )
189 190

				};
191 192 193 194 195

			} else {

				// reset existing normals to zero

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

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

				}

			}

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

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

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

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

251 252 253
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
254

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

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

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

					}

				}

			// non-indexed elements (unconnected triangle soup)

			} else {
274

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

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

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

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

292 293 294
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
295

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

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

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

				}

			}

312
			this.normalizeNormals();
313

314
			this.normalsNeedUpdate = true;
315

316
		}
317

318
	},
319

320
	normalizeNormals: function () {
321

322
		var normals = this.attributes[ "normal" ].array;
323

324 325 326 327 328 329 330 331 332 333 334 335 336
		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;
337 338 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
	},

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

			};

		}

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

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

453 454 455
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
456

457 458 459
			tan2[ a ].add( tdir );
			tan2[ b ].add( tdir );
			tan2[ c ].add( tdir );
460 461 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

		}

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

			// Calculate handedness

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

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

542 543
	},

544
	dispose: function () {
545

546
		this.dispatchEvent( { type: 'dispose' } );
547

548
	}
549 550

};
M
Mr.doob 已提交
551 552

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