09da174ee94b101ee2ff73dab3fd23df006dd221
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_character_view.fs
1 uniform sampler2D uTexMain;
2 uniform vec3 uCamera;
3
4 in vec4 aColour;
5 in vec2 aUv;
6 in vec3 aNorm;
7 in vec3 aCo;
8 in vec3 aWorldCo;
9
10 #include "common_world.glsl"
11 #include "motion_vectors_fs.glsl"
12 #include "depth_compare.glsl"
13
14 vec3 character_clearskies_lighting( vec3 normal, float shadow, vec3 halfview )
15 {
16 float fresnel = 1.0 - abs(dot(normal,halfview));
17
18 vec3 reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb,
19 g_sunset_phase );
20
21
22 vec3 sky_reflection = 0.5 * fresnel * reflect_colour;
23 vec3 light_sun = max(0.0, dot(normal,g_sun_dir.xyz)*0.5+0.5)
24 * g_sun_colour.rgb * g_day_phase;
25
26 float scaled_shadow = max( shadow, 1.0 - max(g_sun_dir.y,0.0) );
27 vec3 ambient = mix( g_ambient_colour.rgb, g_sunset_ambient.rgb,
28 g_sunset_phase );
29
30 return ambient + (light_sun + sky_reflection) * shadow;
31 }
32
33 vec3 character_compute_lighting( vec3 diffuse, vec3 normal, vec3 co,
34 float light_mask )
35 {
36 if( g_light_preview == 1 )
37 diffuse = vec3(0.75);
38
39 // Lighting
40 vec3 halfview = uCamera - co;
41 float fdist = length(halfview);
42 halfview /= fdist;
43
44 float world_shadow = newlight_compute_sun_shadow(
45 co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
46
47 vec3 total_light = character_clearskies_lighting(
48 normal, min( light_mask, world_shadow ), halfview );
49
50 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
51 cube_coord = floor( cube_coord );
52
53 if( g_debug_indices == 1 )
54 {
55 return rand33(cube_coord);
56 }
57
58 if( g_debug_complexity == 1 )
59 {
60 ivec3 coord = ivec3( cube_coord );
61 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
62
63 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
64 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
65 }
66
67 // FIXME: this coord should absolutely must be clamped!
68
69 ivec3 coord = ivec3( cube_coord );
70 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
71
72 total_light +=
73 scene_calculate_packed_light_patch( index_sample.x,
74 halfview, co, normal )
75 * light_mask;
76 total_light +=
77 scene_calculate_packed_light_patch( index_sample.y,
78 halfview, co, normal )
79 * light_mask;
80
81 // Take a section of the sky function to give us a matching fog colour
82
83 vec3 fog_colour = clearskies_ambient( -halfview );
84 float sun_theta = dot( -halfview, g_sun_dir.xyz );
85 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
86 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
87
88 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
89 sun_colour *= sun_shape;
90
91 fog_colour += sun_colour;
92 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
93 }
94
95 void main(){
96 depth_compare_dither();
97 compute_motion_vectors();
98
99 vec3 qnorm = aNorm;
100 vec3 diffuse = texture( uTexMain, aUv ).rgb;
101 vec3 composite = character_compute_lighting( diffuse, qnorm, aWorldCo, 1.0 );
102
103 float dist = distance( aWorldCo, uCamera ) - 0.08;
104 float opacity = clamp( dist*dist, 0.0, 1.0 );
105
106 oColour = vec4( composite, opacity );
107 }