ClippingTool.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {ClipVolume} from "./ClipVolume.js";
  3. import {PolygonClipVolume} from "./PolygonClipVolume.js";
  4. import { EventDispatcher } from "../EventDispatcher.js";
  5. //这个文件只是clipping polygon的,没用到
  6. export class ClippingTool extends EventDispatcher{
  7. constructor(viewer){
  8. super();
  9. this.viewer = viewer;
  10. this.maxPolygonVertices = 8;
  11. this.addEventListener("start_inserting_clipping_volume", e => {
  12. this.viewer.dispatchEvent({
  13. type: "cancel_insertions"
  14. });
  15. });
  16. this.sceneMarker = new THREE.Scene();
  17. this.sceneVolume = new THREE.Scene();
  18. this.sceneVolume.name = "scene_clip_volume";
  19. this.viewer.inputHandler.registerInteractiveScene(this.sceneVolume);
  20. this.onRemove = e => {
  21. this.sceneVolume.remove(e.volume);
  22. };
  23. this.onAdd = e => {
  24. this.sceneVolume.add(e.volume);
  25. };
  26. this.viewer.inputHandler.addEventListener("delete", e => {
  27. let volumes = e.selection.filter(e => (e instanceof ClipVolume));
  28. volumes.forEach(e => this.viewer.scene.removeClipVolume(e));
  29. let polyVolumes = e.selection.filter(e => (e instanceof PolygonClipVolume));
  30. polyVolumes.forEach(e => this.viewer.scene.removePolygonClipVolume(e));
  31. });
  32. }
  33. setScene(scene){
  34. if(this.scene === scene){
  35. return;
  36. }
  37. if(this.scene){
  38. this.scene.removeEventListeners("clip_volume_added", this.onAdd);
  39. this.scene.removeEventListeners("clip_volume_removed", this.onRemove);
  40. this.scene.removeEventListeners("polygon_clip_volume_added", this.onAdd);
  41. this.scene.removeEventListeners("polygon_clip_volume_removed", this.onRemove);
  42. }
  43. this.scene = scene;
  44. this.scene.addEventListener("clip_volume_added", this.onAdd);
  45. this.scene.addEventListener("clip_volume_removed", this.onRemove);
  46. this.scene.addEventListener("polygon_clip_volume_added", this.onAdd);
  47. this.scene.addEventListener("polygon_clip_volume_removed", this.onRemove);
  48. }
  49. startInsertion(args = {}) {
  50. let type = args.type || null;
  51. if(!type) return null;
  52. let domElement = this.viewer.renderer.domElement;
  53. let canvasSize = this.viewer.renderer.getSize(new THREE.Vector2());
  54. let svg = $(`
  55. <svg height="${canvasSize.height}" width="${canvasSize.width}" style="position:absolute; pointer-events: none">
  56. <defs>
  57. <marker id="diamond" markerWidth="24" markerHeight="24" refX="12" refY="12"
  58. markerUnits="userSpaceOnUse">
  59. <circle cx="12" cy="12" r="6" fill="white" stroke="black" stroke-width="3"/>
  60. </marker>
  61. </defs>
  62. <polyline fill="none" stroke="black"
  63. style="stroke:rgb(0, 0, 0);
  64. stroke-width:6;"
  65. stroke-dasharray="9, 6"
  66. stroke-dashoffset="2"
  67. />
  68. <polyline fill="none" stroke="black"
  69. style="stroke:rgb(255, 255, 255);
  70. stroke-width:2;"
  71. stroke-dasharray="5, 10"
  72. marker-start="url(#diamond)"
  73. marker-mid="url(#diamond)"
  74. marker-end="url(#diamond)"
  75. />
  76. </svg>`);
  77. $(domElement.parentElement).append(svg);
  78. let polyClipVol = new PolygonClipVolume(this.viewer.scene.getActiveCamera().clone());
  79. this.dispatchEvent({"type": "start_inserting_clipping_volume"});
  80. this.viewer.scene.addPolygonClipVolume(polyClipVol);
  81. this.sceneMarker.add(polyClipVol);
  82. let cancel = {
  83. callback: null
  84. };
  85. let insertionCallback = (e) => {
  86. if(e.button === THREE.MOUSE.LEFT){
  87. polyClipVol.addMarker();
  88. // SVC Screen Line
  89. svg.find("polyline").each((index, target) => {
  90. let newPoint = svg[0].createSVGPoint();
  91. newPoint.x = e.offsetX;
  92. newPoint.y = e.offsetY;
  93. let polyline = target.points.appendItem(newPoint);
  94. });
  95. if(polyClipVol.markers.length > this.maxPolygonVertices){
  96. cancel.callback();
  97. }
  98. this.viewer.inputHandler.startDragging(
  99. polyClipVol.markers[polyClipVol.markers.length - 1]);
  100. }else if(e.button === THREE.MOUSE.RIGHT){
  101. cancel.callback(e);
  102. }
  103. };
  104. cancel.callback = e => {
  105. //let first = svg.find("polyline")[0].points[0];
  106. //svg.find("polyline").each((index, target) => {
  107. // let newPoint = svg[0].createSVGPoint();
  108. // newPoint.x = first.x;
  109. // newPoint.y = first.y;
  110. // let polyline = target.points.appendItem(newPoint);
  111. //});
  112. svg.remove();
  113. if(polyClipVol.markers.length > 3) {
  114. polyClipVol.removeLastMarker();
  115. polyClipVol.initialized = true;
  116. } else {
  117. this.viewer.scene.removePolygonClipVolume(polyClipVol);
  118. }
  119. this.viewer.renderer.domElement.removeEventListener("mouseup", insertionCallback, true);
  120. this.viewer.removeEventListener("cancel_insertions", cancel.callback);
  121. this.viewer.inputHandler.enabled = true;
  122. };
  123. this.viewer.addEventListener("cancel_insertions", cancel.callback);
  124. this.viewer.renderer.domElement.addEventListener("mouseup", insertionCallback , true);
  125. this.viewer.inputHandler.enabled = false;
  126. polyClipVol.addMarker();
  127. this.viewer.inputHandler.startDragging(
  128. polyClipVol.markers[polyClipVol.markers.length - 1]);
  129. return polyClipVol;
  130. }
  131. update() {
  132. }
  133. };