实现原理

根据片元到摄像机的距离 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)); // 指数平方雾
点击展开查看更多

版权声明

作者: Chaim

链接: https://chaim.eu.org/posts/%E9%9B%BE%E6%95%88/

许可证: 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.

开始搜索

输入关键词搜索文章内容

↑↓
ESC
⌘K 快捷键