]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/shadow.glsl
Check the flat qualifier from the correct member
[libs/gl.git] / shaderlib / shadow.glsl
1 import lighting;
2
3 struct ShadowParameters
4 {
5         int type;
6         float darkness;
7         int matrix_index;
8         vec4 region;
9         vec2 bias;
10 };
11
12 layout(set=0) uniform ShadowMap
13 {
14         ShadowParameters shadows[max_lights];
15         mat4 shd_world_matrix[max_lights*4];
16 };
17
18 layout(set=0) 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.x;
33                 }
34                 else if(type==2)
35                 {
36                         int base = shadows[index].matrix_index;
37                         vec4 clip_coord = shd_world_matrix[base]*world_pos;
38                         for(int i=1; i<4; ++i)
39                         {
40                                 vec4 c = shd_world_matrix[base+i]*world_pos;
41                                 if(c.w>clip_coord.w)
42                                         clip_coord = c;
43                         }
44                         shadow_coord = clip_coord.xyz/clip_coord.w;
45                         vec2 bias = shadows[index].bias;
46                         shadow_coord.z = (shadow_coord.z+bias.x)/bias.y-bias.x;
47                 }
48                 else
49                         return 1.0;
50
51                 if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1)
52                         return 1.0;
53
54                 vec4 region = shadows[index].region;
55                 float shadow_sample = texture(shadow_map, vec3(shadow_coord.xy*region.zw+region.xy, min(shadow_coord.z, 1.0)));
56                 return mix(1.0, shadow_sample, shadows[index].darkness);
57         }
58         else
59                 return 1.0;
60 }