@inweb/viewer-three 25.3.15
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.
- package/LICENSE +20 -0
- package/README.md +63 -0
- package/dist/viewer-three.js +42841 -0
- package/dist/viewer-three.js.map +1 -0
- package/dist/viewer-three.min.js +6 -0
- package/dist/viewer-three.module.js +1102 -0
- package/dist/viewer-three.module.js.map +1 -0
- package/lib/ThreejsViewer/IComponent.d.ts +3 -0
- package/lib/ThreejsViewer/ThreejsViewer.d.ts +54 -0
- package/lib/ThreejsViewer/components/BackgroundComponent.d.ts +10 -0
- package/lib/ThreejsViewer/components/DefaultCameraPositionComponent.d.ts +9 -0
- package/lib/ThreejsViewer/components/LightComponent.d.ts +10 -0
- package/lib/ThreejsViewer/components/ObjectSelectionComponent.d.ts +16 -0
- package/lib/ThreejsViewer/components/RenderLoopComponent.d.ts +9 -0
- package/lib/ThreejsViewer/components/ResizeCanvasComponent.d.ts +9 -0
- package/lib/ThreejsViewer/draggers/ClippingPlaneDragger.d.ts +17 -0
- package/lib/ThreejsViewer/draggers/OrbitDragger.d.ts +10 -0
- package/lib/ThreejsViewer/draggers/PanDragger.d.ts +5 -0
- package/lib/ThreejsViewer/draggers/WalkDragger.d.ts +39 -0
- package/lib/ThreejsViewer/draggers/ZoomDragger.d.ts +5 -0
- package/lib/ThreejsViewer/loaders/GLTFLoadingManager.d.ts +11 -0
- package/lib/index.d.ts +2 -0
- package/package.json +42 -0
- package/src/ThreejsViewer/IComponent.ts +3 -0
- package/src/ThreejsViewer/ThreejsViewer.ts +369 -0
- package/src/ThreejsViewer/components/BackgroundComponent.ts +57 -0
- package/src/ThreejsViewer/components/DefaultCameraPositionComponent.ts +66 -0
- package/src/ThreejsViewer/components/LightComponent.ts +48 -0
- package/src/ThreejsViewer/components/ObjectSelectionComponent.ts +105 -0
- package/src/ThreejsViewer/components/RenderLoopComponent.ts +44 -0
- package/src/ThreejsViewer/components/ResizeCanvasComponent.ts +53 -0
- package/src/ThreejsViewer/draggers/ClippingPlaneDragger.ts +120 -0
- package/src/ThreejsViewer/draggers/OrbitDragger.ts +61 -0
- package/src/ThreejsViewer/draggers/PanDragger.ts +34 -0
- package/src/ThreejsViewer/draggers/WalkDragger.ts +260 -0
- package/src/ThreejsViewer/draggers/ZoomDragger.ts +34 -0
- package/src/ThreejsViewer/loaders/GLTFLoadingManager.ts +69 -0
- package/src/index.ts +25 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import { IComponent } from "../IComponent";
|
|
25
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
26
|
+
|
|
27
|
+
export class RenderLoopComponent implements IComponent {
|
|
28
|
+
protected viewer: ThreejsViewer;
|
|
29
|
+
protected requestID: number;
|
|
30
|
+
|
|
31
|
+
constructor(viewer: ThreejsViewer) {
|
|
32
|
+
this.viewer = viewer;
|
|
33
|
+
this.animate();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
dispose() {
|
|
37
|
+
cancelAnimationFrame(this.requestID);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
animate = (time = 0) => {
|
|
41
|
+
this.requestID = requestAnimationFrame(this.animate);
|
|
42
|
+
this.viewer.render(time);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import { IComponent } from "../IComponent";
|
|
25
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
26
|
+
|
|
27
|
+
export class ResizeCanvasComponent implements IComponent {
|
|
28
|
+
protected viewer: ThreejsViewer;
|
|
29
|
+
protected resizeObserver: ResizeObserver;
|
|
30
|
+
|
|
31
|
+
constructor(viewer: ThreejsViewer) {
|
|
32
|
+
this.viewer = viewer;
|
|
33
|
+
this.resizeObserver = new ResizeObserver(this.resizeViewer);
|
|
34
|
+
this.resizeObserver.observe(viewer.canvas.parentElement);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
dispose() {
|
|
38
|
+
this.resizeObserver.disconnect();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
resizeViewer = (entries: ResizeObserverEntry[]) => {
|
|
42
|
+
const { width, height } = entries[0].contentRect;
|
|
43
|
+
|
|
44
|
+
if (!width || !height) return; // <- invisible viewer, or viewer with parent removed
|
|
45
|
+
|
|
46
|
+
this.viewer.camera.aspect = width / height;
|
|
47
|
+
this.viewer.camera.updateProjectionMatrix();
|
|
48
|
+
this.viewer.renderer.setSize(width, height, true);
|
|
49
|
+
|
|
50
|
+
this.viewer.update(true);
|
|
51
|
+
this.viewer.emitEvent({ type: "resize", width, height });
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import * as THREE from "three";
|
|
25
|
+
|
|
26
|
+
export class ClippingPlaneDragger {
|
|
27
|
+
protected viewer: any;
|
|
28
|
+
protected start: THREE.Vector3;
|
|
29
|
+
protected end: THREE.Vector3;
|
|
30
|
+
protected delta: THREE.Vector3;
|
|
31
|
+
protected planeHelper: THREE.PlaneHelper;
|
|
32
|
+
|
|
33
|
+
public plane: THREE.Plane;
|
|
34
|
+
|
|
35
|
+
constructor(viewer: any) {
|
|
36
|
+
this.viewer = viewer;
|
|
37
|
+
|
|
38
|
+
this.viewer.addEventListener("pointerdown", this.onPointerDown);
|
|
39
|
+
this.viewer.addEventListener("pointerup", this.onPointerUp);
|
|
40
|
+
|
|
41
|
+
this.plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
|
|
42
|
+
|
|
43
|
+
this.start = new THREE.Vector3();
|
|
44
|
+
this.end = new THREE.Vector3();
|
|
45
|
+
this.delta = new THREE.Vector3();
|
|
46
|
+
if (!this.viewer.renderer.clippingPlanes) this.viewer.renderer.clippingPlanes = [];
|
|
47
|
+
|
|
48
|
+
this.viewer.renderer.clippingPlanes.push(this.plane);
|
|
49
|
+
|
|
50
|
+
this.planeHelper = new THREE.PlaneHelper(this.plane, 150, 0xffff00);
|
|
51
|
+
this.viewer.scene.add(this.planeHelper);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
dispose() {
|
|
55
|
+
this.viewer.removeEventListener("pointerdown", this.onPointerDown);
|
|
56
|
+
this.viewer.removeEventListener("pointerup", this.onPointerUp);
|
|
57
|
+
this.viewer.removeEventListener("pointermove", this.onPointerMove);
|
|
58
|
+
|
|
59
|
+
this.viewer.renderer.clippingPlanes = this.viewer.renderer.clippingPlanes.filter((plane) => this.plane !== plane);
|
|
60
|
+
|
|
61
|
+
this.planeHelper.removeFromParent();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
onPointerDown = (event: PointerEvent) => {
|
|
65
|
+
this.viewer.addEventListener("pointermove", this.onPointerMove);
|
|
66
|
+
|
|
67
|
+
const { offsetX, offsetY } = event;
|
|
68
|
+
this.start.set(offsetX, offsetY, 0.5);
|
|
69
|
+
this.start = this.screenToPlane(this.start);
|
|
70
|
+
this.delta.set(0, 0, 0);
|
|
71
|
+
this.end.set(0, 0, 0);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
onPointerUp = (event: PointerEvent) => {
|
|
75
|
+
this.viewer.removeEventListener("pointermove", this.onPointerMove);
|
|
76
|
+
|
|
77
|
+
if (this.end.length() === 0) {
|
|
78
|
+
const plane: THREE.Plane = this.plane;
|
|
79
|
+
plane.normal.multiplyScalar(-1);
|
|
80
|
+
plane.constant *= -1;
|
|
81
|
+
} else {
|
|
82
|
+
const { offsetX, offsetY } = event;
|
|
83
|
+
this.end.set(offsetX, offsetY, 0.5);
|
|
84
|
+
this.end = this.screenToPlane(this.end);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
onPointerMove = (event: PointerEvent) => {
|
|
89
|
+
const { offsetX, offsetY } = event;
|
|
90
|
+
this.end.set(offsetX, offsetY, 0.5);
|
|
91
|
+
this.end = this.screenToPlane(this.end);
|
|
92
|
+
this.delta.copy(this.end).sub(this.start);
|
|
93
|
+
this.start.copy(this.end);
|
|
94
|
+
|
|
95
|
+
const plane: THREE.Plane = this.plane;
|
|
96
|
+
plane.translate(this.delta);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
update() {}
|
|
100
|
+
|
|
101
|
+
screenToWorld(v: THREE.Vector3) {
|
|
102
|
+
v.x = (2 * v.x) / this.viewer.canvas.clientWidth - 1;
|
|
103
|
+
v.y = 1 - (2 * v.y) / this.viewer.canvas.clientHeight;
|
|
104
|
+
return v.unproject(this.viewer.camera);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
screenToPlane(v: THREE.Vector3) {
|
|
108
|
+
v = this.screenToWorld(v);
|
|
109
|
+
const direction = v.sub(this.viewer.camera.position).normalize();
|
|
110
|
+
const ray = new THREE.Ray(this.viewer.camera.position, direction);
|
|
111
|
+
let plane: THREE.Plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
|
|
112
|
+
const dot = plane.normal.dot(this.plane.normal);
|
|
113
|
+
if (Math.abs(dot) === 1) {
|
|
114
|
+
plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), 0);
|
|
115
|
+
}
|
|
116
|
+
const result = new THREE.Vector3();
|
|
117
|
+
ray.intersectPlane(plane, result);
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import { GeometryEndEvent } from "@inweb/viewer-core";
|
|
25
|
+
import * as THREE from "three";
|
|
26
|
+
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
27
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
28
|
+
|
|
29
|
+
export class OrbitDragger extends OrbitControls {
|
|
30
|
+
private viewer: ThreejsViewer;
|
|
31
|
+
|
|
32
|
+
constructor(viewer: ThreejsViewer) {
|
|
33
|
+
super(viewer.camera, viewer.canvas);
|
|
34
|
+
this.mouseButtons = { LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.PAN, RIGHT: THREE.MOUSE.PAN };
|
|
35
|
+
this.touches = { ONE: THREE.TOUCH.ROTATE, TWO: THREE.TOUCH.DOLLY_PAN };
|
|
36
|
+
this.screenSpacePanning = true;
|
|
37
|
+
this.rotateSpeed = 0.33;
|
|
38
|
+
this.viewer = viewer;
|
|
39
|
+
this.viewer.addEventListener("geometryend", this.geometryEnd);
|
|
40
|
+
this.addEventListener("change", this.updateViewer);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override dispose() {
|
|
44
|
+
this.removeEventListener("change", this.updateViewer);
|
|
45
|
+
this.viewer.removeEventListener("geometryend", this.geometryEnd);
|
|
46
|
+
super.dispose();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
geometryEnd = (event: GeometryEndEvent) => {
|
|
50
|
+
const { data: scene } = event;
|
|
51
|
+
const box = new THREE.Box3().setFromObject(scene);
|
|
52
|
+
const size = box.getSize(new THREE.Vector3()).length();
|
|
53
|
+
|
|
54
|
+
this.maxDistance = size * 10;
|
|
55
|
+
this.update();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
updateViewer = () => {
|
|
59
|
+
this.viewer.update();
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import * as THREE from "three";
|
|
25
|
+
import { OrbitDragger } from "./OrbitDragger";
|
|
26
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
27
|
+
|
|
28
|
+
export class PanDragger extends OrbitDragger {
|
|
29
|
+
constructor(viewer: ThreejsViewer) {
|
|
30
|
+
super(viewer);
|
|
31
|
+
this.mouseButtons = { LEFT: THREE.MOUSE.PAN, MIDDLE: THREE.MOUSE.PAN, RIGHT: THREE.MOUSE.PAN };
|
|
32
|
+
this.touches = { ONE: THREE.TOUCH.PAN, TWO: THREE.TOUCH.DOLLY_ROTATE };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import { RenderEvent } from "@inweb/viewer-core";
|
|
25
|
+
import * as THREE from "three";
|
|
26
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
27
|
+
|
|
28
|
+
export class WalkDragger {
|
|
29
|
+
protected viewer: ThreejsViewer;
|
|
30
|
+
protected camera: THREE.Camera;
|
|
31
|
+
protected canvas: HTMLCanvasElement;
|
|
32
|
+
protected _target: THREE.Vector3;
|
|
33
|
+
protected quaternion: THREE.Quaternion;
|
|
34
|
+
protected mouseStart: THREE.Vector2;
|
|
35
|
+
protected mouseDelta: THREE.Vector2;
|
|
36
|
+
protected xAxis: THREE.Vector3;
|
|
37
|
+
protected yAxis: THREE.Vector3;
|
|
38
|
+
protected zAxis: THREE.Vector3;
|
|
39
|
+
protected speed: THREE.Vector3;
|
|
40
|
+
protected _maxDistance: number;
|
|
41
|
+
protected xRotation: THREE.Quaternion;
|
|
42
|
+
protected yRotation: THREE.Quaternion;
|
|
43
|
+
protected yRotationAxis: THREE.Vector3;
|
|
44
|
+
protected touchStartDistance: number;
|
|
45
|
+
|
|
46
|
+
public walkSpeed: number;
|
|
47
|
+
public boostSpeed: number;
|
|
48
|
+
|
|
49
|
+
constructor(viewer: ThreejsViewer) {
|
|
50
|
+
this.viewer = viewer;
|
|
51
|
+
|
|
52
|
+
this._target = new THREE.Vector3();
|
|
53
|
+
|
|
54
|
+
this.quaternion = this.viewer.camera.quaternion.clone();
|
|
55
|
+
this.xRotation = new THREE.Quaternion();
|
|
56
|
+
this.yRotation = new THREE.Quaternion();
|
|
57
|
+
|
|
58
|
+
this.mouseStart = new THREE.Vector2();
|
|
59
|
+
this.mouseDelta = new THREE.Vector2();
|
|
60
|
+
|
|
61
|
+
this.xAxis = new THREE.Vector3();
|
|
62
|
+
this.yAxis = new THREE.Vector3();
|
|
63
|
+
this.zAxis = new THREE.Vector3();
|
|
64
|
+
this.yRotationAxis = new THREE.Vector3(1, 0, 0);
|
|
65
|
+
this.walkSpeed = 1;
|
|
66
|
+
this.boostSpeed = 5;
|
|
67
|
+
this.speed = new THREE.Vector3();
|
|
68
|
+
this._maxDistance = 1;
|
|
69
|
+
|
|
70
|
+
this.touchStartDistance = 0;
|
|
71
|
+
|
|
72
|
+
this.viewer.addEventListener("render", this.onRender);
|
|
73
|
+
|
|
74
|
+
this.viewer.addEventListener("contextmenu", this.onContextMenu);
|
|
75
|
+
|
|
76
|
+
this.viewer.addEventListener("mousedown", this.onMouseDown);
|
|
77
|
+
this.viewer.addEventListener("mouseup", this.onMouseUp);
|
|
78
|
+
|
|
79
|
+
this.viewer.addEventListener("touchstart", this.onTouchStart);
|
|
80
|
+
this.viewer.addEventListener("touchmove", this.onTouchMove);
|
|
81
|
+
this.viewer.addEventListener("touchend", this.onTouchEnd);
|
|
82
|
+
|
|
83
|
+
this.viewer.addEventListener("wheel", this.onMouseWheel);
|
|
84
|
+
|
|
85
|
+
document.addEventListener("keydown", this.onKeyDown);
|
|
86
|
+
document.addEventListener("keyup", this.onKeyUp);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
dispose() {
|
|
90
|
+
this.viewer.removeEventListener("render", this.onRender);
|
|
91
|
+
|
|
92
|
+
this.viewer.removeEventListener("contextmenu", this.onContextMenu);
|
|
93
|
+
|
|
94
|
+
this.viewer.removeEventListener("mousedown", this.onMouseDown);
|
|
95
|
+
this.viewer.removeEventListener("mouseup", this.onMouseUp);
|
|
96
|
+
this.viewer.removeEventListener("mousemove", this.onMouseMove);
|
|
97
|
+
|
|
98
|
+
this.viewer.removeEventListener("touchstart", this.onTouchStart);
|
|
99
|
+
this.viewer.removeEventListener("touchmove", this.onTouchMove);
|
|
100
|
+
this.viewer.removeEventListener("touchend", this.onTouchEnd);
|
|
101
|
+
|
|
102
|
+
this.viewer.removeEventListener("wheel", this.onMouseWheel);
|
|
103
|
+
|
|
104
|
+
document.removeEventListener("keydown", this.onKeyDown);
|
|
105
|
+
document.removeEventListener("keyup", this.onKeyUp);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
onKeyDown = (event: KeyboardEvent) => {
|
|
109
|
+
switch (event.code) {
|
|
110
|
+
case "KeyW": // Forward
|
|
111
|
+
if (event.shiftKey) {
|
|
112
|
+
this.speed.z = this.walkSpeed * this.boostSpeed;
|
|
113
|
+
} else {
|
|
114
|
+
this.speed.z = this.walkSpeed;
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case "KeyS": // Backward
|
|
118
|
+
if (event.shiftKey) {
|
|
119
|
+
this.speed.z = -this.walkSpeed * this.boostSpeed;
|
|
120
|
+
} else {
|
|
121
|
+
this.speed.z = -this.walkSpeed;
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case "KeyA": // Left
|
|
125
|
+
if (event.shiftKey) {
|
|
126
|
+
this.speed.x = this.walkSpeed * this.boostSpeed;
|
|
127
|
+
} else {
|
|
128
|
+
this.speed.x = this.walkSpeed;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case "KeyD": // Right
|
|
132
|
+
if (event.shiftKey) {
|
|
133
|
+
this.speed.x = -this.walkSpeed * this.boostSpeed;
|
|
134
|
+
} else {
|
|
135
|
+
this.speed.x = -this.walkSpeed;
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
default:
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
onKeyUp = (event: KeyboardEvent) => {
|
|
144
|
+
switch (event.code) {
|
|
145
|
+
case "KeyW":
|
|
146
|
+
this.speed.z = 0;
|
|
147
|
+
break;
|
|
148
|
+
case "KeyS":
|
|
149
|
+
this.speed.z = 0;
|
|
150
|
+
break;
|
|
151
|
+
case "KeyA":
|
|
152
|
+
this.speed.x = 0;
|
|
153
|
+
break;
|
|
154
|
+
case "KeyD":
|
|
155
|
+
this.speed.x = 0;
|
|
156
|
+
break;
|
|
157
|
+
default:
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
onMouseDown = (event: PointerEvent) => {
|
|
163
|
+
const { clientX, clientY } = event;
|
|
164
|
+
this.mouseStart.set(clientX, clientY);
|
|
165
|
+
this.mouseDelta.set(0, 0);
|
|
166
|
+
this.quaternion.copy(this.viewer.camera.quaternion);
|
|
167
|
+
this.viewer.addEventListener("mousemove", this.onMouseMove);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
onMouseMove = (event: MouseEvent) => {
|
|
171
|
+
const { clientX, clientY } = event;
|
|
172
|
+
this.mouseDelta.set(this.mouseStart.x - clientX, this.mouseStart.y - clientY);
|
|
173
|
+
this.rotateCamera(this.mouseDelta);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
onMouseUp = (event: PointerEvent) => {
|
|
177
|
+
this.speed.set(0, 0, 0);
|
|
178
|
+
this.mouseStart.set(0, 0);
|
|
179
|
+
this.mouseDelta.set(0, 0);
|
|
180
|
+
this.quaternion.copy(this.viewer.camera.quaternion);
|
|
181
|
+
this.viewer.removeEventListener("mousemove", this.onMouseMove);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
onMouseWheel = (event: WheelEvent) => {
|
|
185
|
+
event.preventDefault();
|
|
186
|
+
|
|
187
|
+
if (-event.deltaY < 0) {
|
|
188
|
+
this.walkSpeed = Math.max(0.001, this.walkSpeed - 1);
|
|
189
|
+
} else if (-event.deltaY > 0) {
|
|
190
|
+
this.walkSpeed++;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
onContextMenu = (event: MouseEvent) => {
|
|
195
|
+
console.log(event);
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
event.stopImmediatePropagation();
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
onTouchStart = (event: TouchEvent) => {
|
|
201
|
+
if (event.touches.length > 1) {
|
|
202
|
+
this.touchStartDistance = this.getTouchsDistance(event.touches);
|
|
203
|
+
} else {
|
|
204
|
+
const { clientX, clientY } = event.touches[0];
|
|
205
|
+
this.mouseStart.set(clientX, clientY);
|
|
206
|
+
this.mouseDelta.set(0, 0);
|
|
207
|
+
this.quaternion.copy(this.viewer.camera.quaternion);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
onTouchEnd = (event: TouchEvent) => {
|
|
212
|
+
if (event.touches.length === 0) {
|
|
213
|
+
this.touchStartDistance = 0;
|
|
214
|
+
}
|
|
215
|
+
this.speed.set(0, 0, 0);
|
|
216
|
+
this.mouseStart.set(0, 0);
|
|
217
|
+
this.mouseDelta.set(0, 0);
|
|
218
|
+
this.quaternion.copy(this.viewer.camera.quaternion);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
onTouchMove = (event: TouchEvent) => {
|
|
222
|
+
if (event.touches.length === 1 && this.touchStartDistance === 0) {
|
|
223
|
+
const { clientX, clientY } = event.touches[0];
|
|
224
|
+
this.mouseDelta.set(this.mouseStart.x - clientX, this.mouseStart.y - clientY);
|
|
225
|
+
this.rotateCamera(this.mouseDelta);
|
|
226
|
+
}
|
|
227
|
+
if (event.touches.length === 2) {
|
|
228
|
+
const distance = this.getTouchsDistance(event.touches);
|
|
229
|
+
this.speed.z = (distance - this.touchStartDistance) / 2;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
getTouchsDistance(touches) {
|
|
234
|
+
const [start, end] = touches;
|
|
235
|
+
const dx = start.clientX - end.clientX;
|
|
236
|
+
const dy = start.clientY - end.clientY;
|
|
237
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
238
|
+
return distance;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
onRender = (event: RenderEvent) => {
|
|
242
|
+
this.viewer.camera.matrix.extractBasis(this.xAxis, this.yAxis, this.zAxis);
|
|
243
|
+
this.viewer.camera.position.add(this.zAxis.multiplyScalar(-event.deltaTime * this.speed.z));
|
|
244
|
+
this.viewer.camera.position.add(this.xAxis.multiplyScalar(-event.deltaTime * this.speed.x));
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
update() {}
|
|
248
|
+
|
|
249
|
+
rotateCamera(delta: THREE.Vector2) {
|
|
250
|
+
const rotateX = (Math.PI * delta.x) / this.viewer.canvas.clientWidth;
|
|
251
|
+
const rotateY = (Math.PI * delta.y) / this.viewer.canvas.clientHeight;
|
|
252
|
+
|
|
253
|
+
const quaternion = this.quaternion.clone();
|
|
254
|
+
this.xRotation.setFromAxisAngle(this.viewer.camera.up, rotateX);
|
|
255
|
+
this.yRotation.setFromAxisAngle(this.yRotationAxis, rotateY);
|
|
256
|
+
quaternion.premultiply(this.xRotation).multiply(this.yRotation).normalize();
|
|
257
|
+
|
|
258
|
+
this.viewer.camera.setRotationFromQuaternion(quaternion);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
//
|
|
5
|
+
// This software and its documentation and related materials are owned by
|
|
6
|
+
// the Alliance. The software may only be incorporated into application
|
|
7
|
+
// programs owned by members of the Alliance, subject to a signed
|
|
8
|
+
// Membership Agreement and Supplemental Software License Agreement with the
|
|
9
|
+
// Alliance. The structure and organization of this software are the valuable
|
|
10
|
+
// trade secrets of the Alliance and its suppliers. The software is also
|
|
11
|
+
// protected by copyright law and international treaty provisions. Application
|
|
12
|
+
// programs incorporating this software must include the following statement
|
|
13
|
+
// with their copyright notices:
|
|
14
|
+
//
|
|
15
|
+
// This application incorporates Open Design Alliance software pursuant to a
|
|
16
|
+
// license agreement with Open Design Alliance.
|
|
17
|
+
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
|
|
18
|
+
// All rights reserved.
|
|
19
|
+
//
|
|
20
|
+
// By use of this software, its documentation or related materials, you
|
|
21
|
+
// acknowledge and accept the above terms.
|
|
22
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
23
|
+
|
|
24
|
+
import * as THREE from "three";
|
|
25
|
+
import { OrbitDragger } from "./OrbitDragger";
|
|
26
|
+
import type { ThreejsViewer } from "../ThreejsViewer";
|
|
27
|
+
|
|
28
|
+
export class ZoomDragger extends OrbitDragger {
|
|
29
|
+
constructor(viewer: ThreejsViewer) {
|
|
30
|
+
super(viewer);
|
|
31
|
+
this.mouseButtons = { LEFT: THREE.MOUSE.DOLLY, MIDDLE: THREE.MOUSE.PAN, RIGHT: THREE.MOUSE.PAN };
|
|
32
|
+
this.touches = { ONE: THREE.TOUCH.DOLLY_PAN, TWO: THREE.TOUCH.DOLLY_PAN };
|
|
33
|
+
}
|
|
34
|
+
}
|