ReflectionCubemapCommon.bslinc 2.9 KB

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