SkyAtmosphereVS.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license
  3. * Copyright (c) 2000-2005, Sean O'Neil (s_p_oneil@hotmail.com)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * * Neither the name of the project nor the names of its contributors may be
  16. * used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Modifications made by Analytical Graphics, Inc.
  31. */
  32. //This file is automatically rebuilt by the Cesium build process.
  33. export default "attribute vec4 position;\n\
  34. uniform vec4 u_cameraAndRadiiAndDynamicAtmosphereColor;\n\
  35. const float Kr = 0.0025;\n\
  36. const float Kr4PI = Kr * 4.0 * czm_pi;\n\
  37. const float Km = 0.0015;\n\
  38. const float Km4PI = Km * 4.0 * czm_pi;\n\
  39. const float ESun = 15.0;\n\
  40. const float KmESun = Km * ESun;\n\
  41. const float KrESun = Kr * ESun;\n\
  42. const vec3 InvWavelength = vec3(\n\
  43. 5.60204474633241,\n\
  44. 9.473284437923038,\n\
  45. 19.643802610477206);\n\
  46. const float rayleighScaleDepth = 0.25;\n\
  47. const int nSamples = 2;\n\
  48. const float fSamples = 2.0;\n\
  49. varying vec3 v_rayleighColor;\n\
  50. varying vec3 v_mieColor;\n\
  51. varying vec3 v_toCamera;\n\
  52. float scale(float cosAngle)\n\
  53. {\n\
  54. float x = 1.0 - cosAngle;\n\
  55. return rayleighScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));\n\
  56. }\n\
  57. void main(void)\n\
  58. {\n\
  59. float cameraHeight = u_cameraAndRadiiAndDynamicAtmosphereColor.x;\n\
  60. float outerRadius = u_cameraAndRadiiAndDynamicAtmosphereColor.y;\n\
  61. float innerRadius = u_cameraAndRadiiAndDynamicAtmosphereColor.z;\n\
  62. vec3 positionV3 = position.xyz;\n\
  63. vec3 ray = positionV3 - czm_viewerPositionWC;\n\
  64. float far = length(ray);\n\
  65. ray /= far;\n\
  66. float atmosphereScale = 1.0 / (outerRadius - innerRadius);\n\
  67. #ifdef SKY_FROM_SPACE\n\
  68. float B = 2.0 * dot(czm_viewerPositionWC, ray);\n\
  69. float C = cameraHeight * cameraHeight - outerRadius * outerRadius;\n\
  70. float det = max(0.0, B*B - 4.0 * C);\n\
  71. float near = 0.5 * (-B - sqrt(det));\n\
  72. vec3 start = czm_viewerPositionWC + ray * near;\n\
  73. far -= near;\n\
  74. float startAngle = dot(ray, start) / outerRadius;\n\
  75. float startDepth = exp(-1.0 / rayleighScaleDepth );\n\
  76. float startOffset = startDepth*scale(startAngle);\n\
  77. #else // SKY_FROM_ATMOSPHERE\n\
  78. vec3 start = czm_viewerPositionWC;\n\
  79. float height = length(start);\n\
  80. float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - cameraHeight));\n\
  81. float startAngle = dot(ray, start) / height;\n\
  82. float startOffset = depth*scale(startAngle);\n\
  83. #endif\n\
  84. float sampleLength = far / fSamples;\n\
  85. float scaledLength = sampleLength * atmosphereScale;\n\
  86. vec3 sampleRay = ray * sampleLength;\n\
  87. vec3 samplePoint = start + sampleRay * 0.5;\n\
  88. vec3 frontColor = vec3(0.0, 0.0, 0.0);\n\
  89. vec3 lightDir = (u_cameraAndRadiiAndDynamicAtmosphereColor.w > 0.0) ? czm_sunPositionWC - czm_viewerPositionWC : czm_viewerPositionWC;\n\
  90. lightDir = normalize(lightDir);\n\
  91. for(int i=0; i<nSamples; i++)\n\
  92. {\n\
  93. float height = length(samplePoint);\n\
  94. float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - height));\n\
  95. float fLightAngle = dot(lightDir, samplePoint) / height;\n\
  96. float fCameraAngle = dot(ray, samplePoint) / height;\n\
  97. float fScatter = (startOffset + depth*(scale(fLightAngle) - scale(fCameraAngle)));\n\
  98. vec3 attenuate = exp(-fScatter * (InvWavelength * Kr4PI + Km4PI));\n\
  99. frontColor += attenuate * (depth * scaledLength);\n\
  100. samplePoint += sampleRay;\n\
  101. }\n\
  102. v_mieColor = frontColor * KmESun;\n\
  103. v_rayleighColor = frontColor * (InvWavelength * KrESun);\n\
  104. v_toCamera = czm_viewerPositionWC - positionV3;\n\
  105. gl_Position = czm_modelViewProjection * position;\n\
  106. }\n\
  107. ";