# Light shading for 'inverted' surface plot in GLMakie

**URL:** <https://discourse.julialang.org/t/light-shading-for-inverted-surface-plot-in-glmakie/107064>\
**Category:** Visualization\
**Tags:** plotting, makie, glmakie\
**Created:** [December 3, 2023, 12:05am UTC](https://discourse.julialang.org/t/light-shading-for-inverted-surface-plot-in-glmakie/107064 "2023-12-03T00:05:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [December 3, 2023, 12:05am UTC](https://discourse.julialang.org/t/light-shading-for-inverted-surface-plot-in-glmakie/107064/1 "2023-12-03T00:05:05Z")

</div>

If I want to plot an ‘inverted’ surface, like the cupcake mold in the top figure below.

I set the light directions to be

```julia
lights = [
        AmbientLight(RGBf(1, 1, 1)),
        SpotLight(0.4*RGBf(1, 1, 1), Point3f(10, 0, 2.5), Vec3f(-1, 0, -0.1), Vec2f(0.25pi, 0.25pi))
    ]

```

I haven’t optimized the lighting effects yet, but right now it’s coming from the right side of the screen. Notice that the lighting for the upper gray mold is incorrect: the inverted protrusion should be lighted, and the inner region should be dark, but I guess it has problems calculating the unit normal to the surface?

For the bottom plot, I invert the mold, and the shading is correct: the right side of the surface, which is facing the light source, becomes brighter. Are there any ways to make the lighting correct for the top plot with the inverted surface?

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

---

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [December 3, 2023, 12:26am UTC](https://discourse.julialang.org/t/light-shading-for-inverted-surface-plot-in-glmakie/107064/2 "2023-12-03T00:26:10Z")

</div>

So after some trial and error I think I have a temporary fix:

```julia
X = ones(length(y)) .* x'
Y = y .* ones(length(x))'

surface!(ax, X, Y, Z)
surface!(ax, X', Y', Z .+ 0.01)

```

So apparently the unit normal of the surface depends on the `x` and `y` coordinate, so by transposing the `X` and `Y` matrix I either get correct lighting for the outer (inverted protrusion) or the inner surface. So if I plot two identical surface `Z` but with different `X` and `Y`, with the surface defining the ‘correct’ inner lighting slightly above the one for the outer surface, then at least qualitatively I get the correct lighting.

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

Still it would be nice if there is a better way to achieve the same effect.

---

<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:** [December 4, 2023, 8:28am UTC](https://discourse.julialang.org/t/light-shading-for-inverted-surface-plot-in-glmakie/107064/3 "2023-12-04T08:28:27Z")

</div>

You can set `invert_normals = true` to get lighting on the other side or set `backlight = 1` to get lighting on both sides
