GroundAtmosphere.glsl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. * Atmosphere code:
  3. *
  4. * Copyright (c) 2000-2005, Sean O'Neil (s_p_oneil@hotmail.com)
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * * Neither the name of the project nor the names of its contributors may be
  17. * used to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * Modifications made by Analytical Graphics, Inc.
  32. */
  33. // Atmosphere:
  34. // Code: http://sponeil.net/
  35. // GPU Gems 2 Article: https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html
  36. const float fInnerRadius = 6378137.0;
  37. const float fOuterRadius = 6378137.0 * 1.025;
  38. const float fOuterRadius2 = fOuterRadius * fOuterRadius;
  39. const float Kr = 0.0025;
  40. const float Km = 0.0015;
  41. const float ESun = 15.0;
  42. const float fKrESun = Kr * ESun;
  43. const float fKmESun = Km * ESun;
  44. const float fKr4PI = Kr * 4.0 * czm_pi;
  45. const float fKm4PI = Km * 4.0 * czm_pi;
  46. const float fScale = 1.0 / (fOuterRadius - fInnerRadius);
  47. const float fScaleDepth = 0.25;
  48. const float fScaleOverScaleDepth = fScale / fScaleDepth;
  49. struct AtmosphereColor
  50. {
  51. vec3 mie;
  52. vec3 rayleigh;
  53. };
  54. const int nSamples = 2;
  55. const float fSamples = 2.0;
  56. float scale(float fCos)
  57. {
  58. float x = 1.0 - fCos;
  59. return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
  60. }
  61. AtmosphereColor computeGroundAtmosphereFromSpace(vec3 v3Pos, bool useSunLighting)
  62. {
  63. vec3 v3InvWavelength = vec3(1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0));
  64. // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
  65. vec3 v3Ray = v3Pos - czm_viewerPositionWC;
  66. float fFar = length(v3Ray);
  67. v3Ray /= fFar;
  68. float fCameraHeight = length(czm_viewerPositionWC);
  69. float fCameraHeight2 = fCameraHeight * fCameraHeight;
  70. // This next line is an ANGLE workaround. It is equivalent to B = 2.0 * dot(czm_viewerPositionWC, v3Ray),
  71. // which is what it should be, but there are problems at the poles.
  72. float B = 2.0 * length(czm_viewerPositionWC) * dot(normalize(czm_viewerPositionWC), v3Ray);
  73. float C = fCameraHeight2 - fOuterRadius2;
  74. float fDet = max(0.0, B*B - 4.0 * C);
  75. float fNear = 0.5 * (-B - sqrt(fDet));
  76. // Calculate the ray's starting position, then calculate its scattering offset
  77. vec3 v3Start = czm_viewerPositionWC + v3Ray * fNear;
  78. fFar -= fNear;
  79. float fDepth = exp((fInnerRadius - fOuterRadius) / fScaleDepth);
  80. // The light angle based on the sun position would be:
  81. // dot(czm_sunDirectionWC, v3Pos) / length(v3Pos);
  82. // When we want the atmosphere to be uniform over the globe so it is set to 1.0.
  83. float fLightAngle = czm_branchFreeTernary(useSunLighting, dot(czm_sunDirectionWC, v3Pos) / length(v3Pos), 1.0);
  84. float fCameraAngle = dot(-v3Ray, v3Pos) / length(v3Pos);
  85. float fCameraScale = scale(fCameraAngle);
  86. float fLightScale = scale(fLightAngle);
  87. float fCameraOffset = fDepth*fCameraScale;
  88. float fTemp = (fLightScale + fCameraScale);
  89. // Initialize the scattering loop variables
  90. float fSampleLength = fFar / fSamples;
  91. float fScaledLength = fSampleLength * fScale;
  92. vec3 v3SampleRay = v3Ray * fSampleLength;
  93. vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
  94. // Now loop through the sample rays
  95. vec3 v3FrontColor = vec3(0.0);
  96. vec3 v3Attenuate = vec3(0.0);
  97. for(int i=0; i<nSamples; i++)
  98. {
  99. float fHeight = length(v3SamplePoint);
  100. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
  101. float fScatter = fDepth*fTemp - fCameraOffset;
  102. v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
  103. v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
  104. v3SamplePoint += v3SampleRay;
  105. }
  106. AtmosphereColor color;
  107. color.mie = v3FrontColor * (v3InvWavelength * fKrESun + fKmESun);
  108. color.rayleigh = v3Attenuate; // Calculate the attenuation factor for the ground
  109. return color;
  110. }