Cube.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as
  4. */
  5. THREE.Cube = function ( width, height, depth, segmentsWidth, segmentsHeight, segmentsDepth, materials, flipped, sides ) {
  6. THREE.Geometry.call( this );
  7. var scope = this,
  8. width_half = width / 2,
  9. height_half = height / 2,
  10. depth_half = depth / 2,
  11. flip = flipped ? - 1 : 1;
  12. if ( materials !== undefined ) {
  13. if ( materials instanceof Array ) {
  14. this.materials = materials;
  15. } else {
  16. this.materials = [];
  17. for ( var i = 0; i < 6; i ++ ) {
  18. this.materials.push( [ materials ] );
  19. }
  20. }
  21. } else {
  22. this.materials = [];
  23. }
  24. this.sides = { px: true, nx: true, py: true, ny: true, pz: true, nz: true };
  25. if( sides != undefined ) {
  26. for( var s in sides ) {
  27. if ( this.sides[ s ] != undefined ) {
  28. this.sides[ s ] = sides[ s ];
  29. }
  30. }
  31. }
  32. this.sides.px && buildPlane( 'z', 'y', 1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px
  33. this.sides.nx && buildPlane( 'z', 'y', - 1 * flip, - 1, depth, height, width_half, this.materials[ 1 ] ); // nx
  34. this.sides.py && buildPlane( 'x', 'z', 1 * flip, 1, width, depth, height_half, this.materials[ 2 ] ); // py
  35. this.sides.ny && buildPlane( 'x', 'z', 1 * flip, - 1, width, depth, - height_half, this.materials[ 3 ] ); // ny
  36. this.sides.pz && buildPlane( 'x', 'y', 1 * flip, - 1, width, height, depth_half, this.materials[ 4 ] ); // pz
  37. this.sides.nz && buildPlane( 'x', 'y', - 1 * flip, - 1, width, height, - depth_half, this.materials[ 5 ] ); // nz
  38. mergeVertices();
  39. function buildPlane( u, v, udir, vdir, width, height, depth, material ) {
  40. var w, ix, iy,
  41. gridX = segmentsWidth || 1,
  42. gridY = segmentsHeight || 1,
  43. width_half = width / 2,
  44. height_half = height / 2,
  45. offset = scope.vertices.length;
  46. if ( ( u == 'x' && v == 'y' ) || ( u == 'y' && v == 'x' ) ) {
  47. w = 'z';
  48. } else if ( ( u == 'x' && v == 'z' ) || ( u == 'z' && v == 'x' ) ) {
  49. w = 'y';
  50. gridY = segmentsDepth || 1;
  51. } else if ( ( u == 'z' && v == 'y' ) || ( u == 'y' && v == 'z' ) ) {
  52. w = 'x';
  53. gridX = segmentsDepth || 1;
  54. }
  55. var gridX1 = gridX + 1,
  56. gridY1 = gridY + 1,
  57. segment_width = width / gridX,
  58. segment_height = height / gridY;
  59. for( iy = 0; iy < gridY1; iy++ ) {
  60. for( ix = 0; ix < gridX1; ix++ ) {
  61. var vector = new THREE.Vector3();
  62. vector[ u ] = ( ix * segment_width - width_half ) * udir;
  63. vector[ v ] = ( iy * segment_height - height_half ) * vdir;
  64. vector[ w ] = depth;
  65. scope.vertices.push( new THREE.Vertex( vector ) );
  66. }
  67. }
  68. for( iy = 0; iy < gridY; iy++ ) {
  69. for( ix = 0; ix < gridX; ix++ ) {
  70. var a = ix + gridX1 * iy;
  71. var b = ix + gridX1 * ( iy + 1 );
  72. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  73. var d = ( ix + 1 ) + gridX1 * iy;
  74. scope.faces.push( new THREE.Face4( a + offset, b + offset, c + offset, d + offset, null, null, material ) );
  75. scope.faceVertexUvs[ 0 ].push( [
  76. new THREE.UV( ix / gridX, iy / gridY ),
  77. new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
  78. new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
  79. new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
  80. ] );
  81. }
  82. }
  83. }
  84. function mergeVertices() {
  85. var unique = [], changes = [];
  86. for ( var i = 0, il = scope.vertices.length; i < il; i ++ ) {
  87. var v = scope.vertices[ i ],
  88. duplicate = false;
  89. for ( var j = 0, jl = unique.length; j < jl; j ++ ) {
  90. var vu = unique[ j ];
  91. if( v.position.x == vu.position.x && v.position.y == vu.position.y && v.position.z == vu.position.z ) {
  92. changes[ i ] = j;
  93. duplicate = true;
  94. break;
  95. }
  96. }
  97. if ( ! duplicate ) {
  98. changes[ i ] = unique.length;
  99. unique.push( new THREE.Vertex( v.position.clone() ) );
  100. }
  101. }
  102. for ( i = 0, il = scope.faces.length; i < il; i ++ ) {
  103. var face = scope.faces[ i ];
  104. face.a = changes[ face.a ];
  105. face.b = changes[ face.b ];
  106. face.c = changes[ face.c ];
  107. face.d = changes[ face.d ];
  108. }
  109. scope.vertices = unique;
  110. }
  111. this.computeCentroids();
  112. this.computeFaceNormals();
  113. };
  114. THREE.Cube.prototype = new THREE.Geometry();
  115. THREE.Cube.prototype.constructor = THREE.Cube;