grid based
[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 uniform usampler3D uLightsIndex;
11
12 #include "common_world.glsl"
13 #include "light_clearskies.glsl"
14
15 float sdLine( vec3 p, vec3 a, vec3 b )
16 {
17 vec3 pa = p - a;
18 vec3 ba = b - a;
19
20 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
21 return length( pa - ba*h );
22 }
23
24 float compute_board_shadow()
25 {
26 // player shadow
27 float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );
28 float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );
29 player_shadow *= player_shadow*player_shadow*player_shadow;
30
31 return 1.0 - player_shadow*0.8;
32 }
33
34 vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )
35 {
36 float dist = pow(fdist*0.0010,0.78);
37 return mix( vfrag, colour, min( 1.0, dist ) );
38 }
39
40 vec3 rand33(vec3 p3)
41 {
42 p3 = fract(p3 * vec3(.1031, .1030, .0973));
43 p3 += dot(p3, p3.yxz+33.33);
44 return fract((p3.xxy + p3.yxx)*p3.zyx);
45 }
46
47 vec3 scene_calculate_light( int light_index,
48 vec3 halfview, vec3 co, vec3 normal )
49 {
50 vec4 light_colour = texelFetch( uLightsArray, light_index+0 );
51 vec4 light_co = texelFetch( uLightsArray, light_index+1 );
52 vec4 light_dir = texelFetch( uLightsArray, light_index+2 );
53
54 vec3 light_delta = light_co.xyz-co;
55 float dist2 = dot(light_delta,light_delta);
56
57 light_delta = normalize( light_delta );
58
59 float quadratic = dist2*100.0;
60 float attenuation = 1.0f/( 1.0f + quadratic );
61 attenuation *= max( dot( light_delta, normal ), 0.0 );
62
63 float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
64
65 if( light_dir.w < 0.999999 )
66 {
67 float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );
68 falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );
69 }
70
71 return light_colour.rgb * attenuation * falloff;
72 }
73
74 vec3 scene_calculate_packed_light_patch( uint packed_index,
75 vec3 halfview, vec3 co, vec3 normal )
76 {
77 uint light_count = packed_index & 0x3u;
78
79 vec3 l = vec3(0.0);
80
81 if( light_count >= 1u )
82 {
83 int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );
84 int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );
85 int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );
86
87 l += scene_calculate_light( index_0, halfview, co, normal );
88
89 if( light_count >= 2u )
90 {
91 l += scene_calculate_light( index_1, halfview, co, normal );
92
93 if( light_count >= 3u )
94 {
95 l += scene_calculate_light( index_2, halfview, co, normal );
96 }
97 }
98 }
99
100 return l;
101 }
102
103 vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )
104 {
105 world_info world;
106 scene_state( g_time, world );
107
108 // Lighting
109 vec3 halfview = uCamera - aWorldCo;
110 float fdist = length(halfview);
111 halfview /= fdist;
112
113 vec3 total_light = vec3(0.0);
114 float world_shadow = newlight_compute_sun_shadow( world.sun_dir
115 * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );
116 float board_shadow = compute_board_shadow();
117
118 total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ),
119 halfview, world );
120
121 vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;
122 cube_coord = floor( cube_coord );
123
124 if( g_debug_indices == 1 )
125 {
126 return rand33(cube_coord);
127 }
128
129 if( g_debug_complexity == 1 )
130 {
131 ivec3 coord = ivec3( cube_coord );
132 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
133
134 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
135 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
136 }
137
138 // FIXME: this should absolutely must be clamped!
139
140 ivec3 coord = ivec3( cube_coord );
141 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
142
143 total_light +=
144 scene_calculate_packed_light_patch( index_sample.x,
145 halfview, aWorldCo, wnormal )
146 * board_shadow;
147 total_light +=
148 scene_calculate_packed_light_patch( index_sample.y,
149 halfview, aWorldCo, wnormal )
150 * board_shadow;
151
152 vec3 fog_colour = scene_sky( -halfview, world );
153 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
154 }