OBB.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. import {
  2. Box3,
  3. MathUtils,
  4. Matrix4,
  5. Matrix3,
  6. Ray,
  7. Vector3
  8. } from 'three';
  9. // module scope helper variables
  10. const a = {
  11. c: null, // center
  12. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  13. e: [] // half width
  14. };
  15. const b = {
  16. c: null, // center
  17. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  18. e: [] // half width
  19. };
  20. const R = [[], [], []];
  21. const AbsR = [[], [], []];
  22. const t = [];
  23. const xAxis = new Vector3();
  24. const yAxis = new Vector3();
  25. const zAxis = new Vector3();
  26. const v1 = new Vector3();
  27. const size = new Vector3();
  28. const closestPoint = new Vector3();
  29. const rotationMatrix = new Matrix3();
  30. const aabb = new Box3();
  31. const matrix = new Matrix4();
  32. const inverse = new Matrix4();
  33. const localRay = new Ray();
  34. /**
  35. * Represents an oriented bounding box (OBB) in 3D space.
  36. */
  37. class OBB {
  38. /**
  39. * Constructs a new OBB.
  40. *
  41. * @param {Vector3} [center] - The center of the OBB.
  42. * @param {Vector3} [halfSize] - Positive halfwidth extents of the OBB along each axis.
  43. * @param {Matrix3} [rotation] - The rotation of the OBB.
  44. */
  45. constructor( center = new Vector3(), halfSize = new Vector3(), rotation = new Matrix3() ) {
  46. /**
  47. * The center of the OBB.
  48. *
  49. * @type {Vector3}
  50. */
  51. this.center = center;
  52. /**
  53. * Positive halfwidth extents of the OBB along each axis.
  54. *
  55. * @type {Vector3}
  56. */
  57. this.halfSize = halfSize;
  58. /**
  59. * The rotation of the OBB.
  60. *
  61. * @type {Matrix3}
  62. */
  63. this.rotation = rotation;
  64. }
  65. /**
  66. * Sets the OBBs components to the given values.
  67. *
  68. * @param {Vector3} [center] - The center of the OBB.
  69. * @param {Vector3} [halfSize] - Positive halfwidth extents of the OBB along each axis.
  70. * @param {Matrix3} [rotation] - The rotation of the OBB.
  71. * @return {OBB} A reference to this OBB.
  72. */
  73. set( center, halfSize, rotation ) {
  74. this.center = center;
  75. this.halfSize = halfSize;
  76. this.rotation = rotation;
  77. return this;
  78. }
  79. /**
  80. * Copies the values of the given OBB to this instance.
  81. *
  82. * @param {OBB} obb - The OBB to copy.
  83. * @return {OBB} A reference to this OBB.
  84. */
  85. copy( obb ) {
  86. this.center.copy( obb.center );
  87. this.halfSize.copy( obb.halfSize );
  88. this.rotation.copy( obb.rotation );
  89. return this;
  90. }
  91. /**
  92. * Returns a new OBB with copied values from this instance.
  93. *
  94. * @return {OBB} A clone of this instance.
  95. */
  96. clone() {
  97. return new this.constructor().copy( this );
  98. }
  99. /**
  100. * Returns the size of this OBB.
  101. *
  102. * @param {Vector3} target - The target vector that is used to store the method's result.
  103. * @return {Vector3} The size.
  104. */
  105. getSize( target ) {
  106. return target.copy( this.halfSize ).multiplyScalar( 2 );
  107. }
  108. /**
  109. * Clamps the given point within the bounds of this OBB.
  110. *
  111. * @param {Vector3} point - The point that should be clamped within the bounds of this OBB.
  112. * @param {Vector3} target - The target vector that is used to store the method's result.
  113. * @returns {Vector3} - The clamped point.
  114. */
  115. clampPoint( point, target ) {
  116. // Reference: Closest Point on OBB to Point in Real-Time Collision Detection
  117. // by Christer Ericson (chapter 5.1.4)
  118. const halfSize = this.halfSize;
  119. v1.subVectors( point, this.center );
  120. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  121. // start at the center position of the OBB
  122. target.copy( this.center );
  123. // project the target onto the OBB axes and walk towards that point
  124. const x = MathUtils.clamp( v1.dot( xAxis ), - halfSize.x, halfSize.x );
  125. target.add( xAxis.multiplyScalar( x ) );
  126. const y = MathUtils.clamp( v1.dot( yAxis ), - halfSize.y, halfSize.y );
  127. target.add( yAxis.multiplyScalar( y ) );
  128. const z = MathUtils.clamp( v1.dot( zAxis ), - halfSize.z, halfSize.z );
  129. target.add( zAxis.multiplyScalar( z ) );
  130. return target;
  131. }
  132. /**
  133. * Returns `true` if the given point lies within this OBB.
  134. *
  135. * @param {Vector3} point - The point to test.
  136. * @returns {boolean} - Whether the given point lies within this OBB or not.
  137. */
  138. containsPoint( point ) {
  139. v1.subVectors( point, this.center );
  140. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  141. // project v1 onto each axis and check if these points lie inside the OBB
  142. return Math.abs( v1.dot( xAxis ) ) <= this.halfSize.x &&
  143. Math.abs( v1.dot( yAxis ) ) <= this.halfSize.y &&
  144. Math.abs( v1.dot( zAxis ) ) <= this.halfSize.z;
  145. }
  146. /**
  147. * Returns `true` if the given AABB intersects this OBB.
  148. *
  149. * @param {Box3} box3 - The AABB to test.
  150. * @returns {boolean} - Whether the given AABB intersects this OBB or not.
  151. */
  152. intersectsBox3( box3 ) {
  153. return this.intersectsOBB( obb.fromBox3( box3 ) );
  154. }
  155. /**
  156. * Returns `true` if the given bounding sphere intersects this OBB.
  157. *
  158. * @param {Sphere} sphere - The bounding sphere to test.
  159. * @returns {boolean} - Whether the given bounding sphere intersects this OBB or not.
  160. */
  161. intersectsSphere( sphere ) {
  162. // find the point on the OBB closest to the sphere center
  163. this.clampPoint( sphere.center, closestPoint );
  164. // if that point is inside the sphere, the OBB and sphere intersect
  165. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  166. }
  167. /**
  168. * Returns `true` if the given OBB intersects this OBB.
  169. *
  170. * @param {OBB} obb - The OBB to test.
  171. * @param {number} [epsilon=Number.EPSILON] - A small value to prevent arithmetic errors.
  172. * @returns {boolean} - Whether the given OBB intersects this OBB or not.
  173. */
  174. intersectsOBB( obb, epsilon = Number.EPSILON ) {
  175. // Reference: OBB-OBB Intersection in Real-Time Collision Detection
  176. // by Christer Ericson (chapter 4.4.1)
  177. // prepare data structures (the code uses the same nomenclature like the reference)
  178. a.c = this.center;
  179. a.e[ 0 ] = this.halfSize.x;
  180. a.e[ 1 ] = this.halfSize.y;
  181. a.e[ 2 ] = this.halfSize.z;
  182. this.rotation.extractBasis( a.u[ 0 ], a.u[ 1 ], a.u[ 2 ] );
  183. b.c = obb.center;
  184. b.e[ 0 ] = obb.halfSize.x;
  185. b.e[ 1 ] = obb.halfSize.y;
  186. b.e[ 2 ] = obb.halfSize.z;
  187. obb.rotation.extractBasis( b.u[ 0 ], b.u[ 1 ], b.u[ 2 ] );
  188. // compute rotation matrix expressing b in a's coordinate frame
  189. for ( let i = 0; i < 3; i ++ ) {
  190. for ( let j = 0; j < 3; j ++ ) {
  191. R[ i ][ j ] = a.u[ i ].dot( b.u[ j ] );
  192. }
  193. }
  194. // compute translation vector
  195. v1.subVectors( b.c, a.c );
  196. // bring translation into a's coordinate frame
  197. t[ 0 ] = v1.dot( a.u[ 0 ] );
  198. t[ 1 ] = v1.dot( a.u[ 1 ] );
  199. t[ 2 ] = v1.dot( a.u[ 2 ] );
  200. // compute common subexpressions. Add in an epsilon term to
  201. // counteract arithmetic errors when two edges are parallel and
  202. // their cross product is (near) null
  203. for ( let i = 0; i < 3; i ++ ) {
  204. for ( let j = 0; j < 3; j ++ ) {
  205. AbsR[ i ][ j ] = Math.abs( R[ i ][ j ] ) + epsilon;
  206. }
  207. }
  208. let ra, rb;
  209. // test axes L = A0, L = A1, L = A2
  210. for ( let i = 0; i < 3; i ++ ) {
  211. ra = a.e[ i ];
  212. rb = b.e[ 0 ] * AbsR[ i ][ 0 ] + b.e[ 1 ] * AbsR[ i ][ 1 ] + b.e[ 2 ] * AbsR[ i ][ 2 ];
  213. if ( Math.abs( t[ i ] ) > ra + rb ) return false;
  214. }
  215. // test axes L = B0, L = B1, L = B2
  216. for ( let i = 0; i < 3; i ++ ) {
  217. ra = a.e[ 0 ] * AbsR[ 0 ][ i ] + a.e[ 1 ] * AbsR[ 1 ][ i ] + a.e[ 2 ] * AbsR[ 2 ][ i ];
  218. rb = b.e[ i ];
  219. if ( Math.abs( t[ 0 ] * R[ 0 ][ i ] + t[ 1 ] * R[ 1 ][ i ] + t[ 2 ] * R[ 2 ][ i ] ) > ra + rb ) return false;
  220. }
  221. // test axis L = A0 x B0
  222. ra = a.e[ 1 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 1 ][ 0 ];
  223. rb = b.e[ 1 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 1 ];
  224. if ( Math.abs( t[ 2 ] * R[ 1 ][ 0 ] - t[ 1 ] * R[ 2 ][ 0 ] ) > ra + rb ) return false;
  225. // test axis L = A0 x B1
  226. ra = a.e[ 1 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 1 ][ 1 ];
  227. rb = b.e[ 0 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 0 ];
  228. if ( Math.abs( t[ 2 ] * R[ 1 ][ 1 ] - t[ 1 ] * R[ 2 ][ 1 ] ) > ra + rb ) return false;
  229. // test axis L = A0 x B2
  230. ra = a.e[ 1 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 1 ][ 2 ];
  231. rb = b.e[ 0 ] * AbsR[ 0 ][ 1 ] + b.e[ 1 ] * AbsR[ 0 ][ 0 ];
  232. if ( Math.abs( t[ 2 ] * R[ 1 ][ 2 ] - t[ 1 ] * R[ 2 ][ 2 ] ) > ra + rb ) return false;
  233. // test axis L = A1 x B0
  234. ra = a.e[ 0 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 0 ][ 0 ];
  235. rb = b.e[ 1 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 1 ];
  236. if ( Math.abs( t[ 0 ] * R[ 2 ][ 0 ] - t[ 2 ] * R[ 0 ][ 0 ] ) > ra + rb ) return false;
  237. // test axis L = A1 x B1
  238. ra = a.e[ 0 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 0 ][ 1 ];
  239. rb = b.e[ 0 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 0 ];
  240. if ( Math.abs( t[ 0 ] * R[ 2 ][ 1 ] - t[ 2 ] * R[ 0 ][ 1 ] ) > ra + rb ) return false;
  241. // test axis L = A1 x B2
  242. ra = a.e[ 0 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 0 ][ 2 ];
  243. rb = b.e[ 0 ] * AbsR[ 1 ][ 1 ] + b.e[ 1 ] * AbsR[ 1 ][ 0 ];
  244. if ( Math.abs( t[ 0 ] * R[ 2 ][ 2 ] - t[ 2 ] * R[ 0 ][ 2 ] ) > ra + rb ) return false;
  245. // test axis L = A2 x B0
  246. ra = a.e[ 0 ] * AbsR[ 1 ][ 0 ] + a.e[ 1 ] * AbsR[ 0 ][ 0 ];
  247. rb = b.e[ 1 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 1 ];
  248. if ( Math.abs( t[ 1 ] * R[ 0 ][ 0 ] - t[ 0 ] * R[ 1 ][ 0 ] ) > ra + rb ) return false;
  249. // test axis L = A2 x B1
  250. ra = a.e[ 0 ] * AbsR[ 1 ][ 1 ] + a.e[ 1 ] * AbsR[ 0 ][ 1 ];
  251. rb = b.e[ 0 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 0 ];
  252. if ( Math.abs( t[ 1 ] * R[ 0 ][ 1 ] - t[ 0 ] * R[ 1 ][ 1 ] ) > ra + rb ) return false;
  253. // test axis L = A2 x B2
  254. ra = a.e[ 0 ] * AbsR[ 1 ][ 2 ] + a.e[ 1 ] * AbsR[ 0 ][ 2 ];
  255. rb = b.e[ 0 ] * AbsR[ 2 ][ 1 ] + b.e[ 1 ] * AbsR[ 2 ][ 0 ];
  256. if ( Math.abs( t[ 1 ] * R[ 0 ][ 2 ] - t[ 0 ] * R[ 1 ][ 2 ] ) > ra + rb ) return false;
  257. // since no separating axis is found, the OBBs must be intersecting
  258. return true;
  259. }
  260. /**
  261. * Returns `true` if the given plane intersects this OBB.
  262. *
  263. * @param {Plane} plane - The plane to test.
  264. * @returns {boolean} Whether the given plane intersects this OBB or not.
  265. */
  266. intersectsPlane( plane ) {
  267. // Reference: Testing Box Against Plane in Real-Time Collision Detection
  268. // by Christer Ericson (chapter 5.2.3)
  269. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  270. // compute the projection interval radius of this OBB onto L(t) = this->center + t * p.normal;
  271. const r = this.halfSize.x * Math.abs( plane.normal.dot( xAxis ) ) +
  272. this.halfSize.y * Math.abs( plane.normal.dot( yAxis ) ) +
  273. this.halfSize.z * Math.abs( plane.normal.dot( zAxis ) );
  274. // compute distance of the OBB's center from the plane
  275. const d = plane.normal.dot( this.center ) - plane.constant;
  276. // Intersection occurs when distance d falls within [-r,+r] interval
  277. return Math.abs( d ) <= r;
  278. }
  279. /**
  280. * Performs a ray/OBB intersection test and stores the intersection point
  281. * in the given 3D vector.
  282. *
  283. * @param {Ray} ray - The ray to test.
  284. * @param {Vector3} target - The target vector that is used to store the method's result.
  285. * @return {?Vector3} The intersection point. If no intersection is detected, `null` is returned.
  286. */
  287. intersectRay( ray, target ) {
  288. // the idea is to perform the intersection test in the local space
  289. // of the OBB.
  290. this.getSize( size );
  291. aabb.setFromCenterAndSize( v1.set( 0, 0, 0 ), size );
  292. // create a 4x4 transformation matrix
  293. matrix.setFromMatrix3( this.rotation );
  294. matrix.setPosition( this.center );
  295. // transform ray to the local space of the OBB
  296. inverse.copy( matrix ).invert();
  297. localRay.copy( ray ).applyMatrix4( inverse );
  298. // perform ray <-> AABB intersection test
  299. if ( localRay.intersectBox( aabb, target ) ) {
  300. // transform the intersection point back to world space
  301. return target.applyMatrix4( matrix );
  302. } else {
  303. return null;
  304. }
  305. }
  306. /**
  307. * Returns `true` if the given ray intersects this OBB.
  308. *
  309. * @param {Ray} ray - The ray to test.
  310. * @returns {boolean} Whether the given ray intersects this OBB or not.
  311. */
  312. intersectsRay( ray ) {
  313. return this.intersectRay( ray, v1 ) !== null;
  314. }
  315. /**
  316. * Defines an OBB based on the given AABB.
  317. *
  318. * @param {Box3} box3 - The AABB to setup the OBB from.
  319. * @return {OBB} A reference of this OBB.
  320. */
  321. fromBox3( box3 ) {
  322. box3.getCenter( this.center );
  323. box3.getSize( this.halfSize ).multiplyScalar( 0.5 );
  324. this.rotation.identity();
  325. return this;
  326. }
  327. /**
  328. * Returns `true` if the given OBB is equal to this OBB.
  329. *
  330. * @param {OBB} obb - The OBB to test.
  331. * @returns {boolean} Whether the given OBB is equal to this OBB or not.
  332. */
  333. equals( obb ) {
  334. return obb.center.equals( this.center ) &&
  335. obb.halfSize.equals( this.halfSize ) &&
  336. obb.rotation.equals( this.rotation );
  337. }
  338. /**
  339. * Applies the given transformation matrix to this OBB. This method can be
  340. * used to transform the bounding volume with the world matrix of a 3D object
  341. * in order to keep both entities in sync.
  342. *
  343. * @param {Matrix4} matrix - The matrix to apply.
  344. * @return {OBB} A reference of this OBB.
  345. */
  346. applyMatrix4( matrix ) {
  347. const e = matrix.elements;
  348. let sx = v1.set( e[ 0 ], e[ 1 ], e[ 2 ] ).length();
  349. const sy = v1.set( e[ 4 ], e[ 5 ], e[ 6 ] ).length();
  350. const sz = v1.set( e[ 8 ], e[ 9 ], e[ 10 ] ).length();
  351. const det = matrix.determinant();
  352. if ( det < 0 ) sx = - sx;
  353. rotationMatrix.setFromMatrix4( matrix );
  354. const invSX = 1 / sx;
  355. const invSY = 1 / sy;
  356. const invSZ = 1 / sz;
  357. rotationMatrix.elements[ 0 ] *= invSX;
  358. rotationMatrix.elements[ 1 ] *= invSX;
  359. rotationMatrix.elements[ 2 ] *= invSX;
  360. rotationMatrix.elements[ 3 ] *= invSY;
  361. rotationMatrix.elements[ 4 ] *= invSY;
  362. rotationMatrix.elements[ 5 ] *= invSY;
  363. rotationMatrix.elements[ 6 ] *= invSZ;
  364. rotationMatrix.elements[ 7 ] *= invSZ;
  365. rotationMatrix.elements[ 8 ] *= invSZ;
  366. this.rotation.multiply( rotationMatrix );
  367. this.halfSize.x *= sx;
  368. this.halfSize.y *= sy;
  369. this.halfSize.z *= sz;
  370. v1.setFromMatrixPosition( matrix );
  371. this.center.add( v1 );
  372. return this;
  373. }
  374. }
  375. const obb = new OBB();
  376. export { OBB };
粤ICP备19079148号