Advertisement
Zgragselus

Back projection soft shadows

May 23rd, 2022
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. float visibility = 1.0f;
  2. float searchTexelSpread = 1.0f;
  3. float oneTexelSize = lightsData[lightInput.id].shadowScale;
  4. float halfTexelSize = oneTexelSize * 0.5f;
  5. float2 ndcCurrentFragmentLightmapTexel = lNDC.xy;
  6. float lightSizeScale = 1.0f / lightsData[lightInput.id].data3.x;
  7. float near_plane = spotLight.shadowNear;
  8. float far_plane = spotLight.shadowFar;
  9. float lightmapFarPlaneSize = tan(spotLight.spotAngle * 0.5f) * 2.0f * far_plane;
  10. float oneFarPlaneTexelSize = lightmapFarPlaneSize * lightsData[lightInput.id].shadowScale;
  11.  
  12. float z = lNDC.z;
  13.  
  14. for (int i = -KERNEL_SEARCH_SIZE; i < KERNEL_SEARCH_SIZE; i++)
  15. {
  16. for (int j = -KERNEL_SEARCH_SIZE; j < KERNEL_SEARCH_SIZE; j++)
  17. {
  18. float sampleTexX = lNDC.x + oneTexelSize * searchTexelSpread * float(i);
  19. float sampleTexY = lNDC.y + oneTexelSize * searchTexelSpread * float(j);
  20.  
  21. float2 sampleCoords = float2(sampleTexX, sampleTexY);
  22.  
  23. float zs = shadowMap.SampleLevel(shadowSampler, sampleCoords.xy, 0.0f).x;
  24.  
  25. if (zs > z)
  26. {
  27. continue;
  28. }
  29.  
  30. float thisSampleZPlaneSize = lightmapFarPlaneSize * zs;
  31.  
  32. float2 ndcNextFragmentLightmapTexel = sampleCoords;
  33. float scaleFactor = z / (z - zs);
  34.  
  35. float left = (ndcNextFragmentLightmapTexel.x - ndcCurrentFragmentLightmapTexel.x - halfTexelSize) * thisSampleZPlaneSize * scaleFactor * lightSizeScale;
  36. float right = (ndcNextFragmentLightmapTexel.x - ndcCurrentFragmentLightmapTexel.x + halfTexelSize) * thisSampleZPlaneSize * scaleFactor * lightSizeScale;
  37. float top = (ndcNextFragmentLightmapTexel.y - ndcCurrentFragmentLightmapTexel.y + halfTexelSize) * thisSampleZPlaneSize * scaleFactor * lightSizeScale;
  38. float bottom = (ndcNextFragmentLightmapTexel.y - ndcCurrentFragmentLightmapTexel.y - halfTexelSize) * thisSampleZPlaneSize * scaleFactor * lightSizeScale;
  39.  
  40. left = clamp(left, -0.5f, 0.5f);
  41. right = clamp(right, -0.5f, 0.5f);
  42. top = clamp(top, -0.5f, 0.5f);
  43. bottom = clamp(bottom, -0.5f, 0.5f);
  44.  
  45. float width = right - left;
  46. float height = top - bottom;
  47.  
  48. float area = width * height;
  49.  
  50. visibility -= area * 1.0f;
  51. }
  52. }
  53.  
  54. if (visibility < 0.0f)
  55. {
  56. visibility = 0.0f;
  57. }
  58.  
  59. shadowMask *= visibility;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement