7 flat in ivec4 light_indices;
9 uniform samplerBuffer uLightsArray;
10 uniform usampler3D uLightsIndex;
12 #include "common_world.glsl"
13 #include "light_clearskies.glsl"
15 float sdLine( vec3 p, vec3 a, vec3 b )
20 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
21 return length( pa - ba*h );
24 float compute_board_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;
31 return 1.0 - player_shadow*0.8;
34 vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )
36 float dist = pow(fdist*0.0010,0.78);
37 return mix( vfrag, colour, min( 1.0, dist ) );
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);
47 vec3 scene_calculate_light( int light_index,
48 vec3 halfview, vec3 co, vec3 normal )
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 );
54 vec3 light_delta = light_co.xyz-co;
55 float dist2 = dot(light_delta,light_delta);
57 light_delta = normalize( light_delta );
59 float quadratic = dist2*100.0;
60 float attenuation = 1.0f/( 1.0f + quadratic );
61 attenuation *= max( dot( light_delta, normal ), 0.0 );
63 float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
65 if( light_dir.w < 0.999999 )
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) );
71 return light_colour.rgb * attenuation * falloff;
74 vec3 scene_calculate_packed_light_patch( uint packed_index,
75 vec3 halfview, vec3 co, vec3 normal )
77 uint light_count = packed_index & 0x3u;
81 if( light_count >= 1u )
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 );
87 l += scene_calculate_light( index_0, halfview, co, normal );
89 if( light_count >= 2u )
91 l += scene_calculate_light( index_1, halfview, co, normal );
93 if( light_count >= 3u )
95 l += scene_calculate_light( index_2, halfview, co, normal );
103 vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )
106 scene_state( g_time, world );
109 vec3 halfview = uCamera - aWorldCo;
110 float fdist = length(halfview);
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();
118 total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ),
121 vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;
122 cube_coord = floor( cube_coord );
124 if( g_debug_indices == 1 )
126 return rand33(cube_coord);
129 if( g_debug_complexity == 1 )
131 ivec3 coord = ivec3( cube_coord );
132 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
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 );
138 // FIXME: this should absolutely must be clamped!
140 ivec3 coord = ivec3( cube_coord );
141 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
144 scene_calculate_packed_light_patch( index_sample.x,
145 halfview, aWorldCo, wnormal )
148 scene_calculate_packed_light_patch( index_sample.y,
149 halfview, aWorldCo, wnormal )
152 vec3 fog_colour = scene_sky( -halfview, world );
153 return scene_apply_fog( diffuse * total_light, fog_colour, fdist );