BoxGeometry.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. import { Geometry } from '../core/Geometry';
  6. import { BufferGeometry } from '../core/BufferGeometry';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute';
  8. import { Vector3 } from '../math/Vector3';
  9. // BoxGeometry
  10. function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  11. Geometry.call( this );
  12. this.type = 'BoxGeometry';
  13. this.parameters = {
  14. width: width,
  15. height: height,
  16. depth: depth,
  17. widthSegments: widthSegments,
  18. heightSegments: heightSegments,
  19. depthSegments: depthSegments
  20. };
  21. this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) );
  22. this.mergeVertices();
  23. }
  24. BoxGeometry.prototype = Object.create( Geometry.prototype );
  25. BoxGeometry.prototype.constructor = BoxGeometry;
  26. // BoxBufferGeometry
  27. function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  28. BufferGeometry.call( this );
  29. this.type = 'BoxBufferGeometry';
  30. this.parameters = {
  31. width: width,
  32. height: height,
  33. depth: depth,
  34. widthSegments: widthSegments,
  35. heightSegments: heightSegments,
  36. depthSegments: depthSegments
  37. };
  38. var scope = this;
  39. // segments
  40. widthSegments = Math.floor( widthSegments ) || 1;
  41. heightSegments = Math.floor( heightSegments ) || 1;
  42. depthSegments = Math.floor( depthSegments ) || 1;
  43. // buffers
  44. var indices = [];
  45. var vertices = [];
  46. var normals = [];
  47. var uvs = [];
  48. // helper variables
  49. var numberOfVertices = 0;
  50. var groupStart = 0;
  51. // build each side of the box geometry
  52. buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
  53. buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
  54. buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
  55. buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
  56. buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
  57. buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
  58. // build geometry
  59. this.setIndex( indices );
  60. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  61. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  62. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  63. function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
  64. var segmentWidth = width / gridX;
  65. var segmentHeight = height / gridY;
  66. var widthHalf = width / 2;
  67. var heightHalf = height / 2;
  68. var depthHalf = depth / 2;
  69. var gridX1 = gridX + 1;
  70. var gridY1 = gridY + 1;
  71. var vertexCounter = 0;
  72. var groupCount = 0;
  73. var ix, iy;
  74. var vector = new Vector3();
  75. // generate vertices, normals and uvs
  76. for ( iy = 0; iy < gridY1; iy ++ ) {
  77. var y = iy * segmentHeight - heightHalf;
  78. for ( ix = 0; ix < gridX1; ix ++ ) {
  79. var x = ix * segmentWidth - widthHalf;
  80. // set values to correct vector component
  81. vector[ u ] = x * udir;
  82. vector[ v ] = y * vdir;
  83. vector[ w ] = depthHalf;
  84. // now apply vector to vertex buffer
  85. vertices.push( vector.x, vector.y, vector.z );
  86. // set values to correct vector component
  87. vector[ u ] = 0;
  88. vector[ v ] = 0;
  89. vector[ w ] = depth > 0 ? 1 : - 1;
  90. // now apply vector to normal buffer
  91. normals.push( vector.x, vector.y, vector.z );
  92. // uvs
  93. uvs.push( ix / gridX );
  94. uvs.push( 1 - ( iy / gridY ) );
  95. // counters
  96. vertexCounter += 1;
  97. }
  98. }
  99. // indices
  100. // 1. you need three indices to draw a single face
  101. // 2. a single segment consists of two faces
  102. // 3. so we need to generate six (2*3) indices per segment
  103. for ( iy = 0; iy < gridY; iy ++ ) {
  104. for ( ix = 0; ix < gridX; ix ++ ) {
  105. var a = numberOfVertices + ix + gridX1 * iy;
  106. var b = numberOfVertices + ix + gridX1 * ( iy + 1 );
  107. var c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
  108. var d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
  109. // faces
  110. indices.push( a, b, d );
  111. indices.push( b, c, d );
  112. // increase counter
  113. groupCount += 6;
  114. }
  115. }
  116. // add a group to the geometry. this will ensure multi material support
  117. scope.addGroup( groupStart, groupCount, materialIndex );
  118. // calculate new start value for groups
  119. groupStart += groupCount;
  120. // update total number of vertices
  121. numberOfVertices += vertexCounter;
  122. }
  123. }
  124. BoxBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  125. BoxBufferGeometry.prototype.constructor = BoxBufferGeometry;
  126. export { BoxGeometry, BoxBufferGeometry };
粤ICP备19079148号