PolygonClipVolume.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. export class PolygonClipVolume extends THREE.Object3D{
  3. constructor(camera){
  4. super();
  5. this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1;
  6. this.name = "polygon_clip_volume_" + this.constructor.counter;
  7. this.camera = camera.clone();
  8. this.camera.rotation.set(...camera.rotation.toArray()); // [r85] workaround because camera.clone() doesn't work on rotation
  9. this.camera.rotation.order = camera.rotation.order;
  10. this.camera.updateMatrixWorld();
  11. this.camera.updateProjectionMatrix();
  12. this.camera.matrixWorldInverse.copy(this.camera.matrixWorld).invert();
  13. this.viewMatrix = this.camera.matrixWorldInverse.clone();
  14. this.projMatrix = this.camera.projectionMatrix.clone();
  15. // projected markers
  16. this.markers = [];
  17. this.initialized = false;
  18. }
  19. addMarker() {
  20. let marker = new THREE.Mesh();
  21. let cancel;
  22. let drag = e => {
  23. let size = e.viewer.renderer.getSize(new THREE.Vector2());
  24. let projectedPos = new THREE.Vector3(
  25. 2.0 * (e.drag.end.x / size.width) - 1.0,
  26. -2.0 * (e.drag.end.y / size.height) + 1.0,
  27. 0
  28. );
  29. marker.position.copy(projectedPos);
  30. };
  31. let drop = e => {
  32. cancel();
  33. };
  34. cancel = e => {
  35. marker.removeEventListener("drag", drag);
  36. marker.removeEventListener("drop", drop);
  37. };
  38. marker.addEventListener("drag", drag);
  39. marker.addEventListener("drop", drop);
  40. this.markers.push(marker);
  41. }
  42. removeLastMarker() {
  43. if(this.markers.length > 0) {
  44. this.markers.splice(this.markers.length - 1, 1);
  45. }
  46. }
  47. };