input update 1
[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 vdir )
58 {
59 vec3 sample_pos = aWorldCo + vdir;
60 float height_sample = world_depth_sample( sample_pos );
61
62 float fdelta = height_sample - sample_pos.y;
63 return clamp( fdelta, 0.2, 0.4 )-0.2;
64 }
65
66 float newlight_compute_sun_shadow( vec3 dir )
67 {
68 if( g_shadow_samples == 0 )
69 {
70 return 1.0;
71 }
72
73 float fspread = g_shadow_spread;
74 float flength = g_shadow_length;
75
76 float famt = 0.0;
77 famt += shadow_sample((dir+vec3(-0.56,0.55, 0.30)*fspread)*flength*0.1);
78 famt += shadow_sample((dir+vec3( 0.80,0.68, 0.34)*fspread)*flength*0.2);
79 famt += shadow_sample((dir+vec3( 0.78,0.07,-0.06)*fspread)*flength*0.3);
80 famt += shadow_sample((dir+vec3(-0.59,0.07,-0.42)*fspread)*flength*0.4);
81
82 //famt+=shadow_sample((dir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
83 //famt+=shadow_sample((dir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
84 //famt+=shadow_sample((dir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
85 //famt+=shadow_sample((dir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
86
87 return 1.0 - famt;
88 }
89
90 float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )
91 {
92 vec3 specdir = reflect( -dir, wnormal );
93 return pow(max(dot( halfview, specdir ), 0.0), exponent);
94 }
95
96 vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )
97 {
98 float dist = pow(fdist*0.0010,0.78);
99 return mix( vfrag, colour, min( 1.0, dist ) );
100 }
101
102 vec3 rand33(vec3 p3)
103 {
104 p3 = fract(p3 * vec3(.1031, .1030, .0973));
105 p3 += dot(p3, p3.yxz+33.33);
106 return fract((p3.xxy + p3.yxx)*p3.zyx);
107 }
108
109 vec3 scene_calculate_light( int light_index,
110 vec3 halfview, vec3 co, vec3 normal )
111 {
112 vec4 light_colour = texelFetch( uLightsArray, light_index+0 );
113 vec4 light_co = texelFetch( uLightsArray, light_index+1 );
114 vec4 light_dir = texelFetch( uLightsArray, light_index+2 );
115
116 vec3 light_delta = light_co.xyz-co;
117 float dist2 = dot(light_delta,light_delta);
118
119 light_delta = normalize( light_delta );
120
121 float quadratic = dist2*100.0;
122 float attenuation = 1.0f/( 1.0f + quadratic );
123 attenuation *= max( dot( light_delta, normal ), 0.0 );
124
125 float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
126
127 if( light_dir.w < 0.999999 ){
128 float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );
129 falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );
130 }
131
132 return light_colour.rgb * attenuation * falloff
133 * step( g_day_phase, light_colour.w );
134 }
135
136 vec3 scene_calculate_packed_light_patch( uint packed_index,
137 vec3 halfview, vec3 co, vec3 normal )
138 {
139 uint light_count = packed_index & 0x3u;
140
141 vec3 l = vec3(0.0);
142
143 if( light_count >= 1u ){
144 int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );
145 int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );
146 int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );
147
148 l += scene_calculate_light( index_0, halfview, co, normal );
149
150 if( light_count >= 2u ){
151 l += scene_calculate_light( index_1, halfview, co, normal );
152
153 if( light_count >= 3u ){
154 l += scene_calculate_light( index_2, halfview, co, normal );
155 }
156 }
157 }
158
159 return l;
160 }
161
162 vec3 world_compute_lighting( vec3 diffuse, vec3 normal, vec3 co,
163 float light_mask )
164 {
165 if( g_light_preview == 1 )
166 diffuse = vec3(0.75);
167
168 // Lighting
169 vec3 halfview = uCamera - co;
170 float fdist = length(halfview);
171 halfview /= fdist;
172
173 float world_shadow = newlight_compute_sun_shadow( g_sun_dir.xyz
174 * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
175
176 vec3 total_light = clearskies_lighting(
177 normal, min( light_mask, world_shadow ), halfview );
178
179 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
180 cube_coord = floor( cube_coord );
181
182 if( g_debug_indices == 1 )
183 {
184 return rand33(cube_coord);
185 }
186
187 if( g_debug_complexity == 1 )
188 {
189 ivec3 coord = ivec3( cube_coord );
190 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
191
192 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
193 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
194 }
195
196 // FIXME: this coord should absolutely must be clamped!
197
198 ivec3 coord = ivec3( cube_coord );
199 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
200
201 total_light +=
202 scene_calculate_packed_light_patch( index_sample.x,
203 halfview, co, normal )
204 * light_mask;
205 total_light +=
206 scene_calculate_packed_light_patch( index_sample.y,
207 halfview, co, normal )
208 * light_mask;
209
210 // Take a section of the sky function to give us a matching fog colour
211
212 vec3 fog_colour = clearskies_ambient( -halfview );
213 float sun_theta = dot( -halfview, g_sun_dir.xyz );
214 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
215 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
216
217 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
218 sun_colour *= sun_shape;
219
220 fog_colour += sun_colour;
221 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
222 }