@inweb/viewer-three 26.10.3 → 26.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34519,11 +34519,205 @@ void main() {
34519
34519
  }
34520
34520
  }
34521
34521
 
34522
+ class JoyStickControls extends Controls {
34523
+ constructor(camera, domElement, canvasElement, groundObjects) {
34524
+ super(camera, domElement);
34525
+ this.EYE_HEIGHT = 1.7;
34526
+ this.FAILING_DISTANCE = 2;
34527
+ this.GROUND_FOLLOWING_SPEED = 0.05;
34528
+ this.WALK_SPEED_DELIMITER = 4;
34529
+ this.movementSpeed = 0.1;
34530
+ this.multiplier = 3;
34531
+ this.isActive = false;
34532
+ this.MAX_JOYSTICK_DISTANCE = 100;
34533
+ this.INTERNAL_RADIUS = 35;
34534
+ this.MAX_MOVE_STICK = 40;
34535
+ this.EXTERNAL_RADIUS = 65;
34536
+ this.CANVAS_SIZE = 200;
34537
+ this.pressed = false;
34538
+ this.onPointerDown = (event) => {
34539
+ event.preventDefault();
34540
+ this.pressed = true;
34541
+ };
34542
+ this.onPointerMove = (event) => {
34543
+ event.preventDefault();
34544
+ if (!this.pressed)
34545
+ return;
34546
+ let movedX = event.pageX;
34547
+ let movedY = event.pageY;
34548
+ if (this.joyStickCanvas.offsetParent &&
34549
+ this.joyStickCanvas.offsetParent.tagName.toUpperCase() === "BODY") {
34550
+ movedX -= this.joyStickCanvas.offsetLeft;
34551
+ movedY -= this.joyStickCanvas.offsetTop;
34552
+ }
34553
+ else if (this.joyStickCanvas.offsetParent) {
34554
+ movedX -= this.joyStickCanvas.offsetParent.offsetLeft;
34555
+ movedY -= this.joyStickCanvas.offsetParent.offsetTop;
34556
+ }
34557
+ const x = 100 * ((movedX - this.centerX) / this.MAX_MOVE_STICK);
34558
+ const y = 100 * ((movedY - this.centerY) / this.MAX_MOVE_STICK) * -1;
34559
+ const distance = Math.sqrt(x * x + y * y);
34560
+ if (distance > 20) {
34561
+ this.joyStickPosition.set(x, y);
34562
+ this.isActive = true;
34563
+ }
34564
+ else {
34565
+ this.joyStickPosition.set(0, 0);
34566
+ this.isActive = false;
34567
+ }
34568
+ this.draw();
34569
+ this.moveClock.start();
34570
+ this.update();
34571
+ };
34572
+ this.onPointerUp = (event) => {
34573
+ event.preventDefault();
34574
+ this.pressed = false;
34575
+ this.joyStickPosition.set(0, 0);
34576
+ this.isActive = false;
34577
+ this.moveClock.stop();
34578
+ this.draw();
34579
+ };
34580
+ this.onResize = () => {
34581
+ this.updateVisibility();
34582
+ this.updatePosition();
34583
+ };
34584
+ this.camera = camera;
34585
+ this.canvas = canvasElement;
34586
+ this.moveClock = new Clock(false);
34587
+ this.joyStickPosition = new Vector2(0, 0);
34588
+ this.groundObjects = groundObjects;
34589
+ this.raycaster = new Raycaster();
34590
+ this.raycaster.near = 0;
34591
+ this.raycaster.far = this.EYE_HEIGHT + this.FAILING_DISTANCE;
34592
+ this.centerX = this.CANVAS_SIZE / 2;
34593
+ this.centerY = this.CANVAS_SIZE / 2;
34594
+ this.overlayElement = document.createElement("div");
34595
+ this.overlayElement.id = "joyStickDiv";
34596
+ this.overlayElement.style.background = "rgba(0,0,0,0)";
34597
+ this.overlayElement.style.position = "fixed";
34598
+ this.overlayElement.style.zIndex = "0";
34599
+ this.overlayElement.style.touchAction = "none";
34600
+ document.body.appendChild(this.overlayElement);
34601
+ this.joyStickCanvas = document.createElement("canvas");
34602
+ this.joyStickCanvas.id = "joyStickCanvas";
34603
+ this.joyStickCanvas.width = this.CANVAS_SIZE;
34604
+ this.joyStickCanvas.height = this.CANVAS_SIZE;
34605
+ this.overlayElement.appendChild(this.joyStickCanvas);
34606
+ this.context = this.joyStickCanvas.getContext("2d");
34607
+ this.joyStickCanvas.addEventListener("pointerdown", this.onPointerDown);
34608
+ document.addEventListener("pointermove", this.onPointerMove);
34609
+ document.addEventListener("pointerup", this.onPointerUp);
34610
+ window.addEventListener("resize", this.onResize);
34611
+ document.addEventListener("fullscreenchange", this.onResize);
34612
+ this.updateVisibility();
34613
+ this.updatePosition();
34614
+ this.draw();
34615
+ }
34616
+ dispose() {
34617
+ this.joyStickCanvas.removeEventListener("pointerdown", this.onPointerDown);
34618
+ document.removeEventListener("pointermove", this.onPointerMove);
34619
+ document.removeEventListener("pointerup", this.onPointerUp);
34620
+ window.removeEventListener("resize", this.onResize);
34621
+ document.removeEventListener("fullscreenchange", this.onResize);
34622
+ this.overlayElement.remove();
34623
+ super.dispose();
34624
+ }
34625
+ updateVisibility() {
34626
+ const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
34627
+ const isNarrowScreen = window.innerWidth < 1024;
34628
+ if (isMobile || isNarrowScreen) {
34629
+ this.overlayElement.style.display = "block";
34630
+ }
34631
+ else {
34632
+ this.overlayElement.style.display = "none";
34633
+ }
34634
+ }
34635
+ updatePosition() {
34636
+ const rect = this.canvas.getBoundingClientRect();
34637
+ this.overlayElement.style.top = `${rect.height - this.CANVAS_SIZE}px`;
34638
+ this.overlayElement.style.left = `${rect.left}px`;
34639
+ this.overlayElement.style.width = `${this.CANVAS_SIZE}px`;
34640
+ this.overlayElement.style.height = `${this.CANVAS_SIZE}px`;
34641
+ }
34642
+ draw() {
34643
+ this.context.clearRect(0, 0, this.CANVAS_SIZE, this.CANVAS_SIZE);
34644
+ this.context.beginPath();
34645
+ this.context.arc(this.centerX, this.centerY, this.EXTERNAL_RADIUS, 0, 2 * Math.PI, false);
34646
+ this.context.lineWidth = 2;
34647
+ this.context.strokeStyle = "#35436E";
34648
+ this.context.globalAlpha = 0.5;
34649
+ this.context.stroke();
34650
+ let movedX = this.centerX + (this.joyStickPosition.x * this.MAX_MOVE_STICK) / 100;
34651
+ let movedY = this.centerY - (this.joyStickPosition.y * this.MAX_MOVE_STICK) / 100;
34652
+ movedX = Math.max(this.MAX_MOVE_STICK, Math.min(this.CANVAS_SIZE - this.MAX_MOVE_STICK, movedX));
34653
+ movedY = Math.max(this.MAX_MOVE_STICK, Math.min(this.CANVAS_SIZE - this.MAX_MOVE_STICK, movedY));
34654
+ this.context.beginPath();
34655
+ this.context.arc(movedX, movedY, this.INTERNAL_RADIUS, 0, 2 * Math.PI, false);
34656
+ this.context.fillStyle = "#35436E";
34657
+ this.context.lineWidth = 2;
34658
+ this.context.strokeStyle = "#35436E";
34659
+ this.context.globalAlpha = 0.5;
34660
+ this.context.fill();
34661
+ this.context.stroke();
34662
+ }
34663
+ updateGroundFollowing() {
34664
+ const up = new Vector3().copy(this.camera.up);
34665
+ this.raycaster.set(this.camera.position, up.negate());
34666
+ this.raycaster.params = this.raycaster.params = {
34667
+ Mesh: {},
34668
+ Line: { threshold: 0 },
34669
+ Line2: { threshold: 0 },
34670
+ LOD: { threshold: 0 },
34671
+ Points: { threshold: 0 },
34672
+ Sprite: { threshold: 0 },
34673
+ };
34674
+ const intersects = this.raycaster.intersectObjects(this.groundObjects, false);
34675
+ if (intersects.length > 0) {
34676
+ const groundY = intersects[0].point.y;
34677
+ const targetY = groundY + this.EYE_HEIGHT;
34678
+ this.camera.position.y = MathUtils.lerp(this.camera.position.y, targetY, this.GROUND_FOLLOWING_SPEED);
34679
+ }
34680
+ }
34681
+ update() {
34682
+ if (!this.isActive)
34683
+ return;
34684
+ const forwardInput = this.joyStickPosition.y / this.MAX_JOYSTICK_DISTANCE;
34685
+ const rightInput = this.joyStickPosition.x / this.MAX_JOYSTICK_DISTANCE;
34686
+ const timeDelta = this.moveClock.getDelta();
34687
+ const moveDelta = (timeDelta * this.multiplier * this.movementSpeed) / this.WALK_SPEED_DELIMITER;
34688
+ const forward = new Vector3();
34689
+ const sideways = new Vector3();
34690
+ this.camera.getWorldDirection(forward);
34691
+ if (this.groundObjects.length > 0) {
34692
+ forward.y = 0;
34693
+ }
34694
+ forward.normalize();
34695
+ sideways.setFromMatrixColumn(this.camera.matrix, 0);
34696
+ if (this.groundObjects.length > 0) {
34697
+ sideways.y = 0;
34698
+ }
34699
+ sideways.normalize();
34700
+ if (forwardInput !== 0) {
34701
+ this.camera.position.addScaledVector(forward, moveDelta * forwardInput);
34702
+ }
34703
+ if (rightInput !== 0) {
34704
+ this.camera.position.addScaledVector(sideways, moveDelta * rightInput);
34705
+ }
34706
+ if (forwardInput !== 0 || rightInput !== 0) {
34707
+ if (this.groundObjects.length > 0)
34708
+ this.updateGroundFollowing();
34709
+ this.dispatchEvent({ type: "change" });
34710
+ }
34711
+ }
34712
+ }
34713
+
34522
34714
  class WalkDragger {
34523
34715
  constructor(viewer) {
34524
34716
  this.updateControls = () => {
34525
34717
  const size = this.viewer.extents.getSize(new Vector3());
34526
34718
  this.controls.movementSpeed = Math.min(size.x, size.y, size.z) / 2;
34719
+ this.joyStickControls.movementSpeed = this.controls.movementSpeed;
34720
+ this.joyStickControls.multiplier = this.controls.multiplier;
34527
34721
  };
34528
34722
  this.updateControlsCamera = () => {
34529
34723
  this.controls.object = this.viewer.camera;
@@ -34534,8 +34728,10 @@ void main() {
34534
34728
  };
34535
34729
  this.walkspeedChange = (event) => {
34536
34730
  this.viewer.emitEvent(event);
34731
+ this.joyStickControls.multiplier = this.controls.multiplier;
34537
34732
  };
34538
34733
  this.viewerRender = () => {
34734
+ this.joyStickControls.update();
34539
34735
  this.controls.update();
34540
34736
  };
34541
34737
  this.viewerZoom = () => {
@@ -34550,6 +34746,8 @@ void main() {
34550
34746
  this.controls = new WalkControls(viewer.camera, viewer.canvas, meshOnlyGround);
34551
34747
  this.controls.addEventListener("change", this.controlsChange);
34552
34748
  this.controls.addEventListener("walkspeedchange", this.walkspeedChange);
34749
+ this.joyStickControls = new JoyStickControls(viewer.camera, viewer.canvas, viewer.canvas, meshOnlyGround);
34750
+ this.joyStickControls.addEventListener("change", this.controlsChange);
34553
34751
  this.viewer = viewer;
34554
34752
  this.viewer.addEventListener("render", this.viewerRender);
34555
34753
  this.viewer.addEventListener("zoom", this.viewerZoom);
@@ -34563,6 +34761,8 @@ void main() {
34563
34761
  this.controls.removeEventListener("walkspeedchange", this.walkspeedChange);
34564
34762
  this.controls.removeEventListener("change", this.controlsChange);
34565
34763
  this.controls.dispose();
34764
+ this.joyStickControls.removeEventListener("change", this.controlsChange);
34765
+ this.joyStickControls.dispose();
34566
34766
  }
34567
34767
  }
34568
34768
 
@@ -34718,6 +34918,8 @@ void main() {
34718
34918
  this.updateControls = () => {
34719
34919
  const size = this.viewer.extents.getSize(new Vector3());
34720
34920
  this.controls.movementSpeed = Math.min(size.x, size.y, size.z) / 2;
34921
+ this.joyStickControls.movementSpeed = this.controls.movementSpeed;
34922
+ this.joyStickControls.multiplier = this.controls.multiplier;
34721
34923
  };
34722
34924
  this.updateControlsCamera = () => {
34723
34925
  this.controls.object = this.viewer.camera;
@@ -34728,8 +34930,10 @@ void main() {
34728
34930
  };
34729
34931
  this.flyspeedChange = (event) => {
34730
34932
  this.viewer.emitEvent(event);
34933
+ this.joyStickControls.multiplier = this.controls.multiplier;
34731
34934
  };
34732
34935
  this.viewerRender = () => {
34936
+ this.joyStickControls.update();
34733
34937
  this.controls.update();
34734
34938
  };
34735
34939
  this.viewerZoom = () => {
@@ -34738,6 +34942,8 @@ void main() {
34738
34942
  this.controls = new FlyControls(viewer.camera, viewer.canvas);
34739
34943
  this.controls.addEventListener("change", this.controlsChange);
34740
34944
  this.controls.addEventListener("flyspeedchange", this.flyspeedChange);
34945
+ this.joyStickControls = new JoyStickControls(viewer.camera, viewer.canvas, viewer.canvas, []);
34946
+ this.joyStickControls.addEventListener("change", this.controlsChange);
34741
34947
  this.viewer = viewer;
34742
34948
  this.viewer.addEventListener("render", this.viewerRender);
34743
34949
  this.viewer.addEventListener("zoom", this.viewerZoom);
@@ -34751,6 +34957,8 @@ void main() {
34751
34957
  this.controls.removeEventListener("flyspeedchange", this.flyspeedChange);
34752
34958
  this.controls.removeEventListener("change", this.controlsChange);
34753
34959
  this.controls.dispose();
34960
+ this.joyStickControls.removeEventListener("change", this.controlsChange);
34961
+ this.joyStickControls.dispose();
34754
34962
  }
34755
34963
  }
34756
34964