1
0

RoundedBoxGeometry.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. class RoundedBoxGeometry extends BoxGeometry {
  28. constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
  29. // ensure segments is odd so we have a plane connecting the rounded corners
  30. segments = segments * 2 + 1;
  31. // ensure radius isn't bigger than shortest side
  32. radius = Math.min( width / 2, height / 2, depth / 2, radius );
  33. super( 1, 1, 1, segments, segments, segments );
  34. // if we just have one segment we're the same as a regular box
  35. if ( segments === 1 ) return;
  36. const geometry2 = this.toNonIndexed();
  37. this.index = null;
  38. this.attributes.position = geometry2.attributes.position;
  39. this.attributes.normal = geometry2.attributes.normal;
  40. this.attributes.uv = geometry2.attributes.uv;
  41. //
  42. const position = new Vector3();
  43. const normal = new Vector3();
  44. const box = new Vector3( width, height, depth ).divideScalar( 2 ).subScalar( radius );
  45. const positions = this.attributes.position.array;
  46. const normals = this.attributes.normal.array;
  47. const uvs = this.attributes.uv.array;
  48. const faceTris = positions.length / 6;
  49. const faceDirVector = new Vector3();
  50. const halfSegmentSize = 0.5 / segments;
  51. for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
  52. position.fromArray( positions, i );
  53. normal.copy( position );
  54. normal.x -= Math.sign( normal.x ) * halfSegmentSize;
  55. normal.y -= Math.sign( normal.y ) * halfSegmentSize;
  56. normal.z -= Math.sign( normal.z ) * halfSegmentSize;
  57. normal.normalize();
  58. positions[ i + 0 ] = box.x * Math.sign( position.x ) + normal.x * radius;
  59. positions[ i + 1 ] = box.y * Math.sign( position.y ) + normal.y * radius;
  60. positions[ i + 2 ] = box.z * Math.sign( position.z ) + normal.z * radius;
  61. normals[ i + 0 ] = normal.x;
  62. normals[ i + 1 ] = normal.y;
  63. normals[ i + 2 ] = normal.z;
  64. const side = Math.floor( i / faceTris );
  65. switch ( side ) {
  66. case 0: // right
  67. // generate UVs along Z then Y
  68. faceDirVector.set( 1, 0, 0 );
  69. uvs[ j + 0 ] = getUv( faceDirVector, normal, 'z', 'y', radius, depth );
  70. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
  71. break;
  72. case 1: // left
  73. // generate UVs along Z then Y
  74. faceDirVector.set( - 1, 0, 0 );
  75. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'y', radius, depth );
  76. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
  77. break;
  78. case 2: // top
  79. // generate UVs along X then Z
  80. faceDirVector.set( 0, 1, 0 );
  81. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
  82. uvs[ j + 1 ] = getUv( faceDirVector, normal, 'z', 'x', radius, depth );
  83. break;
  84. case 3: // bottom
  85. // generate UVs along X then Z
  86. faceDirVector.set( 0, - 1, 0 );
  87. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
  88. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'x', radius, depth );
  89. break;
  90. case 4: // front
  91. // generate UVs along X then Y
  92. faceDirVector.set( 0, 0, 1 );
  93. uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'y', radius, width );
  94. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
  95. break;
  96. case 5: // back
  97. // generate UVs along X then Y
  98. faceDirVector.set( 0, 0, - 1 );
  99. uvs[ j + 0 ] = getUv( faceDirVector, normal, 'x', 'y', radius, width );
  100. uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. export { RoundedBoxGeometry };