Raycaster.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. This class is designed to assist with
  13. [link:https://en.wikipedia.org/wiki/Ray_casting raycasting]. Raycasting is
  14. used for mouse picking (working out what objects in the 3d space the mouse
  15. is over) amongst other things.
  16. </p>
  17. <h2>Code Example</h2>
  18. <code>
  19. const raycaster = new THREE.Raycaster();
  20. const pointer = new THREE.Vector2();
  21. function onPointerMove( event ) {
  22. // calculate pointer position in normalized device coordinates
  23. // (-1 to +1) for both components
  24. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  25. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  26. }
  27. function render() {
  28. // update the picking ray with the camera and pointer position
  29. raycaster.setFromCamera( pointer, camera );
  30. // calculate objects intersecting the picking ray
  31. const intersects = raycaster.intersectObjects( scene.children );
  32. for ( let i = 0; i < intersects.length; i ++ ) {
  33. intersects[ i ].object.material.color.set( 0xff0000 );
  34. }
  35. renderer.render( scene, camera );
  36. }
  37. window.addEventListener( 'pointermove', onPointerMove );
  38. window.requestAnimationFrame(render);
  39. </code>
  40. <h2>Examples</h2>
  41. <p>
  42. [example:webgl_interactive_cubes Raycasting to a Mesh]<br />
  43. [example:webgl_interactive_cubes_ortho Raycasting to a Mesh in using an OrthographicCamera]<br />
  44. [example:webgl_interactive_buffergeometry Raycasting to a Mesh with BufferGeometry]<br />
  45. [example:webgl_instancing_raycast Raycasting to a InstancedMesh]<br />
  46. [example:webgl_interactive_lines Raycasting to a Line]<br />
  47. [example:webgl_interactive_raycasting_points Raycasting to Points]<br />
  48. [example:webgl_geometry_terrain_raycast Terrain raycasting]<br />
  49. [example:webgl_interactive_voxelpainter Raycasting to paint voxels]<br />
  50. [example:webgl_raycaster_texture Raycast to a Texture]
  51. </p>
  52. <h2>Constructor</h2>
  53. <h3>[name]( [param:Vector3 origin], [param:Vector3 direction], [param:Float near], [param:Float far] )</h3>
  54. <p>
  55. [page:Vector3 origin] — The origin vector where the ray casts from.<br />
  56. [page:Vector3 direction] — The direction vector that gives direction to
  57. the ray. Should be normalized.<br />
  58. [page:Float near] — All results returned are further away than near. Near
  59. can't be negative. Default value is `0`.<br />
  60. [page:Float far] — All results returned are closer than far. Far can't be
  61. lower than near. Default value is Infinity.
  62. </p>
  63. <p>This creates a new raycaster object.<br /></p>
  64. <h2>Properties</h2>
  65. <h3>[property:Float far]</h3>
  66. <p>
  67. The far factor of the raycaster. This value indicates which objects can be
  68. discarded based on the distance. This value shouldn't be negative and
  69. should be larger than the near property.
  70. </p>
  71. <h3>[property:Float near]</h3>
  72. <p>
  73. The near factor of the raycaster. This value indicates which objects can
  74. be discarded based on the distance. This value shouldn't be negative and
  75. should be smaller than the far property.
  76. </p>
  77. <h3>[property:Camera camera]</h3>
  78. <p>
  79. The camera to use when raycasting against view-dependent objects such as
  80. billboarded objects like [page:Sprites]. This field can be set manually or
  81. is set when calling "setFromCamera". Defaults to null.
  82. </p>
  83. <h3>[property:Layers layers]</h3>
  84. <p>
  85. Used by [name] to selectively ignore 3D objects when performing
  86. intersection tests. The following code example ensures that only 3D
  87. objects on layer `1` will be honored by the instance of [name].
  88. <code>
  89. raycaster.layers.set( 1 );
  90. object.layers.enable( 1 );
  91. </code>
  92. </p>
  93. <h3>[property:Object params]</h3>
  94. <p>
  95. An object with the following properties:
  96. <code>
  97. {
  98. Mesh: {},
  99. Line: { threshold: 1 },
  100. LOD: {},
  101. Points: { threshold: 1 },
  102. Sprite: {}
  103. }
  104. </code>
  105. Where threshold is the precision of the raycaster when intersecting
  106. objects, in world units.
  107. </p>
  108. <h3>[property:Ray ray]</h3>
  109. <p>The [Page:Ray] used for the raycasting.</p>
  110. <h2>Methods</h2>
  111. <h3>[method:undefined set]( [param:Vector3 origin], [param:Vector3 direction])</h3>
  112. <p>
  113. [page:Vector3 origin] — The origin vector where the ray casts from.<br />
  114. [page:Vector3 direction] — The normalized direction vector that gives
  115. direction to the ray.
  116. </p>
  117. <p>
  118. Updates the ray with a new origin and direction. Please note that this
  119. method only copies the values from the arguments.
  120. </p>
  121. <h3>[method:undefined setFromCamera]( [param:Vector2 coords], [param:Camera camera] )</h3>
  122. <p>
  123. [page:Vector2 coords] — 2D coordinates of the mouse, in normalized device
  124. coordinates (NDC)---X and Y components should be between `-1` and `1`.<br />
  125. [page:Camera camera] — camera from which the ray should originate
  126. </p>
  127. <p>Updates the ray with a new origin and direction.</p>
  128. <h3>[method:this setFromXRController]( [param:WebXRController controller] )</h3>
  129. <p>
  130. [page:WebXRController controller] — The controller to copy the position and direction from.
  131. </p>
  132. <p>Updates the ray with a new origin and direction.</p>
  133. <h3>[method:Array intersectObject]( [param:Object3D object], [param:Boolean recursive], [param:Array optionalTarget] )</h3>
  134. <p>
  135. [page:Object3D object] — The object to check for intersection with the
  136. ray.<br />
  137. [page:Boolean recursive] — If true, it also checks all descendants.
  138. Otherwise it only checks intersection with the object. Default is true.<br />
  139. [page:Array optionalTarget] — (optional) target to set the result.
  140. Otherwise a new [page:Array] is instantiated. If set, you must clear this
  141. array prior to each call (i.e., array.length = 0;).
  142. </p>
  143. <p>
  144. Checks all intersection between the ray and the object with or without the
  145. descendants. Intersections are returned sorted by distance, closest first.
  146. An array of intersections is returned...
  147. </p>
  148. <code> [ { distance, point, face, faceIndex, object }, ... ] </code>
  149. <p>
  150. [page:Float distance] – distance between the origin of the ray and the
  151. intersection<br />
  152. [page:Vector3 point] – point of intersection, in world coordinates<br />
  153. [page:Object face] – intersected face<br />
  154. [page:Integer faceIndex] – index of the intersected face<br />
  155. [page:Object3D object] – the intersected object<br />
  156. [page:Vector2 uv] - U,V coordinates at point of intersection<br />
  157. [page:Vector2 uv1] - Second set of U,V coordinates at point of
  158. intersection<br />
  159. [page:Vector3 normal] - interpolated normal vector at point of
  160. intersection<br />
  161. [page:Integer instanceId] – The index number of the instance where the ray
  162. intersects the InstancedMesh
  163. </p>
  164. <p>
  165. `Raycaster` delegates to the [page:Object3D.raycast raycast] method of the
  166. passed object, when evaluating whether the ray intersects the object or
  167. not. This allows [page:Mesh meshes] to respond differently to ray casting
  168. than [page:Line lines] and [page:Points pointclouds].
  169. </p>
  170. <p>
  171. *Note* that for meshes, faces must be pointed towards the origin of the
  172. [page:.ray ray] in order to be detected; intersections of the ray passing
  173. through the back of a face will not be detected. To raycast against both
  174. faces of an object, you'll want to set the [page:Mesh.material material]'s
  175. [page:Material.side side] property to `THREE.DoubleSide`.
  176. </p>
  177. <h3>[method:Array intersectObjects]( [param:Array objects], [param:Boolean recursive], [param:Array optionalTarget] )</h3>
  178. <p>
  179. [page:Array objects] — The objects to check for intersection with the
  180. ray.<br />
  181. [page:Boolean recursive] — If true, it also checks all descendants of the
  182. objects. Otherwise it only checks intersection with the objects. Default
  183. is true.<br />
  184. [page:Array optionalTarget] — (optional) target to set the result.
  185. Otherwise a new [page:Array] is instantiated. If set, you must clear this
  186. array prior to each call (i.e., array.length = 0;).
  187. </p>
  188. <p>
  189. Checks all intersection between the ray and the objects with or without
  190. the descendants. Intersections are returned sorted by distance, closest
  191. first. Intersections are of the same form as those returned by
  192. [page:.intersectObject].
  193. </p>
  194. <h2>Source</h2>
  195. <p>
  196. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  197. </p>
  198. </body>
  199. </html>