Octree.js 9.3 KB

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