-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
119 lines (100 loc) · 2.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Vector3 } from "three";
import TWEEN from "@tweenjs/tween.js";
export type Orientation = {
offsetFactor: {
x: number;
y: number;
z: number;
};
};
const TOP: Orientation = {
offsetFactor: {
x: 0,
y: 1,
z: 0,
},
};
const BOTTOM: Orientation = {
offsetFactor: {
x: 0,
y: -1,
z: 0,
},
};
const FRONT: Orientation = {
offsetFactor: {
x: 0,
y: 0,
z: 1,
},
};
const BACK: Orientation = {
offsetFactor: {
x: 0,
y: 0,
z: -1,
},
};
const LEFT: Orientation = {
offsetFactor: {
x: -1,
y: 0,
z: 0,
},
};
const RIGHT: Orientation = {
offsetFactor: {
x: 1,
y: 0,
z: 0,
},
};
class ViewCubeController {
static CubeOrientation = {
Top: "top",
Bottom: "bottom",
Front: "front",
Back: "back",
Left: "left",
Right: "right",
};
static ORIENTATIONS = {
[ViewCubeController.CubeOrientation.Top]: TOP,
[ViewCubeController.CubeOrientation.Bottom]: BOTTOM,
[ViewCubeController.CubeOrientation.Front]: FRONT,
[ViewCubeController.CubeOrientation.Back]: BACK,
[ViewCubeController.CubeOrientation.Left]: LEFT,
[ViewCubeController.CubeOrientation.Right]: RIGHT,
};
private camera: THREE.Camera;
constructor(camera: THREE.Camera) {
this.camera = camera;
}
tweenCamera(orientation: Orientation) {
const { offsetFactor } = orientation;
if (this.camera) {
const offsetUnit = this.camera.position.length();
const offset = new Vector3(
offsetUnit * offsetFactor.x,
offsetUnit * offsetFactor.y,
offsetUnit * offsetFactor.z
);
const center = new Vector3();
const finishPosition = center.add(offset);
// The target position the camera should always look at
const targetPosition = new Vector3(0, 0, 0);
const positionTween = new TWEEN.Tween(this.camera.position)
.to(finishPosition, 300)
.easing(TWEEN.Easing.Cubic.InOut)
.onUpdate(() => {
// Update the camera rotation to look at the target position
this.camera.lookAt(targetPosition);
});
positionTween.start();
}
}
tweenCallback() {
TWEEN.update();
}
}
export default ViewCubeController;