LightShadingSkybox.ankiprog 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 mutator METHOD 0 1 2 // 0: solid colod, 1: 2D image, 2: generated
  6. #pragma anki technique vert pixel
  7. #define CUSTOM_DEPTH 1.0
  8. #include <AnKi/Shaders/QuadVert.hlsl>
  9. #if ANKI_PIXEL_SHADER
  10. # include <AnKi/Shaders/Include/MiscRendererTypes.h>
  11. # include <AnKi/Shaders/Functions.hlsl>
  12. # include <AnKi/Shaders/TonemappingFunctions.hlsl>
  13. # include <AnKi/Shaders/Sky.hlsl>
  14. # if METHOD == 0
  15. struct Constants
  16. {
  17. Vec3 m_solidColor;
  18. F32 m_padding;
  19. };
  20. ANKI_FAST_CONSTANTS(Constants, g_consts)
  21. # elif METHOD == 1
  22. SamplerState g_trilinearAnySampler : register(s0);
  23. Texture2D<Vec4> g_envMapTex : register(t0);
  24. struct Constants
  25. {
  26. Mat4 m_invertedViewProjectionJitterMat;
  27. Vec3 m_cameraPos;
  28. F32 m_padding;
  29. Vec3 m_scale;
  30. F32 m_padding1;
  31. Vec3 m_bias;
  32. F32 m_padding2;
  33. };
  34. ANKI_FAST_CONSTANTS(Constants, g_consts)
  35. # else
  36. SamplerState g_linearAnyClampSampler : register(s0);
  37. Texture2D<Vec4> g_skyLut : register(t0);
  38. ConstantBuffer<GlobalRendererConstants> g_consts : register(b0);
  39. # endif
  40. Vec4 main(VertOut input) : SV_TARGET0
  41. {
  42. const Vec2 uv = input.m_uv;
  43. # if METHOD == 0
  44. ANKI_MAYBE_UNUSED(uv);
  45. const Vec3 output = g_consts.m_solidColor;
  46. # elif METHOD == 1
  47. const F32 depth = 1.0;
  48. const Vec2 ndc = uvToNdc(uv);
  49. const Vec4 worldPos4 = mul(g_consts.m_invertedViewProjectionJitterMat, Vec4(ndc, depth, 1.0));
  50. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  51. const Vec3 eyeToFrag = normalize(worldPos - g_consts.m_cameraPos);
  52. const Vec2 uv3 = equirectangularMapping(eyeToFrag);
  53. // When uv is close to the edge of the texture the other quads might be in the oposit coordinate. Then the
  54. // derivatives will be huge causing the texture to use the highest mip and thus create a visible seam. To fix this
  55. // find when the derivatives are large and do some manual work to fix it
  56. const Vec2 dx = abs(ddx_coarse(uv3));
  57. const F32 maxD = max(dx.x, dx.y);
  58. const F32 bias = (maxD > 0.9) ? -100.0f : 0.0f;
  59. const Vec3 output = g_envMapTex.SampleBias(g_trilinearAnySampler, uv3, bias).rgb * g_consts.m_scale + g_consts.m_bias;
  60. # else
  61. const F32 depth = 1.0;
  62. const Vec2 ndc = uvToNdc(uv);
  63. const Vec4 worldPos4 = mul(g_consts.m_matrices.m_invertedViewProjectionJitter, Vec4(ndc, depth, 1.0));
  64. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  65. const Vec3 eyeToFrag = normalize(worldPos - g_consts.m_cameraPosition);
  66. const Vec3 output = computeSkyColor(g_skyLut, g_linearAnyClampSampler, eyeToFrag, -g_consts.m_directionalLight.m_direction,
  67. g_consts.m_directionalLight.m_power, true);
  68. # endif
  69. return Vec4(output, 0.0);
  70. }
  71. #endif // ANKI_PIXEL_SHADER