Geometry.js 3.1 KB
Newer Older
M
Mr.doob 已提交
1 2
/**
 * @author mr.doob / http://mrdoob.com/
M
Mr.doob 已提交
3
 * @author kile / http://kile.stravaganza.org/
M
Mr.doob 已提交
4 5
 */

M
Mr.doob 已提交
6
THREE.Geometry = function () {
7

M
Mr.doob 已提交
8 9
	this.vertices = [];
	this.faces = [];
10
	this.uvs = [];
M
Mr.doob 已提交
11

M
Mr.doob 已提交
12 13 14 15
};

THREE.Geometry.prototype = {

M
Mr.doob 已提交
16 17 18 19 20 21 22 23 24
	computeCentroids: function () {

		var f, fl, face;

		for ( f = 0, fl = this.faces.length; f < fl; f++ ) {

			face = this.faces[ f ];
			face.centroid.set( 0, 0, 0 );

25 26 27 28
			face.centroid.addSelf( this.vertices[ face.a ].position );
			face.centroid.addSelf( this.vertices[ face.b ].position );
			face.centroid.addSelf( this.vertices[ face.c ].position );

M
Mr.doob 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
			if ( face instanceof THREE.Face3 ) {

				face.centroid.divideScalar( 3 );

			} else if ( face instanceof THREE.Face4 ) {

				face.centroid.addSelf( this.vertices[ face.d ].position );
				face.centroid.divideScalar( 4 );

			}

		}

	},

U
unknown 已提交
44
	computeNormals: function ( useVertexNormals ) {
45

46 47
		var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
		cb = new THREE.Vector3(), ab = new THREE.Vector3();
48

M
Mr.doob 已提交
49
		for ( v = 0, vl = this.vertices.length; v < vl; v++ ) {
50

M
Mr.doob 已提交
51 52
			vertex = this.vertices[ v ];
			vertex.normal.set( 0, 0, 0 );
53 54 55

		}

M
Mr.doob 已提交
56
		for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
57

M
Mr.doob 已提交
58
			face = this.faces[ f ];
59

60
			if ( useVertexNormals && face.vertexNormals.length  ) {
61

62
				cb.set( 0, 0, 0 );
63

64
				for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
65

66
					cb.addSelf( face.vertexNormals[n] );
67

68
				}
U
unknown 已提交
69

70
				cb.divideScalar( 3 );
U
unknown 已提交
71

72
				if ( ! cb.isZero() ) {
U
unknown 已提交
73

74
					cb.normalize();
U
unknown 已提交
75

76
				}
U
unknown 已提交
77

78
				face.normal.copy( cb );
U
unknown 已提交
79

80
			} else {
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
				vA = this.vertices[ face.a ];
				vB = this.vertices[ face.b ];
				vC = this.vertices[ face.c ];

				cb.sub( vC.position, vB.position );
				ab.sub( vA.position, vB.position );
				cb.crossSelf( ab );

				if ( !cb.isZero() ) {

				    cb.normalize();

				}

				face.normal.copy( cb );

			}

		}

	},

104 105 106 107 108 109 110 111 112 113 114 115 116
	computeVertexNormals: function () {

		var f, fl;

		for ( f = 0, fl = this.faces.length; f < fl; f++ ) {

			

		}

	},

	computeBoundingBox: function () {
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158

		if ( this.vertices.length > 0 ) {

			this.bbox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
			'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ], 
			'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };

			for ( var v = 1, vl = this.vertices.length; v < vl; v++ ) {

				vertex = this.vertices[ v ];

				if ( vertex.position.x < this.bbox.x[ 0 ] ) {

					this.bbox.x[ 0 ] = vertex.position.x;

				} else if ( vertex.position.x > this.bbox.x[ 1 ] ) {

					this.bbox.x[ 1 ] = vertex.position.x;

				}

				if ( vertex.position.y < this.bbox.y[ 0 ] ) {

					this.bbox.y[ 0 ] = vertex.position.y;

				} else if ( vertex.position.y > this.bbox.y[ 1 ] ) {

					this.bbox.y[ 1 ] = vertex.position.y;

				}

				if ( vertex.position.z < this.bbox.z[ 0 ] ) {

					this.bbox.z[ 0 ] = vertex.position.z;

				} else if ( vertex.position.z > this.bbox.z[ 1 ] ) {

					this.bbox.z[ 1 ] = vertex.position.z;

				}

			}
159 160 161

		}

M
Mr.doob 已提交
162 163 164 165
	},

	toString: function () {

166
		return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ', uvs: ' + this.uvs + ' )';
M
Mr.doob 已提交
167 168

	}
169

M
Mr.doob 已提交
170
};