-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
camera3d.jl
688 lines (565 loc) · 25.7 KB
/
camera3d.jl
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
abstract type AbstractCamera3D <: AbstractCamera end
struct Camera3D <: AbstractCamera3D
# User settings
settings::Attributes
controls::Attributes
# Interactivity
pulser::Observable{Float64}
selected::Observable{Bool}
# view matrix
eyeposition::Observable{Vec3f}
lookat::Observable{Vec3f}
upvector::Observable{Vec3f}
# perspective projection matrix
fov::Observable{Float32}
near::Observable{Float32}
far::Observable{Float32}
end
"""
Camera3D(scene[; kwargs...])
Sets up a 3D camera with mouse and keyboard controls.
The behavior of the camera can be adjusted via keyword arguments or the fields
`settings` and `controls`.
## Settings
Settings include anything that isn't a mouse or keyboard button.
- `projectiontype = Perspective` sets the type of the projection. Can be `Orthographic` or `Perspective`.
- `rotation_center = :lookat` sets the default center for camera rotations. Currently allows `:lookat` or `:eyeposition`.
- `fixed_axis = false`: If true panning uses the (world/plot) z-axis instead of the camera up direction.
- `zoom_shift_lookat = true`: If true keeps the data under the cursor when zooming. Only applies to orthographic cameras.
- `cad = false`: If true rotates the view around `lookat` when zooming off-center.
- `keyboard_rotationspeed = 1f0` sets the speed of keyboard based rotations.
- `keyboard_translationspeed = 0.5f0` sets the speed of keyboard based translations.
- `keyboard_zoomspeed = 1f0` sets the speed of keyboard based zooms.
- `mouse_rotationspeed = 1f0` sets the speed of mouse rotations.
- `mouse_translationspeed = 0.5f0` sets the speed of mouse translations.
- `mouse_zoomspeed = 1f0` sets the speed of mouse zooming (mousewheel).
- `update_rate = 1/30` sets the rate at which keyboard based camera updates are evaluated.
- `circular_rotation = (true, true, true)` enables circular rotations for (fixed x, fixed y, fixed z) rotation axis. (This means drawing a circle with your mouse around the center of the scene will result in a continuous rotation.)
## Controls
Controls include any kind of hotkey setting.
- `up_key = Keyboard.r` sets the key for translations towards the top of the screen.
- `down_key = Keyboard.f` sets the key for translations towards the bottom of the screen.
- `left_key = Keyboard.a` sets the key for translations towards the left of the screen.
- `right_key = Keyboard.d` sets the key for translations towards the right of the screen.
- `forward_key = Keyboard.w` sets the key for translations into the screen.
- `backward_key = Keyboard.s` sets the key for translations out of the screen.
- `zoom_in_key = Keyboard.u` sets the key for zooming into the scene (enlarge, via fov).
- `zoom_out_key = Keyboard.o` sets the key for zooming out of the scene (shrink, via fov).
- `pan_left_key = Keyboard.j` sets the key for rotations around the screens vertical axis.
- `pan_right_key = Keyboard.l` sets the key for rotations around the screens vertical axis.
- `tilt_up_key = Keyboard.i` sets the key for rotations around the screens horizontal axis.
- `tilt_down_key = Keyboard.k` sets the key for rotations around the screens horizontal axis.
- `roll_clockwise_key = Keyboard.e` sets the key for rotations of the screen.
- `roll_counterclockwise_key = Keyboard.q` sets the key for rotations of the screen.
- `fix_x_key = Keyboard.x` sets the key for fixing translations and rotations to the (world/plot) x-axis.
- `fix_y_key = Keyboard.y` sets the key for fixing translations and rotations to the (world/plot) y-axis.
- `fix_z_key = Keyboard.z` sets the key for fixing translations and rotations to the (world/plot) z-axis.
- `reset = Keyboard.home` sets the key for fully resetting the camera. This equivalent to setting `lookat = Vec3f(0)`, `upvector = Vec3f(0, 0, 1)`, `eyeposition = Vec3f(3)` and then calling `center!(scene)`.
- `translation_button = Mouse.right` sets the mouse button for drag-translations. (up/down/left/right)
- `scroll_mod = true` sets an additional modifier button for scroll-based zoom. (true being neutral)
- `rotation_button = Mouse.left` sets the mouse button for drag-rotations. (pan, tilt)
## Other kwargs
Some keyword arguments are used to initialize fields. These include
- `eyeposition = Vec3f(3)`: The position of the camera.
- `lookat = Vec3f(0)`: The point the camera is focused on.
- `upvector = Vec3f(0, 0, 1)`: The world direction corresponding to the up direction of the screen.
- `fov = 45.0` is the field of view. This is irrelevant if the camera uses an orthographic projection.
- `near = 0.1` sets the position of the near clip plane relative to `eyeposition - lookat`. Must be greater 0. Anything between the camera and the near clip plane is hidden.
- `far = 10.0` sets the position of the far clip plane relative to `eyeposition - lookat`. Anything further away than the far clip plane is hidden.
Note that updating these observables in an active camera requires a call to `update_cam(scene)`
for them to be applied. For updating `eyeposition`, `lookat` and/or upvector
`update_cam!(scene, eyeposition, lookat, upvector = Vec3f(0,0,1))` is preferred.
The camera position and orientation can also be adjusted via the functions
- `translate_cam!(scene, v)` will translate the camera by the given world/plot space vector `v`.
- `rotate_cam!(scene, angles)` will rotate the camera around its axes with the corresponding angles. The first angle will rotate around the cameras "right" that is the screens horizontal axis, the second around the up vector/vertical axis or `Vec3f(0, 0, +-1)` if `fixed_axis = true`, and the third will rotate around the view direction i.e. the axis out of the screen. The rotation respects the current `rotation_center` of the camera.
- `zoom!(scene, zoom_step)` will change the zoom level of the scene without translating or rotating the scene. `zoom_step` applies multiplicatively to `cam.zoom_mult` which is used as a multiplier to the fov (perspective projection) or width and height (orthographic projection).
"""
function Camera3D(scene::Scene; kwargs...)
overwrites = Attributes(kwargs)
controls = Attributes(
# Keyboard controls
# Translations
up_key = Keyboard.r,
down_key = Keyboard.f,
left_key = Keyboard.a,
right_key = Keyboard.d,
forward_key = Keyboard.w,
backward_key = Keyboard.s,
# Zooms
zoom_in_key = Keyboard.u,
zoom_out_key = Keyboard.o,
# Rotations
pan_left_key = Keyboard.j,
pan_right_key = Keyboard.l,
tilt_up_key = Keyboard.i,
tilt_down_key = Keyboard.k,
roll_clockwise_key = Keyboard.e,
roll_counterclockwise_key = Keyboard.q,
# Mouse controls
translation_button = Mouse.right,
rotation_button = Mouse.left,
scroll_mod = true,
# Shared controls
fix_x_key = Keyboard.x,
fix_y_key = Keyboard.y,
fix_z_key = Keyboard.z,
reset = Keyboard.home
)
replace!(controls, :Camera3D, scene, overwrites)
settings = Attributes(
keyboard_rotationspeed = 1f0,
keyboard_translationspeed = 0.5f0,
keyboard_zoomspeed = 1f0,
mouse_rotationspeed = 1f0,
mouse_translationspeed = 1f0,
mouse_zoomspeed = 1f0,
projectiontype = Makie.Perspective,
circular_rotation = (true, true, true),
rotation_center = :lookat,
update_rate = 1/30,
zoom_shift_lookat = true,
fixed_axis = true,
cad = false
)
replace!(settings, :Camera3D, scene, overwrites)
cam = Camera3D(
settings, controls,
# Internals - controls
Observable(-1.0),
Observable(true),
# Semi-Internal - view matrix
get(overwrites, :eyeposition, Observable(Vec3f(3, 3, 3))),
get(overwrites, :lookat, Observable(Vec3f(0, 0, 0))),
get(overwrites, :upvector, Observable(Vec3f(0, 0, 1))),
# Semi-Internal - projection matrix
get(overwrites, :fov, Observable(45.0)),
get(overwrites, :near, Observable(0.1)),
get(overwrites, :far, Observable(10.0)),
)
disconnect!(camera(scene))
# Keyboard controls
# ticks every so often to get consistent position updates.
on(cam.pulser) do prev_time
current_time = time()
active = on_pulse(scene, cam, current_time - prev_time)
@async if active && cam.selected[]
sleep(settings.update_rate[])
cam.pulser[] = current_time
else
cam.pulser.val = -1.0
end
end
keynames = (
:up_key, :down_key, :left_key, :right_key, :forward_key, :backward_key,
:zoom_in_key, :zoom_out_key,
:pan_left_key, :pan_right_key, :tilt_up_key, :tilt_down_key,
:roll_clockwise_key, :roll_counterclockwise_key
)
# Start ticking if relevant keys are pressed
on(camera(scene), events(scene).keyboardbutton) do event
if event.action in (Keyboard.press, Keyboard.repeat) && cam.pulser[] == -1.0 &&
cam.selected[] && any(key -> ispressed(scene, controls[key][]), keynames)
cam.pulser[] = time()
return Consume(true)
end
return Consume(false)
end
# de/select plot on click outside/inside
# also deselect other cameras
deselect_all_cameras!(root(scene))
on(camera(scene), events(scene).mousebutton, priority = 100) do event
if event.action == Mouse.press
cam.selected[] = is_mouseinside(scene)
end
return Consume(false)
end
# Mouse controls
add_translation!(scene, cam)
add_rotation!(scene, cam)
# add camera controls to scene
cameracontrols!(scene, cam)
# Trigger updates on scene resize and settings change
on(camera(scene), cam.fov) do _
if settings.projectiontype[] == Makie.Perspective
update_cam!(scene, cam)
end
end
on(camera(scene), scene.px_area, cam.near, cam.far, settings.projectiontype) do _, _, _, _
update_cam!(scene, cam)
end
# reset
on(camera(scene), events(scene).keyboardbutton) do event
if cam.selected[] && event.key == controls[:reset][] && event.action == Keyboard.release
# center keeps the rotation of the camera so we reset that here
# might make sense to keep user set lookat, upvector, eyeposition
# around somewhere for this?
center!(scene)
return Consume(true)
end
return Consume(false)
end
update_cam!(scene, cam)
cam
end
# These imitate the old camera
cam3d!(scene; zoom_shift_lookat = true, fixed_axis = true, kwargs...) =
Camera3D(scene, zoom_shift_lookat = zoom_shift_lookat, fixed_axis = fixed_axis; kwargs...)
cam3d_cad!(scene; cad = true, zoom_shift_lookat = false, fixed_axis = false, kwargs...) =
Camera3D(scene, cad = cad, zoom_shift_lookat = zoom_shift_lookat, fixed_axis = fixed_axis; kwargs...)
function deselect_all_cameras!(scene)
cam = cameracontrols(scene)
cam isa AbstractCamera3D && (cam.selected[] = false)
for child in scene.children
deselect_all_cameras!(child)
end
nothing
end
################################################################################
### Interactivity init
################################################################################
function on_pulse(scene, cam::Camera3D, timestep)
@extractvalue cam.controls (
right_key, left_key, up_key, down_key, backward_key, forward_key,
tilt_up_key, tilt_down_key, pan_left_key, pan_right_key, roll_counterclockwise_key, roll_clockwise_key,
zoom_out_key, zoom_in_key
)
@extractvalue cam.settings (
keyboard_translationspeed, keyboard_rotationspeed, keyboard_zoomspeed, projectiontype
)
# translation
right = ispressed(scene, right_key)
left = ispressed(scene, left_key)
up = ispressed(scene, up_key)
down = ispressed(scene, down_key)
backward = ispressed(scene, backward_key)
forward = ispressed(scene, forward_key)
translating = right || left || up || down || backward || forward
if translating
# translation in camera space x/y/z direction
if projectiontype == Perspective
viewnorm = norm(cam.lookat[] - cam.eyeposition[])
xynorm = 2 * viewnorm * tand(0.5 * cam.fov[])
translation = keyboard_translationspeed * timestep * Vec3f(
xynorm * (right - left),
xynorm * (up - down),
viewnorm * (backward - forward)
)
else
# translation in camera space x/y/z direction
viewnorm = norm(cam.eyeposition[] - cam.lookat[])
translation = 2 * viewnorm * keyboard_translationspeed * timestep * Vec3f(
right - left, up - down, backward - forward
)
end
_translate_cam!(scene, cam, translation)
end
# rotation
up = ispressed(scene, tilt_up_key)
down = ispressed(scene, tilt_down_key)
left = ispressed(scene, pan_left_key)
right = ispressed(scene, pan_right_key)
counterclockwise = ispressed(scene, roll_counterclockwise_key)
clockwise = ispressed(scene, roll_clockwise_key)
rotating = up || down || left || right || counterclockwise || clockwise
if rotating
# rotations around camera space x/y/z axes
angles = keyboard_rotationspeed * timestep *
Vec3f(up - down, left - right, counterclockwise - clockwise)
_rotate_cam!(scene, cam, angles)
end
# zoom
zoom_out = ispressed(scene, zoom_out_key)
zoom_in = ispressed(scene, zoom_in_key)
zooming = zoom_out || zoom_in
if zooming
zoom_step = (1f0 + keyboard_zoomspeed * timestep) ^ (zoom_out - zoom_in)
_zoom!(scene, cam, zoom_step, false)
end
# if any are active, update matrices, else stop clock
if translating || rotating || zooming
update_cam!(scene, cam)
return true
else
return false
end
end
function add_translation!(scene, cam::Camera3D)
@extract cam.controls (translation_button, scroll_mod)
@extract cam.settings (mouse_translationspeed, mouse_zoomspeed, cad, projectiontype)
last_mousepos = RefValue(Vec2f(0, 0))
dragging = RefValue(false)
function compute_diff(delta)
if projectiontype[] == Perspective
# TODO wrong scaling? :(
ynorm = 2 * norm(cam.lookat[] - cam.eyeposition[]) * tand(0.5 * cam.fov[])
return ynorm / widths(scene.px_area[])[2] * delta
else
viewnorm = norm(cam.eyeposition[] - cam.lookat[])
return 2 * viewnorm / widths(scene.px_area[])[2] * delta
end
end
# drag start/stop
on(camera(scene), scene.events.mousebutton) do event
if ispressed(scene, translation_button[])
if event.action == Mouse.press && is_mouseinside(scene) && !dragging[]
last_mousepos[] = mouseposition_px(scene)
dragging[] = true
return Consume(true)
end
elseif event.action == Mouse.release && dragging[]
mousepos = mouseposition_px(scene)
diff = compute_diff(last_mousepos[] .- mousepos)
last_mousepos[] = mousepos
dragging[] = false
translate_cam!(scene, cam, mouse_translationspeed[] .* Vec3f(diff[1], diff[2], 0f0))
return Consume(true)
end
return Consume(false)
end
# in drag
on(camera(scene), scene.events.mouseposition) do mp
if dragging[] && ispressed(scene, translation_button[])
mousepos = screen_relative(scene, mp)
diff = compute_diff(last_mousepos[] .- mousepos)
last_mousepos[] = mousepos
translate_cam!(scene, cam, mouse_translationspeed[] * Vec3f(diff[1], diff[2], 0f0))
return Consume(true)
end
return Consume(false)
end
on(camera(scene), scene.events.scroll) do scroll
if is_mouseinside(scene) && ispressed(scene, scroll_mod[])
zoom_step = (1f0 + 0.1f0 * mouse_zoomspeed[]) ^ -scroll[2]
zoom!(scene, cam, zoom_step, cad[])
return Consume(true)
end
return Consume(false)
end
end
function add_rotation!(scene, cam::Camera3D)
@extract cam.controls (rotation_button, )
@extract cam.settings (mouse_rotationspeed, )
last_mousepos = RefValue(Vec2f(0, 0))
dragging = RefValue(false)
e = events(scene)
# drag start/stop
on(camera(scene), e.mousebutton) do event
if ispressed(scene, rotation_button[])
if event.action == Mouse.press && is_mouseinside(scene) && !dragging[]
last_mousepos[] = mouseposition_px(scene)
dragging[] = true
return Consume(true)
end
elseif event.action == Mouse.release && dragging[]
mousepos = mouseposition_px(scene)
dragging[] = false
rot_scaling = mouse_rotationspeed[] * (e.window_dpi[] * 0.005)
mp = (last_mousepos[] .- mousepos) .* 0.01f0 .* rot_scaling
last_mousepos[] = mousepos
rotate_cam!(scene, cam, Vec3f(-mp[2], mp[1], 0f0), true)
return Consume(true)
end
return Consume(false)
end
# in drag
on(camera(scene), e.mouseposition) do mp
if dragging[] && ispressed(scene, rotation_button[])
mousepos = screen_relative(scene, mp)
rot_scaling = mouse_rotationspeed[] * (e.window_dpi[] * 0.005)
mp = (last_mousepos[] .- mousepos) * 0.01f0 * rot_scaling
last_mousepos[] = mousepos
rotate_cam!(scene, cam, Vec3f(-mp[2], mp[1], 0f0), true)
return Consume(true)
end
return Consume(false)
end
end
################################################################################
### Camera transformations
################################################################################
# Simplified methods
function translate_cam!(scene, cam::Camera3D, t::VecTypes)
_translate_cam!(scene, cam, t)
update_cam!(scene, cam)
nothing
end
function rotate_cam!(scene, cam::Camera3D, angles::VecTypes, from_mouse=false)
_rotate_cam!(scene, cam, angles, from_mouse)
update_cam!(scene, cam)
nothing
end
"""
zoom!(scene, zoom_step)
Zooms the camera in or out based on the multiplier `zoom_step`. A `zoom_step`
of 1.0 is neutral, larger zooms out and lower zooms in.
Note that this method only applies to Camera3D.
"""
zoom!(scene, zoom_step) = zoom!(scene, cameracontrols(scene), zoom_step, false)
function zoom!(scene, cam, zoom_step, cad = false)
_zoom!(scene, cam, zoom_step, cad)
update_cam!(scene, cam)
nothing
end
function _translate_cam!(scene, cam::Camera3D, t)
@extractvalue cam.controls (fix_x_key, fix_y_key, fix_z_key)
# This uses a camera based coordinate system where
# x expands right, y expands up and z expands towards the screen
lookat = cam.lookat[]
eyepos = cam.eyeposition[]
up = normalize(cam.upvector[])
u_z = normalize(eyepos - lookat)
u_x = normalize(cross(up, u_z))
u_y = normalize(cross(u_z, u_x))
trans = u_x * t[1] + u_y * t[2] + u_z * t[3]
# apply world space restrictions
fix_x = ispressed(scene, fix_x_key)::Bool
fix_y = ispressed(scene, fix_y_key)::Bool
fix_z = ispressed(scene, fix_z_key)::Bool
if fix_x || fix_y || fix_z
trans = Vec3f(fix_x, fix_y, fix_z) .* trans
end
cam.eyeposition[] = eyepos + trans
cam.lookat[] = lookat + trans
return
end
function _rotate_cam!(scene, cam::Camera3D, angles::VecTypes, from_mouse=false)
@extractvalue cam.controls (fix_x_key, fix_y_key, fix_z_key)
@extractvalue cam.settings (fixed_axis, circular_rotation, rotation_center)
# This applies rotations around the x/y/z axis of the camera coordinate system
# x expands right, y expands up and z expands towards the screen
lookat = cam.lookat[]
eyepos = cam.eyeposition[]
up = cam.upvector[] # +y
viewdir = lookat - eyepos # -z
right = cross(viewdir, up) # +x
x_axis = right
y_axis = fixed_axis ? Vec3f(0, 0, ifelse(up[3] < 0, -1, 1)) : up
z_axis = -viewdir
fix_x = ispressed(scene, fix_x_key)::Bool
fix_y = ispressed(scene, fix_y_key)::Bool
fix_z = ispressed(scene, fix_z_key)::Bool
cx, cy, cz = circular_rotation
rotation = Quaternionf(0, 0, 0, 1)
if !xor(fix_x, fix_y, fix_z)
# if there are more or less than one restriction apply all rotations
# Note that the y rotation needs to happen first here so that
# fixed_axis = true actually keeps the the axis fixed.
rotation *= qrotation(y_axis, angles[2])
rotation *= qrotation(x_axis, angles[1])
rotation *= qrotation(z_axis, angles[3])
else
# apply world space restrictions
if from_mouse && ((fix_x && cx) || (fix_y && cy) || (fix_z && cz))
# recontextualize the (dy, dx, 0) from mouse rotations so that
# drawing circles creates continuous rotations around the fixed axis
mp = mouseposition_px(scene)
past_half = 0.5f0 .* widths(scene.px_area[]) .> mp
flip = 2f0 * past_half .- 1f0
angle = flip[1] * angles[1] + flip[2] * angles[2]
angles = Vec3f(-angle, -angle, angle)
# only one fix is true so this only rotates around one axis
rotation *= qrotation(
Vec3f(fix_x, fix_y, fix_z) .* Vec3f(sign(right[1]), viewdir[2], sign(up[3])),
dot(Vec3f(fix_x, fix_y, fix_z), angles)
)
else
# restrict total quaternion rotation to one axis
rotation *= qrotation(y_axis, angles[2])
rotation *= qrotation(x_axis, angles[1])
rotation *= qrotation(z_axis, angles[3])
# the first three components are related to rotations around the x/y/z-axis
rotation = Quaternionf(rotation.data .* (fix_x, fix_y, fix_z, 1))
end
end
cam.upvector[] = rotation * up
viewdir = rotation * viewdir
# TODO maybe generalize this to arbitrary center?
# calculate positions from rotated vectors
if rotation_center === :lookat
cam.eyeposition[] = lookat - viewdir
else
cam.lookat[] = eyepos + viewdir
end
return
end
function _zoom!(scene, cam::Camera3D, zoom_step, cad = false)
lookat = cam.lookat[]
eyepos = cam.eyeposition[]
viewdir = lookat - eyepos # -z
if cad
# Rotate view based on offset from center
u_z = normalize(viewdir)
u_x = normalize(cross(u_z, cam.upvector[]))
u_y = normalize(cross(u_x, u_z))
rel_pos = 2f0 * mouseposition_px(scene) ./ widths(scene.px_area[]) .- 1f0
shift = rel_pos[1] * u_x + rel_pos[2] * u_y
shift *= 0.1 * sign(1 - zoom_step) * norm(viewdir)
cam.eyeposition[] = lookat - zoom_step * viewdir + shift
elseif cam.settings.projectiontype[] == Makie.Orthographic && cam.settings.zoom_shift_lookat[]
# keep data under cursor
u_z = normalize(viewdir)
u_x = normalize(cross(u_z, cam.upvector[]))
u_y = normalize(cross(u_x, u_z))
ws = widths(scene.px_area[])
rel_pos = (2.0 .* mouseposition_px(scene) .- ws) ./ ws[2]
shift = (1 - zoom_step) * norm(viewdir) * (rel_pos[1] * u_x + rel_pos[2] * u_y)
cam.lookat[] = lookat + shift
cam.eyeposition[] = lookat - zoom_step * viewdir + shift
else
# just zoom in/out
cam.eyeposition[] = lookat - zoom_step * viewdir
end
return
end
################################################################################
### update_cam! methods
################################################################################
# Update camera matrices
function update_cam!(scene::Scene, cam::Camera3D)
@extractvalue cam (lookat, eyeposition, upvector, near, far, fov)
view = Makie.lookat(eyeposition, lookat, upvector)
aspect = Float32((/)(widths(scene.px_area[])...))
if cam.settings.projectiontype[] == Makie.Perspective
view_norm = norm(eyeposition - lookat)
proj = perspectiveprojection(fov, aspect, view_norm * near, view_norm * far)
else
h = norm(eyeposition - lookat); w = h * aspect
proj = orthographicprojection(-w, w, -h, h, h * near, h * far)
end
set_proj_view!(camera(scene), proj, view)
scene.camera.eyeposition[] = cam.eyeposition[]
end
# Update camera position via bbox
function update_cam!(scene::Scene, cam::Camera3D, area3d::Rect)
bb = Rect3f(area3d)
width = widths(bb)
center = maximum(bb) - 0.5 * width
old_dir = normalize(cam.eyeposition[] .- cam.lookat[])
if cam.settings.projectiontype[] == Makie.Perspective
dist = 0.5 * norm(width) * (1.0 + 0.6 / tand(0.5 * cam.fov[]))
else
dist = 0.6 * norm(width)
end
cam.lookat[] = center
cam.eyeposition[] = cam.lookat[] .+ dist * old_dir
cam.upvector[] = Vec3f(0, 0, 1) # Should we reset this?
update_cam!(scene, cam)
return
end
# Update camera position via camera Position & Orientation
function update_cam!(scene::Scene, camera::Camera3D, eyeposition, lookat, up = camera.upvector[])
camera.lookat[] = Vec3f(lookat)
camera.eyeposition[] = Vec3f(eyeposition)
camera.upvector[] = Vec3f(up)
update_cam!(scene, camera)
return
end
function show_cam(scene)
cam = cameracontrols(scene)
println("cam=cameracontrols(scene)")
println("cam.eyeposition[] = ", round.(cam.eyeposition[], digits=2))
println("cam.lookat[] = ", round.(cam.lookat[], digits=2))
println("cam.upvector[] = ", round.(cam.upvector[], digits=2))
println("cam.fov[] = ", round.(cam.fov[], digits=2))
return
end