Skip to content

Commit

Permalink
Default Lighting tweaks (#3383)
Browse files Browse the repository at this point in the history
# Description

Good defaults are quite subjective but since there has been a discussion
on slack I thought we could consider tweaking things a bit more before
releasing 0.20.

Some constraints:
1. I want to avoid oversaturation, i.e. the bright blob you may see
prior to 0.20. This requires the maximum intensity to b capped at (or
close to) 1.
2. I want to keep `diffuse = 1.0` because it's a nice default value
which means that 100% of incoming light is considered in diffuse
lighting/reflections
3. I want to keep `light_color * diffuse` relatively large to keep a
good contrast between light and dark regions. That contrast can be quite
helpful to see creases and understand curvature.

For now I halfed `specular` to `0.2` and increase `ambient` to `0.45`.

#### Intensity

If I simplified things correctly this should give you the intensity as
function of angle between the camera/view direction (vector between
camera and pixel) and the normal at that pixel.

```julia
using LinearAlgebra, GLMakie
function max_intensity(phi; 
        ambient = Makie.MAKIE_DEFAULT_THEME[:ambient][].r,
        light_color = Makie.MAKIE_DEFAULT_THEME[:light_color][].r,
        lightdir = Makie.MAKIE_DEFAULT_THEME[:light_direction][],
        diffuse = 1.0, specular = 0.4
    )
    camdir = Vec3f(0, 0, -1)
    phi_spec = acos(dot(camdir, normalize(lightdir + camdir)))
    return ambient + light_color * (
        max(0.0, cos(phi)) * diffuse + max(0.0, cos(phi - phi_spec)^32) * specular
    )
end

f = Figure()
ax = Axis(f[1, 1], ylabel = "Intensity", xlabel = "angle(normal, camdir)")
lines!(ax, -pi/2 .. pi/2, 
    x -> max_intensity(x, lightdir = Vec3f(0, 0, -1), ambient = 0.55, light_color = 1.0, diffuse = 0.4, specular = 0.2), 
    color = RGBf(0.9, 0.6, 0.6), label = "master")
lines!(ax, -pi/2 .. pi/2, max_intensity, color = :gray, label = "beta-20")
lines!(ax, -pi/2 .. pi/2, x -> max_intensity(x, ambient = 0.45, specular = 0.2), color = :black, label = "specular = 0.2, ambient = 0.45")
axislegend(ax, position = :cb)
hlines!(ax, 1.0, color = :red, linestyle = :dash)
f
```

![Screenshot from 2023-11-17
16-32-53](https://github.com/MakieOrg/Makie.jl/assets/10947937/88707b75-c02a-4d6c-b9cd-7fc33f6e7efd)

#### Sphere mesh

```julia
fig = Figure(size = (800, 450))
Label(fig[1, 1], "specular = 0.4, ambient = 0.35", tellwidth = false)
ax, p = mesh(fig[2, 1], Sphere(Point3f(0), 1f0), color = :white)

Label(fig[1, 2], "specular = 0.2, ambient = 0.45", tellwidth = false)
ax, p = mesh(fig[2, 2], Sphere(Point3f(0), 1f0), color = :white, specular = 0.2)
ax.scene.lights[1].color[] = RGBf(0.45, 0.45, 0.45)

fig
```
![Screenshot from 2023-11-17
16-32-20](https://github.com/MakieOrg/Makie.jl/assets/10947937/8fded488-913f-4f3a-938f-a4d77fd8cea0)

#### Brain mesh

```julia
brain = Makie.FileIO.load(assetpath("brain.stl"))
color = [abs(tri[1][2]) for tri in brain for i in 1:3]

fig = Figure(size = (800, 450))
Label(fig[1, 1], "specular = 0.4, ambient = 0.35", tellwidth = false)
ax, p = mesh(fig[2, 1], brain, color = color, specular = 0.4)
ax.scene.lights[1].color[] = RGBf(0.35, 0.35, 0.35)

Label(fig[1, 2], "specular = 0.2, ambient = 0.45", tellwidth = false)
ax, p = mesh(fig[2, 2], brain, color = color, specular = 0.2)
ax.scene.lights[1].color[] = RGBf(0.45, 0.45, 0.45)

fig
```

![Screenshot from 2023-11-17
17-00-12](https://github.com/MakieOrg/Makie.jl/assets/10947937/182a52f9-fec6-4530-82fe-1dedc51669b2)

#### Arrows

```julia
pts = fill([0,0,0], 4)
dirs = [[1,0,0], [0.5,0.5,0], [0,0.5,0.5], [-0.5, 0, 0.5]]
color = [:white, :orange, :yellow, :purple]

fig = Figure(size = (800, 450))
Label(fig[1, 1], "specular = 0.4, ambient = 0.35", tellwidth = false)
ax, p = arrows(fig[2, 1], Point3f.(pts), Vec3f.(dirs); color)
update_cam!(ax.scene, Vec3f(2.5, 0.2, 1.3), Vec3f(0.4, 0.3, 0.3))

Label(fig[1, 2], "specular = 0.2, ambient = 0.45", tellwidth = false)
ax, p = arrows(fig[2, 2], Point3f.(pts), Vec3f.(dirs); color, specular = 0.2)
ax.scene.lights[1].color[] = RGBf(0.45, 0.45, 0.45)
update_cam!(ax.scene, Vec3f(2.5, 0.2, 1.3), Vec3f(0.4, 0.3, 0.3))
fig
```

![Screenshot from 2023-11-17
16-42-18](https://github.com/MakieOrg/Makie.jl/assets/10947937/cd962903-ab07-4ffe-b291-7df2ab0d80b4)

## Type of change

Delete options that do not apply:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)

## Checklist

- [ ] Added an entry in NEWS.md (for new features and breaking changes)
- [ ] Added or changed relevant sections in the documentation
- [ ] Added unit tests for new algorithms, conversion methods, etc.
- [ ] Added reference image tests for new plotting functions, recipes,
visual options, etc.
  • Loading branch information
SimonDanisch authored Nov 20, 2023
2 parents 0e6234f + c96d58e commit f7aa963
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MakieCore/src/basic_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ end
function shading_attributes!(attr)
attr[:shading] = automatic
attr[:diffuse] = 1.0
attr[:specular] = 0.4
attr[:specular] = 0.2
attr[:shininess] = 32.0f0
attr[:backlight] = 0f0
attr[:ssao] = false
Expand Down
2 changes: 1 addition & 1 deletion src/theming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const MAKIE_DEFAULT_THEME = Attributes(
light_direction = Vec3f(-0.45679495, -0.6293204, -0.6287243),
camera_relative_light = true, # Only applies to default DirectionalLight
light_color = RGBf(0.5, 0.5, 0.5),
ambient = RGBf(0.35, 0.35, 0.35),
ambient = RGBf(0.45, 0.45, 0.45),

# Note: this can be set too
# lights = AbstractLight[
Expand Down

0 comments on commit f7aa963

Please sign in to comment.