KirilStrezikozin / BakeMaster-Blender-Addon

Welcome to BakeMaster, a powerful and feature-packed baking solution created for Blender - an open-source 3D Computer graphics software.
Other
34 stars 6 forks source link

BUG: wrong colors for sRGB bakes #74

Closed KirilStrezikozin closed 5 months ago

KirilStrezikozin commented 6 months ago

This bug report is:

Describe the bug I rewrote map saving back in BakeMaster 2.5.0 (I guess, needs testing). This rewrite was to properly save maps with configured bit depths and file format, but looks like it messes up colors on sRGB maps, especially making it darker with each next highpoly baked onto the map. This bug was noticed by @KirilStrezikozin for multiple highpolies, but together with soeinfeuerball we noticed that it also happens for simple sRGB maps without any highpolies. See the screenshots below for colors differences.

To Reproduce Steps to reproduce the behavior:

  1. Bake any map (AlbedoM for example) in sRGB color space (default PNG file format for example).
  2. Compare the look of a model between baked and original material.
  3. See that colors went off.

Expected behavior No color messing as this impacts the output image textures. That might lead to unexpected results when users use baked maps in another software or rendering and Blender when colors do not match the ones they initially set up.

Screenshots

  1. soeinfeuerball's AlbedoM bake in sRGB (PNG). Baked is on the left: image

Desktop (please complete the following information):

KirilStrezikozin commented 5 months ago

_mikefr on Discord faces this too.

KirilStrezikozin commented 5 months ago

This bug is confirmed and a fix can be brought at any moment, no stalls.

KirilStrezikozin commented 5 months ago

The issue with wrong sRGB colors on baked images may not be caused by changes in image saving that BakeMaster 2.5.0 brought (which I was afraid of), but the way images with sRGB colorspace get saved by calling image.save_render(filepath). An example with dummy cubes below:

  1. Baking 3 cubes onto one map manually: image The two reddish cubes were expected to be red (RGB(1,0,0)).

  2. Same baking setup, but not saving on the last baking iteration: image The last baked cube (unwrap layout in the top part) has correct color.

From that, the conclusion is that image.save_render(filepath) is the only cause for wrong colors on image save.

  1. Possible but not confirmed solution: same baking setup, but View Transform set to Raw and Contrast to None: image Both baked red cubes are of the same color.

A possible proof 3's solution above is that since an image is in sRGB colorspace, using image.save_render(filepath) converts image's data to display space, which is what View Transofrm stands for.

Contrast property makes a difference, below is Raw + Low Contrast bake: image

And a slight miss in color when baking Diffuse + Color is resolved by baking Emit. This was proven in #73. On the baked image above, the sampled red color is slightly below 1.0 (around 0.984). On the image below, it is 1.0. image

KirilStrezikozin commented 5 months ago

TODO:

KirilStrezikozin commented 5 months ago

check how ACES behaves here. (update) exactly the same.

KirilStrezikozin commented 5 months ago

That was interesting actually.

KirilStrezikozin commented 5 months ago

No need for migrations to public.

KirilStrezikozin commented 5 months ago

A fix was just to reset view settings practically on each image saving iteration. Interesting that I had commented that section out in 2.6.0 which fixed EXR bakes. Probably due to using sRGB and Standard view transform I didn't notice a significant color difference or I just didn't bake sRGB maps with several objects on one. This section had been behaving wrong prior to 2.6.0 (2.5.0 brought it) so I don't blame myself for one more 'not an update but a mess' badge for 2.6.0.

bake_scene_settings(...) function has got some unused arguments, but I was kind of lucky I left them hanging there because it helps understand the context which bake_scene_settings(...) is called in. This saved me time and I actually fixed this issue pretty quickly. It involved resetting view settings when color_manage is False (which is always like this unless apply_scene and is_last are both True:

# In operator_bake, bake_scene_settings(...)
...

if not color_manage:
    context.scene.view_settings.view_transform = 'Raw'
    context.scene.view_settings.look = 'None'
    context.scene.view_settings.exposure = 0.0
    context.scene.view_settings.gamma = 1.0
    context.scene.view_settings.use_curve_mapping = False

    # for video editing
    # context.scene.sequencer_colorspace_settings.name = ...

...