GroundAtmosphere.js 5.7 KB

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