Hi all!
I try to use the Sun shafts effects via post process in my 3DEngine, but i have some artefacts on final image(Please see attached images).
The effect contains the following passes:
1) Depth scene pass;
2) "Shafts pass" Using DepthPass Texture + RGBA BackBuffer texture.
3) Shafts pass texture + RGBA BackBuffer texture.
Shafts shader for 2 pass:
//
uniform sampler2D FullSampler; // RGBA Back Buffer
uniform sampler2D DepthSampler;
varying vec2 tex;
#ifndef saturate
float saturate(float val)
{
return clamp(val, 0.0, 1.0);
}
#endif
void main(void)
{
vec2 uv = tex;
float sceneDepth = texture2D(DepthSampler, uv.xy).r;
vec4 scene = texture2D(FullSampler, tex);
float fShaftsMask = (1.0 - sceneDepth);
gl_FragColor = vec4( scene.xyz * saturate(sceneDepth), fShaftsMask );
}
final shader:
//
uniform sampler2D FullSampler; // RGBA Back Buffer
uniform sampler2D BlurSampler; // shafts sampler
varying vec4 Sun_pos;
const vec4 ShaftParams = vec4(0.1,2.0,0.1,2.0);
varying vec2 Tex_UV;
#ifndef saturate
float saturate(float val)
{
return clamp(val, 0.0, 1.0);
}
#endif
vec4 blendSoftLight(vec4 a, vec4 b)
{
vec4 c = 2.0 * a * b + a * a * (1.0 - 2.0 * b);
vec4 d = sqrt(a) * (2.0 * b - 1.0) + 2.0 * a * (1.0 - b);
// TODO: To look in Crysis what it the shit???
//return ( b < 0.5 )? c : d;
return any(lessThan(b, vec4(0.5,0.5,0.5,0.5)))? c : d;
}
void main(void)
{
vec4 sun_pos = Sun_pos;
vec2 sunPosProj = sun_pos.xy;
//float sign = sun_pos.w;
float sign = 1.0;
vec2 sunVec = sunPosProj.xy - (Tex_UV.xy - vec2(0.5, 0.5));
float sunDist = saturate(sign) * saturate( 1.0 - saturate(length(sunVec) * ShaftParams.y ));
sunVec *= ShaftParams.x * sign;
vec4 accum;
vec2 tc = Tex_UV.xy;
tc += sunVec;
accum = texture2D(BlurSampler, tc);
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.875;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.75;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.625;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.5;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.375;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.25;
tc += sunVec;
accum += texture2D(BlurSampler, tc) * 0.125;
accum *= 0.25 * vec4(sunDist, sunDist, sunDist, 1.0);
accum.w += 1.0 - saturate(saturate(sign * 0.1 + 0.9));
vec4 cScreen = texture2D(FullSampler, Tex_UV.xy);
vec4 cSunShafts = accum;
float fShaftsMask = saturate(1.00001 - cSunShafts.w) * ShaftParams.z * 2.0;
float fBlend = cSunShafts.w;
vec4 sunColor = vec4(0.9, 0.8, 0.6, 1.0);
accum = cScreen + cSunShafts.xyzz * ShaftParams.w * sunColor * (1.0 - cScreen);
accum = blendSoftLight(accum, sunColor * fShaftsMask * 0.5 + 0.5);
gl_FragColor = accum;
}
Demo project:
Shaders for postprocess Shaders/SunShaft/
What i do wrong ?
Thanks!