//**************************
float2 sunpos = 0;
float2 _v1 = 0;
float maxi = 0;
int count = 0;
int ratio = 1.8;
float deltax = 0.04 / ratio;
int samplex = 25 * ratio;
for(int i = 1; i <= 25; i++)
{
for(int j = 1; j <= samplex; j++)
{
r2 = tex2D(s2, _v1.xy);
r2.x = saturate(dot(r2.xyz, 0.333) - 20.0);
if((r2.x >= maxi) && r2.x)
{
maxi = r2.x;
sunpos.xy += _v1.xy;
count++;
}
_v1.x += deltax ;
}
_v1.x = 0.0;
_v1.y += 0.04;
}
if(maxi) sunpos.xy /= count;
float4 color;
//r0=tex2D(s2, _v0.xy);//hdr color
color=r0 + r11;
//color.xyz=0.0;
int NUM_SAMPLES = 128;
float Decay = 0.96;
float Exposure = 1.0;
float Weight = 0.25;
float Density = 1.0;
float2 ScreenLightPos;
ScreenLightPos.xy = sunpos.xy;
float2 texCoord = _v0;
float2 deltaTexCoord = (texCoord.xy - ScreenLightPos.xy);
deltaTexCoord *= 1.0 / NUM_SAMPLES * Density;
float illuminationDecay = 1.0;
if(maxi)
{
for(int x = 0; x < NUM_SAMPLES; x++)
{
texCoord -= deltaTexCoord;
float4 sample = tex2D(s0, texCoord.xy);
if(sample.w) sample.xyz = 0.0;
else
{
sample = tex2D(s2, texCoord.xy);
sample.w = saturate(dot(sample.xyz, 0.3333) - 20);
if(sample.w == 0) sample.xyz = 0.0;
sample.r *= 0.1;
sample.g *= 0.095;
sample.b *= 0.085;
}
sample *= illuminationDecay * Weight;
color += sample;
illuminationDecay *= Decay;
}
}
color *= Exposure;
//**************************