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

AtmosphereCommon.js 9.8KB

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