Search Unity

[SOLVED] Wrong color space when loading textures with ImageConversion.LoadImage?

Discussion in 'General Graphics' started by Hagn, Mar 7, 2018.

  1. Hagn

    Hagn

    Joined:
    Apr 1, 2013
    Posts:
    10
    Hello,

    I'm having a problem with colors when loading a texture at runtime with ImageConversion.LoadImage.
    I'm loading a normal JPEG file into a RGBA32 texture. It is then rendered by a custom shader which uses the "tex2d" function for texture sampling. But the result is way too bright.
    The project uses the gamma color space. Changing the project to the linear color space renders the colors as they should be.

    Now I'm not sure if this is an intended behavior or a bug.

    Tested with Unity 2017.2.2f1 on Windows, Android and iOS.


    Greetings

    Hagn


    EDIT:

    I finally found the cause and a solution.
    I forgot to mention that the custom shader is a surface shader with a custom lighting function.
    Even though the lighting function doesn't do anything than returning the input color as the output color, the surface shader still adds the ambient color. This caused an increase of the image brightness. Changing the color space to linear seems to reduce the changes so much that they weren't recognizable anymore.
    I found three different solutions to fix the problem:

    1. Set the scene ambient color to black.
    2. Use the "noambient" surface shader flag to prevent the addition of ambient colors.
    3. Use the "finalcolor" modifier function to pass through the original texture color.
     
    Last edited: Mar 13, 2018