Geometry.js 3.9 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	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 );

			if ( face instanceof THREE.Face3 ) {

				face.centroid.addSelf( this.vertices[ face.a ].position );
				face.centroid.addSelf( this.vertices[ face.b ].position );
				face.centroid.addSelf( this.vertices[ face.c ].position );
				face.centroid.divideScalar( 3 );

			} else if ( face instanceof THREE.Face4 ) {

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

			}

		}

	},

U
unknown 已提交
46
	computeNormals: function ( useVertexNormals ) {
47

U
unknown 已提交
48
		var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC, cb = new THREE.Vector3(), ab = new THREE.Vector3();
49

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

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

		}

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

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

U
unknown 已提交
61 62 63 64 65
            if ( useVertexNormals && face.vertexNormals.length  ) {
                
                // set face normal to average of vertex normals
                
                cb.set( 0, 0, 0 );
66

U
unknown 已提交
67 68 69 70 71
                for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
                    cb.x += face.vertexNormals[n].x;
                    cb.y += face.vertexNormals[n].y;
                    cb.z += face.vertexNormals[n].z;
                }
72

U
unknown 已提交
73 74 75
                cb.x /= 3;
                cb.y /= 3;
                cb.z /= 3;
76

U
unknown 已提交
77
                if ( !cb.isZero() ) {
78

U
unknown 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                    cb.normalize();

                }

                face.normal.copy( cb );
            }
            
            else {
                
                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();

                }
101

U
unknown 已提交
102 103
                face.normal.copy( cb );
            }
104 105 106

		}

M
Mr.doob 已提交
107
	},
U
unknown 已提交
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
    
    computeBoundingBox: function ( ) {
        
        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 ] };
            
            var v, vl;
            
            for ( 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;

            }
        }
    },
M
Mr.doob 已提交
141 142 143 144 145 146

	toString: function () {

		return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ' )';

	}
147

M
Mr.doob 已提交
148
};