智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_MAX = 16; // Maximum number of times the ray from the camera to the world position (primary ray) is sampled.
  10. const int LIGHT_STEPS_MAX = 4; // Maximum 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. // To deal with smaller values of PRIMARY_STEPS (e.g. 4)
  45. // we implement a split strategy: sky or horizon.
  46. // For performance reasons, instead of a if/else branch
  47. // a soft choice is implemented through a weight 0.0 <= w_stop_gt_lprl <= 1.0
  48. float x = 1e-7 * primaryRayAtmosphereIntersect.stop / length(primaryRayLength);
  49. // Value close to 0.0: close to the horizon
  50. // Value close to 1.0: above in the sky
  51. float w_stop_gt_lprl = 0.5 * (1.0 + czm_approximateTanh(x));
  52. // The ray should start from the first intersection with the outer atmopshere, or from the camera position, if it is inside the atmosphere.
  53. float start_0 = primaryRayAtmosphereIntersect.start;
  54. primaryRayAtmosphereIntersect.start = max(primaryRayAtmosphereIntersect.start, 0.0);
  55. // The ray should end at the exit from the atmosphere or at the distance to the vertex, whichever is smaller.
  56. primaryRayAtmosphereIntersect.stop = min(primaryRayAtmosphereIntersect.stop, length(primaryRayLength));
  57. // For the number of ray steps, distinguish inside or outside atmosphere (outer space)
  58. // (1) from outer space we have to use more ray steps to get a realistic rendering
  59. // (2) within atmosphere we need fewer steps for faster rendering
  60. float x_o_a = start_0 - ATMOSPHERE_THICKNESS; // ATMOSPHERE_THICKNESS used as an ad-hoc constant, no precise meaning here, only the order of magnitude matters
  61. float w_inside_atmosphere = 1.0 - 0.5 * (1.0 + czm_approximateTanh(x_o_a));
  62. int PRIMARY_STEPS = PRIMARY_STEPS_MAX - int(w_inside_atmosphere * 12.0); // Number of times the ray from the camera to the world position (primary ray) is sampled.
  63. int LIGHT_STEPS = LIGHT_STEPS_MAX - int(w_inside_atmosphere * 2.0); // Number of times the light is sampled from the light source's intersection with the atmosphere to a sample position on the primary ray.
  64. // Setup for sampling positions along the ray - starting from the intersection with the outer ring of the atmosphere.
  65. float rayPositionLength = primaryRayAtmosphereIntersect.start;
  66. // (1) Outside the atmosphere: constant rayStepLength
  67. // (2) Inside atmosphere: variable rayStepLength to compensate the rough rendering of the smaller number of ray steps
  68. float totalRayLength = primaryRayAtmosphereIntersect.stop - rayPositionLength;
  69. float rayStepLengthIncrease = w_inside_atmosphere * ((1.0 - w_stop_gt_lprl) * totalRayLength / (float(PRIMARY_STEPS * (PRIMARY_STEPS + 1)) / 2.0));
  70. float rayStepLength = max(1.0 - w_inside_atmosphere, w_stop_gt_lprl) * totalRayLength / max(7.0 * w_inside_atmosphere, float(PRIMARY_STEPS));
  71. vec3 rayleighAccumulation = vec3(0.0);
  72. vec3 mieAccumulation = vec3(0.0);
  73. vec2 opticalDepth = vec2(0.0);
  74. vec2 heightScale = vec2(u_atmosphereRayleighScaleHeight, u_atmosphereMieScaleHeight);
  75. // Sample positions on the primary ray.
  76. for (int i = 0; i < PRIMARY_STEPS_MAX; ++i) {
  77. // The loop should be: for (int i = 0; i < PRIMARY_STEPS; ++i) {...} but WebGL1 cannot
  78. // loop with non-constant condition, so it has to break early instead
  79. if (i >= PRIMARY_STEPS) {
  80. break;
  81. }
  82. // Calculate sample position along viewpoint ray.
  83. vec3 samplePosition = primaryRay.origin + primaryRay.direction * (rayPositionLength + rayStepLength);
  84. // Calculate height of sample position above ellipsoid.
  85. float sampleHeight = length(samplePosition) - atmosphereInnerRadius;
  86. // Calculate and accumulate density of particles at the sample position.
  87. vec2 sampleDensity = exp(-sampleHeight / heightScale) * rayStepLength;
  88. opticalDepth += sampleDensity;
  89. // Generate ray from the sample position segment to the light source, up to the outer ring of the atmosphere.
  90. czm_ray lightRay = czm_ray(samplePosition, lightDirection);
  91. czm_raySegment lightRayAtmosphereIntersect = czm_raySphereIntersectionInterval(lightRay, origin, atmosphereOuterRadius);
  92. float lightStepLength = lightRayAtmosphereIntersect.stop / float(LIGHT_STEPS);
  93. float lightPositionLength = 0.0;
  94. vec2 lightOpticalDepth = vec2(0.0);
  95. // Sample positions along the light ray, to accumulate incidence of light on the latest sample segment.
  96. for (int j = 0; j < LIGHT_STEPS_MAX; ++j) {
  97. // The loop should be: for (int j = 0; i < LIGHT_STEPS; ++j) {...} but WebGL1 cannot
  98. // loop with non-constant condition, so it has to break early instead
  99. if (j >= LIGHT_STEPS) {
  100. break;
  101. }
  102. // Calculate sample position along light ray.
  103. vec3 lightPosition = samplePosition + lightDirection * (lightPositionLength + lightStepLength * 0.5);
  104. // Calculate height of the light sample position above ellipsoid.
  105. float lightHeight = length(lightPosition) - atmosphereInnerRadius;
  106. // Calculate density of photons at the light sample position.
  107. lightOpticalDepth += exp(-lightHeight / heightScale) * lightStepLength;
  108. // Increment distance on light ray.
  109. lightPositionLength += lightStepLength;
  110. }
  111. // Compute attenuation via the primary ray and the light ray.
  112. vec3 attenuation = exp(-((u_atmosphereMieCoefficient * (opticalDepth.y + lightOpticalDepth.y)) + (u_atmosphereRayleighCoefficient * (opticalDepth.x + lightOpticalDepth.x))));
  113. // Accumulate the scattering.
  114. rayleighAccumulation += sampleDensity.x * attenuation;
  115. mieAccumulation += sampleDensity.y * attenuation;
  116. // Increment distance on primary ray.
  117. rayPositionLength += (rayStepLength += rayStepLengthIncrease);
  118. }
  119. // Compute the scattering amount.
  120. rayleighColor = u_atmosphereRayleighCoefficient * rayleighAccumulation;
  121. mieColor = u_atmosphereMieCoefficient * mieAccumulation;
  122. // Compute the transmittance i.e. how much light is passing through the atmosphere.
  123. opacity = length(exp(-((u_atmosphereMieCoefficient * opticalDepth.y) + (u_atmosphereRayleighCoefficient * opticalDepth.x))));
  124. }
  125. vec4 computeAtmosphereColor(
  126. vec3 positionWC,
  127. vec3 lightDirection,
  128. vec3 rayleighColor,
  129. vec3 mieColor,
  130. float opacity
  131. ) {
  132. // Setup the primary ray: from the camera position to the vertex position.
  133. vec3 cameraToPositionWC = positionWC - czm_viewerPositionWC;
  134. vec3 cameraToPositionWCDirection = normalize(cameraToPositionWC);
  135. float cosAngle = dot(cameraToPositionWCDirection, lightDirection);
  136. float cosAngleSq = cosAngle * cosAngle;
  137. float G = u_atmosphereMieAnisotropy;
  138. float GSq = G * G;
  139. // The Rayleigh phase function.
  140. float rayleighPhase = 3.0 / (50.2654824574) * (1.0 + cosAngleSq);
  141. // The Mie phase function.
  142. 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));
  143. // The final color is generated by combining the effects of the Rayleigh and Mie scattering.
  144. vec3 rayleigh = rayleighPhase * rayleighColor;
  145. vec3 mie = miePhase * mieColor;
  146. vec3 color = (rayleigh + mie) * u_atmosphereLightIntensity;
  147. return vec4(color, opacity);
  148. }