reworked lighting uniforms
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
1 layout (std140) uniform ub_world_lighting
2 {
3 vec3 g_directional;
4 vec3 g_sun_colour;
5 vec3 g_shadow_colour;
6 vec4 g_water_plane;
7 vec4 g_depth_bounds;
8 float g_water_fog;
9 };
10
11 uniform sampler2D g_world_depth;
12
13 // Standard diffuse + spec models
14 // ==============================
15
16 vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )
17 {
18 float flight = dot( g_directional, wnormal )*0.5+0.5;
19 return vfrag * mix( g_shadow_colour, g_sun_colour, flight );
20 }
21
22 vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )
23 {
24 vec3 specdir = reflect( -g_directional, wnormal );
25 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
26 return vfrag + g_sun_colour*spec*fintensity;
27 }
28
29 float world_depth_sample( vec3 pos )
30 {
31 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
32 return texture( g_world_depth, depth_coord ).r;
33 }
34
35 float shadow_sample( vec3 vdir )
36 {
37 vec3 sample_pos = aCo + vdir;
38 float height_sample = world_depth_sample( sample_pos );
39
40 float fdelta = height_sample - sample_pos.y;
41 return clamp( fdelta, 0.1, 0.2 )-0.1;
42 }
43
44 vec3 do_light_shadowing( vec3 vfrag )
45 {
46 float faccum = 0.0;
47 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));
48 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));
49 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));
50 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));
51 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);
52 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);
53 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);
54 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);
55 return mix( vfrag, g_shadow_colour, faccum );
56 }
57