Advertisement
Guest User

Untitled

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