SkyAtmosphereVS.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /*global define*/
  34. define(function() {
  35. "use strict";
  36. return "/**\n\
  37. * @license\n\
  38. * Copyright (c) 2000-2005, Sean O'Neil (s_p_oneil@hotmail.com)\n\
  39. * All rights reserved.\n\
  40. * \n\
  41. * Redistribution and use in source and binary forms, with or without\n\
  42. * modification, are permitted provided that the following conditions\n\
  43. * are met:\n\
  44. * \n\
  45. * * Redistributions of source code must retain the above copyright notice,\n\
  46. * this list of conditions and the following disclaimer.\n\
  47. * * Redistributions in binary form must reproduce the above copyright notice,\n\
  48. * this list of conditions and the following disclaimer in the documentation\n\
  49. * and/or other materials provided with the distribution.\n\
  50. * * Neither the name of the project nor the names of its contributors may be\n\
  51. * used to endorse or promote products derived from this software without\n\
  52. * specific prior written permission.\n\
  53. * \n\
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n\
  55. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n\
  56. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n\
  57. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE\n\
  58. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n\
  59. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n\
  60. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n\
  61. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n\
  62. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n\
  63. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\
  64. *\n\
  65. * Modifications made by Analytical Graphics, Inc.\n\
  66. */\n\
  67. \n\
  68. // Code: http://sponeil.net/\n\
  69. // GPU Gems 2 Article: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter16.html\n\
  70. \n\
  71. attribute vec4 position;\n\
  72. \n\
  73. uniform float fCameraHeight;\n\
  74. uniform float fCameraHeight2;\n\
  75. uniform float fOuterRadius; // The outer (atmosphere) radius\n\
  76. uniform float fOuterRadius2; // fOuterRadius^2\n\
  77. uniform float fInnerRadius; // The inner (planetary) radius\n\
  78. uniform float fScale; // 1 / (fOuterRadius - fInnerRadius)\n\
  79. uniform float fScaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)\n\
  80. uniform float fScaleOverScaleDepth; // fScale / fScaleDepth\n\
  81. \n\
  82. const float Kr = 0.0025;\n\
  83. const float fKr4PI = Kr * 4.0 * czm_pi;\n\
  84. const float Km = 0.0015;\n\
  85. const float fKm4PI = Km * 4.0 * czm_pi;\n\
  86. const float ESun = 15.0;\n\
  87. const float fKmESun = Km * ESun;\n\
  88. const float fKrESun = Kr * ESun;\n\
  89. const vec3 v3InvWavelength = vec3(\n\
  90. 5.60204474633241, // Red = 1.0 / Math.pow(0.650, 4.0)\n\
  91. 9.473284437923038, // Green = 1.0 / Math.pow(0.570, 4.0)\n\
  92. 19.643802610477206); // Blue = 1.0 / Math.pow(0.475, 4.0)\n\
  93. const float rayleighScaleDepth = 0.25;\n\
  94. \n\
  95. const int nSamples = 2;\n\
  96. const float fSamples = 2.0;\n\
  97. \n\
  98. varying vec3 v_rayleighColor;\n\
  99. varying vec3 v_mieColor;\n\
  100. varying vec3 v_toCamera;\n\
  101. varying vec3 v_positionEC;\n\
  102. \n\
  103. float scale(float fCos)\n\
  104. {\n\
  105. float x = 1.0 - fCos;\n\
  106. return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));\n\
  107. }\n\
  108. \n\
  109. void main(void)\n\
  110. {\n\
  111. // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)\n\
  112. vec3 v3Pos = position.xyz;\n\
  113. vec3 v3Ray = v3Pos - czm_viewerPositionWC;\n\
  114. float fFar = length(v3Ray);\n\
  115. v3Ray /= fFar;\n\
  116. \n\
  117. #ifdef SKY_FROM_SPACE\n\
  118. // Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)\n\
  119. float B = 2.0 * dot(czm_viewerPositionWC, v3Ray);\n\
  120. float C = fCameraHeight2 - fOuterRadius2;\n\
  121. float fDet = max(0.0, B*B - 4.0 * C);\n\
  122. float fNear = 0.5 * (-B - sqrt(fDet));\n\
  123. \n\
  124. // Calculate the ray's starting position, then calculate its scattering offset\n\
  125. vec3 v3Start = czm_viewerPositionWC + v3Ray * fNear;\n\
  126. fFar -= fNear;\n\
  127. float fStartAngle = dot(v3Ray, v3Start) / fOuterRadius;\n\
  128. float fStartDepth = exp(-1.0 / fScaleDepth);\n\
  129. float fStartOffset = fStartDepth*scale(fStartAngle);\n\
  130. #else // SKY_FROM_ATMOSPHERE\n\
  131. // Calculate the ray's starting position, then calculate its scattering offset\n\
  132. vec3 v3Start = czm_viewerPositionWC;\n\
  133. float fHeight = length(v3Start);\n\
  134. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight));\n\
  135. float fStartAngle = dot(v3Ray, v3Start) / fHeight;\n\
  136. float fStartOffset = fDepth*scale(fStartAngle);\n\
  137. #endif\n\
  138. \n\
  139. // Initialize the scattering loop variables\n\
  140. float fSampleLength = fFar / fSamples;\n\
  141. float fScaledLength = fSampleLength * fScale;\n\
  142. vec3 v3SampleRay = v3Ray * fSampleLength;\n\
  143. vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;\n\
  144. \n\
  145. // Now loop through the sample rays\n\
  146. vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);\n\
  147. for(int i=0; i<nSamples; i++)\n\
  148. {\n\
  149. float fHeight = length(v3SamplePoint);\n\
  150. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));\n\
  151. vec3 lightPosition = normalize(czm_viewerPositionWC); // czm_sunDirectionWC\n\
  152. float fLightAngle = dot(lightPosition, v3SamplePoint) / fHeight;\n\
  153. float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;\n\
  154. float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));\n\
  155. vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));\n\
  156. v3FrontColor += v3Attenuate * (fDepth * fScaledLength);\n\
  157. v3SamplePoint += v3SampleRay;\n\
  158. }\n\
  159. \n\
  160. // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader\n\
  161. v_mieColor = v3FrontColor * fKmESun;\n\
  162. v_rayleighColor = v3FrontColor * (v3InvWavelength * fKrESun);\n\
  163. v_toCamera = czm_viewerPositionWC - v3Pos;\n\
  164. v_positionEC = (czm_modelView * position).xyz;\n\
  165. gl_Position = czm_modelViewProjection * position;\n\
  166. }\n\
  167. ";
  168. });