Line data Source code
1 : import * as THREE from 'three'
2 : import type { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
3 :
4 : import type {
5 : CameraManagerInterface,
6 : CameraState,
7 : CameraType,
8 : EventManagerInterface
9 : } from './interfaces'
10 :
11 7 : function resolveIncomingCustomUp(state: CameraState): THREE.Vector3 | null {
12 7 : if (state.useCustomUp === undefined) return null
13 7 : const storedUp = state.customUp
14 : ? new THREE.Vector3(state.customUp.x, state.customUp.y, state.customUp.z)
15 : : null
16 7 : if (storedUp && storedUp.lengthSq() > 0) return storedUp
17 7 : if (!state.useCustomUp || !state.quaternion) return null
18 0 : const q = new THREE.Quaternion(
19 : state.quaternion.x,
20 : state.quaternion.y,
21 : state.quaternion.z,
22 : state.quaternion.w
23 : )
24 0 : if (q.lengthSq() === 0) q.identity()
25 0 : return new THREE.Vector3(0, 1, 0).applyQuaternion(q)
26 : }
27 :
28 24 : export class CameraManager implements CameraManagerInterface {
29 : perspectiveCamera: THREE.PerspectiveCamera
30 : orthographicCamera: THREE.OrthographicCamera
31 : activeCamera: THREE.Camera
32 :
33 : private eventManager: EventManagerInterface
34 :
35 : private controls: OrbitControls | null = null
36 :
37 : private customUp: THREE.Vector3 | null = null
38 : private usingCustomUp = false
39 :
40 : DEFAULT_DISTANCE = 10
41 : DEFAULT_LOOK_AT = 0
42 :
43 : DEFAULT_CAMERA = {
44 : near: 0.01,
45 : far: 10000
46 : }
47 :
48 : DEFAULT_PERSPECTIVE_CAMERA = {
49 : fov: 35,
50 : aspect: 1
51 : }
52 :
53 : DEFAULT_FRUSTUM_SIZE = 10
54 :
55 : DEFAULT_ORTHOGRAPHIC_CAMERA = {
56 : left: -this.DEFAULT_FRUSTUM_SIZE / 2,
57 : right: this.DEFAULT_FRUSTUM_SIZE / 2,
58 : top: this.DEFAULT_FRUSTUM_SIZE / 2,
59 : bottom: -this.DEFAULT_FRUSTUM_SIZE / 2
60 : }
61 :
62 : constructor(
63 : _renderer: THREE.WebGLRenderer,
64 : eventManager: EventManagerInterface
65 : ) {
66 27 : this.eventManager = eventManager
67 :
68 27 : this.perspectiveCamera = new THREE.PerspectiveCamera(
69 : this.DEFAULT_PERSPECTIVE_CAMERA.fov,
70 : this.DEFAULT_PERSPECTIVE_CAMERA.aspect,
71 : this.DEFAULT_CAMERA.near,
72 : this.DEFAULT_CAMERA.far
73 : )
74 :
75 27 : this.orthographicCamera = new THREE.OrthographicCamera(
76 : this.DEFAULT_ORTHOGRAPHIC_CAMERA.left,
77 : this.DEFAULT_ORTHOGRAPHIC_CAMERA.right,
78 : this.DEFAULT_ORTHOGRAPHIC_CAMERA.top,
79 : this.DEFAULT_ORTHOGRAPHIC_CAMERA.bottom,
80 : this.DEFAULT_CAMERA.near,
81 : this.DEFAULT_CAMERA.far
82 : )
83 :
84 27 : this.reset()
85 :
86 27 : this.activeCamera = this.perspectiveCamera
87 : }
88 :
89 : init(): void {}
90 :
91 : dispose(): void {}
92 :
93 : setControls(controls: OrbitControls): void {
94 27 : this.controls = controls
95 :
96 27 : controls.addEventListener('end', () => {
97 0 : this.eventManager.emitEvent('cameraChanged', this.getCameraState())
98 : })
99 : }
100 :
101 : getCurrentCameraType(): CameraType {
102 18 : return this.activeCamera === this.perspectiveCamera
103 : ? 'perspective'
104 : : 'orthographic'
105 : }
106 :
107 : toggleCamera(cameraType?: CameraType): void {
108 52 : const oldCamera = this.activeCamera
109 :
110 52 : const position = oldCamera.position.clone()
111 52 : const rotation = oldCamera.rotation.clone()
112 52 : const up = oldCamera.up.clone()
113 52 : const target = this.controls?.target.clone() || new THREE.Vector3()
114 :
115 52 : const oldZoom =
116 : oldCamera instanceof THREE.OrthographicCamera
117 : ? oldCamera.zoom
118 : : (oldCamera as THREE.PerspectiveCamera).zoom
119 :
120 52 : if (!cameraType) {
121 0 : this.activeCamera =
122 : oldCamera === this.perspectiveCamera
123 : ? this.orthographicCamera
124 : : this.perspectiveCamera
125 : } else {
126 52 : this.activeCamera =
127 : cameraType === 'perspective'
128 : ? this.perspectiveCamera
129 : : this.orthographicCamera
130 :
131 52 : if (oldCamera === this.activeCamera) {
132 52 : return
133 : }
134 : }
135 :
136 0 : this.activeCamera.position.copy(position)
137 0 : this.activeCamera.rotation.copy(rotation)
138 0 : this.activeCamera.up.copy(up)
139 :
140 0 : if (this.activeCamera instanceof THREE.OrthographicCamera) {
141 0 : this.activeCamera.zoom = oldZoom
142 0 : this.activeCamera.updateProjectionMatrix()
143 0 : } else if (this.activeCamera instanceof THREE.PerspectiveCamera) {
144 0 : this.activeCamera.zoom = oldZoom
145 0 : this.activeCamera.updateProjectionMatrix()
146 : }
147 :
148 0 : if (this.controls) {
149 0 : this.controls.object = this.activeCamera
150 0 : this.controls.target.copy(target)
151 0 : this.controls.update()
152 : }
153 :
154 0 : this.eventManager.emitEvent('cameraTypeChange', cameraType)
155 : }
156 :
157 : setFOV(fov: number): void {
158 54 : if (this.activeCamera === this.perspectiveCamera) {
159 54 : this.perspectiveCamera.fov = fov
160 54 : this.perspectiveCamera.updateProjectionMatrix()
161 : }
162 :
163 54 : this.eventManager.emitEvent('fovChange', fov)
164 : }
165 :
166 : getCameraState(): CameraState {
167 8 : const { x, y, z, w } = this.activeCamera.quaternion
168 8 : const activeCamera = this.activeCamera as
169 : | THREE.PerspectiveCamera
170 : | THREE.OrthographicCamera
171 8 : return {
172 : position: this.activeCamera.position.clone(),
173 : target: this.controls?.target.clone() || new THREE.Vector3(),
174 : zoom:
175 : this.activeCamera instanceof THREE.OrthographicCamera
176 : ? this.activeCamera.zoom
177 : : (this.activeCamera as THREE.PerspectiveCamera).zoom,
178 : cameraType: this.getCurrentCameraType(),
179 : quaternion: { x, y, z, w },
180 : ...(this.customUp !== null && {
181 : useCustomUp: this.usingCustomUp,
182 : customUp: {
183 : x: this.customUp.x,
184 : y: this.customUp.y,
185 : z: this.customUp.z
186 : }
187 : }),
188 : fov: this.perspectiveCamera.fov,
189 : aspect: this.perspectiveCamera.aspect,
190 : near: activeCamera.near,
191 : far: activeCamera.far,
192 : frustum: {
193 : left: this.orthographicCamera.left,
194 : right: this.orthographicCamera.right,
195 : top: this.orthographicCamera.top,
196 : bottom: this.orthographicCamera.bottom
197 : }
198 : }
199 : }
200 :
201 : setCameraState(state: CameraState): void {
202 7 : if (state.cameraType !== this.getCurrentCameraType()) {
203 0 : this.toggleCamera(state.cameraType)
204 : }
205 :
206 7 : this.activeCamera.position.copy(state.position)
207 :
208 7 : this.controls?.target.copy(state.target)
209 :
210 7 : if (
211 : state.fov !== undefined &&
212 : this.activeCamera instanceof THREE.PerspectiveCamera
213 : ) {
214 7 : this.activeCamera.fov = state.fov
215 : }
216 :
217 7 : if (this.activeCamera instanceof THREE.OrthographicCamera) {
218 0 : this.activeCamera.zoom = state.zoom
219 0 : this.activeCamera.updateProjectionMatrix()
220 7 : } else if (this.activeCamera instanceof THREE.PerspectiveCamera) {
221 7 : this.activeCamera.zoom = state.zoom
222 7 : this.activeCamera.updateProjectionMatrix()
223 : }
224 :
225 7 : const incomingUp = resolveIncomingCustomUp(state)
226 7 : if (incomingUp) {
227 0 : this.customUp = incomingUp
228 0 : this.usingCustomUp = state.useCustomUp === true
229 0 : this.activeCamera.up.copy(
230 : this.usingCustomUp ? incomingUp : new THREE.Vector3(0, 1, 0)
231 : )
232 0 : this.eventManager.emitEvent('cameraUpStateChange', {
233 : hasCustomUp: true,
234 : usingCustomUp: this.usingCustomUp
235 : })
236 7 : } else if (this.customUp !== null) {
237 0 : this.customUp = null
238 0 : this.usingCustomUp = false
239 0 : this.activeCamera.up.set(0, 1, 0)
240 0 : this.eventManager.emitEvent('cameraUpStateChange', {
241 : hasCustomUp: false,
242 : usingCustomUp: false
243 : })
244 : }
245 :
246 7 : this.controls?.update()
247 : }
248 :
249 : setUseCustomUp(use: boolean): void {
250 0 : if (use && !this.customUp) return
251 0 : if (use === this.usingCustomUp) return
252 0 : const target =
253 : use && this.customUp ? this.customUp : new THREE.Vector3(0, 1, 0)
254 0 : this.activeCamera.up.copy(target)
255 0 : this.usingCustomUp = use
256 0 : this.controls?.update()
257 0 : this.eventManager.emitEvent('cameraUpStateChange', {
258 : hasCustomUp: this.customUp !== null,
259 : usingCustomUp: this.usingCustomUp
260 : })
261 : }
262 :
263 : handleResize(width: number, height: number): void {
264 149 : const aspect = width / height
265 149 : this.updateAspectRatio(aspect)
266 : }
267 :
268 : updateAspectRatio(aspect: number): void {
269 1068 : if (this.activeCamera === this.perspectiveCamera) {
270 1068 : this.perspectiveCamera.aspect = aspect
271 1068 : this.perspectiveCamera.updateProjectionMatrix()
272 : } else {
273 0 : const frustumSize = 10
274 0 : this.orthographicCamera.left = (-frustumSize * aspect) / 2
275 0 : this.orthographicCamera.right = (frustumSize * aspect) / 2
276 0 : this.orthographicCamera.top = frustumSize / 2
277 0 : this.orthographicCamera.bottom = -frustumSize / 2
278 0 : this.orthographicCamera.updateProjectionMatrix()
279 : }
280 : }
281 :
282 : setupForModel(
283 : size: THREE.Vector3,
284 : center: THREE.Vector3 = new THREE.Vector3(0, size.y / 2, 0)
285 : ): void {
286 17 : const maxDim = Math.max(size.x, size.y, size.z)
287 17 : const distance = Math.max(size.x, size.z) * 2
288 17 : const height = center.y + maxDim
289 :
290 17 : this.perspectiveCamera.position.set(
291 : center.x + distance,
292 : height,
293 : center.z + distance
294 : )
295 17 : this.orthographicCamera.position.set(
296 : center.x + distance,
297 : height,
298 : center.z + distance
299 : )
300 :
301 17 : if (this.activeCamera === this.perspectiveCamera) {
302 17 : this.perspectiveCamera.lookAt(center)
303 17 : this.perspectiveCamera.updateProjectionMatrix()
304 : } else {
305 0 : const frustumSize = maxDim * 2
306 0 : const aspect = this.perspectiveCamera.aspect
307 0 : this.orthographicCamera.left = (-frustumSize * aspect) / 2
308 0 : this.orthographicCamera.right = (frustumSize * aspect) / 2
309 0 : this.orthographicCamera.top = frustumSize / 2
310 0 : this.orthographicCamera.bottom = -frustumSize / 2
311 0 : this.orthographicCamera.lookAt(center)
312 0 : this.orthographicCamera.updateProjectionMatrix()
313 : }
314 :
315 17 : this.controls?.target.copy(center)
316 17 : this.controls?.update()
317 : }
318 :
319 : reset(): void {
320 45 : this.perspectiveCamera.position.set(
321 : this.DEFAULT_DISTANCE,
322 : this.DEFAULT_DISTANCE,
323 : this.DEFAULT_DISTANCE
324 : )
325 :
326 45 : this.orthographicCamera.position.set(
327 : this.DEFAULT_DISTANCE,
328 : this.DEFAULT_DISTANCE,
329 : this.DEFAULT_DISTANCE
330 : )
331 :
332 45 : this.perspectiveCamera.lookAt(
333 : this.DEFAULT_LOOK_AT,
334 : this.DEFAULT_LOOK_AT,
335 : this.DEFAULT_LOOK_AT
336 : )
337 45 : this.orthographicCamera.lookAt(
338 : this.DEFAULT_LOOK_AT,
339 : this.DEFAULT_LOOK_AT,
340 : this.DEFAULT_LOOK_AT
341 : )
342 :
343 45 : this.perspectiveCamera.updateProjectionMatrix()
344 45 : this.orthographicCamera.updateProjectionMatrix()
345 : }
346 : }
|