medfall

A super great game engine
Log | Files | Refs

skybox.glsl (1025B)


      1 #ifdef VERTEX_SHADER
      2 
      3 in vec3 position;
      4 out vec3 pos;
      5 
      6 layout( std140 ) uniform view {
      7 	mat4 V;
      8 	mat4 P;
      9 };
     10 
     11 void main() {
     12 	gl_Position = P * V * vec4( position, 0.0 );
     13 	pos = position;
     14 }
     15 
     16 #else
     17 
     18 in vec3 pos;
     19 out vec4 colour;
     20 
     21 layout( std140 ) uniform sky {
     22 	vec3 A, B, C, D, E, F, G, H, I, Z;
     23 	vec3 sun_dir;
     24 };
     25 
     26 uniform sampler2D blue_noise;
     27 
     28 vec3 perez_ext( float cos_theta, float gamma, float cos_gamma ) {
     29 	vec3 chi = ( 1 + cos_gamma * cos_gamma ) / pow( 1 + H * H - 2 * cos_gamma * H, vec3( 1.5 ) );
     30 	return ( 1 + A * exp( B / ( cos_theta + 0.01 ) ) ) * ( C + D * exp( E * gamma ) + F * ( cos_gamma * cos_gamma ) + G * chi + I * sqrt( cos_theta ) );
     31 }
     32 
     33 void main() {
     34 	vec3 clamped_pos = vec3( pos.x, pos.y, saturate( pos.z ) );
     35 	vec3 V = normalize( clamped_pos );
     36 
     37 	float cos_theta = saturate( pos.z );
     38 	float cos_gamma = dot( V, sun_dir );
     39 	float gamma = acos( cos_gamma );
     40 
     41 	vec3 R = Z * perez_ext( cos_theta, gamma, cos_gamma );
     42 
     43 	colour = vec4( linear_to_srgb( R + get_dither_noise( blue_noise ) ), 1.0 );
     44 }
     45 
     46 #endif