GroundAtmosphere.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "const float fInnerRadius = 6378137.0;\n\
  3. const float fOuterRadius = 6378137.0 * 1.025;\n\
  4. const float fOuterRadius2 = fOuterRadius * fOuterRadius;\n\
  5. const float Kr = 0.0025;\n\
  6. const float Km = 0.0015;\n\
  7. const float ESun = 15.0;\n\
  8. const float fKrESun = Kr * ESun;\n\
  9. const float fKmESun = Km * ESun;\n\
  10. const float fKr4PI = Kr * 4.0 * czm_pi;\n\
  11. const float fKm4PI = Km * 4.0 * czm_pi;\n\
  12. const float fScale = 1.0 / (fOuterRadius - fInnerRadius);\n\
  13. const float fScaleDepth = 0.25;\n\
  14. const float fScaleOverScaleDepth = fScale / fScaleDepth;\n\
  15. struct AtmosphereColor\n\
  16. {\n\
  17. vec3 mie;\n\
  18. vec3 rayleigh;\n\
  19. };\n\
  20. const int nSamples = 2;\n\
  21. const float fSamples = 2.0;\n\
  22. float scale(float fCos)\n\
  23. {\n\
  24. float x = 1.0 - fCos;\n\
  25. return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));\n\
  26. }\n\
  27. AtmosphereColor computeGroundAtmosphereFromSpace(vec3 v3Pos, bool useSunLighting)\n\
  28. {\n\
  29. vec3 v3InvWavelength = vec3(1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0));\n\
  30. vec3 v3Ray = v3Pos - czm_viewerPositionWC;\n\
  31. float fFar = length(v3Ray);\n\
  32. v3Ray /= fFar;\n\
  33. float fCameraHeight = length(czm_viewerPositionWC);\n\
  34. float fCameraHeight2 = fCameraHeight * fCameraHeight;\n\
  35. float B = 2.0 * length(czm_viewerPositionWC) * dot(normalize(czm_viewerPositionWC), v3Ray);\n\
  36. float C = fCameraHeight2 - fOuterRadius2;\n\
  37. float fDet = max(0.0, B*B - 4.0 * C);\n\
  38. float fNear = 0.5 * (-B - sqrt(fDet));\n\
  39. vec3 v3Start = czm_viewerPositionWC + v3Ray * fNear;\n\
  40. fFar -= fNear;\n\
  41. float fDepth = exp((fInnerRadius - fOuterRadius) / fScaleDepth);\n\
  42. float fLightAngle = czm_branchFreeTernary(useSunLighting, dot(czm_sunDirectionWC, v3Pos) / length(v3Pos), 1.0);\n\
  43. float fCameraAngle = dot(-v3Ray, v3Pos) / length(v3Pos);\n\
  44. float fCameraScale = scale(fCameraAngle);\n\
  45. float fLightScale = scale(fLightAngle);\n\
  46. float fCameraOffset = fDepth*fCameraScale;\n\
  47. float fTemp = (fLightScale + fCameraScale);\n\
  48. float fSampleLength = fFar / fSamples;\n\
  49. float fScaledLength = fSampleLength * fScale;\n\
  50. vec3 v3SampleRay = v3Ray * fSampleLength;\n\
  51. vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;\n\
  52. vec3 v3FrontColor = vec3(0.0);\n\
  53. vec3 v3Attenuate = vec3(0.0);\n\
  54. for(int i=0; i<nSamples; i++)\n\
  55. {\n\
  56. float fHeight = length(v3SamplePoint);\n\
  57. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));\n\
  58. float fScatter = fDepth*fTemp - fCameraOffset;\n\
  59. v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));\n\
  60. v3FrontColor += v3Attenuate * (fDepth * fScaledLength);\n\
  61. v3SamplePoint += v3SampleRay;\n\
  62. }\n\
  63. AtmosphereColor color;\n\
  64. color.mie = v3FrontColor * (v3InvWavelength * fKrESun + fKmESun);\n\
  65. color.rayleigh = v3Attenuate;\n\
  66. return color;\n\
  67. }\n\
  68. ";