You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FogShader.glsl 1.3KB

12345678910111213141516171819202122232425262728293031323334353637
  1. in vec2 v_textureCoordinates;
  2. uniform sampler2D colorTexture;
  3. uniform sampler2D depthTexture;
  4. uniform vec4 fogByDistance;
  5. uniform vec4 fogColor;
  6. float getDistance(sampler2D depthTexture, vec2 texCoords){
  7. float depth = czm_unpackDepth(texture(depthTexture, texCoords));
  8. if (depth == 0.0) {
  9. return czm_infinity;
  10. }
  11. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, depth);
  12. return -eyeCoordinate.z / eyeCoordinate.w;
  13. }
  14. float interpolateByDistance(vec4 nearFarScalar, float distance){
  15. float startDistance = nearFarScalar.x;
  16. float startValue = nearFarScalar.y;
  17. float endDistance = nearFarScalar.z;
  18. float endValue = nearFarScalar.w;
  19. float t = clamp((distance - startDistance) / (endDistance - startDistance), 0.0, 1.0);
  20. return mix(startValue, endValue, t);
  21. }
  22. vec4 alphaBlend(vec4 sourceColor, vec4 destinationColor){
  23. return sourceColor * vec4(sourceColor.aaa, 1.0) + destinationColor * (1.0 - sourceColor.a);
  24. }
  25. void main(void){
  26. float distance = getDistance(depthTexture, v_textureCoordinates);
  27. vec4 sceneColor = texture(colorTexture, v_textureCoordinates);
  28. float blendAmount = interpolateByDistance(fogByDistance, distance);
  29. vec4 finalFogColor = vec4(fogColor.rgb, fogColor.a * blendAmount);
  30. out_FragColor = alphaBlend(finalFogColor, sceneColor);
  31. }