sampleOctahedralProjection.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "/**\n\
  3. * Samples the 4 neighboring pixels and return the weighted average.\n\
  4. *\n\
  5. * @private\n\
  6. */\n\
  7. vec3 czm_sampleOctahedralProjectionWithFiltering(sampler2D projectedMap, vec2 textureSize, vec3 direction, float lod)\n\
  8. {\n\
  9. direction /= dot(vec3(1.0), abs(direction));\n\
  10. vec2 rev = abs(direction.zx) - vec2(1.0);\n\
  11. vec2 neg = vec2(direction.x < 0.0 ? rev.x : -rev.x,\n\
  12. direction.z < 0.0 ? rev.y : -rev.y);\n\
  13. vec2 uv = direction.y < 0.0 ? neg : direction.xz;\n\
  14. vec2 coord = 0.5 * uv + vec2(0.5);\n\
  15. vec2 pixel = 1.0 / textureSize;\n\
  16. \n\
  17. if (lod > 0.0)\n\
  18. {\n\
  19. // Each subseqeuent mip level is half the size\n\
  20. float scale = 1.0 / pow(2.0, lod);\n\
  21. float offset = ((textureSize.y + 1.0) / textureSize.x);\n\
  22. \n\
  23. coord.x *= offset;\n\
  24. coord *= scale;\n\
  25. \n\
  26. coord.x += offset + pixel.x;\n\
  27. coord.y += (1.0 - (1.0 / pow(2.0, lod - 1.0))) + pixel.y * (lod - 1.0) * 2.0;\n\
  28. }\n\
  29. else\n\
  30. {\n\
  31. coord.x *= (textureSize.y / textureSize.x);\n\
  32. }\n\
  33. \n\
  34. // Do bilinear filtering\n\
  35. #ifndef OES_texture_float_linear\n\
  36. vec3 color1 = texture(projectedMap, coord + vec2(0.0, pixel.y)).rgb;\n\
  37. vec3 color2 = texture(projectedMap, coord + vec2(pixel.x, 0.0)).rgb;\n\
  38. vec3 color3 = texture(projectedMap, coord + pixel).rgb;\n\
  39. vec3 color4 = texture(projectedMap, coord).rgb;\n\
  40. \n\
  41. vec2 texturePosition = coord * textureSize;\n\
  42. \n\
  43. float fu = fract(texturePosition.x);\n\
  44. float fv = fract(texturePosition.y);\n\
  45. \n\
  46. vec3 average1 = mix(color4, color2, fu);\n\
  47. vec3 average2 = mix(color1, color3, fu);\n\
  48. \n\
  49. vec3 color = mix(average1, average2, fv);\n\
  50. #else\n\
  51. vec3 color = texture(projectedMap, coord).rgb;\n\
  52. #endif\n\
  53. \n\
  54. return color;\n\
  55. }\n\
  56. \n\
  57. \n\
  58. /**\n\
  59. * Samples from a cube map that has been projected using an octahedral projection from the given direction.\n\
  60. *\n\
  61. * @name czm_sampleOctahedralProjection\n\
  62. * @glslFunction\n\
  63. *\n\
  64. * @param {sampler2D} projectedMap The texture with the octahedral projected cube map.\n\
  65. * @param {vec2} textureSize The width and height dimensions in pixels of the projected map.\n\
  66. * @param {vec3} direction The normalized direction used to sample the cube map.\n\
  67. * @param {float} lod The level of detail to sample.\n\
  68. * @param {float} maxLod The maximum level of detail.\n\
  69. * @returns {vec3} The color of the cube map at the direction.\n\
  70. */\n\
  71. vec3 czm_sampleOctahedralProjection(sampler2D projectedMap, vec2 textureSize, vec3 direction, float lod, float maxLod) {\n\
  72. float currentLod = floor(lod + 0.5);\n\
  73. float nextLod = min(currentLod + 1.0, maxLod);\n\
  74. \n\
  75. vec3 colorCurrentLod = czm_sampleOctahedralProjectionWithFiltering(projectedMap, textureSize, direction, currentLod);\n\
  76. vec3 colorNextLod = czm_sampleOctahedralProjectionWithFiltering(projectedMap, textureSize, direction, nextLod);\n\
  77. \n\
  78. return mix(colorNextLod, colorCurrentLod, nextLod - lod);\n\
  79. }\n\
  80. ";