Box3.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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, precise = false ) {
  70. this.makeEmpty();
  71. return this.expandByObject( object, precise );
  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, precise = false ) {
  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 ( precise && geometry.attributes != undefined && geometry.attributes.position !== undefined ) {
  118. const position = geometry.attributes.position;
  119. for ( let i = 0, l = position.count; i < l; i ++ ) {
  120. _vector.fromBufferAttribute( position, i ).applyMatrix4( object.matrixWorld );
  121. this.expandByPoint( _vector );
  122. }
  123. } else {
  124. if ( geometry.boundingBox === null ) {
  125. geometry.computeBoundingBox();
  126. }
  127. _box.copy( geometry.boundingBox );
  128. _box.applyMatrix4( object.matrixWorld );
  129. this.union( _box );
  130. }
  131. }
  132. const children = object.children;
  133. for ( let i = 0, l = children.length; i < l; i ++ ) {
  134. this.expandByObject( children[ i ], precise );
  135. }
  136. return this;
  137. }
  138. containsPoint( point ) {
  139. return point.x < this.min.x || point.x > this.max.x ||
  140. point.y < this.min.y || point.y > this.max.y ||
  141. point.z < this.min.z || point.z > this.max.z ? false : true;
  142. }
  143. containsBox( box ) {
  144. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  145. this.min.y <= box.min.y && box.max.y <= this.max.y &&
  146. this.min.z <= box.min.z && box.max.z <= this.max.z;
  147. }
  148. getParameter( point, target ) {
  149. // This can potentially have a divide by zero if the box
  150. // has a size dimension of 0.
  151. return target.set(
  152. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  153. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  154. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  155. );
  156. }
  157. intersectsBox( box ) {
  158. // using 6 splitting planes to rule out intersections.
  159. return box.max.x < this.min.x || box.min.x > this.max.x ||
  160. box.max.y < this.min.y || box.min.y > this.max.y ||
  161. box.max.z < this.min.z || box.min.z > this.max.z ? false : true;
  162. }
  163. intersectsSphere( sphere ) {
  164. // Find the point on the AABB closest to the sphere center.
  165. this.clampPoint( sphere.center, _vector );
  166. // If that point is inside the sphere, the AABB and sphere intersect.
  167. return _vector.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  168. }
  169. intersectsPlane( plane ) {
  170. // We compute the minimum and maximum dot product values. If those values
  171. // are on the same side (back or front) of the plane, then there is no intersection.
  172. let min, max;
  173. if ( plane.normal.x > 0 ) {
  174. min = plane.normal.x * this.min.x;
  175. max = plane.normal.x * this.max.x;
  176. } else {
  177. min = plane.normal.x * this.max.x;
  178. max = plane.normal.x * this.min.x;
  179. }
  180. if ( plane.normal.y > 0 ) {
  181. min += plane.normal.y * this.min.y;
  182. max += plane.normal.y * this.max.y;
  183. } else {
  184. min += plane.normal.y * this.max.y;
  185. max += plane.normal.y * this.min.y;
  186. }
  187. if ( plane.normal.z > 0 ) {
  188. min += plane.normal.z * this.min.z;
  189. max += plane.normal.z * this.max.z;
  190. } else {
  191. min += plane.normal.z * this.max.z;
  192. max += plane.normal.z * this.min.z;
  193. }
  194. return ( min <= - plane.constant && max >= - plane.constant );
  195. }
  196. intersectsTriangle( triangle ) {
  197. if ( this.isEmpty() ) {
  198. return false;
  199. }
  200. // compute box center and extents
  201. this.getCenter( _center );
  202. _extents.subVectors( this.max, _center );
  203. // translate triangle to aabb origin
  204. _v0.subVectors( triangle.a, _center );
  205. _v1.subVectors( triangle.b, _center );
  206. _v2.subVectors( triangle.c, _center );
  207. // compute edge vectors for triangle
  208. _f0.subVectors( _v1, _v0 );
  209. _f1.subVectors( _v2, _v1 );
  210. _f2.subVectors( _v0, _v2 );
  211. // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
  212. // 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
  213. // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
  214. let axes = [
  215. 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
  216. _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
  217. - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
  218. ];
  219. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  220. return false;
  221. }
  222. // test 3 face normals from the aabb
  223. axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
  224. if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) {
  225. return false;
  226. }
  227. // finally testing the face normal of the triangle
  228. // use already existing triangle edge vectors here
  229. _triangleNormal.crossVectors( _f0, _f1 );
  230. axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
  231. return satForAxes( axes, _v0, _v1, _v2, _extents );
  232. }
  233. clampPoint( point, target ) {
  234. return target.copy( point ).clamp( this.min, this.max );
  235. }
  236. distanceToPoint( point ) {
  237. const clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
  238. return clampedPoint.sub( point ).length();
  239. }
  240. getBoundingSphere( target ) {
  241. this.getCenter( target.center );
  242. target.radius = this.getSize( _vector ).length() * 0.5;
  243. return target;
  244. }
  245. intersect( box ) {
  246. this.min.max( box.min );
  247. this.max.min( box.max );
  248. // 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.
  249. if ( this.isEmpty() ) this.makeEmpty();
  250. return this;
  251. }
  252. union( box ) {
  253. this.min.min( box.min );
  254. this.max.max( box.max );
  255. return this;
  256. }
  257. applyMatrix4( matrix ) {
  258. // transform of empty box is an empty box.
  259. if ( this.isEmpty() ) return this;
  260. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  261. _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  262. _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  263. _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  264. _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  265. _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  266. _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  267. _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  268. _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  269. this.setFromPoints( _points );
  270. return this;
  271. }
  272. translate( offset ) {
  273. this.min.add( offset );
  274. this.max.add( offset );
  275. return this;
  276. }
  277. equals( box ) {
  278. return box.min.equals( this.min ) && box.max.equals( this.max );
  279. }
  280. }
  281. Box3.prototype.isBox3 = true;
  282. const _points = [
  283. /*@__PURE__*/ new Vector3(),
  284. /*@__PURE__*/ new Vector3(),
  285. /*@__PURE__*/ new Vector3(),
  286. /*@__PURE__*/ new Vector3(),
  287. /*@__PURE__*/ new Vector3(),
  288. /*@__PURE__*/ new Vector3(),
  289. /*@__PURE__*/ new Vector3(),
  290. /*@__PURE__*/ new Vector3()
  291. ];
  292. const _vector = /*@__PURE__*/ new Vector3();
  293. const _box = /*@__PURE__*/ new Box3();
  294. // triangle centered vertices
  295. const _v0 = /*@__PURE__*/ new Vector3();
  296. const _v1 = /*@__PURE__*/ new Vector3();
  297. const _v2 = /*@__PURE__*/ new Vector3();
  298. // triangle edge vectors
  299. const _f0 = /*@__PURE__*/ new Vector3();
  300. const _f1 = /*@__PURE__*/ new Vector3();
  301. const _f2 = /*@__PURE__*/ new Vector3();
  302. const _center = /*@__PURE__*/ new Vector3();
  303. const _extents = /*@__PURE__*/ new Vector3();
  304. const _triangleNormal = /*@__PURE__*/ new Vector3();
  305. const _testAxis = /*@__PURE__*/ new Vector3();
  306. function satForAxes( axes, v0, v1, v2, extents ) {
  307. for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) {
  308. _testAxis.fromArray( axes, i );
  309. // project the aabb onto the seperating axis
  310. const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
  311. // project all 3 vertices of the triangle onto the seperating axis
  312. const p0 = v0.dot( _testAxis );
  313. const p1 = v1.dot( _testAxis );
  314. const p2 = v2.dot( _testAxis );
  315. // actual test, basically see if either of the most extreme of the triangle points intersects r
  316. if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
  317. // points of the projected triangle are outside the projected half-length of the aabb
  318. // the axis is seperating and we can exit
  319. return false;
  320. }
  321. }
  322. return true;
  323. }
  324. export { Box3 };
粤ICP备19079148号