sampleOctahedralProjection.glsl 2.8 KB

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