BufferGeometry.js 10.1 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

10
	// attributes
11

12
	this.attributes = {};
13

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

	this.dynamic = false;

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

	this.offsets = [];

22 23 24 25 26
	// boundings

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

27 28
	this.hasTangents = false;

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

	this.morphTargets = [];

};

THREE.BufferGeometry.prototype = {

37
	constructor: THREE.BufferGeometry,
38

39 40
	applyMatrix: function ( matrix ) {

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

49
			matrix.multiplyVector3Array( positionArray );
50 51 52 53
			this.verticesNeedUpdate = true;

		}

54
		if ( normalArray !== undefined ) {
55

56
			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
57 58 59 60

			normalMatrix.multiplyVector3Array( normalArray );

			this.normalizeNormals();
61 62 63 64 65 66 67

			this.normalsNeedUpdate = true;

		}

	},

68 69
	computeBoundingBox: function () {

70
		if ( this.boundingBox === null ) {
71

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

		}

76
		var positions = this.attributes[ "position" ].array;
77

78
		if ( positions ) {
79 80 81 82

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

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

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

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

				}

			}

		}

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

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

		}

	},
139 140 141

	computeBoundingSphere: function () {

142 143
		if ( this.boundingSphere === null ) {

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

B
Ben Houston 已提交
146
		}
147

148
		var positions = this.attributes[ "position" ].array;
149

150
		if ( positions ) {
151

152
			var radiusSq, maxRadiusSq = 0;
153 154
			var x, y, z;

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

157 158 159
				x = positions[ i ];
				y = positions[ i + 1 ];
				z = positions[ i + 2 ];
160

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

			}

166
			this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
167 168 169

		}

170 171 172 173
	},

	computeVertexNormals: function () {

174
		if ( this.attributes[ "position" ] ) {
175 176 177 178

			var i, il;
			var j, jl;

179
			var nVertexElements = this.attributes[ "position" ].array.length;
180 181

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

183
				this.attributes[ "normal" ] = {
184

185
					itemSize: 3,
186
					array: new Float32Array( nVertexElements )
187 188

				};
189 190 191 192 193

			} else {

				// reset existing normals to zero

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

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

				}

			}

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

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

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

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

249 250 251
						cb.subVectors( pC, pB );
						ab.subVectors( pA, pB );
						cb.cross( ab );
252

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

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

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

					}

				}

			// non-indexed elements (unconnected triangle soup)

			} else {
272

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

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

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

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

290 291 292
					cb.subVectors( pC, pB );
					ab.subVectors( pA, pB );
					cb.cross( ab );
293

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

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

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

				}

			}

310
			this.normalizeNormals();
311

312
			this.normalsNeedUpdate = true;
313

314
		}
315

316
	},
317

318
	normalizeNormals: function () {
319

320
		var normals = this.attributes[ "normal" ].array;
321

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

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

			};

		}

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

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

451 452 453
			tan1[ a ].add( sdir );
			tan1[ b ].add( sdir );
			tan1[ c ].add( sdir );
454

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

		}

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

			// Calculate handedness

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

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

540 541
	},

542
	dispose: function () {
543

544
		this.dispatchEvent( { type: 'dispose' } );
545

546
	}
547 548

};
M
Mr.doob 已提交
549 550

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