ReflectionCubemapCommon.bslinc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. mixin ReflectionCubemapCommon
  2. {
  3. code
  4. {
  5. float3 getDirFromCubeFace(uint cubeFace, float2 uv)
  6. {
  7. float3 dir;
  8. if(cubeFace == 0)
  9. dir = float3(1.0f, -uv.y, -uv.x);
  10. else if(cubeFace == 1)
  11. dir = float3(-1.0f, -uv.y, uv.x);
  12. else if(cubeFace == 2)
  13. dir = float3(uv.x, 1.0f, uv.y);
  14. else if(cubeFace == 3)
  15. dir = float3(uv.x, -1.0f, -uv.y);
  16. else if(cubeFace == 4)
  17. dir = float3(uv.x, -uv.y, 1.0f);
  18. else
  19. dir = float3(-uv.x, -uv.y, -1.0f);
  20. return dir;
  21. }
  22. /**
  23. * Calculates a mip level to sample from based on roughness value.
  24. *
  25. * @param roughness Roughness in range [0, 1]. Higher values yield more roughness.
  26. * @param numMips Total number of mip-map levels in the texture we'll be sampling from.
  27. * @return Index of the mipmap level to sample.
  28. */
  29. float mapRoughnessToMipLevel(float roughness, int numMips)
  30. {
  31. // We use the following equation:
  32. // mipLevel = log10(1 - roughness) / log10(dropPercent)
  33. //
  34. // Where dropPercent represent by what % to drop the roughness with each mip level.
  35. // We convert to log2 and a assume a drop percent value of 0.7. This gives us:
  36. // mipLevel = -2.8 * log2(1 - roughness);
  37. // Note: Another value that could be used is drop 0.6, which yields a multiply by -1.35692.
  38. // This more accurately covers the mip range, but early mip levels end up being too smooth,
  39. // and benefits from our cubemap importance sampling strategy seem to be lost as most samples
  40. // fall within one pixel, resulting in same effect as just trivially downsampling. With 0.7 drop
  41. // the roughness increases too early and higher mip levels don't cover the full [0, 1] range. Which
  42. // is better depends on what looks better.
  43. return max(0, -2.8f * log2(1.0f - roughness));
  44. }
  45. /**
  46. * Calculates a roughness value from the provided mip level.
  47. *
  48. * @param mipLevel Mip level to determine roughness for.
  49. * @param numMips Total number of mip-map levels in the texture we'll be sampling from.
  50. * @return Roughness value for the specific mip level.
  51. */
  52. float mapMipLevelToRoughness(int mipLevel, int numMips)
  53. {
  54. // mapRoughnessToMipLevel() solved for roughness
  55. return 1 - exp2((float)mipLevel / -2.8f);
  56. }
  57. };
  58. };