123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /**
- * @author mr.doob / http://mrdoob.com/
- * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as
- */
- THREE.Cube = function ( width, height, depth, segmentsWidth, segmentsHeight, segmentsDepth, materials, flipped, sides ) {
- THREE.Geometry.call( this );
- var scope = this,
- width_half = width / 2,
- height_half = height / 2,
- depth_half = depth / 2,
- flip = flipped ? - 1 : 1;
- 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 = [];
- }
- this.sides = { px: true, nx: true, py: true, ny: true, pz: true, nz: true };
- if( sides != undefined ) {
- for( var s in sides ) {
- if ( this.sides[ s ] != undefined ) {
- this.sides[ s ] = sides[ s ];
- }
- }
- }
- 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
- mergeVertices();
- function buildPlane( u, v, udir, vdir, width, height, depth, material ) {
- var w, ix, iy,
- gridX = segmentsWidth || 1,
- gridY = segmentsHeight || 1,
- width_half = width / 2,
- height_half = height / 2,
- offset = scope.vertices.length;
- if ( ( u == 'x' && v == 'y' ) || ( u == 'y' && v == 'x' ) ) {
- w = 'z';
- } else if ( ( u == 'x' && v == 'z' ) || ( u == 'z' && v == 'x' ) ) {
- w = 'y';
- gridY = segmentsDepth || 1;
- } else if ( ( u == 'z' && v == 'y' ) || ( u == 'y' && v == 'z' ) ) {
- w = 'x';
- gridX = segmentsDepth || 1;
- }
- var gridX1 = gridX + 1,
- gridY1 = gridY + 1,
- segment_width = width / gridX,
- segment_height = height / gridY;
- 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, null, material ) );
- scope.faceVertexUvs[ 0 ].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 )
- ] );
- }
- }
- }
- 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() ) );
- }
- }
- for ( i = 0, il = scope.faces.length; i < il; i ++ ) {
- 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;
- }
- this.computeCentroids();
- this.computeFaceNormals();
- };
- THREE.Cube.prototype = new THREE.Geometry();
- THREE.Cube.prototype.constructor = THREE.Cube;
|