]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/shadow.glsl
438af68d45181a86b6ec16f3cef065ba91ab88c4
[libs/gl.git] / shaderlib / shadow.glsl
1 import msp_interface;
2
3 struct ShadowParameters
4 {
5         int type;
6         float darkness;
7         int matrix_index;
8         vec4 region;
9         float bias;
10 };
11
12 uniform ShadowMap
13 {
14         ShadowParameters shadows[max_lights];
15         mat4 shd_world_matrix[max_lights];
16 };
17
18 uniform sampler2DShadow shadow_map;
19
20 layout(constant_id=auto) const bool use_shadow_map = false;
21
22 #pragma MSP stage(fragment)
23 virtual float get_shadow_factor(int index, vec4 world_pos)
24 {
25         if(use_shadow_map)
26         {
27                 int type = shadows[index].type;
28                 vec3 shadow_coord;
29                 if(type==1)
30                 {
31                         shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz;
32                         shadow_coord.z -= shadows[index].bias;
33                 }
34                 else
35                         return 1.0;
36
37                 if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1)
38                         return 1.0;
39
40                 vec4 region = shadows[index].region;
41                 float shadow_sample = texture(shadow_map, vec3(shadow_coord.xy*region.zw+region.xy, shadow_coord.z));
42                 return mix(1.0, shadow_sample, shadows[index].darkness);
43         }
44         else
45                 return 1.0;
46 }