-
Notifications
You must be signed in to change notification settings - Fork 44
/
camera-orbit-control.ts
327 lines (276 loc) · 8.9 KB
/
camera-orbit-control.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { ObservablePoint } from "@pixi/math"
import type { InteractionEvent } from "@pixi/interaction"
import { Compatibility } from "../compatibility/compatibility"
import { Quat } from "../math/quat"
import { Vec3 } from "../math/vec3"
import { Camera } from "./camera"
/**
* Allows the user to control the camera by orbiting the target.
*/
export class CameraOrbitControl {
protected _autoUpdate = true;
/**
* Whether to auto update the camera on prerender.
* Default is true.
*/
get autoUpdate(): boolean {
return this._autoUpdate
}
set autoUpdate(value: boolean) {
this._autoUpdate = value
}
protected _allowControl = true
/**
* Allows the camera to be controlled by user.
*/
get allowControl(): boolean {
return this._allowControl
}
set allowControl(value: boolean) {
this._allowControl = value
}
protected _camera = Camera.main
/**
* The camera being controlled.
*/
get camera(): Camera {
return this._camera
}
set camera(value: Camera) {
this._camera = value
}
protected _target = { x: 0, y: 0, z: 0 }
/**
* Target position (x, y, z) to orbit.
*/
get target(): { x: number; y: number; z: number } {
return this._target
}
set target(value: { x: number; y: number; z: number }) {
this._target = value
}
protected _angles = new ObservablePoint(() => {
this._angles.x = Math.min(Math.max(-85, this._angles.x), 85)
}, undefined, 0, 180)
/**
* Orientation euler angles (x-axis and y-axis).
* The angle for the x-axis will be clamped between -85 and 85 degrees.
*/
get angles() {
return this._angles
}
protected _distance = 5
/**
* Distance between camera and the target.
* Default value is 5.
*/
get distance(): number {
return this._distance
}
set distance(value: number) {
this._distance = Math.min(Math.max(value, 0.01), Number.MAX_SAFE_INTEGER)
}
protected _enableDamping = false
/**
* Value indicating if damping (inertia) is enabled, which can be used to give a sense of weight to the controls.
* Default is false.
*/
get enableDamping(): boolean {
return this._enableDamping
}
set enableDamping(value: boolean) {
this._enableDamping = value
}
protected _dampingFactor = 0.1
/**
* The damping inertia used if enableDamping is true.
* Default is 0.1.
*/
get dampingFactor(): number {
return this._dampingFactor
}
set dampingFactor(value: number) {
this._dampingFactor = value
}
protected _element: HTMLElement
protected _grabbed = false
protected _previousPinchDistance = 0
protected _previousClientX = 0
protected _previousClientY = 0
protected _dampingAngles = { x: 0, y: 180 }
protected _dampingDistance = 5
/**
* Creates a new camera orbit control.
* @param element The element for listening to user events.
* @param camera The camera to control. If not set, the main camera will be used
* by default.
*/
constructor(element: HTMLElement, camera = Camera.main) {
this._element = element
this._camera = camera
this.bind()
}
destroy(): void {
this.unbind()
}
protected onPointerDown = (clientX: number, clientY: number): void => {
this._grabbed = true
this._previousClientX = clientX
this._previousClientY = clientY
}
protected onPointerUp = (): void => {
this._grabbed = false
}
protected onPointerMove = (clientX: number, clientY: number): void => {
if (this._grabbed) {
const movementX = clientX - this._previousClientX
const movementY = clientY - this._previousClientY
this.angles.x += movementY * 0.5
this.angles.y -= movementX * 0.5
this.updateCamera()
this._previousClientX = clientX
this._previousClientY = clientY
}
}
protected onPreRender = (): void => {
if (this.autoUpdate) {
this.updateCamera()
}
}
protected onMouseDownInteraction = (e: InteractionEvent): void => {
if (this.allowControl) {
if (!e.stopped) {
this._grabbed = true
const originalEvent = e.data.originalEvent
const touchEvent = originalEvent as TouchEvent
const mouseEvent = originalEvent as MouseEvent
const touch = touchEvent?.targetTouches?.[0]
const clientX = touch?.clientX ?? mouseEvent?.clientX
const clientY = touch?.clientY ?? mouseEvent?.clientY
this.onPointerDown(clientX, clientY)
}
}
}
protected onMouseDown = (e: MouseEvent): void => {
if (this.allowControl) {
this.onPointerDown(e.clientX, e.clientY)
}
}
protected onMouseMove = (e: MouseEvent): void => {
if (this.allowControl) {
if (e.buttons === 1) {
this.onPointerMove(e.clientX, e.clientY)
}
}
}
protected onMouseUp = (_e: MouseEvent): void => {
if (this.allowControl) {
this.onPointerUp()
}
}
protected onWheel = (e: WheelEvent): void => {
if (this.allowControl) {
this.distance += e.deltaY * 0.01
e.preventDefault()
this.updateCamera()
}
}
protected onTouchStart = (e: TouchEvent): void => {
if (this.allowControl) {
const touch = e?.targetTouches?.[0]
if (touch) {
const clientX = touch.clientX
const clientY = touch.clientY
this.onPointerDown(clientX, clientY)
}
if (e.touches.length === 2) {
e.preventDefault() // Prevent page scroll
this._previousPinchDistance = Math.hypot(
e.touches[0].clientX - e.touches[1].clientX,
e.touches[0].clientY - e.touches[1].clientY
)
}
}
}
protected onPinch = (e: TouchEvent): void => {
if (this.allowControl) {
e.preventDefault() // Prevent page scroll
const currentPinchDistance = Math.hypot(
e.touches[0].clientX - e.touches[1].clientX,
e.touches[0].clientY - e.touches[1].clientY
)
const deltaPinchDistance =
currentPinchDistance - this._previousPinchDistance
this.distance -= deltaPinchDistance * 0.1
this.updateCamera()
this._previousPinchDistance = currentPinchDistance
}
}
protected onTouchMove = (e: TouchEvent): void => {
if (this.allowControl) {
const touch = e?.targetTouches?.[0]
if (e.touches.length === 1 && touch) {
const clientX = touch.clientX
const clientY = touch.clientY
this.onPointerMove(clientX, clientY)
}
if (e.touches.length === 2) {
this.onPinch(e)
}
}
}
protected onTouchEnd = (e: TouchEvent): void => {
if (this.allowControl) {
if (e.touches.length === 0) {
this.onPointerUp()
}
}
}
protected bind(): void {
this.camera.renderer.on("prerender", this.onPreRender)
let interaction = Compatibility.getInteractionPlugin(this.camera.renderer)
if (interaction) {
interaction.on("mousedown", this.onMouseDownInteraction)
}
this._element.addEventListener("mousedown", this.onMouseDown)
this._element.addEventListener("touchstart", this.onTouchStart)
this._element.addEventListener("wheel", this.onWheel)
// Bind mouse and touch equivalent pointermove and pointerup events to window
// to support the case where the pointer leaves the element while dragging
window.addEventListener("mousemove", this.onMouseMove)
window.addEventListener("touchmove", this.onTouchMove)
window.addEventListener("mouseup", this.onMouseUp)
window.addEventListener("touchend", this.onTouchEnd)
}
protected unbind(): void {
this._element.removeEventListener("mousedown", this.onMouseDown)
this._element.removeEventListener("touchstart", this.onTouchStart)
this._element.removeEventListener("wheel", this.onWheel)
window.removeEventListener("mousemove", this.onMouseMove)
window.removeEventListener("touchmove", this.onTouchMove)
window.removeEventListener("mouseup", this.onMouseUp)
window.removeEventListener("touchend", this.onTouchEnd)
}
/**
* Updates the position and rotation of the camera.
*/
updateCamera(): void {
if (this.enableDamping) {
this._dampingAngles.x +=
(this.angles.x - this._dampingAngles.x) * this.dampingFactor
this._dampingAngles.y +=
(this.angles.y - this._dampingAngles.y) * this.dampingFactor
this._dampingDistance +=
(this.distance - this._dampingDistance) * this.dampingFactor
}
const angles = this.enableDamping ? this._dampingAngles : this.angles
const distance = this.enableDamping ? this._dampingDistance : this.distance
const rot = Quat.fromEuler(angles.x, angles.y, 0, new Float32Array(4))
const dir = Vec3.transformQuat(
Vec3.set(0, 0, 1, new Float32Array(3)), rot, new Float32Array(3))
const pos = Vec3.subtract(
Vec3.set(this.target.x, this.target.y, this.target.z, new Float32Array(3)), Vec3.scale(dir, distance, new Float32Array(3)), new Float32Array(3))
this.camera.position.set(pos[0], pos[1], pos[2])
this.camera.rotationQuaternion.set(rot[0], rot[1], rot[2], rot[3])
}
}