X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=shaders%2Fcommon_world.glsl;h=0bc4e7b6bbcceb2471c9c31523dcd8b8d1d4a035;hb=be5e25dee2c54c2a22ca3bbb5bbe0eb6149343be;hp=45e12dff48267ba71c814a07a3272ea3eb29c339;hpb=f3a2490079baf440238b78e54f4476649eddbda2;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/shaders/common_world.glsl b/shaders/common_world.glsl index 45e12df..0bc4e7b 100644 --- a/shaders/common_world.glsl +++ b/shaders/common_world.glsl @@ -1,23 +1,46 @@ layout (location = 0) out vec4 oColour; +// OpenGL wiki: Recommends do not use vec3 because of drivers. hence the v4s... layout (std140) uniform ub_world_lighting { - vec4 g_light_colours[3]; - vec4 g_light_directions[3]; - vec4 g_ambient_colour; + vec4 g_cube_min; + vec4 g_cube_inv_range; vec4 g_water_plane; vec4 g_depth_bounds; + + vec4 g_daysky_colour; + vec4 g_nightsky_colour; + vec4 g_sunset_colour; + vec4 g_ambient_colour; + vec4 g_sunset_ambient; + vec4 g_sun_colour; + vec4 g_sun_dir; + vec4 g_board_0; + vec4 g_board_1; + float g_water_fog; - int g_light_count; + float g_time; + float g_realtime; + float g_shadow_length; + float g_shadow_spread; + + float g_time_of_day; + float g_day_phase; + float g_sunset_phase; + int g_light_preview; int g_shadow_samples; - vec4 g_point_light_positions[32]; - vec4 g_point_light_colours[32]; + int g_debug_indices; + int g_debug_complexity; }; uniform sampler2D g_world_depth; +uniform samplerBuffer uLightsArray; +uniform usampler3D uLightsIndex; + +#include "light_clearskies.glsl" float world_depth_sample( vec3 pos ) { @@ -27,106 +50,161 @@ float world_depth_sample( vec3 pos ) float world_water_depth( vec3 pos ) { - vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; float ref_depth = g_water_plane.y*g_water_plane.w; - return texture( g_world_depth, depth_coord ).g - ref_depth; + return world_depth_sample( pos ) - ref_depth; } -float shadow_sample( vec3 vdir ) -{ - vec3 sample_pos = aWorldCo + vdir; - float height_sample = world_depth_sample( sample_pos ); +float shadow_sample( vec3 co ){ + float height_sample = world_depth_sample( co ); - float fdelta = height_sample - sample_pos.y; - return clamp( fdelta, 0.1, 0.2 )-0.1; + float fdelta = height_sample - co.y; + return clamp( fdelta, 0.2, 0.4 )-0.2; } -float sdLine( vec3 p, vec3 a, vec3 b ) -{ - vec3 pa = p - a; - vec3 ba = b - a; +float newlight_compute_sun_shadow( vec3 co, vec3 dir ){ + if( g_shadow_samples == 0 ){ + return 1.0; + } - float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); - return length( pa - ba*h ); -} + float fspread = g_shadow_spread; + float flength = g_shadow_length; -vec3 apply_fog( vec3 vfrag, float fdist ) -{ - float dist = pow(fdist*0.0008,1.2); - return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) ); -} + float famt = 0.0; + famt += shadow_sample(co+(dir+vec3(-0.56,0.55, 0.30)*fspread)*flength*0.1); + famt += shadow_sample(co+(dir+vec3( 0.80,0.68, 0.34)*fspread)*flength*0.2); + famt += shadow_sample(co+(dir+vec3( 0.78,0.07,-0.06)*fspread)*flength*0.3); + famt += shadow_sample(co+(dir+vec3(-0.59,0.07,-0.42)*fspread)*flength*0.4); + //famt+=shadow_sample(co+(dir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5); + //famt+=shadow_sample(co+(dir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6); + //famt+=shadow_sample(co+(dir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7); + //famt+=shadow_sample(co+(dir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8); -// New lighting model + return 1.0 - famt; +} -vec3 newlight_compute_ambient() +float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent ) { - return g_ambient_colour.rgb; + vec3 specdir = reflect( -dir, wnormal ); + return pow(max(dot( halfview, specdir ), 0.0), exponent); +} + +vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist ){ + float dist = pow(fdist*0.0010,0.78); + return mix( vfrag, colour, min( 1.0, dist ) ); } -float newlight_compute_sun_shadow() +vec3 scene_calculate_light( int light_index, + vec3 halfview, vec3 co, vec3 normal ) { - if( g_shadow_samples == 0 ) - { - return 1.0; - } + vec4 light_colour = texelFetch( uLightsArray, light_index+0 ); + vec4 light_co = texelFetch( uLightsArray, light_index+1 ); + vec4 light_dir = texelFetch( uLightsArray, light_index+2 ); - float fspread = g_light_colours[0].w; - vec3 vdir = g_light_directions[0].xyz; - float flength = g_light_directions[0].w; + vec3 light_delta = light_co.xyz-co; + float dist2 = dot(light_delta,light_delta); - float famt = 0.0; - famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1); - famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2); - famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3); - famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4); - famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5); - famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6); - famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7); - famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8); - - // player shadow - float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 ); - float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 ); - player_shadow *= player_shadow*player_shadow*player_shadow; - - return 1.0 - max( player_shadow*0.8, famt ); -} + light_delta = normalize( light_delta ); -vec3 newlight_compute_world_diffuse( vec3 wnormal ) -{ - vec3 vtotal = g_ambient_colour.rgb; + float quadratic = dist2*100.0; + float attenuation = 1.0/( 1.0 + quadratic ); + attenuation *= max( dot( light_delta, normal ), 0.0 ); - for( int i=0; i= 1u ){ + int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u ); + int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u ); + int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u ); + + l += scene_calculate_light( index_0, halfview, co, normal ); + + if( light_count >= 2u ){ + l += scene_calculate_light( index_1, halfview, co, normal ); + + if( light_count >= 3u ){ + l += scene_calculate_light( index_2, halfview, co, normal ); + } + } + } - vec3 specdir = reflect( -vdir, wnormal ); - float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0); - return vcolour*spec*fintensity; + return l; } -vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, - vec3 light_pos, vec3 light_colour ) +vec3 world_compute_lighting( vec3 diffuse, vec3 normal, vec3 co, + float light_mask ) { - vec3 light_delta = (light_pos-aWorldCo) * 10.0; + if( g_light_preview == 1 ) + diffuse = vec3(0.75); - float quadratic = dot(light_delta,light_delta); - float attenuation = 1.0f/( 1.0f + quadratic ); - attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) ); + // Lighting + vec3 halfview = uCamera - co; + float fdist = length(halfview); + halfview /= fdist; + + float world_shadow = newlight_compute_sun_shadow( + co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) ); + + vec3 total_light = clearskies_lighting( + normal, min( light_mask, world_shadow ), halfview ); + + vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz; + cube_coord = floor( cube_coord ); + + if( g_debug_indices == 1 ) + { + return rand33(cube_coord); + } + + if( g_debug_complexity == 1 ) + { + ivec3 coord = ivec3( cube_coord ); + uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 ); + + uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u); + return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 ); + } - return light_colour*attenuation; + // FIXME: this coord should absolutely must be clamped! + + ivec3 coord = ivec3( cube_coord ); + uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 ); + + total_light += + scene_calculate_packed_light_patch( index_sample.x, + halfview, co, normal ) + * light_mask; + total_light += + scene_calculate_packed_light_patch( index_sample.y, + halfview, co, normal ) + * light_mask; + + // Take a section of the sky function to give us a matching fog colour + + vec3 fog_colour = clearskies_ambient( -halfview ); + float sun_theta = dot( -halfview, g_sun_dir.xyz ); + float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 ); + float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5; + + vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 ); + sun_colour *= sun_shape; + + fog_colour += sun_colour; + return scene_apply_fog( diffuse * total_light, fog_colour, fdist ); }