threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PickingPlugin.md 2.8KB

1 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ---
  2. prev:
  3. text: 'CanvasSnapshotPlugin'
  4. link: './CanvasSnapshotPlugin'
  5. next:
  6. text: 'FullScreenPlugin'
  7. link: './FullScreenPlugin'
  8. ---
  9. # PickingPlugin
  10. [//]: # (todo: image)
  11. [Example](https://threepipe.org/examples/#picking-plugin/) —
  12. [Source Code](https://github.com/repalash/threepipe/blob/master/src/plugins/interaction/PickingPlugin.ts) —
  13. [API Reference](https://threepipe.org/docs/classes/PickingPlugin.html)
  14. Picking Plugin adds support for selecting and hovering over objects in the viewer with user interactions and selection widgets.
  15. When the plugin is added to the viewer, it starts listening to the mouse move and click events over the canvas.
  16. When an object is clicked, it is selected,
  17. and if a UI plugin is added, the uiconfig for the selected object is populated in the interface.
  18. The events `selectedObjectChanged`, `hoverObjectChanged`, and `hitObject` can be listened to on the plugin.
  19. Picking plugin internally uses [ObjectPicker](https://threepipe.org/docs/classes/ObjectPicker.html),
  20. check out the documentation or source code for more information.
  21. ```typescript
  22. import {ThreeViewer, PickingPlugin} from 'threepipe'
  23. const viewer = new ThreeViewer({...})
  24. const pickingPlugin = viewer.addPluginSync(new PickingPlugin())
  25. // Hovering events are also supported, but since its computationally expensive for large scenes it is disabled by default.
  26. pickingPlugin.hoverEnabled = true
  27. pickingPlugin.addEventListener('hitObject', (e)=>{
  28. // This is fired when the user clicks on the canvas.
  29. // The selected object hasn't been changed yet, and we have the option to change it or disable selection at this point.
  30. // e.intersects.selectedObject contains the object that the user clicked on.
  31. console.log('Hit: ', e.intersects.selectedObject)
  32. // It can be changed here
  33. // e.intersects.selectedObject = e.intersects.selectedObject.parent // select the parent
  34. // e.intersects.selectedObject = null // unselect
  35. // Check other properties on the event like intersects, mouse position, normal etc.
  36. console.log(e)
  37. })
  38. pickingPlugin.addEventListener('selectedObjectChanged', (e)=>{
  39. // This is fired when the selected object is changed.
  40. // e.object contains the new selected object. It can be null if nothing is selected.
  41. console.log('Selected: ', e.object)
  42. })
  43. // Objects can be programmatically selected and unselected
  44. // to select
  45. pickingPlugin.setSelectedObject(object)
  46. // get the selected object
  47. console.log(pickingPlugin.getSelectedObject())
  48. // to unselect
  49. pickingPlugin.setSelectedObject(null)
  50. // Select object with camera animation to the object
  51. pickingPlugin.setSelectedObject(object, true)
  52. pickingPlugin.addEventListener('hoverObjectChanged', (e)=>{
  53. // This is fired when the hovered object is changed.
  54. // e.object contains the new hovered object.
  55. console.log('Hovering: ', e.object)
  56. })
  57. ```