91dc6a19cf7116cae9b1a1e5a110418ab4dbc1ae
[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 if( g_debug_indices == 1 )
76 {
77 float rings = min( fract(distance(light_co_0.xyz,aWorldCo)),
78 min( fract(distance(light_co_1.xyz,aWorldCo)),
79 fract(distance(light_co_2.xyz,aWorldCo)) )
80 );
81
82 return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),
83 fract(light_indices.z * 0.125 )) + (rings-0.5) * 0.25;
84 }
85
86 if( g_debug_complexity == 1 )
87 {
88 return vec3(1.0,0.0,0.0) * ( light_indices.w/3.0 );
89 }
90
91 if( light_indices.w >= 1 )
92 {
93 total_light += newlight_compute_spot
94 (
95 wnormal, halfview,
96 light_colour_0.rgb,
97 light_co_0.xyz,
98 light_dir_0
99 ) * board_shadow
100 * step( world.day_phase, light_colour_0.w );
101
102 if( light_indices.w >= 2 )
103 {
104 total_light += newlight_compute_spot
105 (
106 wnormal, halfview,
107 light_colour_1.rgb,
108 light_co_1.xyz,
109 light_dir_1
110 ) * board_shadow
111 * step( world.day_phase, light_colour_1.w );
112
113 if( light_indices.w >= 3 )
114 {
115 total_light += newlight_compute_spot
116 (
117 wnormal, halfview,
118 light_colour_2.rgb,
119 light_co_2.xyz,
120 light_dir_2
121 ) * board_shadow
122 * step( world.day_phase, light_colour_2.w );
123 }
124 }
125 }
126
127 vec3 fog_colour = scene_sky( -halfview, world );
128
129 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
130 }