Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float ShadowCalculation(vec3 fragPos, uint index, vec3 lightpos)
- {
- // get vector between fragment position and light position
- vec3 fragToLight = vec3(inverse(view) * vec4(fragPos, 1.0)) - vec3(inverse(view) * vec4(lightpos, 1.0));
- // ise the fragment to light vector to sample from the depth map
- float closestDepth = texture(depthMap, vec4(fragToLight, index)).r;
- // it is currently in linear range between [0,1], let's re-transform it back to original depth value
- closestDepth *= far_plane;
- // now get current linear depth as the length between the fragment and light position
- float currentDepth = length(fragToLight);
- // test for shadows
- float bias = 0.15; // we use a much larger bias since depth is now in [near_plane, far_plane] range
- float shadow = 0.0;
- // display closestDepth as debug (to visualize depth cubemap)
- // FragColor = vec4(vec3(closestDepth / far_plane), 1.0);
- float samples = 20.0;
- float offset = 0.1;
- vec3 sampleOffsetDirections[20] = vec3[]
- (
- vec3( 1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
- vec3( 1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
- vec3( 1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
- vec3( 1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
- vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1)
- );
- float diskRadius = 0.05;
- for(int i = 0; i < samples; ++i)
- {
- float closestDepth = texture(depthMap, vec4(fragToLight + sampleOffsetDirections[i] * diskRadius, index)).r;
- closestDepth *= far_plane; // undo mapping [0;1]
- if(currentDepth - bias > closestDepth)
- shadow += 1.0;
- }
- shadow /= float(samples);
- return shadow;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement