Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Lighting tweaks #3383

Merged
merged 2 commits into from
Nov 20, 2023
Merged

Default Lighting tweaks #3383

merged 2 commits into from
Nov 20, 2023

Conversation

ffreyer
Copy link
Collaborator

@ffreyer ffreyer commented Nov 17, 2023

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.

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

Sphere mesh

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

Brain mesh

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

Arrows

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

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.

@MakieBot
Copy link
Collaborator

MakieBot commented Nov 17, 2023

Compile Times benchmark

Note, that these numbers may fluctuate on the CI servers, so take them with a grain of salt. All benchmark results are based on the mean time and negative percent mean faster than the base branch. Note, that GLMakie + WGLMakie run on an emulated GPU, so the runtime benchmark is much slower. Results are from running:

using_time = @ctime using Backend
# Compile time
create_time = @ctime fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @ctime Makie.colorbuffer(display(fig))
# Runtime
create_time = @benchmark fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @benchmark Makie.colorbuffer(fig)
using create display create display
GLMakie 3.54s (3.47, 3.61) 0.06+- 485.95ms (396.21, 578.21) 76.68+- 688.63ms (671.15, 714.43) 18.14+- 7.88ms (7.69, 8.04) 0.11+- 25.92ms (25.55, 26.31) 0.27+-
master 3.54s (3.43, 3.64) 0.08+- 470.13ms (399.02, 557.25) 70.82+- 698.12ms (665.14, 719.82) 19.02+- 7.85ms (7.67, 8.01) 0.12+- 25.97ms (25.59, 26.28) 0.28+-
evaluation 1.00x invariant, -0.01s (-0.08d, 0.88p, 0.07std) 0.97x invariant, 15.82ms (0.21d, 0.70p, 73.75std) 1.01x invariant, -9.5ms (-0.51d, 0.36p, 18.58std) 1.00x invariant, 0.04ms (0.30d, 0.59p, 0.12std) 1.00x invariant, -0.05ms (-0.17d, 0.76p, 0.27std)
CairoMakie 3.06s (3.04, 3.08) 0.01+- 348.17ms (340.39, 355.80) 5.10+- 154.77ms (149.22, 160.93) 3.74+- 7.83ms (7.68, 8.13) 0.15+- 602.35μs (596.57, 614.51) 6.03+-
master 3.05s (3.01, 3.14) 0.04+- 351.13ms (341.11, 372.38) 11.02+- 155.43ms (148.80, 164.55) 4.92+- 7.83ms (7.64, 7.97) 0.12+- 603.28μs (600.17, 607.52) 2.86+-
evaluation 1.00x invariant, 0.01s (0.42d, 0.46p, 0.03std) 1.01x invariant, -2.95ms (-0.34d, 0.54p, 8.06std) 1.00x invariant, -0.66ms (-0.15d, 0.78p, 4.33std) 1.00x invariant, 0.0ms (0.03d, 0.95p, 0.13std) 1.00x invariant, -0.93μs (-0.20d, 0.72p, 4.45std)
WGLMakie 3.82s (3.77, 3.93) 0.05+- 352.19ms (339.36, 364.21) 11.09+- 9.09s (8.89, 9.40) 0.17+- 10.10ms (9.74, 10.42) 0.24+- 72.83ms (68.69, 82.93) 4.91+-
master 3.77s (3.71, 3.82) 0.04+- 349.23ms (342.60, 356.20) 4.75+- 9.13s (9.05, 9.21) 0.07+- 10.11ms (9.83, 10.36) 0.19+- 72.32ms (68.06, 87.30) 7.12+-
evaluation 0.99x invariant, 0.05s (1.02d, 0.08p, 0.05std) 0.99x invariant, 2.96ms (0.35d, 0.53p, 7.92std) 1.00x invariant, -0.04s (-0.31d, 0.58p, 0.12std) 1.00x invariant, -0.0ms (-0.02d, 0.97p, 0.22std) 0.99x invariant, 0.51ms (0.08d, 0.88p, 6.02std)

@jkrumbiegel
Copy link
Member

Looks good, your considerations sound very well thought out

@SimonDanisch SimonDanisch merged commit f7aa963 into master Nov 20, 2023
15 checks passed
@SimonDanisch SimonDanisch deleted the ff/lighting_tweaks branch November 20, 2023 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants