这次来整理下ShadeSH9,不讨论其中的数学。
看了看源码,突然一阵阵后悔。。。。好像这个太复杂了。。。要不。。。我就翻篇吧。。。
想
想
想
想
想
想
想
想
想
想
想
想
想了想,算了,自己整理一下自己对SH的理解算了
先在shader里写一下ShadeSH9输出的结果(worldN我这里用的测试物体没有进行过缩放,所以就那么写了吧)
这里可以看出仅仅ShadeSH9计算的不是完全黑色的。。(这里的4个是点光源,not important的,为啥没有灯光的效果?这是因为这个四个point liaht 目前参与了逐顶点光照计算,shader中虽然没有写,但是unity好像还是把他们留给了其他物体的逐顶点光照计算了),为啥不是黑色,我试着去调了一下
发现了ShadeSH9返回的是AmbientColor。所以使用ShadeSH9的时候,一定不要把Ambient的计算执行多次。
最后上一下SH的结果图,之后就开始源码之路(红框里的全部进入了SH的计算,也就是说,SH的计算似乎没有light的数量上限)
这里先上一下源码。看一下
// 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;
}
然后,为了方便查看,请使用这个链接下的shader,在unity里查看一下效果。
上几个链接,关于球谐光照的,这个,这个,这个,这个,这个,和这个,这个(油管),这个(GDC)。(我就是简单的看了看,数学老师我对不起你)