apply-fog.glsl.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. export const apply_fog = `
  2. float viewZ = depthToViewZ(uIsOrtho, fragmentDepth, uNear, uFar);
  3. float fogFactor = smoothstep(uFogNear, uFogFar, abs(viewZ));
  4. float fogAlpha = (1.0 - fogFactor) * gl_FragColor.a;
  5. float preFogAlpha = gl_FragColor.a;
  6. if (!uTransparentBackground) {
  7. if (gl_FragColor.a < 1.0) {
  8. // transparent objects are blended with background color
  9. gl_FragColor.a = fogAlpha;
  10. } else {
  11. // mix opaque objects with background color
  12. gl_FragColor.rgb = mix(gl_FragColor.rgb, uFogColor, fogFactor);
  13. }
  14. } else {
  15. #if defined(dRenderVariant_colorDpoit)
  16. if (gl_FragColor.a < 1.0) {
  17. // transparent objects are blended with background color
  18. gl_FragColor.a = fogAlpha;
  19. } else {
  20. // opaque objects need to be pre-multiplied alpha
  21. gl_FragColor.rgb *= fogAlpha;
  22. gl_FragColor.a = fogAlpha;
  23. }
  24. #else
  25. // pre-multiplied alpha expected for transparent background
  26. gl_FragColor.rgb *= fogAlpha;
  27. gl_FragColor.a = fogAlpha;
  28. #endif
  29. }
  30. `;