# Plot the camera field of view in Makie?

**URL:** <https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [February 22, 2023, 6:03pm UTC](https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036 "2023-02-22T18:03:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [February 22, 2023, 6:03pm UTC](https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036/1 "2023-02-22T18:03:47Z")

</div>

There is probably a way to achieve this using `camera` properties, but how could the field of view of the current camera be displayed onto another scene in Makie ?

In terms of rendering, one could expect something like  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/0/203e6201026f24965b65f8de26c10d5415fb90f4.jpeg)  
take from [https://www.rerun.io/](https://www.rerun.io/)

This is of particular interest when zooming in a large scene but while having an overall picture in a corner. The idea would be to show the current vision cone upon updating of the camera of the main scene.

I’ll try to provide an example of what I have in mind.

---

<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:** [February 23, 2023, 12:33am UTC](https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036/2 "2023-02-23T00:33:00Z")

</div>

One way to do it is to do an inverse transform from clip space back to data space. (And maybe that’s also a good way to do it since you don’t depend on what kind of projection the camera uses.)

```julia
fig = Figure()
ax = LScene(fig[1, 1], show_axis = false)
cam3d!(ax)

mesh!(ax, Rect2f(-10, -10, 20, 20), color = :lightgray)
meshscatter!(ax, [2 .* rand(Point3f) .- 1 for _ in 1:100], color = rand(100))

# generate clip space rectangle coordinates
frustum_ps = Observable(Point3f[])
r = Rect3f(Point3f(-1, -1, 0), Vec3f(2, 2, 1))
rect_ps = coordinates(r) .|> Point3f
insert!(rect_ps, 13, Point3f(1, -1, 1)) # fix bad line

# on key press, convert clip space back to data space
on(events(fig).keyboardbutton) do e
    if (e.key == Keyboard.t) && (e.action == Keyboard.press)
        inv_pv = inv(ax.scene.camera.projectionview[])
        ps = map(rect_ps) do p
            p = inv_pv * to_ndim(Point4f, p, 1)
            return p[Vec(1,2,3)] / p[4]
        end
        frustum_ps[] = ps
    end
end

# show frustum
lines!(ax, frustum_ps, color = :red)

update_cam!(ax.scene, Rect3f(Point3f(-1), Vec3f(2)))

fig

```

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [February 23, 2023, 8:36am UTC](https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036/3 "2023-02-23T08:36:34Z")

</div>

Thanks for you answer @ffreyer ! My Makie seems to complain about the `coordinates` function though. I’m running this on GLMakie v0.8.2, is this function part of master ?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [February 23, 2023, 9:21am UTC](https://discourse.julialang.org/t/plot-the-camera-field-of-view-in-makie/95036/4 "2023-02-23T09:21:12Z")

</div>

It’s part of GeometrBasics
