Octree.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Octree - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <h1 translate="no">Octree</h1>
  13. <section>
  14. <header>
  15. <div class="class-description"><p>An octree is a hierarchical tree data structure used to partition a three-dimensional
  16. space by recursively subdividing it into eight octants.</p>
  17. <p>This particular implementation can have up to sixteen levels and stores up to eight triangles
  18. in leaf nodes.</p>
  19. <p><code>Octree</code> can be used in games to compute collision between the game world and colliders from
  20. the player or other dynamic 3D objects.</p></div>
  21. <h2>Code Example</h2>
  22. <div translate="no"><pre><code class="language-js">const octree = new Octree().fromGraphNode( scene );
  23. const result = octree.capsuleIntersect( playerCollider ); // collision detection
  24. </code></pre></div>
  25. </header>
  26. <article>
  27. <h2 class="subsection-title">Import</h2>
  28. <p><span translate="no">Octree</span> is an addon, and must be imported explicitly, see <a href="https://threejs.org/manual/#en/installation" target="_blank" rel="noopener">Installation#Addons</a>.</p>
  29. <pre><code class="language-js">import { Octree } from 'three/addons/math/Octree.js';</code></pre>
  30. <div class="container-overview">
  31. <h2>Constructor</h2>
  32. <h3 class="name name-method" id="Octree" translate="no">new <a href="#Octree">Octree</a><span class="signature">( box : <span class="param-type"><a href="Box3.html">Box3</a></span> )</span> </h3>
  33. <div class="method">
  34. <div class="description">
  35. <p>Constructs a new Octree.</p>
  36. </div>
  37. <table class="params">
  38. <tbody>
  39. <tr>
  40. <td class="name">
  41. <strong>box</strong>
  42. </td>
  43. <td class="description last">
  44. <p>The base box with enclose the entire Octree.</p>
  45. </td>
  46. </tr>
  47. </tbody>
  48. </table>
  49. </div>
  50. </div>
  51. <h2 class="subsection-title">Properties</h2>
  52. <div class="member">
  53. <h3 class="name" id="bounds" translate="no">.<a href="#bounds">bounds</a><span class="type-signature"> : <a href="Box3.html">Box3</a></span> </h3>
  54. <div class="description">
  55. <p>The bounds of the Octree. Compared to <a href="Octree.html#box">Octree#box</a>, no
  56. margin is applied.</p>
  57. </div>
  58. </div>
  59. <div class="member">
  60. <h3 class="name" id="box" translate="no">.<a href="#box">box</a><span class="type-signature"> : <a href="Box3.html">Box3</a></span> </h3>
  61. <div class="description">
  62. <p>The base box with enclose the entire Octree.</p>
  63. </div>
  64. </div>
  65. <div class="member">
  66. <h3 class="name" id="layers" translate="no">.<a href="#layers">layers</a><span class="type-signature"> : <a href="Layers.html">Layers</a></span> </h3>
  67. <div class="description">
  68. <p>Can by used for layers configuration for refine testing.</p>
  69. </div>
  70. </div>
  71. <div class="member">
  72. <h3 class="name" id="maxLevel" translate="no">.<a href="#maxLevel">maxLevel</a><span class="type-signature"> : number</span> </h3>
  73. <div class="description">
  74. <p>The maximum level of the Octree. It defines the maximum
  75. hierarchical depth of the data structure.</p>
  76. <p>Default is <code>16</code>.</p>
  77. </div>
  78. </div>
  79. <div class="member">
  80. <h3 class="name" id="trianglesPerLeaf" translate="no">.<a href="#trianglesPerLeaf">trianglesPerLeaf</a><span class="type-signature"> : number</span> </h3>
  81. <div class="description">
  82. <p>The number of triangles a leaf can store before it is split.</p>
  83. <p>Default is <code>8</code>.</p>
  84. </div>
  85. </div>
  86. <h2 class="subsection-title">Methods</h2>
  87. <h3 class="name name-method" id="addTriangle" translate="no">.<a href="#addTriangle">addTriangle</a><span class="signature">( triangle : <span class="param-type"><a href="Triangle.html">Triangle</a></span> )</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  88. <div class="method">
  89. <div class="description">
  90. <p>Adds the given triangle to the Octree. The triangle vertices are clamped if they exceed
  91. the bounds of the Octree.</p>
  92. </div>
  93. <table class="params">
  94. <tbody>
  95. <tr>
  96. <td class="name">
  97. <strong>triangle</strong>
  98. </td>
  99. <td class="description last">
  100. <p>The triangle to add.</p>
  101. </td>
  102. </tr>
  103. </tbody>
  104. </table>
  105. <dl class="details">
  106. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  107. </dl>
  108. </div>
  109. <h3 class="name name-method" id="boxIntersect" translate="no">.<a href="#boxIntersect">boxIntersect</a><span class="signature">( box : <span class="param-type"><a href="Box3.html">Box3</a></span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  110. <div class="method">
  111. <div class="description">
  112. <p>Performs a bounding box intersection test with this Octree.</p>
  113. </div>
  114. <table class="params">
  115. <tbody>
  116. <tr>
  117. <td class="name">
  118. <strong>box</strong>
  119. </td>
  120. <td class="description last">
  121. <p>The bounding box to test.</p>
  122. </td>
  123. </tr>
  124. </tbody>
  125. </table>
  126. <dl class="details">
  127. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  128. is detected, the method returns <code>false</code>.</dt>
  129. </dl>
  130. </div>
  131. <h3 class="name name-method" id="build" translate="no">.<a href="#build">build</a><span class="signature">()</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  132. <div class="method">
  133. <div class="description">
  134. <p>Builds the Octree.</p>
  135. </div>
  136. <dl class="details">
  137. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  138. </dl>
  139. </div>
  140. <h3 class="name name-method" id="calcBox" translate="no">.<a href="#calcBox">calcBox</a><span class="signature">()</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  141. <div class="method">
  142. <div class="description">
  143. <p>Prepares <a href="Octree.html#box">Octree#box</a> for the build.</p>
  144. </div>
  145. <dl class="details">
  146. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  147. </dl>
  148. </div>
  149. <h3 class="name name-method" id="capsuleIntersect" translate="no">.<a href="#capsuleIntersect">capsuleIntersect</a><span class="signature">( capsule : <span class="param-type"><a href="Capsule.html">Capsule</a></span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  150. <div class="method">
  151. <div class="description">
  152. <p>Performs a capsule intersection test with this Octree.</p>
  153. </div>
  154. <table class="params">
  155. <tbody>
  156. <tr>
  157. <td class="name">
  158. <strong>capsule</strong>
  159. </td>
  160. <td class="description last">
  161. <p>The capsule to test.</p>
  162. </td>
  163. </tr>
  164. </tbody>
  165. </table>
  166. <dl class="details">
  167. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  168. is detected, the method returns <code>false</code>.</dt>
  169. </dl>
  170. </div>
  171. <h3 class="name name-method" id="clear" translate="no">.<a href="#clear">clear</a><span class="signature">()</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  172. <div class="method">
  173. <div class="description">
  174. <p>Clears the Octree by making it empty.</p>
  175. </div>
  176. <dl class="details">
  177. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  178. </dl>
  179. </div>
  180. <h3 class="name name-method" id="fromGraphNode" translate="no">.<a href="#fromGraphNode">fromGraphNode</a><span class="signature">( group : <span class="param-type"><a href="Object3D.html">Object3D</a></span> )</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  181. <div class="method">
  182. <div class="description">
  183. <p>Constructs the Octree from the given 3D object.</p>
  184. </div>
  185. <table class="params">
  186. <tbody>
  187. <tr>
  188. <td class="name">
  189. <strong>group</strong>
  190. </td>
  191. <td class="description last">
  192. <p>The scene graph node.</p>
  193. </td>
  194. </tr>
  195. </tbody>
  196. </table>
  197. <dl class="details">
  198. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  199. </dl>
  200. </div>
  201. <h3 class="name name-method" id="getBoxTriangles" translate="no">.<a href="#getBoxTriangles">getBoxTriangles</a><span class="signature">( box : <span class="param-type"><a href="Box3.html">Box3</a></span>, triangles : <span class="param-type">Array.&lt;<a href="Triangle.html">Triangle</a>></span> )</span> </h3>
  202. <div class="method">
  203. <div class="description">
  204. <p>Computes the triangles that potentially intersect with the given bounding box.</p>
  205. </div>
  206. <table class="params">
  207. <tbody>
  208. <tr>
  209. <td class="name">
  210. <strong>box</strong>
  211. </td>
  212. <td class="description last">
  213. <p>The bounding box.</p>
  214. </td>
  215. </tr>
  216. <tr>
  217. <td class="name">
  218. <strong>triangles</strong>
  219. </td>
  220. <td class="description last">
  221. <p>The target array that holds the triangles.</p>
  222. </td>
  223. </tr>
  224. </tbody>
  225. </table>
  226. </div>
  227. <h3 class="name name-method" id="getCapsuleTriangles" translate="no">.<a href="#getCapsuleTriangles">getCapsuleTriangles</a><span class="signature">( capsule : <span class="param-type"><a href="Capsule.html">Capsule</a></span>, triangles : <span class="param-type">Array.&lt;<a href="Triangle.html">Triangle</a>></span> )</span> </h3>
  228. <div class="method">
  229. <div class="description">
  230. <p>Computes the triangles that potentially intersect with the given capsule.</p>
  231. </div>
  232. <table class="params">
  233. <tbody>
  234. <tr>
  235. <td class="name">
  236. <strong>capsule</strong>
  237. </td>
  238. <td class="description last">
  239. <p>The capsule to test.</p>
  240. </td>
  241. </tr>
  242. <tr>
  243. <td class="name">
  244. <strong>triangles</strong>
  245. </td>
  246. <td class="description last">
  247. <p>The target array that holds the triangles.</p>
  248. </td>
  249. </tr>
  250. </tbody>
  251. </table>
  252. </div>
  253. <h3 class="name name-method" id="getRayTriangles" translate="no">.<a href="#getRayTriangles">getRayTriangles</a><span class="signature">( ray : <span class="param-type"><a href="Ray.html">Ray</a></span>, triangles : <span class="param-type">Array.&lt;<a href="Triangle.html">Triangle</a>></span> )</span> </h3>
  254. <div class="method">
  255. <div class="description">
  256. <p>Computes the triangles that potentially intersect with the given ray.</p>
  257. </div>
  258. <table class="params">
  259. <tbody>
  260. <tr>
  261. <td class="name">
  262. <strong>ray</strong>
  263. </td>
  264. <td class="description last">
  265. <p>The ray to test.</p>
  266. </td>
  267. </tr>
  268. <tr>
  269. <td class="name">
  270. <strong>triangles</strong>
  271. </td>
  272. <td class="description last">
  273. <p>The target array that holds the triangles.</p>
  274. </td>
  275. </tr>
  276. </tbody>
  277. </table>
  278. </div>
  279. <h3 class="name name-method" id="getSphereTriangles" translate="no">.<a href="#getSphereTriangles">getSphereTriangles</a><span class="signature">( sphere : <span class="param-type"><a href="Sphere.html">Sphere</a></span>, triangles : <span class="param-type">Array.&lt;<a href="Triangle.html">Triangle</a>></span> )</span> </h3>
  280. <div class="method">
  281. <div class="description">
  282. <p>Computes the triangles that potentially intersect with the given bounding sphere.</p>
  283. </div>
  284. <table class="params">
  285. <tbody>
  286. <tr>
  287. <td class="name">
  288. <strong>sphere</strong>
  289. </td>
  290. <td class="description last">
  291. <p>The sphere to test.</p>
  292. </td>
  293. </tr>
  294. <tr>
  295. <td class="name">
  296. <strong>triangles</strong>
  297. </td>
  298. <td class="description last">
  299. <p>The target array that holds the triangles.</p>
  300. </td>
  301. </tr>
  302. </tbody>
  303. </table>
  304. </div>
  305. <h3 class="name name-method" id="rayIntersect" translate="no">.<a href="#rayIntersect">rayIntersect</a><span class="signature">( ray : <span class="param-type"><a href="Ray.html">Ray</a></span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  306. <div class="method">
  307. <div class="description">
  308. <p>Performs a ray intersection test with this Octree.</p>
  309. </div>
  310. <table class="params">
  311. <tbody>
  312. <tr>
  313. <td class="name">
  314. <strong>ray</strong>
  315. </td>
  316. <td class="description last">
  317. <p>The ray to test.</p>
  318. </td>
  319. </tr>
  320. </tbody>
  321. </table>
  322. <dl class="details">
  323. <dt class="tag-returns"><strong>Returns:</strong> The nearest intersection object. If no intersection
  324. is detected, the method returns <code>false</code>.</dt>
  325. </dl>
  326. </div>
  327. <h3 class="name name-method" id="sphereIntersect" translate="no">.<a href="#sphereIntersect">sphereIntersect</a><span class="signature">( sphere : <span class="param-type"><a href="Sphere.html">Sphere</a></span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  328. <div class="method">
  329. <div class="description">
  330. <p>Performs a bounding sphere intersection test with this Octree.</p>
  331. </div>
  332. <table class="params">
  333. <tbody>
  334. <tr>
  335. <td class="name">
  336. <strong>sphere</strong>
  337. </td>
  338. <td class="description last">
  339. <p>The bounding sphere to test.</p>
  340. </td>
  341. </tr>
  342. </tbody>
  343. </table>
  344. <dl class="details">
  345. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  346. is detected, the method returns <code>false</code>.</dt>
  347. </dl>
  348. </div>
  349. <h3 class="name name-method" id="split" translate="no">.<a href="#split">split</a><span class="signature">( level : <span class="param-type">number</span> )</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  350. <div class="method">
  351. <div class="description">
  352. <p>Splits the Octree. This method is used recursively when
  353. building the Octree.</p>
  354. </div>
  355. <table class="params">
  356. <tbody>
  357. <tr>
  358. <td class="name">
  359. <strong>level</strong>
  360. </td>
  361. <td class="description last">
  362. <p>The current level.</p>
  363. </td>
  364. </tr>
  365. </tbody>
  366. </table>
  367. <dl class="details">
  368. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  369. </dl>
  370. </div>
  371. <h3 class="name name-method" id="triangleBoxIntersect" translate="no">.<a href="#triangleBoxIntersect">triangleBoxIntersect</a><span class="signature">( box : <span class="param-type"><a href="Box3.html">Box3</a></span>, triangle : <span class="param-type"><a href="Triangle.html">Triangle</a></span> )</span><span class="type-signature"> : Object | false</span> </h3>
  372. <div class="method">
  373. <div class="description">
  374. <p>Computes the intersection between the given bounding box and triangle.</p>
  375. </div>
  376. <table class="params">
  377. <tbody>
  378. <tr>
  379. <td class="name">
  380. <strong>box</strong>
  381. </td>
  382. <td class="description last">
  383. <p>The bounding box to test.</p>
  384. </td>
  385. </tr>
  386. <tr>
  387. <td class="name">
  388. <strong>triangle</strong>
  389. </td>
  390. <td class="description last">
  391. <p>The triangle to test.</p>
  392. </td>
  393. </tr>
  394. </tbody>
  395. </table>
  396. <dl class="details">
  397. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  398. is detected, the method returns <code>false</code>.</dt>
  399. </dl>
  400. </div>
  401. <h3 class="name name-method" id="triangleCapsuleIntersect" translate="no">.<a href="#triangleCapsuleIntersect">triangleCapsuleIntersect</a><span class="signature">( capsule : <span class="param-type"><a href="Capsule.html">Capsule</a></span>, triangle : <span class="param-type"><a href="Triangle.html">Triangle</a></span> )</span><span class="type-signature"> : Object | false</span> </h3>
  402. <div class="method">
  403. <div class="description">
  404. <p>Computes the intersection between the given capsule and triangle.</p>
  405. </div>
  406. <table class="params">
  407. <tbody>
  408. <tr>
  409. <td class="name">
  410. <strong>capsule</strong>
  411. </td>
  412. <td class="description last">
  413. <p>The capsule to test.</p>
  414. </td>
  415. </tr>
  416. <tr>
  417. <td class="name">
  418. <strong>triangle</strong>
  419. </td>
  420. <td class="description last">
  421. <p>The triangle to test.</p>
  422. </td>
  423. </tr>
  424. </tbody>
  425. </table>
  426. <dl class="details">
  427. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  428. is detected, the method returns <code>false</code>.</dt>
  429. </dl>
  430. </div>
  431. <h3 class="name name-method" id="triangleSphereIntersect" translate="no">.<a href="#triangleSphereIntersect">triangleSphereIntersect</a><span class="signature">( sphere : <span class="param-type"><a href="Sphere.html">Sphere</a></span>, triangle : <span class="param-type"><a href="Triangle.html">Triangle</a></span> )</span><span class="type-signature"> : Object | false</span> </h3>
  432. <div class="method">
  433. <div class="description">
  434. <p>Computes the intersection between the given sphere and triangle.</p>
  435. </div>
  436. <table class="params">
  437. <tbody>
  438. <tr>
  439. <td class="name">
  440. <strong>sphere</strong>
  441. </td>
  442. <td class="description last">
  443. <p>The sphere to test.</p>
  444. </td>
  445. </tr>
  446. <tr>
  447. <td class="name">
  448. <strong>triangle</strong>
  449. </td>
  450. <td class="description last">
  451. <p>The triangle to test.</p>
  452. </td>
  453. </tr>
  454. </tbody>
  455. </table>
  456. <dl class="details">
  457. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  458. is detected, the method returns <code>false</code>.</dt>
  459. </dl>
  460. </div>
  461. <h2 class="subsection-title">Source</h2>
  462. <p>
  463. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/math/Octree.js" translate="no" target="_blank" rel="noopener">examples/jsm/math/Octree.js</a>
  464. </p>
  465. </article>
  466. </section>
  467. <script src="../scripts/linenumber.js"></script>
  468. <script src="../scripts/page.js"></script>
  469. </body>
  470. </html>
粤ICP备19079148号