# Inset axis on a LScene in Makie

**URL:** <https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125>\
**Category:** General Usage\
**Tags:** makie\
**Created:** [October 18, 2023, 12:57pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125 "2023-10-18T12:57:35Z")\
**Posts on this page:** 7\
**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:** [October 18, 2023, 12:57pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/1 "2023-10-18T12:57:35Z")

</div>

I’m currently trying to display a background image behind a `LScene`.  
I tried to `translate!` the `Axis` containing the image behind the `LScene` as recommended for inset axes in [Makie - Inset axes and their drawing order](https://discourse.julialang.org/t/makie-inset-axes-and-their-drawing-order/60987), but even big translation offsets do not seem to work. Is there something specific to do for `LScene`s ?

Il also tried plotting the image in a `LScene` but I have trouble orienting it properly (horizontally and in the screen plane).

Here isa MWE below with the famous cow example.

```julia
# Setup
using GLMakie
using FileIO
img = load(assetpath("cow.png"))

```

```julia
# Try with image in `Axis` but it is always in foreground
f = Figure()
ax = LScene(f[1,1])
zoom!(ax.scene,0.1)
ax_inset = Axis(f[1,1],aspect=DataAspect())
image!(ax_inset, rotr90(img))
f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/7/b76c8a0702a4c31755317cfc158e12c22cf9284d.jpeg)

```julia
# Try with image in `LScene` but orientation of the image in background is not easy
f = Figure()
ax = LScene(f[1,1],show_axis=true)
image!(ax, rotr90(img))
f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/1/0188fa2a8565f6bf2ee1694b17410af8c3edcdd9.jpeg)

Side note, if this second call is done with `CairoMakie`, the output is different  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/3/a398c745fdfdaf80b42dba99a91798839fec1a98.png)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 19, 2023, 9:39am UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/2 "2023-10-19T09:39:45Z")

</div>

Hm I can also not get that to work in any way I try, the image is always in front. @sdanisch or @ffreyer might know why.

(In CairoMakie, 3D images don’t work because you don’t have 2D transforms available that would work for 3D perspective projections, we’d have to mesh render this)

---

<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:** [October 19, 2023, 12:01pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/3 "2023-10-19T12:01:42Z")

</div>

Thanks @jules, I’ve also tried to replace the sub-`Scene` in the [Scene Tutorial (Scenes and subwindows)](https://docs.makie.org/stable/tutorials/scenes/#scenes_and_subwindows), but it seems the scene inside the `LScene` does not quite behave the same way as a standalone `Scene`

The modified scene example below

```julia
f = Figure()
lscene = LScene(f[1,1],scenekw=(backgroundcolor=:gray,))
scene = lscene.scene
subwindow = Scene(scene, px_area=Rect(100, 100, 200, 200), clear=true, backgroundcolor=:white)
scene
cam3d!(subwindow)
meshscatter!(subwindow, rand(Point3f, 10), color=:gray)
center!(subwindow)
scene
subwindow.clear = false
relative_space = Makie.camrelative(subwindow)
# this draws a line at the scene window boundary
lines!(relative_space, Rect(0, 0, 1, 1))
scene
campixel!(scene)
w, h = size(scene) # get the size of the scene in pixels
# this draws a line at the scene window boundary
image!(scene, [sin(i / w) + cos(j / h) for i in 1:w, j in 1:h])
scene
translate!(scene.plots[1], 0, 0, -10000)
scene

```

returns

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/41d9fed00a2677c3421763718d78a64d1788f15e.png)

---

<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:** [October 19, 2023, 7:51pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/4 "2023-10-19T19:51:06Z")

</div>

If you just want to have a background image you don’t need to have a whole Axis for it. The simplest way to do it is probably this:

```julia

f = Figure()
ax = LScene(f[1,1])
# ...
x = map(r -> minimum(r)[1] .. maximum(r)[1], pixelarea(ax.scene))
y = map(r -> minimum(r)[2] .. maximum(r)[2], pixelarea(ax.scene))
ip = image!(f.scene, x, y, rotr90(img))
translate!(ip, 0, 0, -10_000)
f

```

To get your subscene example to work you need to translate `scene.plots[end]`. Otherwise you’re translating an axis3d that `LScene` generated. I would also suggest you use the LScene for plotting and the extra `Scene` to add a background, since LScene has stuff like axis3d by default and Scene does not

---

<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:** [November 19, 2023, 6:21pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/5 "2023-11-19T18:21:14Z")

</div>

Hey @ffreyer, your solution works fine when the plotted object is a 2D one, but would you know if it is possible to achieve the same thing with an 3D scene inset into another one ? For that case, the `translate` only moves (typically the mesh) plotted object, in the scene…

---

<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:** [November 20, 2023, 1:35pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/6 "2023-11-20T13:35:55Z")

</div>

Kind of… GLMakie currently shares one depth buffer for everything that’s shown, so if you want something to show up in front or behind something you need to adjust the depth value it ends up at. For 2D you can do that easily with `translate!()` because the camera/view direction is fixed.  
For 3D you can rotate the view so translate, which applies before that, is not helpful. You can set the closest and farthest point the camera shows though, which becomes the lowest and largest depth respectively.

```julia
fig = Figure()

main = LScene(fig[1, 1])
cat = Makie.FileIO.load(Makie.assetpath("cat.obj"))
tex = Makie.FileIO.load(Makie.assetpath("diffusemap.png"))
mesh!(main, cat, color = tex)

inset = LScene(fig, bbox = Makie.BBox(200, 400, 100, 300), show_axis = false)
mesh!(inset, Sphere(Point3f(0), 1f0), color = :lightblue)
cam = cameracontrols(inset.scene)
cam.settings.clipping_mode[] = :static # v0.20+
display(fig)

# Need to happen after display & plotting to not get overwritten
# move plot back by moving min depth closer to camera (must be > 0)
cam.near[] = 1f-5

# move plot forward by moving the max depth further from the camera
cam.far[] = 100f0

```

You can also adjust the depth value of a plot after all the other transformation with `depth_shift`. That’s probably not helpful here though, since it only translates the plot and doesn’t avoid one plot clipping into another. Adjusting near and far effectively squishes the plot into a smaller percentage of the depth range so it’ll help with clipping issues.

It used to also be possible to set `scene.clear[] = true` to effectively have a scene wipe the region it draws to before drawing to it, but I think that broke with [Remove stencil buffer by ffreyer · Pull Request #2389 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/2389).

---

<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:** [November 20, 2023, 4:12pm UTC](https://discourse.julialang.org/t/inset-axis-on-a-lscene-in-makie/105125/7 "2023-11-20T16:12:14Z")

</div>

> [@ffreyer](#):
>
> It used to also be possible to set `scene.clear[] = true` to effectively have a scene wipe the region it draws to before drawing to it, but I think that broke with [Remove stencil buffer by ffreyer · Pull Request #2389 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/2389).

Yes it seems that `clear` does not work anymore… I’ll try to adapt the `near`/`far` properties.  
Thanks !
