Octree.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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="build" translate="no">.<a href="#build">build</a><span class="signature">()</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  110. <div class="method">
  111. <div class="description">
  112. <p>Builds the Octree.</p>
  113. </div>
  114. <dl class="details">
  115. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  116. </dl>
  117. </div>
  118. <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>
  119. <div class="method">
  120. <div class="description">
  121. <p>Prepares <a href="Octree.html#box">Octree#box</a> for the build.</p>
  122. </div>
  123. <dl class="details">
  124. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  125. </dl>
  126. </div>
  127. <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>
  128. <div class="method">
  129. <div class="description">
  130. <p>Performs a capsule intersection test with this Octree.</p>
  131. </div>
  132. <table class="params">
  133. <tbody>
  134. <tr>
  135. <td class="name">
  136. <strong>capsule</strong>
  137. </td>
  138. <td class="description last">
  139. <p>The capsule to test.</p>
  140. </td>
  141. </tr>
  142. </tbody>
  143. </table>
  144. <dl class="details">
  145. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  146. is detected, the method returns <code>false</code>.</dt>
  147. </dl>
  148. </div>
  149. <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>
  150. <div class="method">
  151. <div class="description">
  152. <p>Clears the Octree by making it empty.</p>
  153. </div>
  154. <dl class="details">
  155. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  156. </dl>
  157. </div>
  158. <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>
  159. <div class="method">
  160. <div class="description">
  161. <p>Constructs the Octree from the given 3D object.</p>
  162. </div>
  163. <table class="params">
  164. <tbody>
  165. <tr>
  166. <td class="name">
  167. <strong>group</strong>
  168. </td>
  169. <td class="description last">
  170. <p>The scene graph node.</p>
  171. </td>
  172. </tr>
  173. </tbody>
  174. </table>
  175. <dl class="details">
  176. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  177. </dl>
  178. </div>
  179. <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>
  180. <div class="method">
  181. <div class="description">
  182. <p>Computes the triangles that potentially intersect with the given capsule.</p>
  183. </div>
  184. <table class="params">
  185. <tbody>
  186. <tr>
  187. <td class="name">
  188. <strong>capsule</strong>
  189. </td>
  190. <td class="description last">
  191. <p>The capsule to test.</p>
  192. </td>
  193. </tr>
  194. <tr>
  195. <td class="name">
  196. <strong>triangles</strong>
  197. </td>
  198. <td class="description last">
  199. <p>The target array that holds the triangles.</p>
  200. </td>
  201. </tr>
  202. </tbody>
  203. </table>
  204. </div>
  205. <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>
  206. <div class="method">
  207. <div class="description">
  208. <p>Computes the triangles that potentially intersect with the given ray.</p>
  209. </div>
  210. <table class="params">
  211. <tbody>
  212. <tr>
  213. <td class="name">
  214. <strong>ray</strong>
  215. </td>
  216. <td class="description last">
  217. <p>The ray to test.</p>
  218. </td>
  219. </tr>
  220. <tr>
  221. <td class="name">
  222. <strong>triangles</strong>
  223. </td>
  224. <td class="description last">
  225. <p>The target array that holds the triangles.</p>
  226. </td>
  227. </tr>
  228. </tbody>
  229. </table>
  230. </div>
  231. <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>
  232. <div class="method">
  233. <div class="description">
  234. <p>Computes the triangles that potentially intersect with the given bounding sphere.</p>
  235. </div>
  236. <table class="params">
  237. <tbody>
  238. <tr>
  239. <td class="name">
  240. <strong>sphere</strong>
  241. </td>
  242. <td class="description last">
  243. <p>The sphere to test.</p>
  244. </td>
  245. </tr>
  246. <tr>
  247. <td class="name">
  248. <strong>triangles</strong>
  249. </td>
  250. <td class="description last">
  251. <p>The target array that holds the triangles.</p>
  252. </td>
  253. </tr>
  254. </tbody>
  255. </table>
  256. </div>
  257. <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>
  258. <div class="method">
  259. <div class="description">
  260. <p>Performs a ray intersection test with this Octree.</p>
  261. </div>
  262. <table class="params">
  263. <tbody>
  264. <tr>
  265. <td class="name">
  266. <strong>ray</strong>
  267. </td>
  268. <td class="description last">
  269. <p>The ray to test.</p>
  270. </td>
  271. </tr>
  272. </tbody>
  273. </table>
  274. <dl class="details">
  275. <dt class="tag-returns"><strong>Returns:</strong> The nearest intersection object. If no intersection
  276. is detected, the method returns <code>false</code>.</dt>
  277. </dl>
  278. </div>
  279. <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>
  280. <div class="method">
  281. <div class="description">
  282. <p>Performs a bounding sphere intersection test with this Octree.</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 bounding sphere to test.</p>
  292. </td>
  293. </tr>
  294. </tbody>
  295. </table>
  296. <dl class="details">
  297. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  298. is detected, the method returns <code>false</code>.</dt>
  299. </dl>
  300. </div>
  301. <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>
  302. <div class="method">
  303. <div class="description">
  304. <p>Splits the Octree. This method is used recursively when
  305. building the Octree.</p>
  306. </div>
  307. <table class="params">
  308. <tbody>
  309. <tr>
  310. <td class="name">
  311. <strong>level</strong>
  312. </td>
  313. <td class="description last">
  314. <p>The current level.</p>
  315. </td>
  316. </tr>
  317. </tbody>
  318. </table>
  319. <dl class="details">
  320. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  321. </dl>
  322. </div>
  323. <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>
  324. <div class="method">
  325. <div class="description">
  326. <p>Computes the intersection between the given capsule and triangle.</p>
  327. </div>
  328. <table class="params">
  329. <tbody>
  330. <tr>
  331. <td class="name">
  332. <strong>capsule</strong>
  333. </td>
  334. <td class="description last">
  335. <p>The capsule to test.</p>
  336. </td>
  337. </tr>
  338. <tr>
  339. <td class="name">
  340. <strong>triangle</strong>
  341. </td>
  342. <td class="description last">
  343. <p>The triangle to test.</p>
  344. </td>
  345. </tr>
  346. </tbody>
  347. </table>
  348. <dl class="details">
  349. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  350. is detected, the method returns <code>false</code>.</dt>
  351. </dl>
  352. </div>
  353. <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>
  354. <div class="method">
  355. <div class="description">
  356. <p>Computes the intersection between the given sphere and triangle.</p>
  357. </div>
  358. <table class="params">
  359. <tbody>
  360. <tr>
  361. <td class="name">
  362. <strong>sphere</strong>
  363. </td>
  364. <td class="description last">
  365. <p>The sphere to test.</p>
  366. </td>
  367. </tr>
  368. <tr>
  369. <td class="name">
  370. <strong>triangle</strong>
  371. </td>
  372. <td class="description last">
  373. <p>The triangle to test.</p>
  374. </td>
  375. </tr>
  376. </tbody>
  377. </table>
  378. <dl class="details">
  379. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  380. is detected, the method returns <code>false</code>.</dt>
  381. </dl>
  382. </div>
  383. <h2 class="subsection-title">Source</h2>
  384. <p>
  385. <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>
  386. </p>
  387. </article>
  388. </section>
  389. <script src="../scripts/linenumber.js"></script>
  390. <script src="../scripts/page.js"></script>
  391. </body>
  392. </html>
粤ICP备19079148号