1bd2469ccb772a0539c618b56e8463d3f7a10c92
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / light_clearskies.glsl
1 //const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );
2 //const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );
3 //const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );
4 //const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );
5 //const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );
6 //const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 );
7
8 const float SUN_ANGLE = 0.0001;
9 const float PI = 3.14159265358979323846264;
10
11 //struct world_info
12 //{
13 // float time,
14 // time_of_day,
15 // day_phase,
16 // sunset_phase;
17 //
18 // vec3 sun_dir;
19 //};
20
21 vec3 rand33(vec3 p3)
22 {
23 p3 = fract(p3 * vec3(.1031, .1030, .0973));
24 p3 += dot(p3, p3.yxz+33.33);
25 return fract((p3.xxy + p3.yxx)*p3.zyx);
26 }
27
28 float stars( vec3 rd, float rr, float size ){
29 vec3 co = rd * rr;
30
31 float a = atan(co.y, length(co.xz)) + 4.0 * PI;
32
33 float spaces = 1.0 / rr;
34 size = (rr * 0.0015) * fwidth(a) * 1000.0 * size;
35 a -= mod(a, spaces) - spaces * 0.5;
36
37 float count = floor(sqrt(pow(rr, 2.0) * (1.0 - pow(sin(a), 2.0))) * 3.0);
38
39 float plane = atan(co.z, co.x) + 4.0 * PI;
40 plane = plane - mod(plane, PI / count);
41
42 vec2 delta = rand33(vec3(plane, a, 0.0)).xy;
43
44 float level = sin(a + spaces * (delta.y - 0.5) * (1.0 - size)) * rr;
45 float ydist = sqrt(rr * rr - level * level);
46 float angle = plane + (PI * (delta.x * (1.0-size) + size * 0.5) / count);
47 vec3 center = vec3(cos(angle) * ydist, level, sin(angle) * ydist);
48 float star = smoothstep(size, 0.0, distance(center, co));
49 return star;
50 }
51
52 float luminance( vec3 v )
53 {
54 return dot( v, vec3(0.2126, 0.7152, 0.0722) );
55 }
56
57 vec3 clearskies_ambient( vec3 dir )
58 {
59 float sun_azimuth = g_sunset_phase * (dot( dir.xz, g_sun_dir.xz )*0.4+0.6);
60 float sky_gradient = dir.y;
61
62 /* Blend phase colours */
63 vec3 ambient = g_daysky_colour.rgb * (g_day_phase-g_sunset_phase*0.1);
64 ambient += g_sunset_colour.rgb * (1.0-dir.y*0.5)*sun_azimuth;
65 ambient += g_nightsky_colour.rgb * (1.0-g_day_phase);
66
67 /* Add gradient */
68 ambient -= sky_gradient * luminance(ambient);
69
70 return ambient;
71 }
72
73 vec3 clearskies_sky( vec3 ray_dir )
74 {
75 ray_dir.y = abs( ray_dir.y );
76 vec3 sky_colour = clearskies_ambient( ray_dir );
77
78 /* Sun */
79 float sun_theta = dot( ray_dir, g_sun_dir.xyz );
80 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );
81 float sun_shape = pow( sun_size, 2000.0 );
82 sun_shape += sun_size * max(g_sun_dir.y,0.0) * 0.5;
83
84 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
85 sun_colour *= sun_shape;
86
87
88 float star = 0.0;
89 float star_blend = 10.0*max(0.0,(1.0-g_day_phase*2.0));
90
91 if( star_blend > 0.001 ){
92 for( float j = 1.0; j <= 4.1; j += 1.0 ){
93 float m = mix(0.6, 0.9, smoothstep(1.0, 2.0, j));
94 star += stars( ray_dir, 1.94 * pow( 1.64, j ), m ) * (1.0/pow(4.0, j));
95 }
96 }
97
98 vec3 composite = sky_colour + sun_colour + star*star_blend;
99 return composite;
100 }
101
102 vec3 clearskies_lighting( vec3 normal, float shadow, vec3 halfview )
103 {
104 float fresnel = 1.0 - abs(dot(normal,halfview));
105
106 vec3 reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb,
107 g_sunset_phase );
108
109 vec3 sky_reflection = 0.5 * fresnel * reflect_colour;
110 vec3 light_sun = max( CLEARSKIES_LIGHT_DOT_MIN,
111 dot(normal,g_sun_dir.xyz)*0.75+0.25
112 ) * g_sun_colour.rgb * g_day_phase;
113
114 float scaled_shadow = max( shadow, 1.0 - max(g_sun_dir.y,0.0) );
115 vec3 ambient = mix( g_ambient_colour.rgb, g_sunset_ambient.rgb,
116 g_sunset_phase );
117
118 return ambient + (light_sun + sky_reflection) * shadow;
119 }