SkyAtmosphereFS.glsl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license
  3. * Copyright (c) 2000-2005, Sean O'Neil (s_p_oneil@hotmail.com)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * * Neither the name of the project nor the names of its contributors may be
  16. * used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Modifications made by Analytical Graphics, Inc.
  31. */
  32. // Code: http://sponeil.net/
  33. // GPU Gems 2 Article: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter16.html
  34. const float g = -0.95;
  35. const float g2 = g * g;
  36. varying vec3 v_rayleighColor;
  37. varying vec3 v_mieColor;
  38. varying vec3 v_toCamera;
  39. varying vec3 v_positionEC;
  40. void main (void)
  41. {
  42. // TODO: make arbitrary ellipsoid
  43. czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();
  44. vec3 direction = normalize(v_positionEC);
  45. czm_ray ray = czm_ray(vec3(0.0), direction);
  46. czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);
  47. if (!czm_isEmpty(intersection)) {
  48. discard;
  49. }
  50. // Extra normalize added for Android
  51. float fCos = dot(czm_sunDirectionWC, normalize(v_toCamera)) / length(v_toCamera);
  52. float fRayleighPhase = 0.75 * (1.0 + fCos*fCos);
  53. float fMiePhase = 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos*fCos) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
  54. const float fExposure = 2.0;
  55. vec3 rgb = fRayleighPhase * v_rayleighColor + fMiePhase * v_mieColor;
  56. rgb = vec3(1.0) - exp(-fExposure * rgb);
  57. float l = czm_luminance(rgb);
  58. gl_FragColor = vec4(rgb, min(smoothstep(0.0, 0.1, l), 1.0) * smoothstep(0.0, 1.0, czm_morphTime));
  59. }