1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
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 ) {
|
---|
7 |
|
---|
8 | THREE.Geometry.call( this );
|
---|
9 |
|
---|
10 | var scope = this;
|
---|
11 |
|
---|
12 | this.width = width;
|
---|
13 | this.height = height;
|
---|
14 | this.depth = depth;
|
---|
15 |
|
---|
16 | this.widthSegments = widthSegments || 1;
|
---|
17 | this.heightSegments = heightSegments || 1;
|
---|
18 | this.depthSegments = depthSegments || 1;
|
---|
19 |
|
---|
20 | var width_half = this.width / 2;
|
---|
21 | var height_half = this.height / 2;
|
---|
22 | var depth_half = this.depth / 2;
|
---|
23 |
|
---|
24 | buildPlane( 'z', 'y', - 1, - 1, this.depth, this.height, width_half, 0 ); // px
|
---|
25 | buildPlane( 'z', 'y', 1, - 1, this.depth, this.height, - width_half, 1 ); // nx
|
---|
26 | buildPlane( 'x', 'z', 1, 1, this.width, this.depth, height_half, 2 ); // py
|
---|
27 | buildPlane( 'x', 'z', 1, - 1, this.width, this.depth, - height_half, 3 ); // ny
|
---|
28 | buildPlane( 'x', 'y', 1, - 1, this.width, this.height, depth_half, 4 ); // pz
|
---|
29 | buildPlane( 'x', 'y', - 1, - 1, this.width, this.height, - depth_half, 5 ); // nz
|
---|
30 |
|
---|
31 | function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) {
|
---|
32 |
|
---|
33 | var w, ix, iy,
|
---|
34 | gridX = scope.widthSegments,
|
---|
35 | gridY = scope.heightSegments,
|
---|
36 | width_half = width / 2,
|
---|
37 | height_half = height / 2,
|
---|
38 | offset = scope.vertices.length;
|
---|
39 |
|
---|
40 | if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
|
---|
41 |
|
---|
42 | w = 'z';
|
---|
43 |
|
---|
44 | } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
|
---|
45 |
|
---|
46 | w = 'y';
|
---|
47 | gridY = scope.depthSegments;
|
---|
48 |
|
---|
49 | } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
|
---|
50 |
|
---|
51 | w = 'x';
|
---|
52 | gridX = scope.depthSegments;
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | var gridX1 = gridX + 1,
|
---|
57 | gridY1 = gridY + 1,
|
---|
58 | segment_width = width / gridX,
|
---|
59 | segment_height = height / gridY,
|
---|
60 | normal = new THREE.Vector3();
|
---|
61 |
|
---|
62 | normal[ w ] = depth > 0 ? 1 : - 1;
|
---|
63 |
|
---|
64 | for ( iy = 0; iy < gridY1; iy ++ ) {
|
---|
65 |
|
---|
66 | for ( ix = 0; ix < gridX1; ix ++ ) {
|
---|
67 |
|
---|
68 | var vector = new THREE.Vector3();
|
---|
69 | vector[ u ] = ( ix * segment_width - width_half ) * udir;
|
---|
70 | vector[ v ] = ( iy * segment_height - height_half ) * vdir;
|
---|
71 | vector[ w ] = depth;
|
---|
72 |
|
---|
73 | scope.vertices.push( vector );
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | for ( iy = 0; iy < gridY; iy++ ) {
|
---|
80 |
|
---|
81 | for ( ix = 0; ix < gridX; ix++ ) {
|
---|
82 |
|
---|
83 | var a = ix + gridX1 * iy;
|
---|
84 | var b = ix + gridX1 * ( iy + 1 );
|
---|
85 | var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
|
---|
86 | var d = ( ix + 1 ) + gridX1 * iy;
|
---|
87 |
|
---|
88 | var uva = new THREE.Vector2( ix / gridX, 1 - iy / gridY );
|
---|
89 | var uvb = new THREE.Vector2( ix / gridX, 1 - ( iy + 1 ) / gridY );
|
---|
90 | var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iy + 1 ) / gridY );
|
---|
91 | var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iy / gridY );
|
---|
92 |
|
---|
93 | var face = new THREE.Face3( a + offset, b + offset, d + offset );
|
---|
94 | face.normal.copy( normal );
|
---|
95 | face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
|
---|
96 | face.materialIndex = materialIndex;
|
---|
97 |
|
---|
98 | scope.faces.push( face );
|
---|
99 | scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );
|
---|
100 |
|
---|
101 | face = new THREE.Face3( b + offset, c + offset, d + offset );
|
---|
102 | face.normal.copy( normal );
|
---|
103 | face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
|
---|
104 | face.materialIndex = materialIndex;
|
---|
105 |
|
---|
106 | scope.faces.push( face );
|
---|
107 | scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );
|
---|
108 |
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | this.computeCentroids();
|
---|
116 | this.mergeVertices();
|
---|
117 |
|
---|
118 | };
|
---|
119 |
|
---|
120 | THREE.CubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
|
---|