Hello everybody
I’ve read extensively through various sources on the Linear Color Space feature in Unity, e.g.
- Understanding sRGB and gamma corrected values in the render pipeline
and subsequently - Confusion about Gamma vs. Linear
I’m getting a feeling for how it works, but to verify that I’ve created a simple test shader that outputs the UV coordinates a RG (the “mango quad”):
Shader "Unlit/NewUnlitShader"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(i.uv, 0, 1);
return col;
}
ENDCG
}
}
Giving me the following result in Gamma and Linear Color Space:
Mango Quad rendered in Gamma Color Space (left) and Linear Color Space (right)
The Linear Color Space (right) is “incorrect” in my opinion, e.g. compared to the first image or Photoshop’s Color Picker. The black is way too off in the bottom left corner.
Through reading the various sources, I’ve tried to manually correct for Gamma, so adding this to the end of the Fragment shader:
#if UNITY_COLORSPACE_GAMMA
return col;
# else
return fixed4(GammaToLinearSpace(col.xyz), 1);
# endif
Which fixes the gradient in Linear Color Space so it looks like the image on the left.
Questions:
- Why do I need to “manually” correct for Gamma? According to everything I’ve read (see the linked posts), this is done “automatically” by Unity…??
- When is the correction applied?
- How do I know which shaders need this correction line at the end and which don’t?
- Does the Standard shader do this correction?