Advertisement
Guest User

Untitled

a guest
Jun 15th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. float ShadowCalculation(vec3 fragPos, uint index, vec3 lightpos)
  2. {
  3. // get vector between fragment position and light position
  4. vec3 fragToLight = vec3(inverse(view) * vec4(fragPos, 1.0)) - vec3(inverse(view) * vec4(lightpos, 1.0));
  5. // ise the fragment to light vector to sample from the depth map
  6. float closestDepth = texture(depthMap, vec4(fragToLight, index)).r;
  7. // it is currently in linear range between [0,1], let's re-transform it back to original depth value
  8. closestDepth *= far_plane;
  9. // now get current linear depth as the length between the fragment and light position
  10. float currentDepth = length(fragToLight);
  11. // test for shadows
  12. float bias = 0.15; // we use a much larger bias since depth is now in [near_plane, far_plane] range
  13. float shadow = 0.0;
  14. // display closestDepth as debug (to visualize depth cubemap)
  15. // FragColor = vec4(vec3(closestDepth / far_plane), 1.0);
  16. float samples = 20.0;
  17. float offset = 0.1;
  18.  
  19. vec3 sampleOffsetDirections[20] = vec3[]
  20. (
  21. vec3( 1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
  22. vec3( 1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
  23. vec3( 1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
  24. vec3( 1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
  25. vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1)
  26. );
  27. float diskRadius = 0.05;
  28.  
  29. for(int i = 0; i < samples; ++i)
  30. {
  31. float closestDepth = texture(depthMap, vec4(fragToLight + sampleOffsetDirections[i] * diskRadius, index)).r;
  32. closestDepth *= far_plane; // undo mapping [0;1]
  33. if(currentDepth - bias > closestDepth)
  34. shadow += 1.0;
  35. }
  36. shadow /= float(samples);
  37. return shadow;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement