BoxGeometry.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.BoxGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  6. THREE.Geometry.call( this );
  7. this.type = 'BoxGeometry';
  8. this.parameters = {
  9. width: width,
  10. height: height,
  11. depth: depth,
  12. widthSegments: widthSegments,
  13. heightSegments: heightSegments,
  14. depthSegments: depthSegments
  15. };
  16. this.widthSegments = widthSegments || 1;
  17. this.heightSegments = heightSegments || 1;
  18. this.depthSegments = depthSegments || 1;
  19. var scope = this;
  20. var width_half = width / 2;
  21. var height_half = height / 2;
  22. var depth_half = depth / 2;
  23. buildPlane( 'z', 'y', - 1, - 1, depth, height, width_half, 0 ); // px
  24. buildPlane( 'z', 'y', 1, - 1, depth, height, - width_half, 1 ); // nx
  25. buildPlane( 'x', 'z', 1, 1, width, depth, height_half, 2 ); // py
  26. buildPlane( 'x', 'z', 1, - 1, width, depth, - height_half, 3 ); // ny
  27. buildPlane( 'x', 'y', 1, - 1, width, height, depth_half, 4 ); // pz
  28. buildPlane( 'x', 'y', - 1, - 1, width, height, - depth_half, 5 ); // nz
  29. function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) {
  30. var w, ix, iy,
  31. gridX = scope.widthSegments,
  32. gridY = scope.heightSegments,
  33. width_half = width / 2,
  34. height_half = height / 2,
  35. offset = scope.vertices.length;
  36. if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
  37. w = 'z';
  38. } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
  39. w = 'y';
  40. gridY = scope.depthSegments;
  41. } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
  42. w = 'x';
  43. gridX = scope.depthSegments;
  44. }
  45. var gridX1 = gridX + 1,
  46. gridY1 = gridY + 1,
  47. segment_width = width / gridX,
  48. segment_height = height / gridY,
  49. normal = new THREE.Vector3();
  50. normal[ w ] = depth > 0 ? 1 : - 1;
  51. for ( iy = 0; iy < gridY1; iy ++ ) {
  52. for ( ix = 0; ix < gridX1; ix ++ ) {
  53. var vector = new THREE.Vector3();
  54. vector[ u ] = ( ix * segment_width - width_half ) * udir;
  55. vector[ v ] = ( iy * segment_height - height_half ) * vdir;
  56. vector[ w ] = depth;
  57. scope.vertices.push( vector );
  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. var uva = new THREE.Vector2( ix / gridX, 1 - iy / gridY );
  67. var uvb = new THREE.Vector2( ix / gridX, 1 - ( iy + 1 ) / gridY );
  68. var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iy + 1 ) / gridY );
  69. var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iy / gridY );
  70. var face = new THREE.Face3( a + offset, b + offset, d + offset );
  71. face.normal.copy( normal );
  72. face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
  73. face.materialIndex = materialIndex;
  74. scope.faces.push( face );
  75. scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );
  76. face = new THREE.Face3( b + offset, c + offset, d + offset );
  77. face.normal.copy( normal );
  78. face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
  79. face.materialIndex = materialIndex;
  80. scope.faces.push( face );
  81. scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );
  82. }
  83. }
  84. }
  85. this.mergeVertices();
  86. };
  87. THREE.BoxGeometry.prototype = Object.create( THREE.Geometry.prototype );
  88. THREE.BoxGeometry.prototype.constructor = THREE.BoxGeometry;
粤ICP备19079148号