时间:2025-10-01 09:26
人气:
作者:admin
【从UnityURP开始探索游戏渲染】专栏-直达
几何遮蔽(G)在BRDF中用于模拟微表面间的自阴影和遮蔽效应,其计算流程通常分为三个步骤:
原理:
公式:
$G_{Cook-Torrance}=min(1,\frac{2(n⋅h)(n⋅v)}{v⋅h},\frac{2(n⋅h)(n⋅l)}{v⋅h})$
特点:
原理:
公式:
$G_{Smith}=G_1(v)⋅G_1(l)$
Unity URP选择:
hlsl
// URP中Smith联合Schlick-GGX实现
float V_SmithGGX(float NdotL, float NdotV, float roughness)
{
float a = roughness;
float a2 = a * a;
float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - a2) + a2);
float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - a2) + a2);
return 0.5 / max((GGXV + GGXL), 0.000001);
}
选择原因:
原理:
公式:
$G_{Schlick}(n,v)=\frac{n⋅v}{(n⋅v)(1−k)+k},k=\frac{(α+1)^2}8$
特点:
原理:
公式:
$G_{KSK}=\frac1{1+Λ(v)+Λ(l)}$
应用场景:
实现代码:
hlsl
// Packages/com.unity.render-pipelines.universal/ShaderLibrary/BRDF.hlsl
float V_SmithJointGGX(float NdotL, float NdotV, float roughness)
{
float a2 = roughness * roughness;
float lambdaV = NdotL * (NdotV * (1.0 - a2) + a2);
float lambdaL = NdotV * (NdotL * (1.0 - a2) + a2);
return 0.5 / (lambdaV + lambdaL + 1e-5f);
}
预计算部分项:
hlsl
// 预计算粗糙度平方
float a2 = roughness * roughness;
数值稳定性处理:
hlsl
// 避免除零错误
return 0.5 / (lambdaV + lambdaL + 1e-5f);
移动端简化版:
hlsl
#if defined(SHADER_API_MOBILE)
float V_SmithMobile(float NdotL, float NdotV, float roughness)
{
float a = roughness;
float GGXV = NdotL * (NdotV * (1.0 - a) + a);
float GGXL = NdotV * (NdotL * (1.0 - a) + a);
return 0.5 / (GGXV + GGXL);
}
#endif
| 模型 | 指令数 | 特殊函数 | 移动端适用性 | 视觉质量 |
|---|---|---|---|---|
| Cook-Torrance | 8 | 无 | ★★★★☆ | ★★☆☆☆ |
| Smith完整版 | 15+ | sqrt | ★★☆☆☆ | ★★★★☆ |
| Smith-Schlick | 10 | 无 | ★★★★☆ | ★★★☆☆ |
| URP实现 | 12 | sqrt | ★★★☆☆ | ★★★★☆ |
| Kelemen | 18+ | 复杂运算 | ★☆☆☆☆ | ★★★★★ |
Unity URP的选择在视觉质量和计算开销之间取得了最佳平衡,特别是考虑到现代GPU的架构特性(SIMD执行),即使包含sqrt运算也不会造成显著性能瓶颈。
【从UnityURP开始探索游戏渲染】专栏-直达
(欢迎点赞留言探讨,更多人加入进来能更加完善这个探索的过程,????)