Cube.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. var Cube = function ( width, height, depth, segments_width, segments_height, materials, flipped ) {
  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. buildPlane( 'z', 'y', 1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px
  25. buildPlane( 'z', 'y', - 1 * flip, - 1, depth, height, width_half, this.materials[ 1 ] ); // nx
  26. buildPlane( 'x', 'z', 1 * flip, 1, width, depth, height_half, this.materials[ 2 ] ); // py
  27. buildPlane( 'x', 'z', 1 * flip, - 1, width, depth, - height_half, this.materials[ 3 ] ); // ny
  28. buildPlane( 'x', 'y', 1 * flip, - 1, width, height, depth_half, this.materials[ 4 ] ); // pz
  29. buildPlane( 'x', 'y', - 1 * flip, - 1, width, height, - depth_half, this.materials[ 5 ] ); // nz
  30. mergeVertices();
  31. function buildPlane( u, v, udir, vdir, width, height, depth, material ) {
  32. var gridX = segments_width || 1,
  33. gridY = segments_height || 1,
  34. gridX1 = gridX + 1,
  35. gridY1 = gridY + 1,
  36. segment_width = width / gridX,
  37. segment_height = height / gridY,
  38. offset = scope.vertices.length, w;
  39. if ( ( u == 'x' && v == 'y' ) || ( u == 'y' && v == 'x' ) ) {
  40. w = 'z';
  41. } else if ( ( u == 'x' && v == 'z' ) || ( u == 'z' && v == 'x' ) ) {
  42. w = 'y';
  43. } else if ( ( u == 'z' && v == 'y' ) || ( u == 'y' && v == 'z' ) ) {
  44. w = 'x';
  45. }
  46. for( iy = 0; iy < gridY1; iy++ ) {
  47. for( ix = 0; ix < gridX1; ix++ ) {
  48. var vector = new THREE.Vector3();
  49. vector[ u ] = ( ix * segment_width - width_half ) * udir;
  50. vector[ v ] = ( iy * segment_height - height_half ) * vdir;
  51. vector[ w ] = depth;
  52. scope.vertices.push( new THREE.Vertex( vector ) );
  53. }
  54. }
  55. for( iy = 0; iy < gridY; iy++ ) {
  56. for( ix = 0; ix < gridX; ix++ ) {
  57. var a = ix + gridX1 * iy;
  58. var b = ix + gridX1 * ( iy + 1 );
  59. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  60. var d = ( ix + 1 ) + gridX1 * iy;
  61. scope.faces.push( new THREE.Face4( a + offset, b + offset, c + offset, d + offset, null, material ) );
  62. scope.uvs.push( [
  63. new THREE.UV( ix / gridX, iy / gridY ),
  64. new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
  65. new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
  66. new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
  67. ] );
  68. }
  69. }
  70. }
  71. function mergeVertices() {
  72. var unique = [], changes = [];
  73. for ( var i = 0, il = scope.vertices.length; i < il; i ++ ) {
  74. var v = scope.vertices[ i ],
  75. duplicate = false;
  76. for ( var j = 0, jl = unique.length; j < jl; j ++ ) {
  77. var vu = unique[ j ];
  78. if( v.position.x == vu.position.x && v.position.y == vu.position.y && v.position.z == vu.position.z ) {
  79. changes[ i ] = j;
  80. duplicate = true;
  81. break;
  82. }
  83. }
  84. if ( ! duplicate ) {
  85. changes[ i ] = unique.length;
  86. unique.push( new THREE.Vertex( v.position.clone() ) );
  87. }
  88. }
  89. for ( var i = 0, l = scope.faces.length; i < l; i ++ ) {
  90. var face = scope.faces[ i ];
  91. face.a = changes[ face.a ];
  92. face.b = changes[ face.b ];
  93. face.c = changes[ face.c ];
  94. face.d = changes[ face.d ];
  95. }
  96. scope.vertices = unique;
  97. }
  98. this.computeCentroids();
  99. this.computeNormals();
  100. this.sortFacesByMaterial();
  101. }
  102. Cube.prototype = new THREE.Geometry();
  103. Cube.prototype.constructor = Cube;
粤ICP备19079148号