Advertisement
Guest User

screen_space_shadow

a guest
Sep 7th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 450
  2.  
  3. layout(binding = 0) uniform CameraProjection {
  4.     mat4 model;
  5.     mat4 view;
  6.     mat4 proj;
  7. }camera;
  8.  
  9. layout(binding = 4) uniform Light {
  10.     vec4 LightPosition;
  11.     vec4 ObjectColor;
  12.     vec4 LightColor;
  13.     vec4 LightViewPosition;
  14.     mat4 LightSpaceMatrix;
  15.     vec4 CameraPosition;
  16. }light;
  17.  
  18. layout(binding = 1) uniform sampler2D gPosition;
  19. layout(binding = 2) uniform sampler2D gNormal;
  20. layout(binding = 3) uniform sampler2D depthMap;
  21.  
  22. layout(location = 0) in vec3 fragColor;
  23. layout(location = 1) in vec2 uvCoords;
  24.  
  25. layout(location = 0) out vec4 outColor;
  26.  
  27. uint MAX_STEPS = 12;
  28. float MAX_DISTANCE = 0.05;
  29. float THICKNESS = 0.05;
  30. float STEP_LENGTH = MAX_DISTANCE / float(MAX_STEPS);
  31. float MAX_DELTA_FROM_ORIGINAL_DEPTH = 0.005; //max depth variation from origina pixel
  32. const float zNear = 0.1;
  33. const float zFar = 1000.0;
  34.  
  35. float linearize_depth(float d)
  36. {
  37.     return zNear * zFar / (zFar + d * (zNear - zFar));
  38. }
  39.  
  40. vec2 ViewToScreen(vec3 position)
  41. {
  42.     vec4 project = camera.proj * vec4(position, 1.0);
  43.     project.xy /= project.w;
  44.     vec2 screen = project.xy * 0.5 + 0.5;
  45.     return screen;
  46. }
  47.  
  48. bool ValidRay(vec2 uv)
  49. {
  50.     float bias = 0.0001;
  51.     if(uv.x > bias && uv.x < 1.0 - bias && uv.y > bias && uv.y < 1.0 - bias){
  52.         return true;
  53.     }
  54.  
  55.     return false;
  56. }
  57.  
  58. float computeScreenSpaceShadow()
  59. {
  60.     vec3 FragPos = texture(gPosition, uvCoords).rgb;
  61.     vec4 ViewSpaceLightPosition = camera.view * light.LightPosition;
  62.     vec3 LightDirection = ViewSpaceLightPosition.xyz - FragPos.xyz;
  63.        
  64.     // Ray position and direction in view-space.
  65.     vec3 RayPos = texture(gPosition, uvCoords).xyz; // ray start position
  66.     vec3 RayDirection = normalize(-LightDirection.xyz);
  67.    
  68.     // Save original depth of the position
  69.     float DepthOriginal = RayPos.z;
  70.     // Ray step
  71.     vec3 RayStep = RayDirection * STEP_LENGTH;
  72.    
  73.     float occlusion = 0.0;
  74.  
  75.     for(uint i = 0; i < MAX_STEPS; i++)
  76.     {
  77.         RayPos += RayStep;
  78.         vec2 Ray_UV = ViewToScreen(RayPos);
  79.  
  80.  
  81.         // Make sure the UV is inside screen-space
  82.         if(!ValidRay(Ray_UV)){
  83.             return 1.0;
  84.         }
  85.        
  86.         // Compute difference between ray and cameras depth
  87.         float DepthZ = linearize_depth(texture(depthMap, Ray_UV).x);
  88.         float DepthDelta = RayPos.z - DepthZ;
  89.  
  90.         // Check if camera cannot see the ray. Ray depth must be larger than camera depth = positive delta
  91.         bool canCameraSeeRay = (DepthDelta > 0.0) && (DepthDelta < THICKNESS);
  92.         bool occludedByOriginalPixel = abs(RayPos.z - DepthOriginal) < MAX_DELTA_FROM_ORIGINAL_DEPTH;
  93.         if(canCameraSeeRay && occludedByOriginalPixel)
  94.         {
  95.             // Mark as occluded
  96.             occlusion = 1.0;
  97.  
  98.             break;
  99.         }
  100.     }
  101.     return 1.0 - occlusion;
  102. }
  103.  
  104.  
  105. void main()
  106. {
  107.     outColor = vec4(vec3(computeScreenSpaceShadow()), 1.0);
  108. }
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement