RoundedBoxGeometry.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. BoxGeometry,
  3. Vector3
  4. } from 'three';
  5. const _tempNormal = new Vector3();
  6. function getUv( faceDirVector, normal, uvAxis, projectionAxis, radius, sideLength ) {
  7. const totArcLength = 2 * Math.PI * radius / 4;
  8. // length of the planes between the arcs on each axis
  9. const centerLength = Math.max( sideLength - 2 * radius, 0 );
  10. const halfArc = Math.PI / 4;
  11. // Get the vector projected onto the Y plane
  12. _tempNormal.copy( normal );
  13. _tempNormal[ projectionAxis ] = 0;
  14. _tempNormal.normalize();
  15. // total amount of UV space alloted to a single arc
  16. const arcUvRatio = 0.5 * totArcLength / ( totArcLength + centerLength );
  17. // the distance along one arc the point is at
  18. const arcAngleRatio = 1.0 - ( _tempNormal.angleTo( faceDirVector ) / halfArc );
  19. if ( Math.sign( _tempNormal[ uvAxis ] ) === 1 ) {
  20. return arcAngleRatio * arcUvRatio;
  21. } else {
  22. // total amount of UV space alloted to the plane between the arcs
  23. const lenUv = centerLength / ( totArcLength + centerLength );
  24. return lenUv + arcUvRatio + arcUvRatio * ( 1.0 - arcAngleRatio );
  25. }
  26. }
  27. /**
  28. * A special type of box geometry with rounded corners and edges.
  29. *
  30. * ```js
  31. * const geometry = new THREE.RoundedBoxGeometry();
  32. * const material = new THREE.MeshStandardMaterial( { color: 0x00ff00 } );
  33. * const cube = new THREE.Mesh( geometry, material );
  34. * scene.add( cube );
  35. * ```
  36. *
  37. * @augments BoxGeometry
  38. * @three_import import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  39. */
  40. class RoundedBoxGeometry extends BoxGeometry {
  41. /**
  42. * Constructs a new rounded box geometry.
  43. *
  44. * @param {number} [width=1] - The width. That is, the length of the edges parallel to the X axis.
  45. * @param {number} [height=1] - The height. That is, the length of the edges parallel to the Y axis.
  46. * @param {number} [depth=1] - The depth. That is, the length of the edges parallel to the Z axis.
  47. * @param {number} [segments=2] - Number of segmented that form the rounded corners.
  48. * @param {number} [radius=0.1] - The radius of the rounded corners.
  49. */
  50. constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
  51. // ensure segments is odd so we have a plane connecting the rounded corners
  52. segments = segments * 2 + 1;
  53. // ensure radius isn't bigger than shortest side
  54. radius = Math.min( width / 2, height / 2, depth / 2, radius );
  55. super( 1, 1, 1, segments, segments, segments );
  56. // if we just have one segment we're the same as a regular box
  57. if ( segments === 1 ) return;
  58. const geometry2 = this.toNonIndexed();
  59. this.index = null;
  60. this.attributes.position = geometry2.attributes.position;
  61. this.attributes.normal = geometry2.attributes.normal;
  62. this.attributes.uv = geometry2.attributes.uv;
  63. //
  64. const position = new Vector3();
  65. const normal = new Vector3();
  66. const box = new Vector3( width, height, depth ).divideScalar( 2 ).subScalar( radius );
  67. const positions = this.attributes.position.array;
  68. const normals = this.attributes.normal.array;
  69. const uvs = this.attributes.uv.array;
  70. const faceTris = positions.length / 6;
  71. const faceDirVector = new Vector3();
  72. const halfSegmentSize = 0.5 / segments;
  73. for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
  74. position.fromArray( positions, i );
  75. normal.copy( position );
  76. normal.x -= Math.sign( normal.x ) * halfSegmentSize;
  77. normal.y -= Math.sign( normal.y ) * halfSegmentSize;
  78. normal.z -= Math.sign( normal.z ) * halfSegmentSize;
  79. normal.normalize();
  80. positions[ i + 0 ] = box.x * Math.sign( position.x ) + normal.x * radius;
  81. positions[ i + 1 ] = box.y * Math.sign( position.y ) + normal.y * radius;
  82. positions[ i + 2 ] = box.z * Math.sign( position.z ) + normal.z * radius;
  83. normals[ i + 0 ] = normal.x;
  84. normals[ i + 1 ] = normal.y;
  85. normals[ i + 2 ] = normal.z;
  86. const side = Math.floor( i / faceTris );
  87. switch ( side ) {
  88. case 0: // right
  89. // generate UVs along Z then Y
  90. faceDirVector.set( 1, 0, 0 );
  91. uvs[ j + 0 ] = getUv( faceDirVector, normal, 'z', 'y', radius, depth );
  92. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
  93. break;
  94. case 1: // left
  95. // generate UVs along Z then Y
  96. faceDirVector.set( - 1, 0, 0 );
  97. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'y', radius, depth );
  98. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
  99. break;
  100. case 2: // top
  101. // generate UVs along X then Z
  102. faceDirVector.set( 0, 1, 0 );
  103. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
  104. uvs[ j + 1 ] = getUv( faceDirVector, normal, 'z', 'x', radius, depth );
  105. break;
  106. case 3: // bottom
  107. // generate UVs along X then Z
  108. faceDirVector.set( 0, - 1, 0 );
  109. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
  110. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'x', radius, depth );
  111. break;
  112. case 4: // front
  113. // generate UVs along X then Y
  114. faceDirVector.set( 0, 0, 1 );
  115. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'y', radius, width );
  116. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
  117. break;
  118. case 5: // back
  119. // generate UVs along X then Y
  120. faceDirVector.set( 0, 0, - 1 );
  121. uvs[ j + 0 ] = getUv( faceDirVector, normal, 'x', 'y', radius, width );
  122. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. export { RoundedBoxGeometry };