1 layout (location = 0) out vec4 oColour;
3 layout (std140) uniform ub_world_lighting
5 vec4 g_light_colours[3];
6 vec4 g_light_directions[3];
17 uniform sampler2D g_world_depth;
19 // Standard diffuse + spec models
20 // ==============================
22 vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )
24 vec3 vtotal = g_ambient_colour.rgb;
26 for( int i=0; i<g_light_count; i++ )
28 vec3 vcolour = g_light_colours[i].rgb;
29 vec3 vdir = g_light_directions[i].xyz;
31 float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
32 vtotal += vcolour*flight;
35 return vfrag * vtotal;
38 vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )
40 vec3 vcolour = g_light_colours[0].rgb;
41 vec3 vdir = g_light_directions[0].xyz;
43 vec3 specdir = reflect( -vdir, wnormal );
44 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
45 return vfrag + vcolour*spec*fintensity;
48 float world_depth_sample( vec3 pos )
50 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
51 return texture( g_world_depth, depth_coord ).r;
54 float shadow_sample( vec3 vdir )
56 vec3 sample_pos = aWorldCo + vdir;
57 float height_sample = world_depth_sample( sample_pos );
59 float fdelta = height_sample - sample_pos.y;
60 return clamp( fdelta, 0.1, 0.2 )-0.1;
63 vec3 do_light_shadowing_old( vec3 vfrag )
66 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));
67 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));
68 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));
69 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));
70 faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);
71 faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);
72 faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);
73 faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);
74 return mix( vfrag, g_ambient_colour.rgb, faccum );
78 float sdLine( vec3 p, vec3 a, vec3 b )
83 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
84 return length( pa - ba*h );
87 vec3 do_light_shadowing( vec3 vfrag )
89 if( g_shadow_samples == 0 )
94 float fspread = g_light_colours[0].w;
95 vec3 vdir = g_light_directions[0].xyz;
96 float flength = g_light_directions[0].w;
99 famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);
100 famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);
101 famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);
102 famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);
103 famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
104 famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
105 famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
106 famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
109 float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );
110 float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );
111 player_shadow *= player_shadow*player_shadow*player_shadow;
113 famt = max( player_shadow*0.6, famt );
114 return mix( vfrag, g_ambient_colour.rgb, famt );
117 vec3 apply_fog( vec3 vfrag, float fdist )
119 float dist = pow(fdist*0.0008,1.2);
120 return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );