AtmosphereCommon.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform vec3 u_radiiAndDynamicAtmosphereColor;\n\
  3. \n\
  4. uniform float u_atmosphereLightIntensity;\n\
  5. uniform float u_atmosphereRayleighScaleHeight;\n\
  6. uniform float u_atmosphereMieScaleHeight;\n\
  7. uniform float u_atmosphereMieAnisotropy;\n\
  8. uniform vec3 u_atmosphereRayleighCoefficient;\n\
  9. uniform vec3 u_atmosphereMieCoefficient;\n\
  10. \n\
  11. const float ATMOSPHERE_THICKNESS = 111e3; // The thickness of the atmosphere in meters.\n\
  12. const int PRIMARY_STEPS = 16; // Number of times the ray from the camera to the world position (primary ray) is sampled.\n\
  13. const int LIGHT_STEPS = 4; // Number of times the light is sampled from the light source's intersection with the atmosphere to a sample position on the primary ray.\n\
  14. \n\
  15. /**\n\
  16. * This function computes the colors contributed by Rayliegh and Mie scattering on a given ray, as well as\n\
  17. * the transmittance value for the ray.\n\
  18. *\n\
  19. * @param {czm_ray} primaryRay The ray from the camera to the position.\n\
  20. * @param {float} primaryRayLength The length of the primary ray.\n\
  21. * @param {vec3} lightDirection The direction of the light to calculate the scattering from.\n\
  22. * @param {vec3} rayleighColor The variable the Rayleigh scattering will be written to.\n\
  23. * @param {vec3} mieColor The variable the Mie scattering will be written to.\n\
  24. * @param {float} opacity The variable the transmittance will be written to.\n\
  25. * @glslFunction\n\
  26. */\n\
  27. void computeScattering(\n\
  28. czm_ray primaryRay,\n\
  29. float primaryRayLength,\n\
  30. vec3 lightDirection,\n\
  31. float atmosphereInnerRadius,\n\
  32. out vec3 rayleighColor,\n\
  33. out vec3 mieColor,\n\
  34. out float opacity\n\
  35. ) {\n\
  36. \n\
  37. // Initialize the default scattering amounts to 0.\n\
  38. rayleighColor = vec3(0.0);\n\
  39. mieColor = vec3(0.0);\n\
  40. opacity = 0.0;\n\
  41. \n\
  42. float atmosphereOuterRadius = atmosphereInnerRadius + ATMOSPHERE_THICKNESS;\n\
  43. \n\
  44. vec3 origin = vec3(0.0);\n\
  45. \n\
  46. // Calculate intersection from the camera to the outer ring of the atmosphere.\n\
  47. czm_raySegment primaryRayAtmosphereIntersect = czm_raySphereIntersectionInterval(primaryRay, origin, atmosphereOuterRadius);\n\
  48. \n\
  49. // Return empty colors if no intersection with the atmosphere geometry.\n\
  50. if (primaryRayAtmosphereIntersect == czm_emptyRaySegment) {\n\
  51. return;\n\
  52. }\n\
  53. \n\
  54. // The ray should start from the first intersection with the outer atmopshere, or from the camera position, if it is inside the atmosphere.\n\
  55. primaryRayAtmosphereIntersect.start = max(primaryRayAtmosphereIntersect.start, 0.0);\n\
  56. // The ray should end at the exit from the atmosphere or at the distance to the vertex, whichever is smaller.\n\
  57. primaryRayAtmosphereIntersect.stop = min(primaryRayAtmosphereIntersect.stop, length(primaryRayLength));\n\
  58. \n\
  59. // Setup for sampling positions along the ray - starting from the intersection with the outer ring of the atmosphere.\n\
  60. float rayStepLength = (primaryRayAtmosphereIntersect.stop - primaryRayAtmosphereIntersect.start) / float(PRIMARY_STEPS);\n\
  61. float rayPositionLength = primaryRayAtmosphereIntersect.start;\n\
  62. \n\
  63. vec3 rayleighAccumulation = vec3(0.0);\n\
  64. vec3 mieAccumulation = vec3(0.0);\n\
  65. vec2 opticalDepth = vec2(0.0);\n\
  66. vec2 heightScale = vec2(u_atmosphereRayleighScaleHeight, u_atmosphereMieScaleHeight);\n\
  67. \n\
  68. // Sample positions on the primary ray.\n\
  69. for (int i = 0; i < PRIMARY_STEPS; i++) {\n\
  70. // Calculate sample position along viewpoint ray.\n\
  71. vec3 samplePosition = primaryRay.origin + primaryRay.direction * (rayPositionLength + rayStepLength);\n\
  72. \n\
  73. // Calculate height of sample position above ellipsoid.\n\
  74. float sampleHeight = length(samplePosition) - atmosphereInnerRadius;\n\
  75. \n\
  76. // Calculate and accumulate density of particles at the sample position.\n\
  77. vec2 sampleDensity = exp(-sampleHeight / heightScale) * rayStepLength;\n\
  78. opticalDepth += sampleDensity;\n\
  79. \n\
  80. // Generate ray from the sample position segment to the light source, up to the outer ring of the atmosphere.\n\
  81. czm_ray lightRay = czm_ray(samplePosition, lightDirection);\n\
  82. czm_raySegment lightRayAtmosphereIntersect = czm_raySphereIntersectionInterval(lightRay, origin, atmosphereOuterRadius);\n\
  83. \n\
  84. float lightStepLength = lightRayAtmosphereIntersect.stop / float(LIGHT_STEPS);\n\
  85. float lightPositionLength = 0.0;\n\
  86. \n\
  87. vec2 lightOpticalDepth = vec2(0.0);\n\
  88. \n\
  89. // Sample positions along the light ray, to accumulate incidence of light on the latest sample segment.\n\
  90. for (int j = 0; j < LIGHT_STEPS; j++) {\n\
  91. \n\
  92. // Calculate sample position along light ray.\n\
  93. vec3 lightPosition = samplePosition + lightDirection * (lightPositionLength + lightStepLength * 0.5);\n\
  94. \n\
  95. // Calculate height of the light sample position above ellipsoid.\n\
  96. float lightHeight = length(lightPosition) - atmosphereInnerRadius;\n\
  97. \n\
  98. // Calculate density of photons at the light sample position.\n\
  99. lightOpticalDepth += exp(-lightHeight / heightScale) * lightStepLength;\n\
  100. \n\
  101. // Increment distance on light ray.\n\
  102. lightPositionLength += lightStepLength;\n\
  103. }\n\
  104. \n\
  105. // Compute attenuation via the primary ray and the light ray.\n\
  106. vec3 attenuation = exp(-((u_atmosphereMieCoefficient * (opticalDepth.y + lightOpticalDepth.y)) + (u_atmosphereRayleighCoefficient * (opticalDepth.x + lightOpticalDepth.x))));\n\
  107. \n\
  108. // Accumulate the scattering.\n\
  109. rayleighAccumulation += sampleDensity.x * attenuation;\n\
  110. mieAccumulation += sampleDensity.y * attenuation;\n\
  111. \n\
  112. // Increment distance on primary ray.\n\
  113. rayPositionLength += rayStepLength;\n\
  114. }\n\
  115. \n\
  116. // Compute the scattering amount.\n\
  117. rayleighColor = u_atmosphereRayleighCoefficient * rayleighAccumulation;\n\
  118. mieColor = u_atmosphereMieCoefficient * mieAccumulation;\n\
  119. \n\
  120. // Compute the transmittance i.e. how much light is passing through the atmosphere.\n\
  121. opacity = length(exp(-((u_atmosphereMieCoefficient * opticalDepth.y) + (u_atmosphereRayleighCoefficient * opticalDepth.x))));\n\
  122. }\n\
  123. \n\
  124. vec4 computeAtmosphereColor(\n\
  125. vec3 positionWC,\n\
  126. vec3 lightDirection,\n\
  127. vec3 rayleighColor,\n\
  128. vec3 mieColor,\n\
  129. float opacity\n\
  130. ) {\n\
  131. // Setup the primary ray: from the camera position to the vertex position.\n\
  132. vec3 cameraToPositionWC = positionWC - czm_viewerPositionWC;\n\
  133. vec3 cameraToPositionWCDirection = normalize(cameraToPositionWC);\n\
  134. \n\
  135. float cosAngle = dot(cameraToPositionWCDirection, lightDirection);\n\
  136. float cosAngleSq = cosAngle * cosAngle;\n\
  137. \n\
  138. float G = u_atmosphereMieAnisotropy;\n\
  139. float GSq = G * G;\n\
  140. \n\
  141. // The Rayleigh phase function.\n\
  142. float rayleighPhase = 3.0 / (50.2654824574) * (1.0 + cosAngleSq);\n\
  143. // The Mie phase function.\n\
  144. float miePhase = 3.0 / (25.1327412287) * ((1.0 - GSq) * (cosAngleSq + 1.0)) / (pow(1.0 + GSq - 2.0 * cosAngle * G, 1.5) * (2.0 + GSq));\n\
  145. \n\
  146. // The final color is generated by combining the effects of the Rayleigh and Mie scattering.\n\
  147. vec3 rayleigh = rayleighPhase * rayleighColor;\n\
  148. vec3 mie = miePhase * mieColor;\n\
  149. \n\
  150. vec3 color = (rayleigh + mie) * u_atmosphereLightIntensity;\n\
  151. \n\
  152. return vec4(color, opacity);\n\
  153. }\n\
  154. ";