BoxGeometry2.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. THREE.BoxGeometry2 = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  6. var scope = this;
  7. this.width = width;
  8. this.height = height;
  9. this.depth = depth;
  10. this.widthSegments = widthSegments || 1;
  11. this.heightSegments = heightSegments || 1;
  12. this.depthSegments = depthSegments || 1;
  13. var width_half = this.width / 2;
  14. var height_half = this.height / 2;
  15. var depth_half = this.depth / 2;
  16. var vector = new THREE.Vector3();
  17. var vectors = [];
  18. var vertices = [];
  19. var addVertex = function ( a, b, c ) {
  20. vertices.push( vectors[ a ], vectors[ a + 1 ], vectors[ a + 2 ] );
  21. vertices.push( vectors[ b ], vectors[ b + 1 ], vectors[ b + 2 ] );
  22. vertices.push( vectors[ c ], vectors[ c + 1 ], vectors[ c + 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. function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) {
  31. var w, ix, iy,
  32. gridX = scope.widthSegments,
  33. gridY = scope.heightSegments,
  34. width_half = width / 2,
  35. height_half = height / 2,
  36. offset = vectors.length;
  37. if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
  38. w = 'z';
  39. } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
  40. w = 'y';
  41. gridY = scope.depthSegments;
  42. } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
  43. w = 'x';
  44. gridX = scope.depthSegments;
  45. }
  46. var gridX1 = gridX + 1,
  47. gridY1 = gridY + 1,
  48. segment_width = width / gridX,
  49. segment_height = height / gridY,
  50. normal = new THREE.Vector3();
  51. normal[ w ] = depth > 0 ? 1 : - 1;
  52. for ( iy = 0; iy < gridY1; iy ++ ) {
  53. for ( ix = 0; ix < gridX1; ix ++ ) {
  54. vector[ u ] = ( ix * segment_width - width_half ) * udir;
  55. vector[ v ] = ( iy * segment_height - height_half ) * vdir;
  56. vector[ w ] = depth;
  57. vectors.push( vector.x, vector.y, vector.z );
  58. }
  59. }
  60. for ( iy = 0; iy < gridY; iy++ ) {
  61. for ( ix = 0; ix < gridX; ix++ ) {
  62. var a = ix + gridX1 * iy;
  63. var b = ix + gridX1 * ( iy + 1 );
  64. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  65. var d = ( ix + 1 ) + gridX1 * iy;
  66. addVertex( a * 3 + offset, b * 3 + offset, d * 3 + offset );
  67. addVertex( b * 3 + offset, c * 3 + offset, d * 3 + offset );
  68. }
  69. }
  70. }
  71. THREE.Geometry2.call( this, vertices.length / 3 );
  72. this.vertices.set( vertices );
  73. };
  74. THREE.BoxGeometry2.prototype = Object.create( THREE.Geometry2.prototype );
  75. THREE.BoxGeometry2.prototype.constructor = THREE.BoxGeometry2;