shadows.glsl (1686B)
1 2 float texture2DCompare(sampler2D depths, vec2 uv, float compare){ 3 float depth = texture2D(depths, uv).r; 4 return step(compare, depth); 5 } 6 7 float texture2DShadowLerp(sampler2D depths, vec2 size, vec2 uv, float compare){ 8 vec2 texelSize = vec2(1.0)/size; 9 vec2 f = fract(uv*size+0.5); 10 vec2 centroidUV = floor(uv*size+0.5)/size; 11 12 float lb = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 0.0), compare); 13 float lt = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 1.0), compare); 14 float rb = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 0.0), compare); 15 float rt = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 1.0), compare); 16 float a = mix(lb, lt, f.y); 17 float b = mix(rb, rt, f.y); 18 float c = mix(a, b, f.x); 19 return c; 20 } 21 22 float PCF(sampler2D depths, vec2 size, vec2 uv, float compare){ 23 float result = 0.0; 24 for(int x=-1; x<=1; x++){ 25 for(int y=-1; y<=1; y++){ 26 vec2 off = vec2(x,y)/size; 27 result += texture2DShadowLerp(depths, size, uv+off, compare); 28 } 29 } 30 return result/9.0; 31 } 32 33 vec3 shadow_strength(vec4 position, vec4 normal, vec4 v_shadow_coord) { 34 vec3 visibility = vec3(1.0); 35 vec4 shadow_sample = texture(shadow_map, v_shadow_coord.xy); 36 37 float light_angle = dot(light_dir, normal.xyz); 38 float bias = 0.002; 39 40 bool in_shadow = 41 shadow_sample.z < v_shadow_coord.z - bias 42 && shadow_sample.y < 1.0; 43 44 if (light_dir.z > 0.0 && in_shadow) { 45 float factor = 1.0-abs(light_angle)*0.2; 46 // float factor = 1.0; 47 visibility = vec3(1.0)*factor; 48 // visibility = shadow_sample; 49 } 50 51 // visibility += normal_offset_scale; 52 53 54 return visibility; 55 }