update helpers/location to 'frosted' ui
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
1 layout (location = 0) out vec4 oColour;
2
3 // OpenGL wiki: Recommends do not use vec3 because of drivers. hence the v4s...
4 layout (std140) uniform ub_world_lighting
5 {
6 vec4 g_cube_min;
7 vec4 g_cube_inv_range;
8
9 vec4 g_water_plane;
10 vec4 g_depth_bounds;
11
12 vec4 g_daysky_colour;
13 vec4 g_nightsky_colour;
14 vec4 g_sunset_colour;
15 vec4 g_ambient_colour;
16 vec4 g_sunset_ambient;
17 vec4 g_sun_colour;
18 vec4 g_sun_dir;
19 vec4 g_board_0;
20 vec4 g_board_1;
21
22 float g_water_fog;
23 float g_time;
24 float g_realtime;
25 float g_shadow_length;
26 float g_shadow_spread;
27
28 float g_time_of_day;
29 float g_day_phase;
30 float g_sunset_phase;
31
32 int g_light_preview;
33 int g_shadow_samples;
34
35 int g_debug_indices;
36 int g_debug_complexity;
37 };
38
39 uniform sampler2D g_world_depth;
40 uniform samplerBuffer uLightsArray;
41 uniform usampler3D uLightsIndex;
42
43 #include "light_clearskies.glsl"
44
45 float world_depth_sample( vec3 pos )
46 {
47 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
48 return texture( g_world_depth, depth_coord ).r;
49 }
50
51 float world_water_depth( vec3 pos )
52 {
53 float ref_depth = g_water_plane.y*g_water_plane.w;
54 return world_depth_sample( pos ) - ref_depth;
55 }
56
57 float shadow_sample( vec3 co ){
58 float height_sample = world_depth_sample( co );
59
60 float fdelta = height_sample - co.y;
61 return clamp( fdelta, 0.2, 0.4 )-0.2;
62 }
63
64 float newlight_compute_sun_shadow( vec3 co, vec3 dir ){
65 if( g_shadow_samples == 0 ){
66 return 1.0;
67 }
68
69 float fspread = g_shadow_spread;
70 float flength = g_shadow_length;
71
72 float famt = 0.0;
73 famt += shadow_sample(co+(dir+vec3(-0.56,0.55, 0.30)*fspread)*flength*0.1);
74 famt += shadow_sample(co+(dir+vec3( 0.80,0.68, 0.34)*fspread)*flength*0.2);
75 famt += shadow_sample(co+(dir+vec3( 0.78,0.07,-0.06)*fspread)*flength*0.3);
76 famt += shadow_sample(co+(dir+vec3(-0.59,0.07,-0.42)*fspread)*flength*0.4);
77
78 //famt+=shadow_sample(co+(dir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
79 //famt+=shadow_sample(co+(dir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
80 //famt+=shadow_sample(co+(dir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
81 //famt+=shadow_sample(co+(dir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
82
83 return 1.0 - famt;
84 }
85
86 float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )
87 {
88 vec3 specdir = reflect( -dir, wnormal );
89 return pow(max(dot( halfview, specdir ), 0.0), exponent);
90 }
91
92 vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist ){
93 float dist = pow(fdist*0.0010,0.78);
94 return mix( vfrag, colour, min( 1.0, dist ) );
95 }
96
97 vec3 scene_calculate_light( int light_index,
98 vec3 halfview, vec3 co, vec3 normal )
99 {
100 vec4 light_colour = texelFetch( uLightsArray, light_index+0 );
101 vec4 light_co = texelFetch( uLightsArray, light_index+1 );
102 vec4 light_dir = texelFetch( uLightsArray, light_index+2 );
103
104 vec3 light_delta = light_co.xyz-co;
105 float dist2 = dot(light_delta,light_delta);
106
107 light_delta = normalize( light_delta );
108
109 float quadratic = dist2*100.0;
110 float attenuation = 1.0/( 1.0 + quadratic );
111 attenuation *= max( dot( light_delta, normal ), 0.0 );
112
113 float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
114
115 if( light_dir.w < 0.999999 ){
116 float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );
117 falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );
118 }
119
120 return light_colour.rgb * attenuation * falloff
121 * step( g_day_phase, light_colour.w );
122 }
123
124 vec3 scene_calculate_packed_light_patch( uint packed_index,
125 vec3 halfview, vec3 co, vec3 normal )
126 {
127 uint light_count = packed_index & 0x3u;
128
129 vec3 l = vec3(0.0);
130
131 if( light_count >= 1u ){
132 int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );
133 int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );
134 int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );
135
136 l += scene_calculate_light( index_0, halfview, co, normal );
137
138 if( light_count >= 2u ){
139 l += scene_calculate_light( index_1, halfview, co, normal );
140
141 if( light_count >= 3u ){
142 l += scene_calculate_light( index_2, halfview, co, normal );
143 }
144 }
145 }
146
147 return l;
148 }
149
150 vec3 world_compute_lighting( vec3 diffuse, vec3 normal, vec3 co,
151 float light_mask )
152 {
153 if( g_light_preview == 1 )
154 diffuse = vec3(0.75);
155
156 // Lighting
157 vec3 halfview = uCamera - co;
158 float fdist = length(halfview);
159 halfview /= fdist;
160
161 float world_shadow = newlight_compute_sun_shadow(
162 co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
163
164 vec3 total_light = clearskies_lighting(
165 normal, min( light_mask, world_shadow ), halfview );
166
167 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
168 cube_coord = floor( cube_coord );
169
170 if( g_debug_indices == 1 )
171 {
172 return rand33(cube_coord);
173 }
174
175 if( g_debug_complexity == 1 )
176 {
177 ivec3 coord = ivec3( cube_coord );
178 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
179
180 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
181 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
182 }
183
184 // FIXME: this coord should absolutely must be clamped!
185
186 ivec3 coord = ivec3( cube_coord );
187 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
188
189 total_light +=
190 scene_calculate_packed_light_patch( index_sample.x,
191 halfview, co, normal )
192 * light_mask;
193 total_light +=
194 scene_calculate_packed_light_patch( index_sample.y,
195 halfview, co, normal )
196 * light_mask;
197
198 // Take a section of the sky function to give us a matching fog colour
199
200 vec3 fog_colour = clearskies_ambient( -halfview );
201 float sun_theta = dot( -halfview, g_sun_dir.xyz );
202 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
203 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
204
205 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
206 sun_colour *= sun_shape;
207
208 fog_colour += sun_colour;
209 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
210 }