一個美術(shù)需求
今天美術(shù)大哥要我把角色的間接光照固定痛单,不受場景影響丽柿,即把下圖的 環(huán)境光 和 環(huán)境反射 從 Lighting Settings 面板搬到 角色材質(zhì)球 上:
這種環(huán)境光的反差,也是突出角色的方法之一毁渗。
固定環(huán)境光
美術(shù)的要求是,無論場景如何布置,程序只認下圖這個預(yù)設(shè)的環(huán)境光:
這里的做法很簡單驳遵,打開美術(shù)預(yù)烘培好的場景,用 FrameDebugger 截取 球諧光照 的參數(shù)值山涡,然后在任何情況下都用這些值計算環(huán)境光即可:
把上圖中的 unity_SHXXX 帶入下面的函數(shù)就大功告成了:
// normal should be normalized, w=1.0
half3 SHEvalLinearL0L1 (half4 normal)
{
half3 x;
// Linear (L1) + constant (L0) polynomial terms
x.r = dot(unity_SHAr,normal);
x.g = dot(unity_SHAg,normal);
x.b = dot(unity_SHAb,normal);
return x;
}
// normal should be normalized, w=1.0
half3 SHEvalLinearL2 (half4 normal)
{
half3 x1, x2;
// 4 of the quadratic (L2) polynomials
half4 vB = normal.xyzz * normal.yzzx;
x1.r = dot(unity_SHBr,vB);
x1.g = dot(unity_SHBg,vB);
x1.b = dot(unity_SHBb,vB);
// Final (5th) quadratic (L2) polynomial
half vC = normal.x*normal.x - normal.y*normal.y;
x2 = unity_SHC.rgb * vC;
return x1 + x2;
}
// normal should be normalized, w=1.0
// output in active color space
half3 ShadeSH9 (half4 normal)
{
// Linear + constant polynomial terms
half3 res = SHEvalLinearL0L1 (normal);
// Quadratic polynomials
res += SHEvalLinearL2 (normal);
# ifdef UNITY_COLORSPACE_GAMMA
res = LinearToGammaSpace (res);
# endif
return res;
}
當然堤结,這樣做之后,場景的 光照探頭 對角色就失效了鸭丛,不過美術(shù)要的就是這個效果......
固定環(huán)境反射
固定環(huán)境光竞穷,只需要在shader里寫死參數(shù),美術(shù)不需要做任何材質(zhì)設(shè)置鳞溉。
下面開始固定環(huán)境反射瘾带。
首先,把用于環(huán)境反射的 Cubemap 從 Lighting Settings 面板搬到 角色材質(zhì)球 上熟菲,如下圖:
然后看政,間接高光的計算直接認角色材質(zhì)指定的環(huán)境,即上圖中的 Env Cubemap抄罕,主要代碼如下:
half envRoughness = perceptualRoughness * (1.7 - 0.7 * perceptualRoughness);
half envMip = envRoughness * UNITY_SPECCUBE_LOD_STEPS;
half4 envColor = texCUBElod(_EnvCubemap, half4(R, envMip)) * _EnvReflectStrength;
envColor.rgb = DecodeHDR(envColor, _EnvCubemap_HDR).rgb;
用上面代碼的計算結(jié)果 envColor 替代Unity全局光照的 gi.indirect.specular 就大功告成了允蚣。
最后,需要注意的是呆贿,環(huán)境圖是需要勾選 Generate Mip Maps 的嚷兔,否則 粗糙度 的表現(xiàn)就錯誤了。
個人主頁
本文的個人主頁鏈接:https://baddogzz.github.io/2020/03/25/Fix-Indirect-Lighting/做入。
好了冒晰,拜拜!