needs a lot of cleaning but lights are OK
[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 ) * 1.125;
7
8 const float SUN_ANGLE = 0.0001;
9 const float TIME_RATE = 0.025;
10
11 const float PI = 3.14159265;
12
13 struct world_info
14 {
15 float time,
16 time_of_day,
17 day_phase,
18 sunset_phase;
19
20 vec3 sun_dir;
21 };
22
23 float luminance( vec3 v )
24 {
25 return dot( v, vec3(0.2126, 0.7152, 0.0722) );
26 }
27
28 vec3 scene_ambient( vec3 dir, const world_info w )
29 {
30 float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;
31 float sky_gradient = dir.y;
32
33 /* Blend phase colours */
34 vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);
35 ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;
36 ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);
37
38 /* Add gradient */
39 ambient -= sky_gradient * luminance(ambient);
40
41 return ambient;
42 }
43
44 vec3 scene_sky( vec3 ray_dir, const world_info w )
45 {
46 ray_dir.y = abs( ray_dir.y );
47 vec3 sky_colour = scene_ambient( ray_dir, w );
48
49 /* Sun */
50 float sun_theta = dot( ray_dir, w.sun_dir );
51 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );
52 float sun_shape = pow( sun_size, 2000.0 );
53 sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;
54
55 vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );
56 sun_colour *= sun_shape;
57
58 vec3 composite = sky_colour + sun_colour;
59 return composite;
60 }
61
62 vec3 scene_compute_ambient( vec3 normal, const world_info w )
63 {
64 return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );
65 }
66
67 vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )
68 {
69 vec3 dir3 = vec3
70 (
71 cos(dir.y) * cos(dir.x),
72 sin(dir.x),
73 sin(dir.y) * cos(dir.x)
74 );
75
76 float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );
77
78 return flight * colour;
79 }
80
81 vec3 scene_lighting_old( vec3 normal, const world_info w )
82 {
83 vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );
84 vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );
85 vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );
86
87 return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +
88 SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) +
89 SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;
90 }
91
92 vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )
93 {
94 float fresnel = 1.0 - abs(dot(normal,halfview));
95
96 vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );
97 vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR
98 * w.day_phase;
99
100 float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );
101
102 vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );
103
104 return ambient + (light_sun + sky_reflection) * shadow;
105
106
107
108
109
110
111 float sun_theta = dot( normal, w.sun_dir );
112
113 float softness_min = 0.5;
114 float softness = softness_min + w.sunset_phase * (1.0-softness_min);
115 float light_min = 0.0 * w.day_phase;
116 float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);
117 light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;
118
119 float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );
120 light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );
121
122 vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);
123 vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );
124
125 float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 )
126 * 0.2 * shadow * w.day_phase;
127
128 return mix(dark_colour, light_colour, light_direct) +
129 spec +
130 dark_colour * light_bounce;
131 }
132
133 void scene_state( float world_time, out world_info w )
134 {
135 w.time = world_time;
136 w.time_of_day = fract( w.time );
137 w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;
138 w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;
139 w.sunset_phase = pow( w.sunset_phase, 6.0 );
140
141 float a = w.time_of_day * PI * 2.0;
142 w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );
143 }
144