AtmosphereCommon.glsl 6.9 KB

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