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