Raycaster.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Raycaster - 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">Raycaster</h1>
  13. <section>
  14. <header>
  15. <div class="class-description"><p>This class is designed to assist with raycasting. Raycasting is used for
  16. mouse picking (working out what objects in the 3d space the mouse is over)
  17. amongst other things.</p></div>
  18. </header>
  19. <article>
  20. <div class="container-overview">
  21. <h2>Constructor</h2>
  22. <h3 class="name name-method" id="Raycaster" translate="no">new <a href="#Raycaster">Raycaster</a><span class="signature">( origin : <span class="param-type"><a href="Vector3.html">Vector3</a></span>, direction : <span class="param-type"><a href="Vector3.html">Vector3</a></span>, near : <span class="param-type">number</span>, far : <span class="param-type">number</span> )</span> </h3>
  23. <div class="method">
  24. <div class="description">
  25. <p>Constructs a new raycaster.</p>
  26. </div>
  27. <table class="params">
  28. <tbody>
  29. <tr>
  30. <td class="name">
  31. <strong>origin</strong>
  32. </td>
  33. <td class="description last">
  34. <p>The origin vector where the ray casts from.</p>
  35. </td>
  36. </tr>
  37. <tr>
  38. <td class="name">
  39. <strong>direction</strong>
  40. </td>
  41. <td class="description last">
  42. <p>The (normalized) direction vector that gives direction to the ray.</p>
  43. </td>
  44. </tr>
  45. <tr>
  46. <td class="name">
  47. <strong>near</strong>
  48. </td>
  49. <td class="description last">
  50. <p>All results returned are further away than near. Near can't be negative.</p>
  51. <p>Default is <code>0</code>.</p>
  52. </td>
  53. </tr>
  54. <tr>
  55. <td class="name">
  56. <strong>far</strong>
  57. </td>
  58. <td class="description last">
  59. <p>All results returned are closer than far. Far can't be lower than near.</p>
  60. <p>Default is <code>Infinity</code>.</p>
  61. </td>
  62. </tr>
  63. </tbody>
  64. </table>
  65. </div>
  66. </div>
  67. <h2 class="subsection-title">Properties</h2>
  68. <div class="member">
  69. <h3 class="name" id="camera" translate="no">.<a href="#camera">camera</a><span class="type-signature"> : <a href="Camera.html">Camera</a></span> </h3>
  70. <div class="description">
  71. <p>The camera to use when raycasting against view-dependent objects such as
  72. billboarded objects like sprites. This field can be set manually or
  73. is set when calling <code>setFromCamera()</code>.</p>
  74. <p>Default is <code>null</code>.</p>
  75. </div>
  76. </div>
  77. <div class="member">
  78. <h3 class="name" id="far" translate="no">.<a href="#far">far</a><span class="type-signature"> : number</span> </h3>
  79. <div class="description">
  80. <p>All results returned are closer than far. Far can't be lower than near.</p>
  81. <p>Default is <code>Infinity</code>.</p>
  82. </div>
  83. </div>
  84. <div class="member">
  85. <h3 class="name" id="layers" translate="no">.<a href="#layers">layers</a><span class="type-signature"> : <a href="Layers.html">Layers</a></span> </h3>
  86. <div class="description">
  87. <p>Allows to selectively ignore 3D objects when performing intersection tests.
  88. The following code example ensures that only 3D objects on layer <code>1</code> will be
  89. honored by raycaster.</p>
  90. <pre><code class="language-js">raycaster.layers.set( 1 );
  91. object.layers.enable( 1 );
  92. </code></pre>
  93. </div>
  94. </div>
  95. <div class="member">
  96. <h3 class="name" id="near" translate="no">.<a href="#near">near</a><span class="type-signature"> : number</span> </h3>
  97. <div class="description">
  98. <p>All results returned are further away than near. Near can't be negative.</p>
  99. <p>Default is <code>0</code>.</p>
  100. </div>
  101. </div>
  102. <div class="member">
  103. <h3 class="name" id="params" translate="no">.<a href="#params">params</a><span class="type-signature"> : Object</span> </h3>
  104. <div class="description">
  105. <p>A parameter object that configures the raycasting. It has the structure:</p>
  106. <pre class="prettyprint source"><code>{
  107. Mesh: {},
  108. Line: { threshold: 1 },
  109. LOD: {},
  110. Points: { threshold: 1 },
  111. Sprite: {}
  112. }
  113. </code></pre>
  114. <p>Where <code>threshold</code> is the precision of the raycaster when intersecting objects, in world units.</p>
  115. </div>
  116. </div>
  117. <div class="member">
  118. <h3 class="name" id="ray" translate="no">.<a href="#ray">ray</a><span class="type-signature"> : <a href="Ray.html">Ray</a></span> </h3>
  119. <div class="description">
  120. <p>The ray used for raycasting.</p>
  121. </div>
  122. </div>
  123. <h2 class="subsection-title">Methods</h2>
  124. <h3 class="name name-method" id="intersectObject" translate="no">.<a href="#intersectObject">intersectObject</a><span class="signature">( object : <span class="param-type"><a href="Object3D.html">Object3D</a></span>, recursive : <span class="param-type">boolean</span>, intersects : <span class="param-type">Array.&lt;<a href="Raycaster.html#~Intersection">Raycaster~Intersection</a>></span> )</span><span class="type-signature"> : Array.&lt;<a href="Raycaster.html#~Intersection">Raycaster~Intersection</a>></span> </h3>
  125. <div class="method">
  126. <div class="description">
  127. <p>Checks all intersection between the ray and the object with or without the
  128. descendants. Intersections are returned sorted by distance, closest first.</p>
  129. <p><code>Raycaster</code> delegates to the <code>raycast()</code> method of the passed 3D object, when
  130. evaluating whether the ray intersects the object or not. This allows meshes to respond
  131. differently to ray casting than lines or points.</p>
  132. <p>Note that for meshes, faces must be pointed towards the origin of the ray in order
  133. to be detected; intersections of the ray passing through the back of a face will not
  134. be detected. To raycast against both faces of an object, you'll want to set <a href="Material.html#side">Material#side</a>
  135. to <code>THREE.DoubleSide</code>.</p>
  136. </div>
  137. <table class="params">
  138. <tbody>
  139. <tr>
  140. <td class="name">
  141. <strong>object</strong>
  142. </td>
  143. <td class="description last">
  144. <p>The 3D object to check for intersection with the ray.</p>
  145. </td>
  146. </tr>
  147. <tr>
  148. <td class="name">
  149. <strong>recursive</strong>
  150. </td>
  151. <td class="description last">
  152. <p>If set to <code>true</code>, it also checks all descendants.
  153. Otherwise it only checks intersection with the object.</p>
  154. <p>Default is <code>true</code>.</p>
  155. </td>
  156. </tr>
  157. <tr>
  158. <td class="name">
  159. <strong>intersects</strong>
  160. </td>
  161. <td class="description last">
  162. <p>The target array that holds the result of the method.</p>
  163. <p>Default is <code>[]</code>.</p>
  164. </td>
  165. </tr>
  166. </tbody>
  167. </table>
  168. <dl class="details">
  169. <dt class="tag-returns"><strong>Returns:</strong> An array holding the intersection points.</dt>
  170. </dl>
  171. </div>
  172. <h3 class="name name-method" id="intersectObjects" translate="no">.<a href="#intersectObjects">intersectObjects</a><span class="signature">( objects : <span class="param-type">Array.&lt;<a href="Object3D.html">Object3D</a>></span>, recursive : <span class="param-type">boolean</span>, intersects : <span class="param-type">Array.&lt;<a href="Raycaster.html#~Intersection">Raycaster~Intersection</a>></span> )</span><span class="type-signature"> : Array.&lt;<a href="Raycaster.html#~Intersection">Raycaster~Intersection</a>></span> </h3>
  173. <div class="method">
  174. <div class="description">
  175. <p>Checks all intersection between the ray and the objects with or without
  176. the descendants. Intersections are returned sorted by distance, closest first.</p>
  177. </div>
  178. <table class="params">
  179. <tbody>
  180. <tr>
  181. <td class="name">
  182. <strong>objects</strong>
  183. </td>
  184. <td class="description last">
  185. <p>The 3D objects to check for intersection with the ray.</p>
  186. </td>
  187. </tr>
  188. <tr>
  189. <td class="name">
  190. <strong>recursive</strong>
  191. </td>
  192. <td class="description last">
  193. <p>If set to <code>true</code>, it also checks all descendants.
  194. Otherwise it only checks intersection with the object.</p>
  195. <p>Default is <code>true</code>.</p>
  196. </td>
  197. </tr>
  198. <tr>
  199. <td class="name">
  200. <strong>intersects</strong>
  201. </td>
  202. <td class="description last">
  203. <p>The target array that holds the result of the method.</p>
  204. <p>Default is <code>[]</code>.</p>
  205. </td>
  206. </tr>
  207. </tbody>
  208. </table>
  209. <dl class="details">
  210. <dt class="tag-returns"><strong>Returns:</strong> An array holding the intersection points.</dt>
  211. </dl>
  212. </div>
  213. <h3 class="name name-method" id="set" translate="no">.<a href="#set">set</a><span class="signature">( origin : <span class="param-type"><a href="Vector3.html">Vector3</a></span>, direction : <span class="param-type"><a href="Vector3.html">Vector3</a></span> )</span> </h3>
  214. <div class="method">
  215. <div class="description">
  216. <p>Updates the ray with a new origin and direction by copying the values from the arguments.</p>
  217. </div>
  218. <table class="params">
  219. <tbody>
  220. <tr>
  221. <td class="name">
  222. <strong>origin</strong>
  223. </td>
  224. <td class="description last">
  225. <p>The origin vector where the ray casts from.</p>
  226. </td>
  227. </tr>
  228. <tr>
  229. <td class="name">
  230. <strong>direction</strong>
  231. </td>
  232. <td class="description last">
  233. <p>The (normalized) direction vector that gives direction to the ray.</p>
  234. </td>
  235. </tr>
  236. </tbody>
  237. </table>
  238. </div>
  239. <h3 class="name name-method" id="setFromCamera" translate="no">.<a href="#setFromCamera">setFromCamera</a><span class="signature">( coords : <span class="param-type"><a href="Vector2.html">Vector2</a></span>, camera : <span class="param-type"><a href="Camera.html">Camera</a></span> )</span> </h3>
  240. <div class="method">
  241. <div class="description">
  242. <p>Uses the given coordinates and camera to compute a new origin and direction for the internal ray.</p>
  243. </div>
  244. <table class="params">
  245. <tbody>
  246. <tr>
  247. <td class="name">
  248. <strong>coords</strong>
  249. </td>
  250. <td class="description last">
  251. <p>2D coordinates of the mouse, in normalized device coordinates (NDC).
  252. X and Y components should be between <code>-1</code> and <code>1</code>.</p>
  253. </td>
  254. </tr>
  255. <tr>
  256. <td class="name">
  257. <strong>camera</strong>
  258. </td>
  259. <td class="description last">
  260. <p>The camera from which the ray should originate.</p>
  261. </td>
  262. </tr>
  263. </tbody>
  264. </table>
  265. </div>
  266. <h3 class="name name-method" id="setFromXRController" translate="no">.<a href="#setFromXRController">setFromXRController</a><span class="signature">( controller : <span class="param-type"><a href="WebXRController.html">WebXRController</a></span> )</span><span class="type-signature"> : <a href="Raycaster.html">Raycaster</a></span> </h3>
  267. <div class="method">
  268. <div class="description">
  269. <p>Uses the given WebXR controller to compute a new origin and direction for the internal ray.</p>
  270. </div>
  271. <table class="params">
  272. <tbody>
  273. <tr>
  274. <td class="name">
  275. <strong>controller</strong>
  276. </td>
  277. <td class="description last">
  278. <p>The controller to copy the position and direction from.</p>
  279. </td>
  280. </tr>
  281. </tbody>
  282. </table>
  283. <dl class="details">
  284. <dt class="tag-returns"><strong>Returns:</strong> A reference to this raycaster.</dt>
  285. </dl>
  286. </div>
  287. <h2 class="subsection-title">Type Definitions</h2>
  288. <div class="member">
  289. <h3 class="name" id="~Intersection" translate="no">.<a href="#~Intersection">Intersection</a> </h3>
  290. <div class="description">
  291. <p>The intersection point of a raycaster intersection test.</p>
  292. </div>
  293. <table class="props">
  294. <tbody>
  295. <tr>
  296. <td class="name">
  297. <strong>distance</strong>
  298. <br>
  299. <span class="param-type">number</span>
  300. </td>
  301. <td class="description last">
  302. <p>The distance from the ray's origin to the intersection point.</p>
  303. </td>
  304. </tr>
  305. <tr>
  306. <td class="name">
  307. <strong>distanceToRay</strong>
  308. <br>
  309. <span class="param-type">number</span>
  310. </td>
  311. <td class="description last">
  312. <p>Some 3D objects e.g. <a href="Points.html">Points</a> provide the distance of the
  313. intersection to the nearest point on the ray. For other objects it will be <code>undefined</code>.</p>
  314. </td>
  315. </tr>
  316. <tr>
  317. <td class="name">
  318. <strong>point</strong>
  319. <br>
  320. <span class="param-type"><a href="Vector3.html">Vector3</a></span>
  321. </td>
  322. <td class="description last">
  323. <p>The intersection point, in world coordinates.</p>
  324. </td>
  325. </tr>
  326. <tr>
  327. <td class="name">
  328. <strong>face</strong>
  329. <br>
  330. <span class="param-type">Object</span>
  331. </td>
  332. <td class="description last">
  333. <p>The face that has been intersected.</p>
  334. </td>
  335. </tr>
  336. <tr>
  337. <td class="name">
  338. <strong>faceIndex</strong>
  339. <br>
  340. <span class="param-type">number</span>
  341. </td>
  342. <td class="description last">
  343. <p>The face index.</p>
  344. </td>
  345. </tr>
  346. <tr>
  347. <td class="name">
  348. <strong>object</strong>
  349. <br>
  350. <span class="param-type"><a href="Object3D.html">Object3D</a></span>
  351. </td>
  352. <td class="description last">
  353. <p>The 3D object that has been intersected.</p>
  354. </td>
  355. </tr>
  356. <tr>
  357. <td class="name">
  358. <strong>uv</strong>
  359. <br>
  360. <span class="param-type"><a href="Vector2.html">Vector2</a></span>
  361. </td>
  362. <td class="description last">
  363. <p>U,V coordinates at point of intersection.</p>
  364. </td>
  365. </tr>
  366. <tr>
  367. <td class="name">
  368. <strong>uv1</strong>
  369. <br>
  370. <span class="param-type"><a href="Vector2.html">Vector2</a></span>
  371. </td>
  372. <td class="description last">
  373. <p>Second set of U,V coordinates at point of intersection.</p>
  374. </td>
  375. </tr>
  376. <tr>
  377. <td class="name">
  378. <strong>normal</strong>
  379. <br>
  380. <span class="param-type"><a href="Vector3.html">Vector3</a></span>
  381. </td>
  382. <td class="description last">
  383. <p>Interpolated normal vector at point of intersection.</p>
  384. </td>
  385. </tr>
  386. <tr>
  387. <td class="name">
  388. <strong>instanceId</strong>
  389. <br>
  390. <span class="param-type">number</span>
  391. </td>
  392. <td class="description last">
  393. <p>The index number of the instance where the ray
  394. intersects the <a href="InstancedMesh.html">InstancedMesh</a>.</p>
  395. </td>
  396. </tr>
  397. </tbody>
  398. </table>
  399. </div>
  400. <h2 class="subsection-title">Source</h2>
  401. <p>
  402. <a href="https://github.com/mrdoob/three.js/blob/master/src/core/Raycaster.js" translate="no" target="_blank" rel="noopener">src/core/Raycaster.js</a>
  403. </p>
  404. </article>
  405. </section>
  406. <script src="../scripts/linenumber.js"></script>
  407. <script src="../scripts/page.js"></script>
  408. </body>
  409. </html>
粤ICP备19079148号