Jump to content
  • Advertisement
Sign in to follow this  

Lens flare: sun position?

This topic is 2926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I want to use an oldschool lens-flar like in this article:

First of all I want this to happen when looking into my games sun. However, I'm rendering the sun just using atmospheric scattering, so all I've got is the suns direction. Does anyone know how to calculate the suns screen position based on knowing sun direction? It's rendered in this shader:


o.vWorldPos = float4(mul(i.vPos, World), 1.0f);
float4 vWVPPos = mul(i.vPos, WorldViewProj);
float4 vPosLast = mul(i.vPos, LastWorldViewProj);
o.vPos = vWVPPos;
o.vPos.w = o.vPos.z * (1.00001f);
o.vTex0 = i.vTex0;

vWVPPos.xy /= vWVPPos.z;
vPosLast.xy /= vPosLast.z;

o.vVelocity = vWVPPos.xy - vPosLast.xy;
o.vVelocity /= 2.0f * 6.0f;

float3 vertex = mul(i.vPos, WorldView).xyz;

float3 viewDirection = normalize( vertex );
float distance = length( vertex )*100.0f;

// Angle between sun direction and view direction
float3 sunDirection = normalize(mul(float4(SunDir, 1.0),WorldView).xyz );

float theta = dot( sunDirection, viewDirection );

// Phase1 and Phase2

float phase1 = 1.0 + theta * theta;
float phase2 = pow( rsqrt( HGg.y - HGg.z * theta ), 3 ) * HGg.x;

// Extinction term

float3 extinction = exp( -BetaRPlusBetaM * distance );
float3 totalExtinction = extinction * Multipliers.yzw;

// Inscattering term

float3 betaRay = BetaDashR * phase1;
float3 betaMie = BetaDashM * phase2;

float3 inscatter = (betaRay + betaMie) * OneOverBetaRPlusBetaM * (1.0 - extinction);

// Apply inscattering contribution factors

inscatter *= Multipliers.x;

// Scale with sun color & intensity

inscatter *= SunColorIntensity.xyz * SunColorIntensity.w;

o.vDiff.rgb = inscatter;
o.vDiff.rgb = (1.5f - sqrt(i.vNrm.y))*inscatter;
o.vDiff.a = 1.0f;
return o;

My first thought was to just use the suns direction (0, 1, 1), take a point at a certain lenght like (0, 10, 10) and multiply it with the WorldViewProjection-matrix. then I should get the sun position in screen space, at least in theory. However I really need the point of the sun on my sky-sphere, which is modulated in the shader to always stay behind everything.. Any ideas? Thanks in advance!

Share this post

Link to post
Share on other sites
To avoid advanced ray tracing when the sun is halfway hidden behind something and seen thru reflection or refraction, just use a post effect.
Sample the image in multiple scales from the center and fade it away on the edges to use it for anything that is very bright.

This example in my SDK have the post effect for sun flare combined with the bloom effect to reuse the downsampling.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!