Octree.js 9.3 KB

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