GroundAtmosphere.glsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Kr = 0.0025;
  37. const float Km = 0.0015;
  38. const float ESun = 15.0;
  39. const float fKrESun = Kr * ESun;
  40. const float fKmESun = Km * ESun;
  41. const float fKr4PI = Kr * 4.0 * czm_pi;
  42. const float fKm4PI = Km * 4.0 * czm_pi;
  43. // Original: vec3(1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0));
  44. const vec3 v3InvWavelength = vec3(5.60204474633241, 9.473284437923038, 19.64380261047721);
  45. const float fScaleDepth = 0.25;
  46. struct AtmosphereColor
  47. {
  48. vec3 mie;
  49. vec3 rayleigh;
  50. };
  51. const int nSamples = 2;
  52. const float fSamples = 2.0;
  53. float scale(float fCos)
  54. {
  55. float x = 1.0 - fCos;
  56. return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
  57. }
  58. AtmosphereColor computeGroundAtmosphereFromSpace(vec3 v3Pos, bool dynamicLighting, vec3 lightDirectionWC)
  59. {
  60. float fInnerRadius = czm_ellipsoidRadii.x;
  61. float fOuterRadius = czm_ellipsoidRadii.x * 1.025;
  62. float fOuterRadius2 = fOuterRadius * fOuterRadius;
  63. float fScale = 1.0 / (fOuterRadius - fInnerRadius);
  64. float fScaleOverScaleDepth = fScale / fScaleDepth;
  65. // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
  66. vec3 v3Ray = v3Pos - czm_viewerPositionWC;
  67. float fFar = length(v3Ray);
  68. v3Ray /= fFar;
  69. float fCameraHeight = length(czm_viewerPositionWC);
  70. float fCameraHeight2 = fCameraHeight * fCameraHeight;
  71. // This next line is an ANGLE workaround. It is equivalent to B = 2.0 * dot(czm_viewerPositionWC, v3Ray),
  72. // which is what it should be, but there are problems at the poles.
  73. float B = 2.0 * length(czm_viewerPositionWC) * dot(normalize(czm_viewerPositionWC), v3Ray);
  74. float C = fCameraHeight2 - fOuterRadius2;
  75. float fDet = max(0.0, B*B - 4.0 * C);
  76. float fNear = 0.5 * (-B - sqrt(fDet));
  77. // Calculate the ray's starting position, then calculate its scattering offset
  78. vec3 v3Start = czm_viewerPositionWC + v3Ray * fNear;
  79. fFar -= fNear;
  80. float fDepth = exp((fInnerRadius - fOuterRadius) / fScaleDepth);
  81. // The light angle based on the scene's light source would be:
  82. // dot(lightDirectionWC, v3Pos) / length(v3Pos);
  83. // When we want the atmosphere to be uniform over the globe so it is set to 1.0.
  84. float fLightAngle = czm_branchFreeTernary(dynamicLighting, dot(lightDirectionWC, v3Pos) / length(v3Pos), 1.0);
  85. float fCameraAngle = dot(-v3Ray, v3Pos) / length(v3Pos);
  86. float fCameraScale = scale(fCameraAngle);
  87. float fLightScale = scale(fLightAngle);
  88. float fCameraOffset = fDepth*fCameraScale;
  89. float fTemp = (fLightScale + fCameraScale);
  90. // Initialize the scattering loop variables
  91. float fSampleLength = fFar / fSamples;
  92. float fScaledLength = fSampleLength * fScale;
  93. vec3 v3SampleRay = v3Ray * fSampleLength;
  94. vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
  95. // Now loop through the sample rays
  96. vec3 v3FrontColor = vec3(0.0);
  97. vec3 v3Attenuate = vec3(0.0);
  98. for(int i=0; i<nSamples; i++)
  99. {
  100. float fHeight = length(v3SamplePoint);
  101. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
  102. float fScatter = fDepth*fTemp - fCameraOffset;
  103. v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
  104. v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
  105. v3SamplePoint += v3SampleRay;
  106. }
  107. AtmosphereColor color;
  108. color.mie = v3FrontColor * (v3InvWavelength * fKrESun + fKmESun);
  109. color.rayleigh = v3Attenuate; // Calculate the attenuation factor for the ground
  110. return color;
  111. }