needs a lot of cleaning but lights are OK
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_scene.glsl
1 // :D
2
3 in vec2 aUv;
4 in vec4 aNorm;
5 in vec3 aCo;
6 in vec3 aWorldCo;
7 flat in ivec4 light_indices;
8
9 uniform samplerBuffer uLightsArray;
10
11 #include "common_world.glsl"
12 #include "light_clearskies.glsl"
13
14 float sdLine( vec3 p, vec3 a, vec3 b )
15 {
16 vec3 pa = p - a;
17 vec3 ba = b - a;
18
19 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
20 return length( pa - ba*h );
21 }
22
23 float compute_board_shadow()
24 {
25 // player shadow
26 float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );
27 float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );
28 player_shadow *= player_shadow*player_shadow*player_shadow;
29
30 return 1.0 - player_shadow*0.8;
31 }
32
33 vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )
34 {
35 float dist = pow(fdist*0.0010,0.78);
36 return mix( vfrag, colour, min( 1.0, dist ) );
37 }
38
39 vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )
40 {
41 world_info world;
42 scene_state( g_time, world );
43
44 // Lighting
45 vec3 halfview = uCamera - aWorldCo;
46 float fdist = length(halfview);
47 halfview /= fdist;
48
49 vec3 total_light = vec3(0.0);
50
51
52 float world_shadow = newlight_compute_sun_shadow( world.sun_dir
53 * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );
54 float board_shadow = compute_board_shadow();
55
56 total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ),
57 halfview, world );
58
59 //total_light += scene_lighting_old( wnormal, world );
60
61 // Compute the other lights that exist in the map, not effected by the sun
62 // shadow
63
64 // read lights
65 vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );
66 vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );
67 vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );
68 vec4 light_co_0 = texelFetch( uLightsArray, light_indices.x*3+1 );
69 vec4 light_co_1 = texelFetch( uLightsArray, light_indices.y*3+1 );
70 vec4 light_co_2 = texelFetch( uLightsArray, light_indices.z*3+1 );
71 vec4 light_dir_0 = texelFetch( uLightsArray, light_indices.x*3+2 );
72 vec4 light_dir_1 = texelFetch( uLightsArray, light_indices.y*3+2 );
73 vec4 light_dir_2 = texelFetch( uLightsArray, light_indices.z*3+2 );
74
75 //return vec3(fract(distance(light_co_0.xyz,aWorldCo)),
76 // fract(distance(light_co_1.xyz,aWorldCo)),
77 // fract(distance(light_co_2.xyz,aWorldCo)));
78
79 // return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),
80 // fract(light_indices.z * 0.125 ));
81
82 total_light += newlight_compute_spot
83 (
84 wnormal, halfview,
85 light_colour_0.rgb,
86 light_co_0.xyz,
87 light_dir_0
88 ) * board_shadow;
89
90 total_light += newlight_compute_spot
91 (
92 wnormal, halfview,
93 light_colour_1.rgb,
94 light_co_1.xyz,
95 light_dir_1
96 ) * board_shadow;
97 total_light += newlight_compute_spot
98 (
99 wnormal, halfview,
100 light_colour_2.rgb,
101 light_co_2.xyz,
102 light_dir_2
103 ) * board_shadow;
104
105 vec3 fog_colour = scene_sky( -halfview, world );
106
107 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
108 }