X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=shaders%2Fcommon_world.glsl;h=5c06d73db33caf07636035c02042dd0a7a7388da;hb=a1056ed8198f0f5be0e0f341da8bd49aa6c47198;hp=4d9b0d3d6a7211f6cdb38499338264ab252ffc84;hpb=168eb5c363f510d60703498e01ffcdb52bf9fd07;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/shaders/common_world.glsl b/shaders/common_world.glsl index 4d9b0d3..5c06d73 100644 --- a/shaders/common_world.glsl +++ b/shaders/common_world.glsl @@ -1,96 +1,221 @@ +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; + 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; + + int g_debug_indices; + int g_debug_complexity; }; uniform sampler2D g_world_depth; +uniform samplerBuffer uLightsArray; +uniform usampler3D uLightsIndex; -// Standard diffuse + spec models -// ============================== +#include "light_clearskies.glsl" -vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal ) +float world_depth_sample( vec3 pos ) { - vec3 vtotal = g_ambient_colour.rgb; + vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; + return texture( g_world_depth, depth_coord ).r; +} - 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 ); + } + } + } + + return l; } -vec3 apply_fog( vec3 vfrag, float fdist ) +vec3 world_compute_lighting( vec3 diffuse, vec3 normal, vec3 co, + float light_mask ) { - float dist = pow(fdist*0.0008,1.2); - return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) ); + if( g_light_preview == 1 ) + diffuse = vec3(0.75); + + // Lighting + vec3 halfview = uCamera - co; + float fdist = length(halfview); + halfview /= fdist; + + float world_shadow = newlight_compute_sun_shadow( 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 ); + } + + // 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 ); }