Tool.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Tool.js
  3. *
  4. * @author realor
  5. */
  6. import * as THREE from '../lib/three.module.js'
  7. import { I18N } from '../i18n/I18N.js'
  8. class Tool {
  9. constructor(application) {
  10. this.application = application
  11. this.name = 'command' // must be unique
  12. this.label = 'Command'
  13. this.help = 'command help'
  14. this.className = 'command'
  15. this.immediate = false
  16. }
  17. activate() {
  18. console.info('activate ' + this.id)
  19. }
  20. deactivate() {
  21. console.info('deactivate ' + this.id)
  22. }
  23. execute() {
  24. console.info('execute ' + this.id)
  25. }
  26. setOptions(options) {
  27. if (options) {
  28. for (var option in options) {
  29. this[option] = options[option]
  30. }
  31. }
  32. }
  33. intersect(pointerPosition, baseObject, recursive) {
  34. const application = this.application
  35. const camera = application.camera
  36. const container = application.container
  37. const raycaster = new THREE.Raycaster()
  38. let pointercc = new THREE.Vector2()
  39. pointercc.x = (pointerPosition.x / container.clientWidth) * 2 - 1
  40. pointercc.y = -(pointerPosition.y / container.clientHeight) * 2 + 1
  41. raycaster.setFromCamera(pointercc, camera)
  42. raycaster.far = Math.Infinity
  43. raycaster.camera = camera
  44. raycaster.params.Line.threshold = 0.1
  45. let intersects = raycaster.intersectObjects([baseObject], recursive)
  46. let i = 0
  47. let firstIntersect = null
  48. while (i < intersects.length && firstIntersect === null) {
  49. let intersect = intersects[i]
  50. let object = intersect.object
  51. if (application.clippingPlane === null || application.clippingPlane.distanceToPoint(intersect.point) > 0) {
  52. if (this.isPathVisible(object)) firstIntersect = intersect
  53. }
  54. i++
  55. }
  56. return firstIntersect
  57. }
  58. isPathVisible(object) {
  59. while (object !== null && object.visible) {
  60. object = object.parent
  61. }
  62. return object === null
  63. }
  64. getEventPosition(event) {
  65. const x = event.offsetX || event.layerX
  66. const y = event.offsetY || event.layerY
  67. return new THREE.Vector2(x, y)
  68. }
  69. isCanvasEvent(event) {
  70. if (this.application.menuBar.armed) return false
  71. const target = event.target || event.srcElement
  72. return target.nodeName.toLowerCase() === 'canvas'
  73. }
  74. }
  75. export { Tool }