September 10, 2014

# 移动平台上基于物理的着色

#### 环境 BRDF

half3 EnvBRDFApprox( half3 SpecularColor, half Roughness, half NoV )

{

const half4 c0 = { -1, -0.0275, -0.572, 0.022 };

const half4 c1 = { 1, 0.0425, 1.04, -0.04 };

half4 r = Roughness * c0 + c1;

half a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;

half2 AB = half2( -1.04, 1.04 ) * a004 + r.zw;

return SpecularColor * AB.x + AB.y;

}

##### 在移动平台上使用的ENVBRDF近似贴图

half EnvBRDFApproxNonmetal( half Roughness, half NoV )

{

// Same as EnvBRDFApprox( 0.04, Roughness, NoV )

const half2 c0 = { -1, -0.0275 };

const half2 c1 = { 1, 0.0425 };

half2 r = Roughness * c0 + c1;

return min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;

}

EnvBRDFApprox( SpecularColor, 1, 1 ) == SpecularColor * 0.4524 - 0.0024

DiffuseColor += SpecularColor * 0.45;

SpecularColor = 0;

#### 定向光源（Directional Light）

$$\newcommand{\nv}{\mathbf{n}} \newcommand{\lv}{\mathbf{l}} \newcommand{\vv}{\mathbf{v}} \newcommand{\hv}{\mathbf{h}} \newcommand{\mv}{\mathbf{m}} \newcommand{\rv}{\mathbf{r}} \newcommand{\ndotl}{\nv\cdot\lv} \newcommand{\ndotv}{\nv\cdot\vv} \newcommand{\ndoth}{\nv\cdot\hv} \newcommand{\ndotm}{\nv\cdot\mv} \newcommand{\vdoth}{\vv\cdot\hv} D_{Blinn}(\hv) = \frac{1}{ \pi \alpha^2 } (\ndoth)^{ \left( \frac{2}{ \alpha^2 } - 2 \right) } \approx \frac{1}{ \pi \alpha^2 } (\rv\cdot\lv)^{ \left( \frac{1}{ 2 \alpha^2 } - \frac{1}{2} \right) }$$

$\rv$ 为反射方向时， $\rv = 2(\ndotv)\nv - \vv$

Phong进一步使用球形高斯来近似处理approximated with a Spherical Gaussian）：

$$x^n \approx e^{ (n + 0.775) (x - 1) }$$

half D_Approx( half Roughness, half RoL )

{

half a = Roughness * Roughness;

half a2 = a * a;

float rcp_a2 = rcp(a2);

// 0.5 / ln(2), 0.275 / ln(2)

half c = 0.72134752 * rcp_a2 + 0.39674113;

return rcp_a2 * exp2( c * RoL - c );

}

Epic Games, Inc.赋予您使用、复制、修改并发布本文中您认为无需版权处理的代码示例的权利。