Octree.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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">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">Box3</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"><code>box</code></td>
  41. <td class="description last"><p>The base box with enclose the entire Octree.</p></td>
  42. </tr>
  43. </tbody>
  44. </table>
  45. </div>
  46. </div>
  47. <h2 class="subsection-title">Properties</h2>
  48. <div class="member">
  49. <h3 class="name" id="bounds" translate="no">.<a href="#bounds">bounds</a><span class="type-signature"> : <a href="Box3.html">Box3</a></span> </h3>
  50. <div class="description">
  51. <p>The bounds of the Octree. Compared to <a href="Octree.html#box">Octree#box</a>, no
  52. margin is applied.</p>
  53. </div>
  54. </div>
  55. <div class="member">
  56. <h3 class="name" id="box" translate="no">.<a href="#box">box</a><span class="type-signature"> : <a href="Box3.html">Box3</a></span> </h3>
  57. <div class="description">
  58. <p>The base box with enclose the entire Octree.</p>
  59. </div>
  60. </div>
  61. <div class="member">
  62. <h3 class="name" id="layers" translate="no">.<a href="#layers">layers</a><span class="type-signature"> : <a href="Layers.html">Layers</a></span> </h3>
  63. <div class="description">
  64. <p>Can by used for layers configuration for refine testing.</p>
  65. </div>
  66. </div>
  67. <div class="member">
  68. <h3 class="name" id="maxLevel" translate="no">.<a href="#maxLevel">maxLevel</a><span class="type-signature"> : number</span> </h3>
  69. <div class="description">
  70. <p>The maximum level of the Octree. It defines the maximum
  71. hierarchical depth of the data structure.<br/>Default is <code>16</code>.</p>
  72. </div>
  73. </div>
  74. <div class="member">
  75. <h3 class="name" id="trianglesPerLeaf" translate="no">.<a href="#trianglesPerLeaf">trianglesPerLeaf</a><span class="type-signature"> : number</span> </h3>
  76. <div class="description">
  77. <p>The number of triangles a leaf can store before it is split.<br/>Default is <code>8</code>.</p>
  78. </div>
  79. </div>
  80. <h2 class="subsection-title">Methods</h2>
  81. <h3 class="name name-method" id="addTriangle" translate="no">.<a href="#addTriangle">addTriangle</a><span class="signature">( triangle : <span class="param-type">Triangle</span> )</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  82. <div class="method">
  83. <div class="description">
  84. <p>Adds the given triangle to the Octree. The triangle vertices are clamped if they exceed
  85. the bounds of the Octree.</p>
  86. </div>
  87. <table class="params">
  88. <tbody>
  89. <tr>
  90. <td class="name"><code>triangle</code></td>
  91. <td class="description last"><p>The triangle to add.</p></td>
  92. </tr>
  93. </tbody>
  94. </table>
  95. <dl class="details">
  96. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  97. </dl>
  98. </div>
  99. <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>
  100. <div class="method">
  101. <div class="description">
  102. <p>Builds the Octree.</p>
  103. </div>
  104. <dl class="details">
  105. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  106. </dl>
  107. </div>
  108. <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>
  109. <div class="method">
  110. <div class="description">
  111. <p>Prepares <a href="Octree.html#box">Octree#box</a> for the build.</p>
  112. </div>
  113. <dl class="details">
  114. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  115. </dl>
  116. </div>
  117. <h3 class="name name-method" id="capsuleIntersect" translate="no">.<a href="#capsuleIntersect">capsuleIntersect</a><span class="signature">( capsule : <span class="param-type">Capsule</span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  118. <div class="method">
  119. <div class="description">
  120. <p>Performs a capsule intersection test with this Octree.</p>
  121. </div>
  122. <table class="params">
  123. <tbody>
  124. <tr>
  125. <td class="name"><code>capsule</code></td>
  126. <td class="description last"><p>The capsule to test.</p></td>
  127. </tr>
  128. </tbody>
  129. </table>
  130. <dl class="details">
  131. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  132. is detected, the method returns <code>false</code>.</dt>
  133. </dl>
  134. </div>
  135. <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>
  136. <div class="method">
  137. <div class="description">
  138. <p>Clears the Octree by making it empty.</p>
  139. </div>
  140. <dl class="details">
  141. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  142. </dl>
  143. </div>
  144. <h3 class="name name-method" id="fromGraphNode" translate="no">.<a href="#fromGraphNode">fromGraphNode</a><span class="signature">( group : <span class="param-type">Object3D</span> )</span><span class="type-signature"> : <a href="Octree.html">Octree</a></span> </h3>
  145. <div class="method">
  146. <div class="description">
  147. <p>Constructs the Octree from the given 3D object.</p>
  148. </div>
  149. <table class="params">
  150. <tbody>
  151. <tr>
  152. <td class="name"><code>group</code></td>
  153. <td class="description last"><p>The scene graph node.</p></td>
  154. </tr>
  155. </tbody>
  156. </table>
  157. <dl class="details">
  158. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  159. </dl>
  160. </div>
  161. <h3 class="name name-method" id="getCapsuleTriangles" translate="no">.<a href="#getCapsuleTriangles">getCapsuleTriangles</a><span class="signature">( capsule : <span class="param-type">Capsule</span>, triangles : <span class="param-type">Array.&lt;Triangle></span> )</span> </h3>
  162. <div class="method">
  163. <div class="description">
  164. <p>Computes the triangles that potentially intersect with the given capsule.</p>
  165. </div>
  166. <table class="params">
  167. <tbody>
  168. <tr>
  169. <td class="name"><code>capsule</code></td>
  170. <td class="description last"><p>The capsule to test.</p></td>
  171. </tr>
  172. <tr>
  173. <td class="name"><code>triangles</code></td>
  174. <td class="description last"><p>The target array that holds the triangles.</p></td>
  175. </tr>
  176. </tbody>
  177. </table>
  178. </div>
  179. <h3 class="name name-method" id="getRayTriangles" translate="no">.<a href="#getRayTriangles">getRayTriangles</a><span class="signature">( ray : <span class="param-type">Ray</span>, triangles : <span class="param-type">Array.&lt;Triangle></span> )</span> </h3>
  180. <div class="method">
  181. <div class="description">
  182. <p>Computes the triangles that potentially intersect with the given ray.</p>
  183. </div>
  184. <table class="params">
  185. <tbody>
  186. <tr>
  187. <td class="name"><code>ray</code></td>
  188. <td class="description last"><p>The ray to test.</p></td>
  189. </tr>
  190. <tr>
  191. <td class="name"><code>triangles</code></td>
  192. <td class="description last"><p>The target array that holds the triangles.</p></td>
  193. </tr>
  194. </tbody>
  195. </table>
  196. </div>
  197. <h3 class="name name-method" id="getSphereTriangles" translate="no">.<a href="#getSphereTriangles">getSphereTriangles</a><span class="signature">( sphere : <span class="param-type">Sphere</span>, triangles : <span class="param-type">Array.&lt;Triangle></span> )</span> </h3>
  198. <div class="method">
  199. <div class="description">
  200. <p>Computes the triangles that potentially intersect with the given bounding sphere.</p>
  201. </div>
  202. <table class="params">
  203. <tbody>
  204. <tr>
  205. <td class="name"><code>sphere</code></td>
  206. <td class="description last"><p>The sphere to test.</p></td>
  207. </tr>
  208. <tr>
  209. <td class="name"><code>triangles</code></td>
  210. <td class="description last"><p>The target array that holds the triangles.</p></td>
  211. </tr>
  212. </tbody>
  213. </table>
  214. </div>
  215. <h3 class="name name-method" id="rayIntersect" translate="no">.<a href="#rayIntersect">rayIntersect</a><span class="signature">( ray : <span class="param-type">Ray</span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  216. <div class="method">
  217. <div class="description">
  218. <p>Performs a ray intersection test with this Octree.</p>
  219. </div>
  220. <table class="params">
  221. <tbody>
  222. <tr>
  223. <td class="name"><code>ray</code></td>
  224. <td class="description last"><p>The ray to test.</p></td>
  225. </tr>
  226. </tbody>
  227. </table>
  228. <dl class="details">
  229. <dt class="tag-returns"><strong>Returns:</strong> The nearest intersection object. If no intersection
  230. is detected, the method returns <code>false</code>.</dt>
  231. </dl>
  232. </div>
  233. <h3 class="name name-method" id="sphereIntersect" translate="no">.<a href="#sphereIntersect">sphereIntersect</a><span class="signature">( sphere : <span class="param-type">Sphere</span> )</span><span class="type-signature"> : Object | boolean</span> </h3>
  234. <div class="method">
  235. <div class="description">
  236. <p>Performs a bounding sphere intersection test with this Octree.</p>
  237. </div>
  238. <table class="params">
  239. <tbody>
  240. <tr>
  241. <td class="name"><code>sphere</code></td>
  242. <td class="description last"><p>The bounding sphere to test.</p></td>
  243. </tr>
  244. </tbody>
  245. </table>
  246. <dl class="details">
  247. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  248. is detected, the method returns <code>false</code>.</dt>
  249. </dl>
  250. </div>
  251. <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>
  252. <div class="method">
  253. <div class="description">
  254. <p>Splits the Octree. This method is used recursively when
  255. building the Octree.</p>
  256. </div>
  257. <table class="params">
  258. <tbody>
  259. <tr>
  260. <td class="name"><code>level</code></td>
  261. <td class="description last"><p>The current level.</p></td>
  262. </tr>
  263. </tbody>
  264. </table>
  265. <dl class="details">
  266. <dt class="tag-returns"><strong>Returns:</strong> A reference to this Octree.</dt>
  267. </dl>
  268. </div>
  269. <h3 class="name name-method" id="triangleCapsuleIntersect" translate="no">.<a href="#triangleCapsuleIntersect">triangleCapsuleIntersect</a><span class="signature">( capsule : <span class="param-type">Capsule</span>, triangle : <span class="param-type">Triangle</span> )</span><span class="type-signature"> : Object | false</span> </h3>
  270. <div class="method">
  271. <div class="description">
  272. <p>Computes the intersection between the given capsule and triangle.</p>
  273. </div>
  274. <table class="params">
  275. <tbody>
  276. <tr>
  277. <td class="name"><code>capsule</code></td>
  278. <td class="description last"><p>The capsule to test.</p></td>
  279. </tr>
  280. <tr>
  281. <td class="name"><code>triangle</code></td>
  282. <td class="description last"><p>The triangle to test.</p></td>
  283. </tr>
  284. </tbody>
  285. </table>
  286. <dl class="details">
  287. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  288. is detected, the method returns <code>false</code>.</dt>
  289. </dl>
  290. </div>
  291. <h3 class="name name-method" id="triangleSphereIntersect" translate="no">.<a href="#triangleSphereIntersect">triangleSphereIntersect</a><span class="signature">( sphere : <span class="param-type">Sphere</span>, triangle : <span class="param-type">Triangle</span> )</span><span class="type-signature"> : Object | false</span> </h3>
  292. <div class="method">
  293. <div class="description">
  294. <p>Computes the intersection between the given sphere and triangle.</p>
  295. </div>
  296. <table class="params">
  297. <tbody>
  298. <tr>
  299. <td class="name"><code>sphere</code></td>
  300. <td class="description last"><p>The sphere to test.</p></td>
  301. </tr>
  302. <tr>
  303. <td class="name"><code>triangle</code></td>
  304. <td class="description last"><p>The triangle to test.</p></td>
  305. </tr>
  306. </tbody>
  307. </table>
  308. <dl class="details">
  309. <dt class="tag-returns"><strong>Returns:</strong> The intersection object. If no intersection
  310. is detected, the method returns <code>false</code>.</dt>
  311. </dl>
  312. </div>
  313. <h2 class="subsection-title">Source</h2>
  314. <p>
  315. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/math/Octree.js" target="_blank" rel="noopener" translate="no">examples/jsm/math/Octree.js</a>
  316. </p>
  317. </article>
  318. </section>
  319. <script src="../scripts/linenumber.js"></script>
  320. <script src="../scripts/page.js"></script>
  321. </body>
  322. </html>
粤ICP备19079148号