關於Unity裏的ColorSpace

Gamma和Linear是目前兩種可選的colorspace,  Linear須要OpenGL3.0支持,目前大部分手機已經支持。默認就是Gamma形式,如下介紹在Unity實現Linear方式:html

1. 手動在Shader中設置,使用官方提供的兩個函數:app

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直接切換spa

在PlayerSetting中選擇Linear. .net

 

因爲Shader始終使用linear形式採樣,在使用Unity Linear時,若是非32位的貼圖時,PS生成的是圖是gamma的,因此導入時若是不選sRGB,會被默認轉成linear的形式,若是一張(127,0,0,0)的圖,會被轉成(187,0,0,0),即進行了一次gamma矯正(^1/gamma)。debug

 

相關參考文章:3d

https://blog.csdn.net/u012871784/article/details/78701996code

https://www.jianshu.com/p/9a91e6ad0d38htm

https://docs.unity3d.com/Manual/LinearRendering-LinearOrGammaWorkflow.htmlblog

相關文章
相關標籤/搜索