# Precise control of navigation in 3D scene in Makie?

**URL:** <https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [June 4, 2021, 12:05pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387 "2021-06-04T12:05:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)\
**Post date:** [June 4, 2021, 12:05pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/1 "2021-06-04T12:05:22Z")

</div>

Hi,  
I’m starting to visualize a collection of meshes in Makie, currently using the mouse to navigate the content. However, after a while the rotate, drag and zoom performed usually make me end up with parts of the content getting clipped (i assume there is a frustum defined somewhere) when I try to take a closer look.  
I believe that what I would need is essentially better camera control, preferably by a combination of mous and keyboard eg right/left/forward/back/up/down (strafe?) to control both position and orientation. I guess someone has dome something simliar - or even cooler - already, but searching for something like it I failed find something.  
When I did try the keyboard example from documentation I ran into events not liking being called with a ::Make.Scene  
Currently I use GLMakie from the REPL for this, but I could see it moving to WGLMakie eventually.

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [June 4, 2021, 1:58pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/2 "2021-06-04T13:58:31Z")

</div>

I have some ancient code that added some keyboard controls, though not the ones you want. I also don’t know how well it works on other operating systems. Maybe it’s helpful anyway, so here’s a version that should work with the current version of Makie:

> **Code**
>
> ```julia
> using GLMakie, LinearAlgebra
> 
> function key_controls!(
> ax::LScene;
> tilt_up = Keyboard.w,
> tilt_down = Keyboard.s,
> roll_clockwise = Keyboard.e,
> roll_counterclockwise = Keyboard.q,
> pan_left = Keyboard.a,
> pan_right = Keyboard.d,
> step_size::AbstractFloat = Float32(pi/180)
> )
> 
> camera = ax.scene.camera_controls[]
> 
> map(events(ax.scene).keyboardbutton) do event
> event.action == Keyboard.release && return false
> 
> # Get camera information
> eyepos = camera.eyeposition[]
> lookat = camera.lookat[]
> up = camera.upvector[]
> 
> # camera coordinate system
> dir = eyepos - lookat
> dir_norm = normalize(dir)
> right = normalize(cross(dir, up))
> 
> # compute combined rotation
> rotation = Makie.Quaternion{Float32}(0, 0, 0, 1)
> for key in events(ax.scene).keyboardstate
> if key in (pan_left, pan_right)
> rotation *= qrotation(up, step_size * ((pan_left == key) - (pan_right == key)))
> end
> if key in (tilt_down, tilt_up)
> rotation *= qrotation(right, step_size * ((tilt_down == key) - (tilt_up == key)))
> end
> if key in (roll_clockwise, roll_counterclockwise)
> rotation *= qrotation(dir_norm, 
> step_size * ((roll_clockwise == key) - (roll_counterclockwise == key))
> )
> end
> end
> 
> # Set values, update camera
> camera.eyeposition[] = lookat + rotation * dir
> camera.upvector[] = normalize(rotation * up)
> update_cam!(ax.scene, camera)
> 
> return false
> end
> end
> 
> fig, ax, p = scatter(rand(Point3f0, 10))
> controls = key_controls!(ax)
> fig
> 
> ```

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [June 4, 2021, 10:14pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/3 "2021-06-04T22:14:27Z")

</div>

I also started a pull request for a more controllable/adjustable camera:

> <https://github.com/MakieOrg/Makie.jl/pull/1024>
>
> My goal is to create a more controllable 3d camera. If I can figure out how to t…ransfer everything maybe this can be a replacement for the current 3d camera.
> 
> TODO
> \- \[x\] translations (x/y/z or truck/pedestal/dolly) via keyboard
> \- \[x\] rotations (around y/x/z axis or pan/tilt/roll) via keyboad
> \- \[x\] zoom via keyboard
> \- \[x\] switch for rotation center (lookat vs eyeposition as center)
> \- \[x\] pan/tilt via left mouse drag
> \- \[x\] truck/pedestal via right mouse drag
> \- \[x\] zoom via mousewheel
> \- \[x\] fix issue with far reseting (fixes #801, see also #413)
> \- \[x\] add perspective switch
> \- \[x\] use fov zoom in perspective camera (fixes #602, #941)
> \- \[x\] add cad version (Idk what exactly this is supposed to do so it might be wrong)
> \- \[x\] add translations/rotations snapping to axis directions (i.e. x + drag -\> only translate in x direction / rotate around x-axis)
> \- \[x\] fix issue with camera partially resetting to cam3d! on plot! calls
> \- \[x\] cleanup camera correctly
> \- \[x\] cleanup restricted rotations with key interaction
> \- \[x\] ~~maybe make rotations around the up axis loop-like~~ This only feels right when fixing an axis
> \- \[x\] adjust default keys (a, d to translation)
> \- \[x\] scale translations with zoom level
> \- \[x\] update docs 
> 
> Related:
> https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie
> https://github.com/JuliaPlots/Makie.jl/issues/941
> I'm surprised I'm not finding more issues related to this

---

<div class="post-metadata">

**Author:** ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)\
**Post date:** [July 17, 2021, 8:12pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/4 "2021-07-17T20:12:59Z")

</div>

Nice job, I did use the version in the PR with pretty good results. But now that it’s merged into 0.15.0, how do I continue to use it - the keyboard controls don’t seem to be active as default and mesh no longer takes a camera attribute?

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 18, 2021, 9:54am UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/5 "2021-07-18T09:54:26Z")

</div>

It should be the default in an `LScene`. If not you can try calling `Camera3D(lscene.scene)` (or `cam3d!` or `cam3d_cad!`).

---

<div class="post-metadata">

**Author:** ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)\
**Post date:** [July 20, 2021, 11:32am UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/6 "2021-07-20T11:32:53Z")

</div>

Tried the example at [https://makie.juliaplots.org/stable/makielayout/lscene.html](https://makie.juliaplots.org/stable/makielayout/lscene.html) with GLMakie rather than WGLMakie, but no keyboard controls working in the Makie window (mouse works as before).  
My env:  
(annotell) pkg\> st  
Status `~/proj/julia/annotell/Project.toml`  
[cd3eb016] HTTP v0.9.12  
[682c06a0] JSON v0.21.1  
[ee78f7c6] Makie v0.15.0

(annotell) pkg\>

Trying to call Camera3D(lscene.scene) fails:  
julia\> Camera3D(lscene.scene)

ERROR: MethodError: no method matching Camera3D(::Scene)

---

<div class="post-metadata">

**Author:** ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)\
**Post date:** [July 27, 2021, 1:21pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/7 "2021-07-27T13:21:17Z")

</div>

ping @ffreyer

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 27, 2021, 11:27pm UTC](https://discourse.julialang.org/t/precise-control-of-navigation-in-3d-scene-in-makie/62387/8 "2021-07-27T23:27:56Z")

</div>

I don’t know what’s wrong. [0.15 has Camera3D](https://github.com/JuliaPlots/Makie.jl/blob/ed312d512e6a0a947a039a427a76d2dfb89c48c6/src/camera/camera3d.jl#L84), with GLMakie you should be getting keyboard controls unless you explicitly use `old_cam3d!` etc…
