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

Add missing sRGB conversion when baking mmd_color into sRGB mmd_base_tex #485

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

Mysteryem
Copy link

  1. Add missing sRGB conversion when baking mmd_color into sRGB mmd_base_tex
    • Shader node colors are in scene linear colorspace. Image.pixels is always accessed in scene linear colorspace, but if the image is set to use sRGB colorspace, when it is used in shader nodes, Blender automatically converts it to sRGB. Blender's automatic conversion to sRGB has to be taken into account when multiplying Image.pixels by a linear color in order to match the same result of multiplying the image and color using shader nodes.
    • Technically, there are other non-linear colorspaces that should be accounted for, but they're so rarely used and there's no clear conversions between them and linear available (it looks like Blender loads matrices and tables for conversion from OpenColorIO C code internally, which are inaccessible to the Python API). The code ignores these other non-linear colorspaces for now.
  2. Clean up messy control flow with lots of break and continue statements
  3. Always remove all extra material output nodes instead of only sometimes depending on node order
    • Previously, as soon as one of the Cats nodes was found, the loop would be broken out of, meaning extra material output nodes would only be removed if they were found before one of the Cats nodes
  4. Set PrincipledBSDF Base Color default_value instead of creating and saving a blank generated image of the required color
    • Was there a reason to be creating a separate image that I don't know about? It seems like a very odd thing to do unless there's a specific reason for it.
  5. Add common functions for faster reading and writing of image pixels
    • I do know of faster methods for Blender versions without the fast access methods for bpy_prop_array types, but they require using ctypes to swap around data pointers and other field values which requires considerable additional setup. Since Blender 2.83 is the oldest LTS release, which has the fast access methods for bpy_prop_array types, I think it's reasonable to keep the slower, but significantly simpler methods on Blender versions older than 2.83.

As a small note to avoid confusion, the MMD Ambient Color was previously being referred to as AO, when it is actually Ambient Light.

Clean up messy control flow with lots of break and continue statements
Always remove all extra material output nodes instead of only sometimes depending on node order
Set PrincipledBSDF Base Color default_value instead of creating and saving a blank generated image of the required color
Add common functions for faster reading and writing of image pixels
@Mysteryem Mysteryem marked this pull request as draft July 4, 2022 17:11
@Mysteryem
Copy link
Author

I can possibly see why textures were being generated for the Principled BSDF base color, because at least by Blender's default settings, when Unity imports the colors, it uses the RGB values directly, but typically, they need to be gamma adjusted. For example, here is the color (0.1, 0.1, 0.1) in Blender with Blender's default Color Management of having the Display Device set to sRGB and in Unity with Unity set up to use Linear colorspace as per VRChat's requirements.
With the Display Device set to None in Blender, the colors in Unity and Blender would match up.

It seems that in Unity, changing the Color Space of the project will only affect shaders that actually account for the Color Space, e.g. Standard Shader, but using a shader such as Unlit/Color will look too dark in Unity compared to Blender, regardless of the Color Space used in Unity.

Since this affects all FBX exports that use PrincipedBSDF that are intended for use with VRChat, my current idea would be to add some extra code to Cats' FBX export that temporarily adjusts all Color inputs of PrincipledBSDF shaders that would be used by Blender's FBX export before running the export and then restores the Color inputs afterwards. Gamma adjusting the colors during Fix Model wouldn't work, because then they would display incorrectly in Blender and changing the Display Device to None won't work because then all the other sources of color, such as textures would look the wrong color in Blender.

We can probably assume that Unity will be in Linear Color Space since VRChat requires it and then we can check the Display Device setting of the Scene in Blender to determine if the conversion needs to take place. Additionally, an option for this conversion could be added to Cats' FBX export window, defaulted to being turned on so that if someone were wanting to export for a Gamma Unity project or for something other than Unity, they have the option to turn it off.
image

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.

None yet

1 participant