Box3.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import { Vector3 } from './Vector3.js';
  2. class Box3 {
  3. constructor( min = new Vector3( + Infinity, + Infinity, + Infinity ), max = new Vector3( - Infinity, - Infinity, - Infinity ) ) {
  4. this.min = min;
  5. this.max = max;
  6. }
  7. set( min, max ) {
  8. this.min.copy( min );
  9. this.max.copy( max );
  10. return this;
  11. }
  12. setFromArray( array ) {
  13. let minX = + Infinity;
  14. let minY = + Infinity;
  15. let minZ = + Infinity;
  16. let maxX = - Infinity;
  17. let maxY = - Infinity;
  18. let maxZ = - Infinity;
  19. for ( let i = 0, l = array.length; i < l; i += 3 ) {
  20. const x = array[ i ];
  21. const y = array[ i + 1 ];
  22. const z = array[ i + 2 ];
  23. if ( x < minX ) minX = x;
  24. if ( y < minY ) minY = y;
  25. if ( z < minZ ) minZ = z;
  26. if ( x > maxX ) maxX = x;
  27. if ( y > maxY ) maxY = y;
  28. if ( z > maxZ ) maxZ = z;
  29. }
  30. this.min.set( minX, minY, minZ );
  31. this.max.set( maxX, maxY, maxZ );
  32. return this;
  33. }
  34. setFromBufferAttribute( attribute ) {
  35. let minX = + Infinity;
  36. let minY = + Infinity;
  37. let minZ = + Infinity;
  38. let maxX = - Infinity;
  39. let maxY = - Infinity;
  40. let maxZ = - Infinity;
  41. for ( let i = 0, l = attribute.count; i < l; i ++ ) {
  42. const x = attribute.getX( i );
  43. const y = attribute.getY( i );
  44. const z = attribute.getZ( i );
  45. if ( x < minX ) minX = x;
  46. if ( y < minY ) minY = y;
  47. if ( z < minZ ) minZ = z;
  48. if ( x > maxX ) maxX = x;
  49. if ( y > maxY ) maxY = y;
  50. if ( z > maxZ ) maxZ = z;
  51. }
  52. this.min.set( minX, minY, minZ );
  53. this.max.set( maxX, maxY, maxZ );
  54. return this;
  55. }
  56. setFromPoints( points ) {
  57. this.makeEmpty();
  58. for ( let i = 0, il = points.length; i < il; i ++ ) {
  59. this.expandByPoint( points[ i ] );
  60. }
  61. return this;
  62. }
  63. setFromCenterAndSize( center, size ) {
  64. const halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
  65. this.min.copy( center ).sub( halfSize );
  66. this.max.copy( center ).add( halfSize );
  67. return this;
  68. }
  69. setFromObject( object ) {
  70. this.makeEmpty();
  71. return this.expandByObject( object );
  72. }
  73. clone() {
  74. return new this.constructor().copy( this );
  75. }
  76. copy( box ) {
  77. this.min.copy( box.min );
  78. this.max.copy( box.max );
  79. return this;
  80. }
  81. makeEmpty() {
  82. this.min.x = this.min.y = this.min.z = + Infinity;
  83. this.max.x = this.max.y = this.max.z = - Infinity;
  84. return this;
  85. }
  86. isEmpty() {
  87. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  88. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  89. }
  90. getCenter( target ) {
  91. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  92. }
  93. getSize( target ) {
  94. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
  95. }
  96. expandByPoint( point ) {
  97. this.min.min( point );
  98. this.max.max( point );
  99. return this;
  100. }
  101. expandByVector( vector ) {
  102. this.min.sub( vector );
  103. this.max.add( vector );
  104. return this;
  105. }
  106. expandByScalar( scalar ) {
  107. this.min.addScalar( - scalar );
  108. this.max.addScalar( scalar );
  109. return this;
  110. }
  111. expandByObject( object ) {
  112. // Computes the world-axis-aligned bounding box of an object (including its children),
  113. // accounting for both the object's, and children's, world transforms
  114. object.updateWorldMatrix( false, false );
  115. const geometry = object.geometry;
  116. if ( geometry !== undefined ) {
  117. if ( geometry.boundingBox === null ) {
  118. geometry.computeBoundingBox();
  119. }
  120. _box.copy( geometry.boundingBox );
  121. _box.applyMatrix4( object.matrixWorld );
  122. this.union( _box );
  123. }
  124. const children = object.children;
  125. for ( let i = 0, l = children.length; i < l; i ++ ) {
  126. this.expandByObject( children[ i ] );
  127. }
  128. return this;
  129. }
  130. containsPoint( point ) {
  131. return point.x < this.min.x || point.x > this.max.x ||
  132. point.y < this.min.y || point.y > this.max.y ||
  133. point.z < this.min.z || point.z > this.max.z ? false : true;
  134. }
  135. containsBox( box ) {
  136. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  137. this.min.y <= box.min.y && box.max.y <= this.max.y &&
  138. this.min.z <= box.min.z && box.max.z <= this.max.z;
  139. }
  140. getParameter( point, target ) {
  141. // This can potentially have a divide by zero if the box
  142. // has a size dimension of 0.
  143. return target.set(
  144. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  145. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  146. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  147. );
  148. }
  149. intersectsBox( box ) {
  150. // using 6 splitting planes to rule out intersections.
  151. return box.max.x < this.min.x || box.min.x > this.max.x ||
  152. box.max.y < this.min.y || box.min.y > this.max.y ||
  153. box.max.z < this.min.z || box.min.z > this.max.z ? false : true;
  154. }
  155. intersectsSphere( sphere ) {
  156. // Find the point on the AABB closest to the sphere center.
  157. this.clampPoint( sphere.center, _vector );
  158. // If that point is inside the sphere, the AABB and sphere intersect.
  159. return _vector.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  160. }
  161. intersectsPlane( plane ) {
  162. // We compute the minimum and maximum dot product values. If those values
  163. // are on the same side (back or front) of the plane, then there is no intersection.
  164. let min, max;
  165. if ( plane.normal.x > 0 ) {
  166. min = plane.normal.x * this.min.x;
  167. max = plane.normal.x * this.max.x;
  168. } else {
  169. min = plane.normal.x * this.max.x;
  170. max = plane.normal.x * this.min.x;
  171. }
  172. if ( plane.normal.y > 0 ) {
  173. min += plane.normal.y * this.min.y;
  174. max += plane.normal.y * this.max.y;
  175. } else {
  176. min += plane.normal.y * this.max.y;
  177. max += plane.normal.y * this.min.y;
  178. }
  179. if ( plane.normal.z > 0 ) {
  180. min += plane.normal.z * this.min.z;
  181. max += plane.normal.z * this.max.z;
  182. } else {
  183. min += plane.normal.z * this.max.z;
  184. max += plane.normal.z * this.min.z;
  185. }
  186. return ( min <= - plane.constant && max >= - plane.constant );
  187. }
  188. intersectsTriangle( triangle ) {
  189. if ( this.isEmpty() ) {
  190. return false;
  191. }
  192. // compute box center and extents
  193. this.getCenter( _center );
  194. _extents.subVectors( this.max, _center );
  195. // translate triangle to aabb origin
  196. _v0.subVectors( triangle.a, _center );
  197. _v1.subVectors( triangle.b, _center );
  198. _v2.subVectors( triangle.c, _center );
  199. // compute edge vectors for triangle
  200. _f0.subVectors( _v1, _v0 );
  201. _f1.subVectors( _v2, _v1 );
  202. _f2.subVectors( _v0, _v2 );
  203. // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
  204. // make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
  205. // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
  206. let axes = [
  207. 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
  208. _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
  209. - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
  210. ];
  211. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  212. return false;
  213. }
  214. // test 3 face normals from the aabb
  215. axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
  216. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  217. return false;
  218. }
  219. // finally testing the face normal of the triangle
  220. // use already existing triangle edge vectors here
  221. _triangleNormal.crossVectors( _f0, _f1 );
  222. axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
  223. return satForAxes( axes, _v0, _v1, _v2, _extents );
  224. }
  225. clampPoint( point, target ) {
  226. return target.copy( point ).clamp( this.min, this.max );
  227. }
  228. distanceToPoint( point ) {
  229. const clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
  230. return clampedPoint.sub( point ).length();
  231. }
  232. getBoundingSphere( target ) {
  233. this.getCenter( target.center );
  234. target.radius = this.getSize( _vector ).length() * 0.5;
  235. return target;
  236. }
  237. intersect( box ) {
  238. this.min.max( box.min );
  239. this.max.min( box.max );
  240. // ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values.
  241. if ( this.isEmpty() ) this.makeEmpty();
  242. return this;
  243. }
  244. union( box ) {
  245. this.min.min( box.min );
  246. this.max.max( box.max );
  247. return this;
  248. }
  249. applyMatrix4( matrix ) {
  250. // transform of empty box is an empty box.
  251. if ( this.isEmpty() ) return this;
  252. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  253. _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  254. _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  255. _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  256. _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  257. _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  258. _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  259. _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  260. _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  261. this.setFromPoints( _points );
  262. return this;
  263. }
  264. translate( offset ) {
  265. this.min.add( offset );
  266. this.max.add( offset );
  267. return this;
  268. }
  269. equals( box ) {
  270. return box.min.equals( this.min ) && box.max.equals( this.max );
  271. }
  272. }
  273. Box3.prototype.isBox3 = true;
  274. const _points = [
  275. /*@__PURE__*/ new Vector3(),
  276. /*@__PURE__*/ new Vector3(),
  277. /*@__PURE__*/ new Vector3(),
  278. /*@__PURE__*/ new Vector3(),
  279. /*@__PURE__*/ new Vector3(),
  280. /*@__PURE__*/ new Vector3(),
  281. /*@__PURE__*/ new Vector3(),
  282. /*@__PURE__*/ new Vector3()
  283. ];
  284. const _vector = /*@__PURE__*/ new Vector3();
  285. const _box = /*@__PURE__*/ new Box3();
  286. // triangle centered vertices
  287. const _v0 = /*@__PURE__*/ new Vector3();
  288. const _v1 = /*@__PURE__*/ new Vector3();
  289. const _v2 = /*@__PURE__*/ new Vector3();
  290. // triangle edge vectors
  291. const _f0 = /*@__PURE__*/ new Vector3();
  292. const _f1 = /*@__PURE__*/ new Vector3();
  293. const _f2 = /*@__PURE__*/ new Vector3();
  294. const _center = /*@__PURE__*/ new Vector3();
  295. const _extents = /*@__PURE__*/ new Vector3();
  296. const _triangleNormal = /*@__PURE__*/ new Vector3();
  297. const _testAxis = /*@__PURE__*/ new Vector3();
  298. function satForAxes( axes, v0, v1, v2, extents ) {
  299. for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) {
  300. _testAxis.fromArray( axes, i );
  301. // project the aabb onto the seperating axis
  302. const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
  303. // project all 3 vertices of the triangle onto the seperating axis
  304. const p0 = v0.dot( _testAxis );
  305. const p1 = v1.dot( _testAxis );
  306. const p2 = v2.dot( _testAxis );
  307. // actual test, basically see if either of the most extreme of the triangle points intersects r
  308. if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
  309. // points of the projected triangle are outside the projected half-length of the aabb
  310. // the axis is seperating and we can exit
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. export { Box3 };
粤ICP备19079148号