CubeGeometry.js 3.8 KB
Newer Older
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / 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
THREE.CubeGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments, materials, sides ) {
7

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

10 11 12 13 14 15 16 17 18 19 20 21 22
	var scope = this;

	this.width = width;
	this.height = height;
	this.depth = depth;

	this.widthSegments = widthSegments || 1;
	this.heightSegments = heightSegments || 1;
	this.depthSegments = depthSegments || 1;

	var width_half = this.width / 2;
	var height_half = this.height / 2;
	var depth_half = this.depth / 2;
M
Mr.doob 已提交
23

24 25
	var mpx, mpy, mpz, mnx, mny, mnz;

M
Mr.doob 已提交
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 ++ ) {

38
				this.materials.push( materials );
M
Mr.doob 已提交
39 40 41 42 43

			}

		}

44 45
		mpx = 0; mnx = 1; mpy = 2; mny = 3; mpz = 4; mnz = 5;

M
Mr.doob 已提交
46 47 48 49
	} else {

		this.materials = [];

50 51
	}

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

M
Mr.doob 已提交
54
	if ( sides != undefined ) {
55

M
Mr.doob 已提交
56
		for ( var s in sides ) {
57

A
alteredq 已提交
58
			if ( this.sides[ s ] !== undefined ) {
59

60
				this.sides[ s ] = sides[ s ];
61

62
			}
63

64
		}
65

66
	}
67

68 69 70 71 72 73
	this.sides.px && buildPlane( 'z', 'y', - 1, - 1, this.depth, this.height, width_half, mpx ); // px
	this.sides.nx && buildPlane( 'z', 'y',   1, - 1, this.depth, this.height, - width_half, mnx ); // nx
	this.sides.py && buildPlane( 'x', 'z',   1,   1, this.width, this.depth, height_half, mpy ); // py
	this.sides.ny && buildPlane( 'x', 'z',   1, - 1, this.width, this.depth, - height_half, mny ); // ny
	this.sides.pz && buildPlane( 'x', 'y',   1, - 1, this.width, this.height, depth_half, mpz ); // pz
	this.sides.nz && buildPlane( 'x', 'y', - 1, - 1, this.width, this.height, - depth_half, mnz ); // nz
M
Mr.doob 已提交
74 75 76

	function buildPlane( u, v, udir, vdir, width, height, depth, material ) {

77
		var w, ix, iy,
78 79
		gridX = scope.widthSegments,
		gridY = scope.heightSegments,
M
Mr.doob 已提交
80 81 82
		width_half = width / 2,
		height_half = height / 2,
		offset = scope.vertices.length;
M
Mr.doob 已提交
83

84
		if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
M
Mr.doob 已提交
85 86 87

			w = 'z';

88
		} else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
M
Mr.doob 已提交
89 90

			w = 'y';
91
			gridY = scope.depthSegments;
M
Mr.doob 已提交
92

93
		} else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
M
Mr.doob 已提交
94 95

			w = 'x';
96
			gridX = scope.depthSegments;
M
Mr.doob 已提交
97 98 99

		}

S
staunsholm 已提交
100 101 102
		var gridX1 = gridX + 1,
		gridY1 = gridY + 1,
		segment_width = width / gridX,
M
Mr.doob 已提交
103 104 105 106
		segment_height = height / gridY,
		normal = new THREE.Vector3();

		normal[ w ] = depth > 0 ? 1 : - 1;
M
Mr.doob 已提交
107

M
Mr.doob 已提交
108
		for ( iy = 0; iy < gridY1; iy ++ ) {
M
Mr.doob 已提交
109

M
Mr.doob 已提交
110
			for ( ix = 0; ix < gridX1; ix ++ ) {
M
Mr.doob 已提交
111

M
Mr.doob 已提交
112
				var vector = new THREE.Vector3();
M
Mr.doob 已提交
113 114 115 116
				vector[ u ] = ( ix * segment_width - width_half ) * udir;
				vector[ v ] = ( iy * segment_height - height_half ) * vdir;
				vector[ w ] = depth;

117
				scope.vertices.push( vector );
M
Mr.doob 已提交
118 119 120 121

			}

		}
122

M
Mr.doob 已提交
123
		for ( iy = 0; iy < gridY; iy++ ) {
M
Mr.doob 已提交
124

M
Mr.doob 已提交
125
			for ( ix = 0; ix < gridX; ix++ ) {
M
Mr.doob 已提交
126 127 128 129 130 131

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

M
Mr.doob 已提交
132 133 134 135 136 137
				var face = new THREE.Face4( a + offset, b + offset, c + offset, d + offset );
				face.normal.copy( normal );
				face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone(), normal.clone() );
				face.materialIndex = material;

				scope.faces.push( face );
A
alteredq 已提交
138
				scope.faceVertexUvs[ 0 ].push( [
A
alteredq 已提交
139 140 141 142
							new THREE.UV( ix / gridX, 1 - iy / gridY ),
							new THREE.UV( ix / gridX, 1 - ( iy + 1 ) / gridY ),
							new THREE.UV( ( ix + 1 ) / gridX, 1- ( iy + 1 ) / gridY ),
							new THREE.UV( ( ix + 1 ) / gridX, 1 - iy / gridY )
M
Mr.doob 已提交
143 144 145 146 147
						] );

			}

		}
148 149 150 151

	}

	this.computeCentroids();
M
Mr.doob 已提交
152
	this.mergeVertices();
153

154
};
155

156
THREE.CubeGeometry.prototype = Object.create( THREE.Geometry.prototype );