实现原理

根据片元到摄像机的距离 d 计算出雾浓度因子 f,再根据该值将片元颜色与雾颜色混合:

$$ f = \text{ComputeFogFactor}(d) $$

$$ Color = \text{Lerp}(Color, FogColor, f) $$

常用算法

雾类型 公式 参数说明 特点
线性雾 $$ f = \frac{d_\text{max} - d}{d_\text{max} - d_\text{min}} $$ $$ d_\text{min} \text{: 雾起始距离 } \newline d_\text{max} \text{: 雾结束距离 } $$ 简单高效,但过渡不自然
指数雾 $$ f = 1 - e^{-k \cdot d} $$ $$ k \text{: 雾密度系数 } (k > 0) $$ 模拟真实雾衰减
指数²雾 $$ f = 1 - e^{-(k \cdot d)^2} $$ $$ k \text{: 雾密度系数 } (k > 0) $$ 衰减更快,更接近真实大气

线性雾 (Linear Fog)

HLSL
float FogFactor = saturate((Distance - FogStart) / (FogEnd - FogStart));
  • 适用: 快速实现、风格化渲染。
  • 问题:在 雾起始 和 雾结束 处有明显硬边缘。

指数雾 (Exponential Fog)

HLSL
float FogFactor = 1.0 - exp(-FogDensity * Distance); // 指数雾
float FogFactor = 1.0 - exp(-pow(FogDensity * Distance, 2)); // 指数平方雾
  • 调参:密度 FogDensity 控制雾浓度(值越大雾越浓)。
  • 优化:可用 exp2 代替 exp 避免高精度计算开销。

: Cheyne Xie

: https://chaim.eu.org/posts/da7a58af/

: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.