AnnotationTool.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Annotation} from "../Annotation.js";
  3. import {Utils} from "../utils.js";
  4. import {CameraMode} from "../defines.js";
  5. import {EventDispatcher} from "../EventDispatcher.js";
  6. export class AnnotationTool extends EventDispatcher{
  7. constructor (viewer) {
  8. super();
  9. this.viewer = viewer;
  10. this.renderer = viewer.renderer;
  11. this.sg = new THREE.SphereGeometry(0.1);
  12. this.sm = new THREE.MeshNormalMaterial();
  13. this.s = new THREE.Mesh(this.sg, this.sm);
  14. }
  15. startInsertion (args = {}) {
  16. let domElement = this.viewer.renderer.domElement;
  17. let annotation = new Annotation({
  18. position: [589748.270, 231444.540, 753.675],
  19. title: "Annotation Title",
  20. description: `Annotation Description`
  21. });
  22. this.dispatchEvent({type: 'start_inserting_annotation', annotation: annotation});
  23. const annotations = this.viewer.scene.annotations;
  24. annotations.add(annotation);
  25. let callbacks = {
  26. cancel: null,
  27. finish: null,
  28. };
  29. let insertionCallback = (e) => {
  30. if (e.button === THREE.MOUSE.LEFT) {
  31. callbacks.finish();
  32. } else if (e.button === THREE.MOUSE.RIGHT) {
  33. callbacks.cancel();
  34. }
  35. };
  36. callbacks.cancel = e => {
  37. annotations.remove(annotation);
  38. domElement.removeEventListener('mouseup', insertionCallback, true);
  39. };
  40. callbacks.finish = e => {
  41. domElement.removeEventListener('mouseup', insertionCallback, true);
  42. };
  43. domElement.addEventListener('mouseup', insertionCallback, true);
  44. let drag = (e) => {
  45. let I = Utils.getMousePointCloudIntersection(
  46. e.drag.end,
  47. e.viewer.scene.getActiveCamera(),
  48. e.viewer,
  49. e.viewer.scene.pointclouds,
  50. {pickClipped: true});
  51. if (I) {
  52. this.s.position.copy(I.location);
  53. annotation.position.copy(I.location);
  54. }
  55. };
  56. let drop = (e) => {
  57. viewer.scene.scene.remove(this.s);
  58. this.s.removeEventListener("drag", drag);
  59. this.s.removeEventListener("drop", drop);
  60. };
  61. this.s.addEventListener('drag', drag);
  62. this.s.addEventListener('drop', drop);
  63. this.viewer.scene.scene.add(this.s);
  64. this.viewer.inputHandler.startDragging(this.s);
  65. return annotation;
  66. }
  67. update(){
  68. // let camera = this.viewer.scene.getActiveCamera();
  69. // let domElement = this.renderer.domElement;
  70. // let measurements = this.viewer.scene.measurements;
  71. // const renderAreaSize = this.renderer.getSize(new THREE.Vector2());
  72. // let clientWidth = renderAreaSize.width;
  73. // let clientHeight = renderAreaSize.height;
  74. }
  75. render(){
  76. //this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera());
  77. }
  78. };