LightShadingApplyFog.ankiprog 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki technique vert pixel
  6. #include <AnKi/Shaders/QuadVert.hlsl>
  7. #if ANKI_PIXEL_SHADER
  8. # include <AnKi/Shaders/Functions.hlsl>
  9. SamplerState g_nearestAnyClampSampler : register(s0);
  10. SamplerState g_linearAnyClampSampler : register(s1);
  11. Texture2D g_depthRt : register(t0);
  12. Texture3D<Vec4> g_fogVolume : register(t1);
  13. struct Constants
  14. {
  15. F32 m_zSplitCount;
  16. F32 m_finalZSplit;
  17. F32 m_near;
  18. F32 m_far;
  19. };
  20. ANKI_FAST_CONSTANTS(Constants, g_consts)
  21. Vec4 main(VertOut input) : SV_TARGET0
  22. {
  23. const Vec2 uv = input.m_uv;
  24. Vec3 uvw;
  25. // Compute W coordinate
  26. const F32 depth = g_depthRt.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).r;
  27. const F32 linearDepth = linearizeDepth(depth, g_consts.m_near, g_consts.m_far);
  28. uvw.z = linearDepth * (g_consts.m_zSplitCount / (g_consts.m_finalZSplit + 1.0f));
  29. // Compute UV coordinates
  30. uvw.xy = uv;
  31. // Read the volume
  32. const Vec4 fogVals = g_fogVolume.SampleLevel(g_linearAnyClampSampler, uvw, 0.0);
  33. const Vec3 inScattering = fogVals.rgb;
  34. const F32 transmittance = fogVals.a;
  35. // Apply the fog
  36. return Vec4(inScattering, transmittance);
  37. }
  38. #endif // ANKI_PIXEL_SHADER