SkyAtmosphereVS.glsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Code: http://sponeil.net/
  33. // GPU Gems 2 Article: https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html
  34. attribute vec4 position;
  35. uniform vec4 u_cameraAndRadiiAndDynamicAtmosphereColor; // Camera height, outer radius, inner radius, dynamic atmosphere color flag
  36. const float Kr = 0.0025;
  37. const float Kr4PI = Kr * 4.0 * czm_pi;
  38. const float Km = 0.0015;
  39. const float Km4PI = Km * 4.0 * czm_pi;
  40. const float ESun = 15.0;
  41. const float KmESun = Km * ESun;
  42. const float KrESun = Kr * ESun;
  43. const vec3 InvWavelength = vec3(
  44. 5.60204474633241, // Red = 1.0 / Math.pow(0.650, 4.0)
  45. 9.473284437923038, // Green = 1.0 / Math.pow(0.570, 4.0)
  46. 19.643802610477206); // Blue = 1.0 / Math.pow(0.475, 4.0)
  47. const float rayleighScaleDepth = 0.25;
  48. const int nSamples = 2;
  49. const float fSamples = 2.0;
  50. varying vec3 v_rayleighColor;
  51. varying vec3 v_mieColor;
  52. varying vec3 v_toCamera;
  53. float scale(float cosAngle)
  54. {
  55. float x = 1.0 - cosAngle;
  56. return rayleighScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
  57. }
  58. void main(void)
  59. {
  60. // Unpack attributes
  61. float cameraHeight = u_cameraAndRadiiAndDynamicAtmosphereColor.x;
  62. float outerRadius = u_cameraAndRadiiAndDynamicAtmosphereColor.y;
  63. float innerRadius = u_cameraAndRadiiAndDynamicAtmosphereColor.z;
  64. // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
  65. vec3 positionV3 = position.xyz;
  66. vec3 ray = positionV3 - czm_viewerPositionWC;
  67. float far = length(ray);
  68. ray /= far;
  69. float atmosphereScale = 1.0 / (outerRadius - innerRadius);
  70. #ifdef SKY_FROM_SPACE
  71. // Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
  72. float B = 2.0 * dot(czm_viewerPositionWC, ray);
  73. float C = cameraHeight * cameraHeight - outerRadius * outerRadius;
  74. float det = max(0.0, B*B - 4.0 * C);
  75. float near = 0.5 * (-B - sqrt(det));
  76. // Calculate the ray's starting position, then calculate its scattering offset
  77. vec3 start = czm_viewerPositionWC + ray * near;
  78. far -= near;
  79. float startAngle = dot(ray, start) / outerRadius;
  80. float startDepth = exp(-1.0 / rayleighScaleDepth );
  81. float startOffset = startDepth*scale(startAngle);
  82. #else // SKY_FROM_ATMOSPHERE
  83. // Calculate the ray's starting position, then calculate its scattering offset
  84. vec3 start = czm_viewerPositionWC;
  85. float height = length(start);
  86. float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - cameraHeight));
  87. float startAngle = dot(ray, start) / height;
  88. float startOffset = depth*scale(startAngle);
  89. #endif
  90. // Initialize the scattering loop variables
  91. float sampleLength = far / fSamples;
  92. float scaledLength = sampleLength * atmosphereScale;
  93. vec3 sampleRay = ray * sampleLength;
  94. vec3 samplePoint = start + sampleRay * 0.5;
  95. // Now loop through the sample rays
  96. vec3 frontColor = vec3(0.0, 0.0, 0.0);
  97. vec3 lightDir = (u_cameraAndRadiiAndDynamicAtmosphereColor.w > 0.0) ? czm_sunPositionWC - czm_viewerPositionWC : czm_viewerPositionWC;
  98. lightDir = normalize(lightDir);
  99. for(int i=0; i<nSamples; i++)
  100. {
  101. float height = length(samplePoint);
  102. float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - height));
  103. float fLightAngle = dot(lightDir, samplePoint) / height;
  104. float fCameraAngle = dot(ray, samplePoint) / height;
  105. float fScatter = (startOffset + depth*(scale(fLightAngle) - scale(fCameraAngle)));
  106. vec3 attenuate = exp(-fScatter * (InvWavelength * Kr4PI + Km4PI));
  107. frontColor += attenuate * (depth * scaledLength);
  108. samplePoint += sampleRay;
  109. }
  110. // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
  111. v_mieColor = frontColor * KmESun;
  112. v_rayleighColor = frontColor * (InvWavelength * KrESun);
  113. v_toCamera = czm_viewerPositionWC - positionV3;
  114. gl_Position = czm_modelViewProjection * position;
  115. }