day & night cycle sky shader for you

Started by
12 comments, last by pachesantiago 11 years, 10 months ago
Hi, so here's my first contribution to this site i've learned so much from. a day & night cycle sky shader written in hlsl. here's a video of it on youtube:
"> the shader is written for ps/vs 2.0 and uses one pass. Credits go to RaveniX for some lines i translated from his glsl shader that he posted here. I've produced a little demo application that uses the shader: download sky shader demo app source The sky itself renders at a fps rate of 2000+ on my ati hd 4850. Feel free to use it for your projects. Any feedback or optimization advice would be highly appreciated. i hope somebody likes it :) here's the hlsl of the shader:

vector SunPos = {1.0, 1.0, 1.0, 1.0 };

float time_of_day;

vector Zenith  = { 0.00, 0.34, 0.71, 0.00 };
vector Horizon = { 0.70, 0.40, 0.20, 0.00 };

float4x4 MatWVP 	: ViewProjection;
float4x4 MatViewInv	: ViewInverse;
float4x4 MatWorld	: World;

float3 LightDir	= {1.0f, 1.0f, 1.0f };

sampler TextureOne = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureTwo = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureThree = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

struct VS_input
{
	float4 Position : POSITION0;
	float2 tex1 : TEXCOORD0;
};

struct VS_output
{
	float4 Position : POSITION0;
   	float2 tex1 : TEXCOORD0;
	float3 Vertex : TEXCOORD1;
};

struct PS_output
{
   float4 diffuse : COLOR0;
};

VS_output vs_main( VS_input input )
{
   VS_output output;

   output.Vertex = input.Position;

   output.Position = mul( float4(input.Position.xyz , 1.0f) , MatWVP);
   
   output.tex1 = input.tex1;

   return( output );
}


PS_output ps_main(in VS_output input)
{
   	PS_output output = (PS_output)0;
   	
	float3 norm = normalize(input.Vertex).xyz;
	float3 Sun = normalize(SunPos.xyz);
	float  SunDot = dot(Sun, norm);

    	float2 cycle = {fmod(SunPos.w, 100.0), 0};
    	float fact_clouds = saturate(SunPos.y * 4.0) * 0.3;
   	fact_clouds *= time_of_day;
	
	// first texture: clouds

    	float4 clouds1 = tex2D(TextureOne, input.tex1 * 2.0 + cycle);   
	clouds1.rgb *= fact_clouds;
   	
    	// second texture: stars
    
    	float3 stars = tex2D(TextureTwo, input.tex1 * 4.0  - cycle);
	stars *= 1 - saturate(time_of_day * 1.5);
    
   	// third texture: more clouds

   	float4 clouds2 = tex2D(TextureThree, input.tex1 + cycle * 1.5 );    
	clouds2.rgb *= fact_clouds;
	
	// brighten up the clouds near the sun

	vector rays = 0.7f * pow( max(0.0, SunDot), 60.0f );
	rays *= clouds2.a;

	// use alpha from the clouds to hide stars behind clouds
	
	float clouds_alpha = max(clouds1.a, clouds2.a);

	stars *= 1 - clouds_alpha * time_of_day;
    
    	// moon and sun

	vector light = 0.8f * pow( max(0.0001, SunDot), 360.0f );
	//vector moon  = 0.3f * pow( max(0.0, dot(-Sun, norm)), 60.0f );

	// atmosphere
	
	float Curve = 0.25f * (2.0f - SunPos.y);

	vector sky = Zenith * (1-Curve) + Horizon * Curve;
	sky = sky * (1+SunDot) + Zenith * -SunDot;
	sky *= (1.0-clouds_alpha*(1.0-time_of_day));
	sky *= saturate(SunPos.y+2.0);
			
	// combine everything

    	output.diffuse.rgb = sky + clouds1 + stars + clouds2 + light + rays; // + moon 
	output.diffuse.a = 1.0f;

	return output;

} 


technique sky
{
   pass Pass_0
   {
      VertexShader = compile vs_2_0 vs_main();
      PixelShader = compile ps_2_0 ps_main();

   }
}


Advertisement
Very Nice!

Keep up the great work!
There's no "hard", and "the impossible" takes just a little time.
The YouTube video looks fantastic, however your demo app presents me with a window with a white background and nothing else (well, except the menu).

This is compiling and running on Vista x64 with VS2008.
@adt7:

thank you! I just downloaded and tested the demo and it works fine here.

Which kind of graphics device are you using? I have only tested this on a hd4850 and geforce 6200.

Maybe try changing

d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;

to

d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

in MhDirectx.cpp line 38

maybe this could cause problems on some hardware.

i hope we can fix this :)
nVidia 9800GT. Changing the depth stencil format didn't help :(
well it worked on my buddy's 9300gs...

maybe something about my project and vista?

i created it using VS 2005 and XP 32 bit.
If anyone feels like testing if the demo app works that would be very kind :)
Quote:Original post by adt7
The YouTube video looks fantastic, however your demo app presents me with a window with a white background and nothing else (well, except the menu).

This is compiling and running on Vista x64 with VS2008.
What do the Debug Runtimes say?
Your problem appears to be that your effect won't compile. D3DXCreateEffectFromFile is returning INVALIDDATA, and compiling with fxc gives a really bizarre error. The problems seem to be stemming from the line where you do the fmod on SunPos.w. I think it's a compiler bug...disabling optimizations or the preshader gets it to compile with fxc. Still won't compile in the app, though...
Test subject:
A8Js (notebook)
Nvidia Geforce Go 7700
512mb AGP (dedicated)
Shader model: 3.0
2GB Ram
Core2Duo 2GHZ

Debug build: (and mode)
FPS:
Starts at 8 then slowly goes down to 3.
Can reach a peek of 4 fps when the sun has gone up.
FPS drops to ~2.2 when the world is turning dark.

Errors upon exit:
D3DSemArbVS05.exe has triggered a breakpoint (free.c)

Comments:
What i found odd in this sample is that it was running at 8fps when it started, only to slowly drop to 3 fps, and never to return to it's previous speed.
Is this a flaw in the fps calculation, or is it actually running slower and slower?


Release build:
FPS is at ~steady 147.

Errors upon exit:
D3DSemArbVS05.exe has triggered a breakpoint.

Comments:
Still loaded with DirectX debug runtimes, FPS can be expected to exceed 147 with standard runtimes.
Decreasing fps not witnessed on release build.

[Edited by - marius1930 on June 20, 2009 3:41:31 AM]

This topic is closed to new replies.

Advertisement