ReflectionCubemapCommon.bslinc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(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.6. This gives us:
  39. // mipLevel = -1.35692 * log2( roughness);
  40. return max(0, (float)numMips - 1.0f - (-1.35692f * log2(roughness)));
  41. }
  42. /**
  43. * Calculates a roughness value from the provided mip level.
  44. *
  45. * @param mipLevel Mip level to determine roughness for.
  46. * @param numMips Total number of mip-map levels in the texture we'll be sampling from.
  47. * @return Roughness value for the specific mip level.
  48. */
  49. float mapMipLevelToRoughness(int mipLevel, int numMips)
  50. {
  51. // mapRoughnessToMipLevel() solved for roughness
  52. return saturate(exp2(((float)mipLevel - (float)numMips + 1.0f) / 1.35692));
  53. }
  54. };
  55. };
  56. };
  57. Technique : base("ReflectionCubemapCommon") =
  58. {
  59. Language = "GLSL";
  60. Pass =
  61. {
  62. Common =
  63. {
  64. vec3 getDirFromCubeFace(uint cubeFace, vec2 uv)
  65. {
  66. vec3 dir;
  67. if(cubeFace == 0)
  68. dir = vec3(1.0f, -uv.y, -uv.x);
  69. else if(cubeFace == 1)
  70. dir = vec3(-1.0f, -uv.y, uv.x);
  71. else if(cubeFace == 2)
  72. dir = vec3(uv.x, 1.0f, uv.y);
  73. else if(cubeFace == 3)
  74. dir = vec3(uv.x, -1.0f, -uv.y);
  75. else if(cubeFace == 4)
  76. dir = vec3(uv.x, -uv.y, 1.0f);
  77. else
  78. dir = vec3(-uv.x, -uv.y, -1.0f);
  79. return dir;
  80. }
  81. };
  82. };
  83. };