ReflectionCubemapCommon.bslinc 2.3 KB

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