AnnotationTool.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. radius: 1,//add
  22. cameraPosition: [589748.270, 231444.540, 753.675],//add
  23. });
  24. this.dispatchEvent({type: 'start_inserting_annotation', annotation: annotation});
  25. const annotations = this.viewer.scene.annotations;
  26. annotations.add(annotation);
  27. let callbacks = {
  28. cancel: null,
  29. finish: null,
  30. };
  31. let insertionCallback = (e) => {
  32. if (e.button === THREE.MOUSE.LEFT) {
  33. callbacks.finish();
  34. } else if (e.button === THREE.MOUSE.RIGHT) {
  35. callbacks.cancel();
  36. }
  37. };
  38. callbacks.cancel = e => {
  39. annotations.remove(annotation);
  40. domElement.removeEventListener('mouseup', insertionCallback, true);
  41. };
  42. callbacks.finish = e => {
  43. domElement.removeEventListener('mouseup', insertionCallback, true);
  44. };
  45. domElement.addEventListener('mouseup', insertionCallback, true);
  46. let drag = (e) => {
  47. let camera = e.viewer.scene.getActiveCamera()
  48. /* let I = Utils.getMousePointCloudIntersection(
  49. e.drag.end,
  50. camera,
  51. e.viewer,
  52. e.viewer.scene.pointclouds,
  53. {pickClipped: true}); */
  54. let I = e.viewer.inputHandler.intersectPoint
  55. if (I) {
  56. this.s.position.copy(I.location);
  57. annotation.position.copy(I.location);
  58. annotation.cameraTarget.copy(I.location);//add
  59. annotation.cameraPosition.copy(camera.position);//add
  60. }
  61. };
  62. let drop = (e) => {
  63. viewer.scene.scene.remove(this.s);
  64. this.s.removeEventListener("drag", drag);
  65. this.s.removeEventListener("drop", drop);
  66. };
  67. this.s.addEventListener('drag', drag);
  68. this.s.addEventListener('drop', drop);
  69. this.viewer.scene.scene.add(this.s);
  70. this.viewer.inputHandler.startDragging(this.s);
  71. return annotation;
  72. }
  73. update(){
  74. // let camera = this.viewer.scene.getActiveCamera();
  75. // let domElement = this.renderer.domElement;
  76. // let measurements = this.viewer.scene.measurements;
  77. // const renderAreaSize = this.renderer.getSize(new THREE.Vector2());
  78. // let clientWidth = renderAreaSize.width;
  79. // let clientHeight = renderAreaSize.height;
  80. }
  81. render(){
  82. //this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera());
  83. }
  84. };