X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=shaders%2Fmodel_character_view.fs;h=502d67299d95d95c87dc6ff96393447928608694;hb=4eccfd7252f8ff165670842df537441afae5458b;hp=efee606c86b7b0151505dd268d258af035f9d8cd;hpb=f1fe55f957a3dbdb6ca20a696f0c1171e2d5e7ca;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/shaders/model_character_view.fs b/shaders/model_character_view.fs index efee606..502d672 100644 --- a/shaders/model_character_view.fs +++ b/shaders/model_character_view.fs @@ -1,9 +1,5 @@ uniform sampler2D uTexMain; -uniform sampler2D uTexSceneDepth; uniform vec3 uCamera; -uniform vec3 uInverseRatioDepth; -uniform vec3 uInverseRatioMain; -uniform bool uDepthCompare; in vec4 aColour; in vec2 aUv; @@ -11,42 +7,102 @@ in vec3 aNorm; in vec3 aCo; in vec3 aWorldCo; +#include "light_clearskies_stddef.glsl" #include "common_world.glsl" #include "motion_vectors_fs.glsl" +#include "depth_compare.glsl" -float linear_depth( float depth, float near, float far ) +vec3 character_clearskies_lighting( vec3 normal, float shadow, vec3 halfview ) { - float z = depth * 2.0 - 1.0; - return (2.0 * near * far) / (far + near - z * (far - near)); + float fresnel = 1.0 - abs(dot(normal,halfview)); + + vec3 reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb, + g_sunset_phase ); + + + vec3 sky_reflection = 0.5 * fresnel * reflect_colour; + vec3 light_sun = max(0.0, dot(normal,g_sun_dir.xyz)*0.5+0.5) + * g_sun_colour.rgb * g_day_phase; + + float scaled_shadow = max( shadow, 1.0 - max(g_sun_dir.y,0.0) ); + vec3 ambient = mix( g_ambient_colour.rgb, g_sunset_ambient.rgb, + g_sunset_phase ); + + return ambient + (light_sun + sky_reflection) * shadow; } -void main() +vec3 character_compute_lighting( vec3 diffuse, vec3 normal, vec3 co, + float light_mask ) { - compute_motion_vectors(); + if( g_light_preview == 1 ) + diffuse = vec3(0.75); - vec3 qnorm = aNorm; - vec3 diffuse = texture( uTexMain, aUv ).rgb; - vec3 composite = world_compute_lighting( diffuse, qnorm, aWorldCo, 1.0 ); + // Lighting + vec3 halfview = uCamera - co; + float fdist = length(halfview); + halfview /= fdist; - float dist = distance( aWorldCo, uCamera ) - 0.08; - float opacity = clamp( dist*dist, 0.0, 1.0 ); + float world_shadow = newlight_compute_sun_shadow( + co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) ); - if( uDepthCompare ){ - vec2 back_coord = gl_FragCoord.xy * uInverseRatioMain.xy - * uInverseRatioDepth.xy; - float back_depth = texture( uTexSceneDepth, back_coord ).r; - float front_depth = gl_FragCoord.z/gl_FragCoord.w; + vec3 total_light = character_clearskies_lighting( + normal, min( light_mask, world_shadow ), halfview ); - back_depth = linear_depth( back_depth, 0.1, 2100.0 ); - float diff = back_depth - front_depth; + vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz; + cube_coord = floor( cube_coord ); - vec2 ssuv = gl_FragCoord.xy; - vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) ); - float dither = fract( vDither.g / 71.0 ) - 0.5; + 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 ); - if( step(0.0,diff)+dither<0.3 ) - discard; + 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 ); +} + +void main(){ + depth_compare_dither(); + compute_motion_vectors(); + + vec3 qnorm = aNorm; + vec3 diffuse = texture( uTexMain, aUv ).rgb; + vec3 composite = character_compute_lighting( diffuse, qnorm, aWorldCo, 1.0 ); + + float dist = distance( aWorldCo, uCamera ) - 0.08; + float opacity = clamp( dist*dist, 0.0, 1.0 ); + oColour = vec4( composite, opacity ); }