Octree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import {
  2. Box3,
  3. Line3,
  4. Plane,
  5. Sphere,
  6. Triangle,
  7. Vector3
  8. } from 'three';
  9. import { Capsule } from '../math/Capsule.js';
  10. const _v1 = new Vector3();
  11. const _v2 = new Vector3();
  12. const _point1 = new Vector3();
  13. const _point2 = new Vector3();
  14. const _plane = new Plane();
  15. const _line1 = new Line3();
  16. const _line2 = new Line3();
  17. const _sphere = new Sphere();
  18. const _capsule = new Capsule();
  19. const _temp1 = new Vector3();
  20. const _temp2 = new Vector3();
  21. const _temp3 = new Vector3();
  22. const EPS = 1e-10;
  23. function lineToLineClosestPoints( line1, line2, target1 = null, target2 = null ) {
  24. const r = _temp1.copy( line1.end ).sub( line1.start );
  25. const s = _temp2.copy( line2.end ).sub( line2.start );
  26. const w = _temp3.copy( line2.start ).sub( line1.start );
  27. const a = r.dot( s ),
  28. b = r.dot( r ),
  29. c = s.dot( s ),
  30. d = s.dot( w ),
  31. e = r.dot( w );
  32. let t1, t2;
  33. const divisor = b * c - a * a;
  34. if ( Math.abs( divisor ) < EPS ) {
  35. const d1 = - d / c;
  36. const d2 = ( a - d ) / c;
  37. if ( Math.abs( d1 - 0.5 ) < Math.abs( d2 - 0.5 ) ) {
  38. t1 = 0;
  39. t2 = d1;
  40. } else {
  41. t1 = 1;
  42. t2 = d2;
  43. }
  44. } else {
  45. t1 = ( d * a + e * c ) / divisor;
  46. t2 = ( t1 * a - d ) / c;
  47. }
  48. t2 = Math.max( 0, Math.min( 1, t2 ) );
  49. t1 = Math.max( 0, Math.min( 1, t1 ) );
  50. if ( target1 ) {
  51. target1.copy( r ).multiplyScalar( t1 ).add( line1.start );
  52. }
  53. if ( target2 ) {
  54. target2.copy( s ).multiplyScalar( t2 ).add( line2.start );
  55. }
  56. }
  57. class Octree {
  58. constructor( box ) {
  59. this.box = box;
  60. this.bounds = new Box3();
  61. this.subTrees = [];
  62. this.triangles = [];
  63. }
  64. addTriangle( triangle ) {
  65. this.bounds.min.x = Math.min( this.bounds.min.x, triangle.a.x, triangle.b.x, triangle.c.x );
  66. this.bounds.min.y = Math.min( this.bounds.min.y, triangle.a.y, triangle.b.y, triangle.c.y );
  67. this.bounds.min.z = Math.min( this.bounds.min.z, triangle.a.z, triangle.b.z, triangle.c.z );
  68. this.bounds.max.x = Math.max( this.bounds.max.x, triangle.a.x, triangle.b.x, triangle.c.x );
  69. this.bounds.max.y = Math.max( this.bounds.max.y, triangle.a.y, triangle.b.y, triangle.c.y );
  70. this.bounds.max.z = Math.max( this.bounds.max.z, triangle.a.z, triangle.b.z, triangle.c.z );
  71. this.triangles.push( triangle );
  72. return this;
  73. }
  74. calcBox() {
  75. this.box = this.bounds.clone();
  76. // offset small amount to account for regular grid
  77. this.box.min.x -= 0.01;
  78. this.box.min.y -= 0.01;
  79. this.box.min.z -= 0.01;
  80. return this;
  81. }
  82. split( level ) {
  83. if ( ! this.box ) return;
  84. const subTrees = [];
  85. const halfsize = _v2.copy( this.box.max ).sub( this.box.min ).multiplyScalar( 0.5 );
  86. for ( let x = 0; x < 2; x ++ ) {
  87. for ( let y = 0; y < 2; y ++ ) {
  88. for ( let z = 0; z < 2; z ++ ) {
  89. const box = new Box3();
  90. const v = _v1.set( x, y, z );
  91. box.min.copy( this.box.min ).add( v.multiply( halfsize ) );
  92. box.max.copy( box.min ).add( halfsize );
  93. subTrees.push( new Octree( box ) );
  94. }
  95. }
  96. }
  97. let triangle;
  98. while ( triangle = this.triangles.pop() ) {
  99. for ( let i = 0; i < subTrees.length; i ++ ) {
  100. if ( subTrees[ i ].box.intersectsTriangle( triangle ) ) {
  101. subTrees[ i ].triangles.push( triangle );
  102. }
  103. }
  104. }
  105. for ( let i = 0; i < subTrees.length; i ++ ) {
  106. const len = subTrees[ i ].triangles.length;
  107. if ( len > 8 && level < 16 ) {
  108. subTrees[ i ].split( level + 1 );
  109. }
  110. if ( len !== 0 ) {
  111. this.subTrees.push( subTrees[ i ] );
  112. }
  113. }
  114. return this;
  115. }
  116. build() {
  117. this.calcBox();
  118. this.split( 0 );
  119. return this;
  120. }
  121. getRayTriangles( ray, triangles ) {
  122. for ( let i = 0; i < this.subTrees.length; i ++ ) {
  123. const subTree = this.subTrees[ i ];
  124. if ( ! ray.intersectsBox( subTree.box ) ) continue;
  125. if ( subTree.triangles.length > 0 ) {
  126. for ( let j = 0; j < subTree.triangles.length; j ++ ) {
  127. if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );
  128. }
  129. } else {
  130. subTree.getRayTriangles( ray, triangles );
  131. }
  132. }
  133. return triangles;
  134. }
  135. triangleCapsuleIntersect( capsule, triangle ) {
  136. triangle.getPlane( _plane );
  137. const d1 = _plane.distanceToPoint( capsule.start ) - capsule.radius;
  138. const d2 = _plane.distanceToPoint( capsule.end ) - capsule.radius;
  139. if ( ( d1 > 0 && d2 > 0 ) || ( d1 < - capsule.radius && d2 < - capsule.radius ) ) {
  140. return false;
  141. }
  142. const delta = Math.abs( d1 / ( Math.abs( d1 ) + Math.abs( d2 ) ) );
  143. const intersectPoint = _v1.copy( capsule.start ).lerp( capsule.end, delta );
  144. if ( triangle.containsPoint( intersectPoint ) ) {
  145. return { normal: _plane.normal.clone(), point: intersectPoint.clone(), depth: Math.abs( Math.min( d1, d2 ) ) };
  146. }
  147. const r2 = capsule.radius * capsule.radius;
  148. const line1 = _line1.set( capsule.start, capsule.end );
  149. const lines = [
  150. [ triangle.a, triangle.b ],
  151. [ triangle.b, triangle.c ],
  152. [ triangle.c, triangle.a ]
  153. ];
  154. for ( let i = 0; i < lines.length; i ++ ) {
  155. const line2 = _line2.set( lines[ i ][ 0 ], lines[ i ][ 1 ] );
  156. lineToLineClosestPoints( line1, line2, _point1, _point2 );
  157. if ( _point1.distanceToSquared( _point2 ) < r2 ) {
  158. return {
  159. normal: _point1.clone().sub( _point2 ).normalize(),
  160. point: _point2.clone(),
  161. depth: capsule.radius - _point1.distanceTo( _point2 )
  162. };
  163. }
  164. }
  165. return false;
  166. }
  167. triangleSphereIntersect( sphere, triangle ) {
  168. triangle.getPlane( _plane );
  169. if ( ! sphere.intersectsPlane( _plane ) ) return false;
  170. const depth = Math.abs( _plane.distanceToSphere( sphere ) );
  171. const r2 = sphere.radius * sphere.radius - depth * depth;
  172. const plainPoint = _plane.projectPoint( sphere.center, _v1 );
  173. if ( triangle.containsPoint( sphere.center ) ) {
  174. return { normal: _plane.normal.clone(), point: plainPoint.clone(), depth: Math.abs( _plane.distanceToSphere( sphere ) ) };
  175. }
  176. const lines = [
  177. [ triangle.a, triangle.b ],
  178. [ triangle.b, triangle.c ],
  179. [ triangle.c, triangle.a ]
  180. ];
  181. for ( let i = 0; i < lines.length; i ++ ) {
  182. _line1.set( lines[ i ][ 0 ], lines[ i ][ 1 ] );
  183. _line1.closestPointToPoint( plainPoint, true, _v2 );
  184. const d = _v2.distanceToSquared( sphere.center );
  185. if ( d < r2 ) {
  186. return { normal: sphere.center.clone().sub( _v2 ).normalize(), point: _v2.clone(), depth: sphere.radius - Math.sqrt( d ) };
  187. }
  188. }
  189. return false;
  190. }
  191. getSphereTriangles( sphere, triangles ) {
  192. for ( let i = 0; i < this.subTrees.length; i ++ ) {
  193. const subTree = this.subTrees[ i ];
  194. if ( ! sphere.intersectsBox( subTree.box ) ) continue;
  195. if ( subTree.triangles.length > 0 ) {
  196. for ( let j = 0; j < subTree.triangles.length; j ++ ) {
  197. if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );
  198. }
  199. } else {
  200. subTree.getSphereTriangles( sphere, triangles );
  201. }
  202. }
  203. }
  204. getCapsuleTriangles( capsule, triangles ) {
  205. for ( let i = 0; i < this.subTrees.length; i ++ ) {
  206. const subTree = this.subTrees[ i ];
  207. if ( ! capsule.intersectsBox( subTree.box ) ) continue;
  208. if ( subTree.triangles.length > 0 ) {
  209. for ( let j = 0; j < subTree.triangles.length; j ++ ) {
  210. if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );
  211. }
  212. } else {
  213. subTree.getCapsuleTriangles( capsule, triangles );
  214. }
  215. }
  216. }
  217. sphereIntersect( sphere ) {
  218. _sphere.copy( sphere );
  219. const triangles = [];
  220. let result, hit = false;
  221. this.getSphereTriangles( sphere, triangles );
  222. for ( let i = 0; i < triangles.length; i ++ ) {
  223. if ( result = this.triangleSphereIntersect( _sphere, triangles[ i ] ) ) {
  224. hit = true;
  225. _sphere.center.add( result.normal.multiplyScalar( result.depth ) );
  226. }
  227. }
  228. if ( hit ) {
  229. const collisionVector = _sphere.center.clone().sub( sphere.center );
  230. const depth = collisionVector.length();
  231. return { normal: collisionVector.normalize(), depth: depth };
  232. }
  233. return false;
  234. }
  235. capsuleIntersect( capsule ) {
  236. _capsule.copy( capsule );
  237. const triangles = [];
  238. let result, hit = false;
  239. this.getCapsuleTriangles( _capsule, triangles );
  240. for ( let i = 0; i < triangles.length; i ++ ) {
  241. if ( result = this.triangleCapsuleIntersect( _capsule, triangles[ i ] ) ) {
  242. hit = true;
  243. _capsule.translate( result.normal.multiplyScalar( result.depth ) );
  244. }
  245. }
  246. if ( hit ) {
  247. const collisionVector = _capsule.getCenter( new Vector3() ).sub( capsule.getCenter( _v1 ) );
  248. const depth = collisionVector.length();
  249. return { normal: collisionVector.normalize(), depth: depth };
  250. }
  251. return false;
  252. }
  253. rayIntersect( ray ) {
  254. if ( ray.direction.length() === 0 ) return;
  255. const triangles = [];
  256. let triangle, position, distance = 1e100;
  257. this.getRayTriangles( ray, triangles );
  258. for ( let i = 0; i < triangles.length; i ++ ) {
  259. const result = ray.intersectTriangle( triangles[ i ].a, triangles[ i ].b, triangles[ i ].c, true, _v1 );
  260. if ( result ) {
  261. const newdistance = result.sub( ray.origin ).length();
  262. if ( distance > newdistance ) {
  263. position = result.clone().add( ray.origin );
  264. distance = newdistance;
  265. triangle = triangles[ i ];
  266. }
  267. }
  268. }
  269. return distance < 1e100 ? { distance: distance, triangle: triangle, position: position } : false;
  270. }
  271. fromGraphNode( group ) {
  272. group.updateWorldMatrix( true, true );
  273. group.traverse( ( obj ) => {
  274. if ( obj.isMesh === true ) {
  275. let geometry, isTemp = false;
  276. if ( obj.geometry.index !== null ) {
  277. isTemp = true;
  278. geometry = obj.geometry.toNonIndexed();
  279. } else {
  280. geometry = obj.geometry;
  281. }
  282. const positionAttribute = geometry.getAttribute( 'position' );
  283. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  284. const v1 = new Vector3().fromBufferAttribute( positionAttribute, i );
  285. const v2 = new Vector3().fromBufferAttribute( positionAttribute, i + 1 );
  286. const v3 = new Vector3().fromBufferAttribute( positionAttribute, i + 2 );
  287. v1.applyMatrix4( obj.matrixWorld );
  288. v2.applyMatrix4( obj.matrixWorld );
  289. v3.applyMatrix4( obj.matrixWorld );
  290. this.addTriangle( new Triangle( v1, v2, v3 ) );
  291. }
  292. if ( isTemp ) {
  293. geometry.dispose();
  294. }
  295. }
  296. } );
  297. this.build();
  298. return this;
  299. }
  300. clear() {
  301. this.box = null;
  302. this.bounds.makeEmpty();
  303. this.subTrees.length = 0;
  304. this.triangles.length = 0;
  305. return this;
  306. }
  307. }
  308. export { Octree };