BoxBufferGeometry.js 5.4 KB

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