Gamma和Linear是目前两种可选的colorspace, Linear需要OpenGL3.0支持,目前大部分手机已经支持。默认就是Gamma形式,以下介绍在Unity实现Linear方式:
1. 手动在Shader中设置,使用官方提供的两个函数:
inline half3 GammaToLinearSpace (half3 sRGB)
{
// Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
return sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h);
// Precise version, useful for debugging.
//return half3(GammaToLinearSpaceExact(sRGB.r), GammaToLinearSpaceExact(sRGB.g), GammaToLinearSpaceExact(sRGB.b));
}
比如想让场景由Gamma转成linear的形式,参考简书中的过程,我理解为过程可简化为这样:
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb = GammaToLinearSpace(col.rgb); // 转换到Linear Space
//TODO:Some shader codes here.
col.rgb = LinearToGammaSpace(col.rgb* unity_ColorSpaceDouble);
return col;
}
2. 使用Unity的ColorSpace直接切换
在PlayerSetting中选择Linear.
由于Shader始终使用linear形式采样,在使用Unity Linear时,如果非32位的贴图时,PS生成的是图是gamma的,所以导入时如果不选sRGB,会被默认转成linear的形式,如果一张(127,0,0,0)的图,会被转成(187,0,0,0),即进行了一次gamma矫正(^1/gamma)。
相关参考文章:
https://blog.csdn.net/u012871784/article/details/78701996
https://www.jianshu.com/p/9a91e6ad0d38
https://docs.unity3d.com/Manual/LinearRendering-LinearOrGammaWorkflow.html