Is this a gamma color space problem?

I used PBRCustomMaterial and customized surfaceAlbedo in the Fragment_Custom_Albedo function, but the sampled color ended up being the equivalent of a lighter map color

  customMaterial.Fragment_Custom_Albedo(`
        vec3 dayColor = texture2D(dayTex, vUV).rgb;
        vec3 nightColor = texture2D(nightTex, vUV).rgb;
        vec3 mixColor = pow(mix(dayColor,nightColor,dissolve).rgb,vec3(2.2));
        // vec3 mixColor = mix(dayColor,nightColor,dissolve);
        surfaceAlbedo.rgb = mixColor;
    `)

chatGPT gave me the answer and let me do this

 vec3 mixColor = pow(mix(dayColor,nightColor,dissolve).rgb,vec3(2.2));

After this,it works very well, the color and texture after sampling are almost the same

Is this the right solution?

Yes, pow(x,2.2) will convert a gamma space color to linear space, which is the space expected by the PBR code. However, you can avoid doing a costly exponential operation for each pixel by modifying the day/night textures to store linear data instead of gamma space data.

Another possibility is to keep the day/night textures in gamma space but create the texture in Babylon with a sRGB format (the Texture constructor has a useSRGBBuffer option parameter). In this case, the GPU will take care of converting the gamma space value to linear space when you sample the texture.

thank u very much , I will try latter!

1 Like