1 layout (location = 0) out vec4 oColour;
3 layout (std140) uniform ub_world_lighting
8 vec4 g_light_colours[3];
9 vec4 g_light_directions[3];
10 vec4 g_ambient_colour;
21 int g_debug_complexity;
25 //vec4 g_point_light_positions[32];
26 //vec4 g_point_light_colours[32];
29 uniform sampler2D g_world_depth;
31 float world_depth_sample( vec3 pos )
33 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
34 return texture( g_world_depth, depth_coord ).r;
37 float world_water_depth( vec3 pos )
39 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
40 float ref_depth = g_water_plane.y*g_water_plane.w;
41 return texture( g_world_depth, depth_coord ).g - ref_depth;
44 float shadow_sample( vec3 vdir )
46 vec3 sample_pos = aWorldCo + vdir;
47 float height_sample = world_depth_sample( sample_pos );
49 float fdelta = height_sample - sample_pos.y;
50 return clamp( fdelta, 0.1, 0.2 )-0.1;
53 vec3 apply_fog( vec3 vfrag, float fdist )
55 float dist = pow(fdist*0.0008,1.2);
56 return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
62 vec3 newlight_compute_ambient()
64 return g_ambient_colour.rgb;
67 float newlight_compute_sun_shadow( vec3 dir )
69 if( g_shadow_samples == 0 )
74 float fspread = g_light_colours[0].w;
76 float flength = g_light_directions[0].w;
79 famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);
80 famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);
81 famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);
82 famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);
83 famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
84 famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
85 famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
86 famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
91 vec3 newlight_compute_world_diffuse( vec3 wnormal )
93 vec3 vtotal = g_ambient_colour.rgb;
95 for( int i=0; i<g_light_count; i++ )
97 vec3 vcolour = g_light_colours[i].rgb;
98 vec3 vdir = g_light_directions[i].xyz;
100 float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
101 vtotal += vcolour*flight;
107 vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )
109 vec3 vcolour = g_light_colours[0].rgb;
110 vec3 vdir = g_light_directions[0].xyz;
112 vec3 specdir = reflect( -vdir, wnormal );
113 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
114 return vcolour*spec*fintensity;
117 float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )
119 vec3 specdir = reflect( -dir, wnormal );
120 return pow(max(dot( halfview, specdir ), 0.0), exponent);
123 vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,
124 vec3 light_colour, vec3 light_pos )
126 vec3 light_delta = light_pos-aWorldCo;
128 float dist2 = dot(light_delta,light_delta);
130 float quadratic = dist2*100.0;
131 float attenuation = 1.0f/( 1.0f + quadratic );
132 attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );
134 float falloff = max( 0.0, 1.0-(dist2*max_dist) );
135 return light_colour * attenuation * falloff;
138 vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview,
139 vec3 light_colour, vec3 light_pos,
142 vec3 light_delta = (light_pos-aWorldCo) * 10.0;
144 float quadratic = dot(light_delta,light_delta);
145 float attenuation = 1.0f/( 1.0f + quadratic );
147 light_delta = normalize( light_delta );
148 attenuation *= max( 0.0, dot( light_delta, wnormal ) );
150 float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),
151 falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );
153 return light_colour*attenuation*falloff;