better low qual mode
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
1 layout (std140) uniform ub_world_lighting
2 {
3 vec4 g_light_colours[3];
4 vec4 g_light_directions[3];
5 vec4 g_ambient_colour;
6
7 vec4 g_water_plane;
8 vec4 g_depth_bounds;
9 float g_water_fog;
10 int g_light_count;
11 int g_light_preview;
12 int g_shadow_samples;
13 };
14
15 uniform sampler2D g_world_depth;
16
17 // Standard diffuse + spec models
18 // ==============================
19
20 vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )
21 {
22 vec3 vtotal = g_ambient_colour.rgb;
23
24 for( int i=0; i<g_light_count; i++ )
25 {
26 vec3 vcolour = g_light_colours[i].rgb;
27 vec3 vdir = g_light_directions[i].xyz;
28
29 float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
30 vtotal += vcolour*flight;
31 }
32
33 return vfrag * vtotal;
34 }
35
36 vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )
37 {
38 vec3 vcolour = g_light_colours[0].rgb;
39 vec3 vdir = g_light_directions[0].xyz;
40
41 vec3 specdir = reflect( -vdir, wnormal );
42 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
43 return vfrag + vcolour*spec*fintensity;
44 }
45
46 float world_depth_sample( vec3 pos )
47 {
48 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
49 return texture( g_world_depth, depth_coord ).r;
50 }
51
52 float shadow_sample( vec3 vdir )
53 {
54 vec3 sample_pos = aWorldCo + vdir;
55 float height_sample = world_depth_sample( sample_pos );
56
57 float fdelta = height_sample - sample_pos.y;
58 return clamp( fdelta, 0.1, 0.2 )-0.1;
59 }
60
61 vec3 do_light_shadowing_old( vec3 vfrag )
62 {
63 float faccum = 0.0;
64 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));
65 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));
66 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));
67 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));
68 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);
69 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);
70 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);
71 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);
72 return mix( vfrag, g_ambient_colour.rgb, faccum );
73 }
74
75 vec3 do_light_shadowing( vec3 vfrag )
76 {
77 if( g_shadow_samples == 0 )
78 {
79 return vfrag;
80 }
81
82 float fspread = g_light_colours[0].w;
83 vec3 vdir = g_light_directions[0].xyz;
84 float flength = g_light_directions[0].w;
85
86 float famt = 0.0;
87 famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);
88 famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);
89 famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);
90 famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);
91 famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
92 famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
93 famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
94 famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
95 return mix( vfrag, g_ambient_colour.rgb, famt );
96 }
97
98 vec3 apply_fog( vec3 vfrag, float fdist )
99 {
100 float dist = pow(fdist*0.0008,1.2);
101 return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
102 }