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