Box3.js 11 KB

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