unity shader——BRDF分析

在这里插入图片描述
在这个文件中,我们先来看一个函数:

// Based on Minimalist CookTorrance BRDF 
// Implementation is slightly different from original derivation: http://www.thetenthplanet.de/archives/255
// * NDF (depending on UNITY_BRDF_GGX):
//  a) BlinnPhong
//  b) [Modified] GGX
// * Modified Kelemen and Szirmay-​Kalos for Visibility term
// * Fresnel approximated with 1/LdotH
half4 BRDF2_Unity_PBS (half3 diffColor, half3 specColor, half oneMinusReflectivity, half smoothness,
    float3 normal, float3 viewDir,
    UnityLight light, UnityIndirect gi)
{

最简化的CookTorrance模型的BRDF。

参数的解释:
diffColor——漫反射颜色
specColor——镜面反射颜色
oneMinusReflectivity——1减去反射率
smoothness——光滑度
normal——法线
viewDir——视角向量
light——unity内置光源结构体,直接光UnityLight
gi——全局光照gi结构体,间接光UnityIndirect

这两个结构体在UnityLightingCommon.cginc中:

struct UnityLight
{
    half3 color;
    half3 dir;
    half  ndotl; // Deprecated: Ndotl is now calculated on the fly and is no longer stored. Do not used it.
};

struct UnityIndirect
{
    half3 diffuse;
    half3 specular;
};

接着函数:

float3 halfDir = Unity_SafeNormalize (float3(light.dir) + viewDir);
half nl = saturate(dot(normal, light.dir));
float nh = saturate(dot(normal, halfDir));
half nv = saturate(dot(normal, viewDir));
float lh = saturate(dot(light.dir, halfDir));

1、计算了光方向和视角向量的半角向量
2、ndotl
3、nh
4、nv
5、lh

// Specular term
half perceptualRoughness = SmoothnessToPerceptualRoughness (smoothness);
half roughness = PerceptualRoughnessToRoughness(perceptualRoughness);

perceptual是知觉的意思。SmoothnessToPerceptualRoughness 函数和PerceptualRoughnessToRoughness函数的原型如下:

float SmoothnessToPerceptualRoughness(float smoothness)
{
    return (1 - smoothness);
}
float PerceptualRoughnessToRoughness(float perceptualRoughness)
{
    return perceptualRoughness * perceptualRoughness;
}
#if UNITY_BRDF_GGX

    // GGX Distribution multiplied by combined approximation of Visibility and Fresnel
    // See "Optimizing PBR for Mobile" from Siggraph 2015 moving mobile graphics course
    // https://community.arm.com/events/1155
    half a = roughness;
    float a2 = a*a;
    float d = nh * nh * (a2 - 1.f) + 1.00001f;
#ifdef UNITY_COLORSPACE_GAMMA

https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf

5.1 choosing F, D, and G
using equations 20 and 21, requires appropriate choices for the F, D, and G, terms. the fresnel term is the best understood, and exact equations are available in the literature. the fresnel term is typically small at normal incidence (e.g., 0.04 for glass with ηt = 1.5) and increases to unity at grazing angles or for total internal reflection. a convenient exact for mulation for dielectrics with unpolarized light is:
在这里插入图片描述

note that if g is imaginary 虚数, this indicates total internal reflection and F=1 in this case. cheaper approximations for F are also sometimes used.
a wide variety of microfacet distribution functions D have been proposed. in this paper, we discuss three different types: beckmann, phong, and ggx. the beckmann distribution arises from guassian roughness assumptions for the microsurfuace and is widely used in the optics literature. the phong distribution is purely empirical one developed in the graphics literature; however, with suitable choices of width parameters it is very similar to the beckmann distribution. the ggx distribution is new, and we developed it to better match our measured data for transmission. equations for the three distribution types and related functions are given at the end of this section.

the shadowing-masking term G depends on the distribution function D and the details of the microsurface, so exact solutions are rarely possible. cook & torrance used a G based on a 1D model of parallel grooves that guarantees energy conservation for any distribution D, but we do not recommend using it because it contains first derivative discontinuities and other features not seen in real surfaces. instead we will use the smith shadowing-masking approximation. the smith G was originally derived for gaussian rough surfaces, but has since been extend to handle surfaces with arbitrary distribution functions though in some cases, (e.g…, phong), the resulting integrals have no simple closed form solution.

the smith G approximates the bidirectional shadowing masking as the separable product of two monodirectional shadowing terms G1:
在这里插入图片描述
where G1 is derived form the microfacet distribution D as described in [SMITH B. G.: Geometrical shadowing of a random rough surface. IEEE Trans. on Antennas and Propagation (1967), 668–671.] and appendix A.
smith actually derived two different shadowing functions: one when the microsurface normal m is known, and another averaged over all microsurface normals. although the latter is more frequently used in the literature, in microfacet models, where we know the microsurface normal of interest, the former is more appropriate and we use it in this paper.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值