Cube.js 4.1 KB
Newer Older
1 2
/**
 * @author mr.doob / http://mrdoob.com/
M
Mr.doob 已提交
3
 * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as
4 5
 */

6
var Cube = function ( width, height, depth, segments_width, segments_height, materials, flipped, sides ) {
7

M
Mr.doob 已提交
8
	THREE.Geometry.call( this );
9 10 11 12

	var scope = this,
	width_half = width / 2,
	height_half = height / 2,
M
Mr.doob 已提交
13
	depth_half = depth / 2,
14
	flip = flipped ? - 1 : 1;
M
Mr.doob 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

	if ( materials !== undefined ) {

		if ( materials instanceof Array ) {

			this.materials = materials;

		} else {

			this.materials = [];

			for ( var i = 0; i < 6; i ++ ) {

				this.materials.push( [ materials ] );

			}

		}

	} else {

		this.materials = [];

38 39
	}

40
	this.sides = { px: true, nx: true, py: true, ny: true, pz: true, nz: true };
41

42
	if( sides != undefined ) {
43

44
		for( var s in sides ) {
45

46
			if ( this.sides[ s ] != undefined ) {
47

48
				this.sides[ s ] = sides[ s ];
49

50
			}
51

52
		}
53

54
	}
55

56 57 58 59 60 61
	this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px
	this.sides.nx && buildPlane( 'z', 'y', - 1 * flip, - 1, depth, height, width_half, this.materials[ 1 ] );   // nx
	this.sides.py && buildPlane( 'x', 'z',   1 * flip,   1, width, depth, height_half, this.materials[ 2 ] );   // py
	this.sides.ny && buildPlane( 'x', 'z',   1 * flip, - 1, width, depth, - height_half, this.materials[ 3 ] ); // ny
	this.sides.pz && buildPlane( 'x', 'y',   1 * flip, - 1, width, height, depth_half, this.materials[ 4 ] );   // pz
	this.sides.nz && buildPlane( 'x', 'y', - 1 * flip, - 1, width, height, - depth_half, this.materials[ 5 ] ); // nz
M
Mr.doob 已提交
62

63
	mergeVertices();
M
Mr.doob 已提交
64

M
Mr.doob 已提交
65 66
	function buildPlane( u, v, udir, vdir, width, height, depth, material ) {

67
		var w, ix, iy,
M
Mr.doob 已提交
68
		gridX = segments_width || 1,
M
Mr.doob 已提交
69 70 71
		gridY = segments_height || 1,
		gridX1 = gridX + 1,
		gridY1 = gridY + 1,
M
Mr.doob 已提交
72 73
		width_half = width / 2,
		height_half = height / 2,
M
Mr.doob 已提交
74 75
		segment_width = width / gridX,
		segment_height = height / gridY,
M
Mr.doob 已提交
76
		offset = scope.vertices.length;
M
Mr.doob 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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

		if ( ( u == 'x' && v == 'y' ) || ( u == 'y' && v == 'x' ) ) {

			w = 'z';

		} else if ( ( u == 'x' && v == 'z' ) || ( u == 'z' && v == 'x' ) ) {

			w = 'y';

		} else if ( ( u == 'z' && v == 'y' ) || ( u == 'y' && v == 'z' ) ) {

			w = 'x';

		}


		for( iy = 0; iy < gridY1; iy++ ) {

			for( ix = 0; ix < gridX1; ix++ ) {

				var vector = new THREE.Vector3();
				vector[ u ] = ( ix * segment_width - width_half ) * udir;
				vector[ v ] = ( iy * segment_height - height_half ) * vdir;
				vector[ w ] = depth;

				scope.vertices.push( new THREE.Vertex( vector ) );

			}

		}

		for( iy = 0; iy < gridY; iy++ ) {

			for( ix = 0; ix < gridX; ix++ ) {

				var a = ix + gridX1 * iy;
				var b = ix + gridX1 * ( iy + 1 );
				var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
				var d = ( ix + 1 ) + gridX1 * iy;

				scope.faces.push( new THREE.Face4( a + offset, b + offset, c + offset, d + offset, null, material ) );
				scope.uvs.push( [
							new THREE.UV( ix / gridX, iy / gridY ),
							new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
							new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
							new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
						] );

			}

		}
128 129 130

	}

M
Mr.doob 已提交
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 159 160 161 162
	function mergeVertices() {

		var unique = [], changes = [];

		for ( var i = 0, il = scope.vertices.length; i < il; i ++ ) {

			var v = scope.vertices[ i ],
			duplicate = false;

			for ( var j = 0, jl = unique.length; j < jl; j ++ ) {

				var vu = unique[ j ];

				if( v.position.x == vu.position.x && v.position.y == vu.position.y && v.position.z == vu.position.z ) {

					changes[ i ] = j;
					duplicate = true;
					break;

				}

			}

			if ( ! duplicate ) {

				changes[ i ] = unique.length;
				unique.push( new THREE.Vertex( v.position.clone() ) );

			}

		}

163
		for ( i = 0, il = scope.faces.length; i < il; i ++ ) {
M
Mr.doob 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177

			var face = scope.faces[ i ];

			face.a = changes[ face.a ];
			face.b = changes[ face.b ];
			face.c = changes[ face.c ];
			face.d = changes[ face.d ];

		}

		scope.vertices = unique;

	}

178
	this.computeCentroids();
179
	this.computeFaceNormals();
M
Mr.doob 已提交
180
	this.sortFacesByMaterial();
181

182
};
183 184 185

Cube.prototype = new THREE.Geometry();
Cube.prototype.constructor = Cube;