Debian Codegraph @

libs/opengl/shaders/shadow-calculation.f.glsl

raw
1 R"XXX(#version 300 es
2
3 // "#include" file for shadow calculation in fragment shaders (2nd pass)
4 // Jose Luis Blanco Claraco (C) 2019-2023
5 // Part of the MRPT project
6
7 uniform highp sampler2D shadowMap;
8 uniform highp vec3 light_direction;
9
10 uniform highp float shadow_bias, shadow_bias_cam2frag, shadow_bias_normal;
11
12 mediump float ShadowCalculation(
13 highp vec4 fragPosLightSpace,
14 mediump vec3 normal,
15 mediump float cam2fragDist)
16 {
17 // perform perspective divide
18 highp vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
19
20 // transform to [0,1] range
21 projCoords = projCoords * 0.5 + 0.5;
22
23 if(projCoords.z > 1.0) return 0.0;
24
25 // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
26 highp float closestDepth = texture(shadowMap, projCoords.xy).r;
27 // get depth of current fragment from light's perspective
28 highp float currentDepth = projCoords.z;
29
30 // check whether current frag pos is in shadow1D
31 highp float bias = shadow_bias + shadow_bias_cam2frag*cam2fragDist + shadow_bias_normal*(1.0-max(0.0,dot(normal, light_direction)));
32 #if 0
33 mediump float shadow = currentDepth-bias > closestDepth ? 1.0 : 0.0;
34 #else
35 mediump float shadow = 0.0f;
36 mediump vec2 texelSize = 1.0 / vec2(textureSize(shadowMap, 0));
37 for(int x = -1; x <= 1; ++x)
38 {
39 for(int y = -1; y <= 1; ++y)
40 {
41 highp float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
42 shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
43 }
44 }
45 shadow /= 9.0;
46 #endif
47 return shadow;
48 }
49 )XXX"