Jump to content
  • Advertisement
Sign in to follow this  

aerial perspective haze shader

This topic is 3768 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 been looking around for some source code and theories about this mather for a time now. (source code to experiment with and learn - i find it easier that way) The most fit implementation for my work seem to be Preetham's "A Practical Analytic Model for Daylight" and out of some researching regarding his theroies i found a shader source at this homepage.(very good job by s.Wickens by the way). I tried to use the effect file in fxComposer2 - first timer using that program though. The resason why it aint working is with my mathemathical lack of wisdom i think, so the thing i need explained is the values needed to start exploring the effects of the light and aerial perspective. so lets begin, from below code the shader needs some variables. and if i have understand everything so far - some are constants and some are varying values. the thing im asking for is a brief very newbie explonation of below variables - which values are constants and which isn't. the only thing i want out of this exploring and playing with the shader is to recreate the haze vision you automaticly get from this theory. (so i can have a static sun, and everything - but the haze must appear on the terrain). I dont need to understand the mathemathical formulas either, i simply want to know how to set up this in fXcomposer (is it possible?, the only thing is to set symanthics right - where?) and from there i can develop something out of my needs. tnx for some basic information and some pointing towards right directions, and maybe even a little clue on what values i need to define in fxComposer to get a vision at all) (plane only black now ofcourse with no values set except the matrices). - well look at the bright side i can rotate the plane :). The fx file i try to use on that plane in fxComposer2. all code here below is not mine!, it is pasted from site mentioned above i reclaim none of this code and use it in learning purpouse only.
float4x4 world, worldViewProj;

float3 zenith; //x, y, Y format
float perez_x[5];
float perez_y[5];
float perez_Y[5];

float3 eye, dir;
float3 sunDir;
float thetaS; //sun angle from zenith

float Perez(float perez[5], float gamma, float theta, float luminance) {

    float cg = cos(gamma), cs = cos(thetaS);

	float num = (1 + perez[0]*exp(perez[1]/cos(theta))) * 
		(1 + perez[2]*exp(perez[3]*gamma) + perez[4]*cg*cg); //eq 3 F(theta, gamma)

	float den = (1 + perez[0]*exp(perez[1])) * 
		(1 + perez[2]*exp(perez[3]*thetaS) + perez[4]*cs*cs); //eq 3 F(0, thetaSun)

	return luminance*num/den; //eq 4


struct vsIn {
    float3 pos: POSITION;
    float2 uv: TEXCOORD0;

struct vsOut {
    float4 final: POSITION;
    float3 pos: TEXCOORD0;
    float2 uv: TEXCOORD1;

void VS(in vsIn input, out vsOut output) {
    output.pos = mul(float4(input.pos, 1.0), world).xyz;
    output.final = mul(float4(input.pos, 1.0), worldViewProj);
    output.uv = input.uv;

struct psIn {
    float3 pos: TEXCOORD0;
    float2 uv: TEXCOORD1;

float4 PS(in psIn input) : COLOR {

    float3 v = input.pos - eye;
    float ex = exp(-length(v)*0.0001);

    v = normalize(input.pos);

    //angle to the sun from this point
    float gamma = acos(dot(v, sunDir));

    //angle from y axis to this point
    float theta = acos(v.y);

    float x = Perez(perez_x, gamma, theta, zenith.x);
    float y = Perez(perez_y, gamma, theta, zenith.y);
    float Y = Perez(perez_Y, gamma, theta, zenith.z);

    //convert xyY -> XYZ
    float X = x*Y/y;
    float Z = (1.0f - x - y)*Y/y;

    //convert XYZ -> RGB
    float3 inscatter;
    inscatter.r = 1.0 - exp(-0.00006*(3.240479*X - 1.53715*Y - 0.49853*Z));
    inscatter.g = 1.0 - exp(-0.00006*(-0.969256*X + 1.875991*Y + 0.041556*Z));
    inscatter.b = 1.0 - exp(-0.00006*(0.055648*X - 0.204043*Y + 1.057311*Z));

   // float3 colour = tex2D(detailMapSampler, input.uv);
    float3 colour = {0.1, 0.1, 0.1};
    return float4(colour*ex + inscatter*(1.0 - ex), 1.0);


technique shader {
    pass P0 {
        VertexShader = compile vs_1_1 VS();
        PixelShader = compile ps_3_0 PS();

[Edited by - Nyocia on February 28, 2008 5:11:49 AM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!