LensFlare.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform sampler2D colorTexture;\n\
  3. uniform sampler2D dirtTexture;\n\
  4. uniform sampler2D starTexture;\n\
  5. uniform vec2 dirtTextureDimensions;\n\
  6. uniform float distortion;\n\
  7. uniform float ghostDispersal;\n\
  8. uniform float haloWidth;\n\
  9. uniform float dirtAmount;\n\
  10. uniform float earthRadius;\n\
  11. uniform float intensity;\n\
  12. varying vec2 v_textureCoordinates;\n\
  13. #define DISTANCE_TO_SPACE 6500000.0\n\
  14. vec4 getNDCFromWC(vec3 WC, float earthRadius)\n\
  15. {\n\
  16. vec4 positionEC = czm_view * vec4(WC, 1.0);\n\
  17. positionEC = vec4(positionEC.x + earthRadius, positionEC.y, positionEC.z, 1.0);\n\
  18. vec4 positionWC = czm_eyeToWindowCoordinates(positionEC);\n\
  19. return czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0);\n\
  20. }\n\
  21. float isInEarth(vec2 texcoord, vec2 sceneSize)\n\
  22. {\n\
  23. vec2 NDC = texcoord * 2.0 - 1.0;\n\
  24. vec4 earthPosSC = getNDCFromWC(vec3(0.0), 0.0);\n\
  25. vec4 earthPosSCEdge = getNDCFromWC(vec3(0.0), earthRadius * 1.5);\n\
  26. NDC.xy -= earthPosSC.xy;\n\
  27. float X = abs(NDC.x) * sceneSize.x;\n\
  28. float Y = abs(NDC.y) * sceneSize.y;\n\
  29. return clamp(0.0, 1.0, max(sqrt(X * X + Y * Y) / max(abs(earthPosSCEdge.x * sceneSize.x), 1.0) - 0.8 , 0.0));\n\
  30. }\n\
  31. vec4 textureDistorted(sampler2D tex, vec2 texcoord, vec2 direction, vec3 distortion, bool isSpace)\n\
  32. {\n\
  33. vec2 sceneSize = czm_viewport.zw;\n\
  34. vec3 color;\n\
  35. if(isSpace)\n\
  36. {\n\
  37. color.r = isInEarth(texcoord + direction * distortion.r, sceneSize) * texture2D(tex, texcoord + direction * distortion.r).r;\n\
  38. color.g = isInEarth(texcoord + direction * distortion.g, sceneSize) * texture2D(tex, texcoord + direction * distortion.g).g;\n\
  39. color.b = isInEarth(texcoord + direction * distortion.b, sceneSize) * texture2D(tex, texcoord + direction * distortion.b).b;\n\
  40. }\n\
  41. else\n\
  42. {\n\
  43. color.r = texture2D(tex, texcoord + direction * distortion.r).r;\n\
  44. color.g = texture2D(tex, texcoord + direction * distortion.g).g;\n\
  45. color.b = texture2D(tex, texcoord + direction * distortion.b).b;\n\
  46. }\n\
  47. return vec4(clamp(color, 0.0, 1.0), 0.0);\n\
  48. }\n\
  49. void main(void)\n\
  50. {\n\
  51. vec4 originalColor = texture2D(colorTexture, v_textureCoordinates);\n\
  52. vec3 rgb = originalColor.rgb;\n\
  53. bool isSpace = length(czm_viewerPositionWC.xyz) > DISTANCE_TO_SPACE;\n\
  54. vec4 sunPos = czm_morphTime == 1.0 ? vec4(czm_sunPositionWC, 1.0) : vec4(czm_sunPositionColumbusView.zxy, 1.0);\n\
  55. vec4 sunPositionEC = czm_view * sunPos;\n\
  56. vec4 sunPositionWC = czm_eyeToWindowCoordinates(sunPositionEC);\n\
  57. sunPos = czm_viewportOrthographic * vec4(sunPositionWC.xy, -sunPositionWC.z, 1.0);\n\
  58. if(!isSpace || !((sunPos.x >= -1.1 && sunPos.x <= 1.1) && (sunPos.y >= -1.1 && sunPos.y <= 1.1)))\n\
  59. {\n\
  60. gl_FragColor = originalColor;\n\
  61. return;\n\
  62. }\n\
  63. vec2 texcoord = vec2(1.0) - v_textureCoordinates;\n\
  64. vec2 pixelSize = czm_pixelRatio / czm_viewport.zw;\n\
  65. vec2 invPixelSize = 1.0 / pixelSize;\n\
  66. vec3 distortionVec = pixelSize.x * vec3(-distortion, 0.0, distortion);\n\
  67. vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal;\n\
  68. vec3 direction = normalize(vec3(ghostVec, 0.0));\n\
  69. vec4 result = vec4(0.0);\n\
  70. vec4 ghost = vec4(0.0);\n\
  71. for (int i = 0; i < 4; ++i)\n\
  72. {\n\
  73. vec2 offset = fract(texcoord + ghostVec * float(i));\n\
  74. ghost += textureDistorted(colorTexture, offset, direction.xy, distortionVec, isSpace);\n\
  75. }\n\
  76. result += ghost;\n\
  77. vec2 haloVec = normalize(ghostVec) * haloWidth;\n\
  78. float weightForHalo = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5));\n\
  79. weightForHalo = pow(1.0 - weightForHalo, 5.0);\n\
  80. result += textureDistorted(colorTexture, texcoord + haloVec, direction.xy, distortionVec, isSpace) * weightForHalo * 1.5;\n\
  81. vec2 dirtTexCoords = (v_textureCoordinates * invPixelSize) / dirtTextureDimensions;\n\
  82. if (dirtTexCoords.x > 1.0)\n\
  83. {\n\
  84. dirtTexCoords.x = mod(floor(dirtTexCoords.x), 2.0) == 1.0 ? 1.0 - fract(dirtTexCoords.x) : fract(dirtTexCoords.x);\n\
  85. }\n\
  86. if (dirtTexCoords.y > 1.0)\n\
  87. {\n\
  88. dirtTexCoords.y = mod(floor(dirtTexCoords.y), 2.0) == 1.0 ? 1.0 - fract(dirtTexCoords.y) : fract(dirtTexCoords.y);\n\
  89. }\n\
  90. result += dirtAmount * texture2D(dirtTexture, dirtTexCoords);\n\
  91. float camrot = czm_view[0].z + czm_view[1].y;\n\
  92. float cosValue = cos(camrot);\n\
  93. float sinValue = sin(camrot);\n\
  94. mat3 rotation = mat3(\n\
  95. cosValue, -sinValue, 0.0,\n\
  96. sinValue, cosValue, 0.0,\n\
  97. 0.0, 0.0, 1.0\n\
  98. );\n\
  99. vec3 st1 = vec3(v_textureCoordinates * 2.0 - vec2(1.0), 1.0);\n\
  100. vec3 st2 = vec3((rotation * st1).xy, 1.0);\n\
  101. vec3 st3 = st2 * 0.5 + vec3(0.5);\n\
  102. vec2 lensStarTexcoord = st3.xy;\n\
  103. float weightForLensFlare = length(vec3(sunPos.xy, 0.0));\n\
  104. float oneMinusWeightForLensFlare = max(1.0 - weightForLensFlare, 0.0);\n\
  105. if (!isSpace)\n\
  106. {\n\
  107. result *= oneMinusWeightForLensFlare * intensity * 0.2;\n\
  108. }\n\
  109. else\n\
  110. {\n\
  111. result *= oneMinusWeightForLensFlare * intensity;\n\
  112. result *= texture2D(starTexture, lensStarTexcoord) * pow(weightForLensFlare, 1.0) * max((1.0 - length(vec3(st1.xy, 0.0))), 0.0) * 2.0;\n\
  113. }\n\
  114. result += texture2D(colorTexture, v_textureCoordinates);\n\
  115. gl_FragColor = result;\n\
  116. }\n\
  117. ";