Octree.js 9.0 KB

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