采用GPU GEMS3 上面的方法,在 Post-Process计算了容积光效果,不过这极为依赖于采样频率,我设置的是75次采样,帧率从60多下降了20多帧,不过仍然有明显的痕迹,目前没有找到什么好的优化方式,些许可以根据光源到屏幕像素点的具体动态调整采样频率,不过效率也应该提高不了太多。我再到网上找找。
改动了一下,缩小到原场景图的 1/4 ,100次采样 PS:其实如果是Shader Model 4.0的话可以对采样次数做动态计算,就是根据太阳的位置和像素的位置计算出经过的像素的个数,然后动态进行for()循环,可惜,Shader Model 3.0不支持,因此我也只能人为地设置固定的采样计算,不过还好,效率提高了不少
sampler sScene : register(s0);
float2 vec2SunScreenPos;
static float fExposure = 1.0f ;
static int iSampleNums = 100.0f ;
static float fDecay = 1.0f ;
static float fWeight = 1.0f ;
float4 ps_main(float2 texCoord : TEXCOORD) : COLOR
{
float2 vec2UnitVec = texCoord - vec2SunScreenPos;
vec2UnitVec /= iSampleNums;
float illuminationDecay = 1.0f ;
float4 sumColor = 0.0f ;
for ( int i = 0 ; i < iSampleNums; i ++ )
{
float4 color = tex2D(sScene, texCoord - vec2UnitVec * i);
color *= color.a;
sumColor += illuminationDecay * fWeight * color;
illuminationDecay *= fDecay;
}
sumColor /= iSampleNums;
return 1 - exp( - sumColor * fExposure );
}
float2 vec2SunScreenPos;
static float fExposure = 1.0f ;
static int iSampleNums = 100.0f ;
static float fDecay = 1.0f ;
static float fWeight = 1.0f ;
float4 ps_main(float2 texCoord : TEXCOORD) : COLOR
{
float2 vec2UnitVec = texCoord - vec2SunScreenPos;
vec2UnitVec /= iSampleNums;
float illuminationDecay = 1.0f ;
float4 sumColor = 0.0f ;
for ( int i = 0 ; i < iSampleNums; i ++ )
{
float4 color = tex2D(sScene, texCoord - vec2UnitVec * i);
color *= color.a;
sumColor += illuminationDecay * fWeight * color;
illuminationDecay *= fDecay;
}
sumColor /= iSampleNums;
return 1 - exp( - sumColor * fExposure );
}
Pixel Shader 代码