# Changing the background color of a 3D Scene in Makie

**URL:** <https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [January 17, 2021, 4:17pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490 "2021-01-17T16:17:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [January 17, 2021, 4:17pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/1 "2021-01-17T16:17:29Z")

</div>

How can I make the background color of a (3D) scene contained within an `LScene` ? Including `backgroundcolor=:black` in the `scenekw` arg doesn’t seem to do it, nor does setting the attribute directly on `lscene.scene` . However, setting the attribute on `scene` seems to change `lscene.scene.attributes[:background]`.

With a bit of fumbling around I found that if I set `lscene.scene.clear=true` then I get the effect that I’m looking for in particular which is a black background. However if I use a different color, e.g. `lscene.scene.attributes[:background] = :purple` then this kills performance; also it seems that once that attribute has been set it can’t be changed.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 30, 2021, 6:11pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/2 "2021-01-30T18:11:15Z")

</div>

@airpmb, was this issue solved?

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [January 30, 2021, 8:15pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/3 "2021-01-30T20:15:21Z")

</div>

Not as yet. Though I do wonder if it’s related to some of the other performance issues on MacOS and wonder if anyone can duplicate it on another platform.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [January 30, 2021, 9:55pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/4 "2021-01-30T21:55:28Z")

</div>

I’m not sure what are you looking for but the following works just fine:

```julia
using GLMakie, ColorSchemes, Colors
# archimedean spiral
a, m, z₀ = 1, 2.1, 0
φ = LinRange(0,20π,500)
r = a*φ
x, y, z = r .* cos.(φ), r .* sin.(φ), m .* r .+ z₀;

set_theme!(backgroundcolor = :black)

fig = Figure(resolution = (500, 500))
ax = LScene(fig)
lines!(x, y, z, color = z, colormap = :viridis)
axis = ax.scene[OldAxis] # you can change more colors here!
axis[:ticks][:textcolor] = :white
fig[1, 1] = ax
#save("./results/FigBlack.png", fig, px_per_unit = 1)
fig

```

![FigBlack](https://global.discourse-cdn.com/julialang/original/3X/7/7/7718614ae548c19be7efaad51582b5bf23331ab1.png)  
For more details you can take a look at this notebook.  
[https://nbviewer.jupyter.org/github/lazarusA/MakieNotebooks/blob/main/MakieBlack.ipynb](https://nbviewer.jupyter.org/github/lazarusA/MakieNotebooks/blob/main/MakieBlack.ipynb)

Best,  
Lazaro

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 30, 2021, 10:43pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/5 "2021-01-30T22:43:57Z")

</div>

@lazarusA, do you know why the following is not enough to set the background color (at least in Win10, Julia Version 1.6.0-beta1.0):

```julia
lines(x, y, z, color = z, backgroundcolor = :black)

```

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [January 30, 2021, 10:50pm UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/6 "2021-01-30T22:50:19Z")

</div>

To be honest, nope 😃 (this also doesn’t work for me), but probably @jules knows ?

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [January 31, 2021, 2:05am UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/7 "2021-01-31T02:05:34Z")

</div>

> [@lazarusA](#):
>
> `set_theme!(backgroundcolor = :black)`

That works for me as well but I think the issue I’m seeing has to do with wanting to have a different background color in a 3D LScene within a larger layout which has another background color. I probably should have made that part clearer in my post.

With regards to what I was observing about performance when I used `:purple` I just noticed something that I hadn’t before: it seems that the background in this case actually isn’t a constant purple but actually there is a very subtle color gradient towards black at the edges:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/c/6ca2fe0f1aea172b7496b085da31559e9ff91eb4.jpeg)

The really weird thing is that not every color causes this. I just picked purple at random; when I change that one word `:purple` in the code to `:green` instead, I see no gradient and no sluggishness:

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

Fortunately, as I mentioned in the original post, `:black` works in that it doesn’t kill performance, and I do get what I’m looking for which is something like this:

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

(in this last I also used the `set_theme!` as suggested to get the grey background outside of the 3D view).

Anyhow to be clear, for now I have the output I’m looking for but I really can’t say I understand why it works, and in particularly why `lscene.scene.clear=true` is important, and why I sometimes see the gradient and performance behavior based just on what color I specify in the `scenekw` args in the `LScene` call.

Is someone aware of a similar example combining controls on a different-color background, with a 3D view (subscene? LScene?), all using the newer layout API?

I’ll try to get to making a MWE and post it so others can explore this if they want, but unless maybe something about the gradient makes one of you Makie experts say ‘aha’, I won’t be too sad about going forward with what I have.

---

<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:** [January 31, 2021, 7:39am UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/8 "2021-01-31T07:39:14Z")

</div>

The gradient is an SSAO artifact I think, and that’s also what drags down performance. At least @ffreyer said something to that effect if I’m not mistaken, and he’s the one who implemented it and currently has a PR active to fix this (and disable SSAO by default)

---

<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:** [January 31, 2021, 10:34am UTC](https://discourse.julialang.org/t/changing-the-background-color-of-a-3d-scene-in-makie/53490/9 "2021-01-31T10:34:13Z")

</div>

Oh, I messed up a bit there.

SSAO uses a normal buffer which gets cleared to (1,1,1) at the start of the drawing process. I use that clear value to discard pixels that have no geometry. What I overlooked is that the background color again clears that buffer, so if you set it to something other than white the ssao pipeline thinks there is geometry there. I pushed a fix in the pr that allows you to skip the whole SSAO pipeline.
