“游戏中基于物理的渲染”系列由4-5篇文章组成,介绍了如何在实时渲染中使用基于物理的方法。内容主要来自于SIGGRAPH 2010的course:Physically-Based Shading Models in Film and Game Production。本系列文章讲述的渲染技术用在了KlayGE中。
“Physically-Based Rendering in Game” serial, which contains 4-5 articals, introduces how to use physically based method in realtime rendering. The main idea comes from a course of SIGGRAPH 2010: Physically-Based Shading Models in Film and Game Production. The rendering technique in this serial is used in KlayGE 3.11.
引言
Introduction
基于物理的渲染已经提出好多年了,但在游戏中使用的渲染模型仍然是拼凑出来的经验公式(比如Phong)。这些拼凑的模型如果要渲染高质量的图像, 就需要繁复地调参数。而基于物理的、保能量的渲染模型可以很容易地建立出在不同光照环境下都接近真实的材质来。
神奇的是,基于物理的模型并不会比传统上拼 凑的模型更难实现,计算量也差不多。
Physically based rendering have been known for many years, but the “ad-hoc” rendering models (such as Phong) are still widely used in game. These “ad-hoc” models require laborious tweaking to produce high-quality images. However, physically based, energy-conserving rendering models easily create materials that hold up under a variety of lighting environments.
Surprisingly, physically based models are not more difficult to implement or evaluate than the traditional “ad-hoc” ones.
反射方程
Reflectance equation
游戏中最常使用的渲染模型描述的是反射,不考虑SSS等。反射方程可以表示成:
The most common used rendering model in game describes only reflectance, not including terms such as SSS. The reflectance equation is:
其中,表示BRDF,表示光源给的贡献,表示光源和表面法线的夹角。这个积分的结果就是所有光源对一个点的贡献之和。
Here is BRDF, is the contribution from light source, is the angle between light and surface normal. This integration results the sum of all light sources contribute to a surface point.
Diffuse项
Diffuse term
最简单的一个BRDF就是Lambert,在游戏中就是用来表示。但实际上,是属于反射方程的一项,而Lambert则是一个常量:
The simplest BRDF is the Lambert. The well-known Lambertian BRDF in game is present as . However, is part of reflectance equation, and lambertian term is actually a constant value:
本系列的第一篇就介绍到此,下一篇将介绍如何从这两个公式引出其他的基于物理的渲染公式。
精确光源
Punctual Light Sources
游戏中经典的光源有point, directional和spot,这些局部光源都可以抽象成“精确光源”的概念,表示一个方向确定、大小为无穷小的光源。由于要计算的是到达表面点时的光照,所以不考虑从光源到表面之间的衰减。因此,精确光源都可以用颜色和光源方向向量这两个参数来表示。光源颜色的确切定义是,白色的Lambert表面被平行于表面法线()的光照照亮的颜色。
如何计算这个光源对点的贡献呢?这里需要先引入一个叫做微面光源的概念。顾名思义,微面光源是一个非常小的面光源,中心是,张角是。该微面光源照亮表面的一个点可以用来表示。有两个性质:
These are classic point, directional, and spot lights in compute games. These local light source can be abstracted to a concept named “punctual light sources“. They are infinitely small light with centain direction. Since what we want is the lighting reach a surface point, we don’t need to consider the attenuation. A punctual light source are parameterized by the light color and the light direction vector . The light color is specified as the color a white Lambertian surface would have when illuminated by the light from a direction parallel to the surface normal ().
How to calculate the contribution from this light source to a point? Here we will start by defining a tiny area light source centered on , with a small angular extent . This tiny area light illuminates a shaded surface point with the incoming radiance function . The incoming radiance function has the following two properties:
第一个性质表示如果入射方向和的夹角大于, 那么亮度为0。第二个性质是从的定义而来,白色表面使得,结合上文所说的反射方程和Lambert,就可以得出性质二。由于要求,所以也表示了当趋近 于0的时候的极限,也就是
The first property says that no light is incoming for any light directions which form an angle greater than with . The second property follows from the definition of . Since the surface is white, . Applying reflectance equation and Lambert from last article, there’s the second property. Because requires , it is the limit as goes to 0:
因为而且,我们可以认为,所以得到:
Since and , we can assume , which gives us:
也就是
That is:
把微面光源带入一般的BRDF,得到的就是当趋近于0时的极限:
Now we shall apply our tiny area light to a general BRDF, and look at its behavior in the limit as goes to 0:
所以
So
入你所见,刚才引入的微面光源已经从公式中消失,剩下的部分又回到了熟悉的几个项。
Microfacet BRDF
大多数基于物理的specular BRDF的基础是microfacet理论。这个理论是用来描述来自于一般表面(不是光学平滑的)的反射。Microfacet理论的基本假设是,表面是由很多微平面(microfacet)组成,这些微平面都太小了,没有办法一个一个地看到;并假设每个microfacet都是光学平滑的。
每个microfacet把一个入射方向的光反射到单独的一个出射方向,这取决于microfacet的法向m。当计算BRDF的时候,光源方向l和视线方向v都得给定。这意味着在表面上的所有microfacet中,只有刚好把l反射到v的那部分对BRDF有贡献。在下图中,我们可以看到这些有效microfacet的表面法向m正好在l和v的中间,也就是h。
The basis for most physically-based specular BRDF terms is microfacet theory. This theory was developed to describe surface reflection from general (non-optically flat) surfaces. The basic assumption underlying microfacet theory is that the surface is composed of many microfacets, too small to be seen individually. Each microfacet is assumed to be optically flat.
Each of these microfacets reflects light from a given incoming direction into a single outgoing direction which depends on the orientation of the microfacet normal m. When evaluating a BRDF term, both the light direction l and the view direction v are specified. This means that of all the millions of microfacets on the surface, only those that happen to be angled just right to reflect l into v have any contribution to the BRDF value. In the figure below, we can see that these active microfacets have their surface normal m oriented exactly halfway between l and v. We will denote it as h.
并不是所有m = h的microfacet对反射都有贡献。有些microfacet会在光源方向l被其他microfacet阻挡(shadowing),有些会在视线方向v被阻挡(masking),也可能都有。Microfacet理论假设所有被遮挡的没有对BRDF的贡献。Microfacet的specular BRDF可以表达成:
Not all microfacets for which m = h will contribute to the reflection; some are blocked by other microfacets from the direction of l (shadowing), from the direction of v (masking), or from both. Microfacet theory assumes that all shadowed light is lost from the specular term. The microfacet specular BRDF term has the following form:
是有效microfacet(m = h)产生的Fresnel反射。是有效microfacet中没有被shadow或mask的比例。是microfacet的法线分布函数,或者说,microfacet的法线等于h的密度。最后,分母是个校正因子,用来校正从microfacet的局部空间转到整体表面的数量差异。
is the Fresnel reflectance of the active microfacets (m = h). is the proportion of active microfacets which are not shadowed or masked. is the microfacet normal distribution function evaluated at the active microfacet normal m = h; in other words, the concentration of microfacets with normals equal to h. Finally, the denominator is a correction factor which accounts for quantities being transformed between the local space of the microfacets and that of the overall macrosurface.
Fresnel项
Fresnel反射项计算从光学平滑的表面反射的比率。它的值取决于两件事情:入射角(光源方向和表面法线之间的角),以及材质的折射率。Schlick近似的Fresnel项是:
Fresnel Term
The Fresnel reflectance term computes the fraction of light reflected from an optically flat surface. Its value depends on two things: the incoming angle (angle between light vector and surface normal) and the refractive index of the material. The Schlick approximation of Fresnel term is:
其中是specular颜色。
which is the specular color.
法线分布项
在大部分表面上,microfacet的方向不是均匀分布的。Microfacet的法线越接近宏表面的法线,就越光滑。这个分布由microfacet的法线分布函数D(m)来定义。函数D()决定了specular高光的大小、亮度和形状。 法线分布函数一般有类似于“粗糙度”这样的参数(各向异性的函数通常有两个参数)。
遮挡项(shadowing-masking term)
遮挡项G(l, v, h)也经常称为几何项 。在很多情况下,遮挡项在某种程度上抵消了microfacet方程中的分母,替换成类似于这样的形式。遮挡项对BRDF保能量是很重要的——没有这个项的话,BRDF反射出的能量可能比接收到的更多。Microfacet BRDF中很重要的一个事情是把能量从l反射到v的microfacet面积之和(有效面积)和宏表面总面积之间的比例。如果不考虑遮挡,那么有效面积可能会超过总面积,这回造成BRDF不保能量,甚至可能会很明显,如下图所示:
Normal Distribution Term
In most surfaces, the microfacet’s orientations are not uniformly distributed. Microfacet normals closer to the macroscopic surface normal tend to appear with smoother. The exact distribution is defined via the microfacet normal distribution function D(m). The function D() determines the size, brightness, and shape of the specular highlight. Normal distribution functions have some kind of “roughness” or variance parameter (anisotropic functions typically have two variance parameters).
Shadowing-Masking Term
The shadowing and masking term G(l, v, h) is also often called the geometry term. In many cases, the shadowing-masking term partially cancels out the denominator in microfacet BRDF equation, replacing it with some other expression such as . The shadowing-masking term is essential for BRDF energy conservation–without such a term the BRDF can reflect arbitrarily more light energy than it receives. A key part of the microfacet BRDF is the ratio between the active area (combined area of the microfacets which reflect light energy from l to v) and the total area (of the macroscopic surface). If shadowing and masking are not accounted for, then the active area may exceed the total area, an obvious impossibility which can lead to the BRDF not conserving energy, in some cases by a huge amount, as you see below:
在上半部分,平的宏表面为绿色,粗糙的微观表面为蓝色。m = h的microfacet标记为红色。宏表面投到视线方向就是左上角的绿线。同时,投出来的单个红色的microfacet显示成独立的红线。左下图表示在没有遮挡项的情况下,红色的microfacet加起来的面积,结果就是有效面积大于总面积,所以BRDF的反射能量错误地大于了接收能量。右下图里红色区域考虑了遮挡,重叠的区域不再计算多次,所以有效面积小于总面积。
这里先回顾一下第二篇中推导出来的公式:
In my last article “Physically-based Rendering in Game, Part 3“, I explained microfacet BRDF. Here I will talk about the difference between BRDF in most current game and physically-based BRDF.
Firstly we review the equation from my second article:
对比
游戏中常用的的反射方程是这样的:
Comparison
The reflectance equation common used in game look like this:
根据第一篇文章所述,实际上是属于反射方程,而不是diffuse,所以应该改成:
According to my first article, is part of reflectance equation, not diffuse term. So it should be:
其中,是视线方向对法线的反射向量,来自Phong模型。但是上一篇文章所论述的microfacet BRDF中并没有这一项。Blinn-Phong模型的形式和它类似,但用的是更有物理意义的h,也就是microfacet的m。改用Blinn-Phong的话就得到:
where is the view vector reflected about the normal. It’s from Phong model. It doesn’t seem to correspond to anything from microfacet theory in my last article. The Blinn-Phong model is very similar to this equation, but it uses the more physically meaningful half-vector h, which is microfacet’s normal m. Changing from Phong to Blinn-Phong gives us the following model:
从第一篇文章提到的Lambert可以发现,diffuse项已经和基于物理的一样了:
Recall the Lambert term from the first article, the diffuse term now is the same with physically-based one:
所以,现在我们专注于specular项,前面的公式如果只看specular是这样的
So, now we focus on specular term. If we look at the specular term only in the equation before:
比较microfacet specular:
Compare to microfacet specular:
首先,从公式可以看出,游戏中用的应该用表示Fresnel的替换。
第二,上篇文章说过,与粗糙度有关,所以可以认为和Blinn-Phong的功能相似。但是,要把替换成正确的microfacet分布函数,就必须做一个归一化。也就是说,对于任意的视线方向v,微表面投影的面积之和必须等于宏表面投影的面积,数学上就是:
Firstly, we can see from these two equations, the in game can be replaced by Fresnel’s .
Secondly, recall my last article, I mentioned that is related to roughness. So and Blinn-Phong’s have similar functions. However, to convert the term into a microfacet normal distribution function it must be correctly normalized. It means the sum of the signed projected areas of the microfacets needs to equal the signed projected area of the macroscopic surface; this must hold true for any viewing direction. Mathematically, this means that the function must fulfil this equation for any v:
既然这个方程是对任何视线方向都得成立,那么对于特殊情况v = n也得成立:
This equation holds for any viewing direction. In the special case, v = n:
Blinn-Phong的项如果也要满足这个方程,就得乘上一个归一化系数:
The Blinn-Phong cosine power term can be made to obey this equation by multiplying it with a simple normalization factor:
现在剩下的一项,就是:
The only term left is:
这个项的物理意义是可见性(遮挡项除以校正因子)。我们修改过的specular模型并不打算包含可见性,所以简单地把这个项设成1就行了,也就是:
It’s shadowing/masking, or geometry term divided by the foreshortening factors. The physical meaning of this term is visibility. Since our modified specular model has no visibility term, we will simply set it to 1. That is:
实际上,这对于仅包含height field的微表面是成立的(Blinn-Phong的表面分布函数也对应于此,因为所有背面的microfacet都等于0)。当l = n而且v = n的时候等于1, 这对于height field也是正确的(在宏表面的法线方向没有任何遮挡)。对于很斜的视角或者很斜的光线角都趋向于0,这也是正确的(microfacet被其他microfacet遮挡的可能性随着视角的增加而增加)。
经过整理,来自microfacet specular BRDF的几个项都明朗了,于是得到下面的渲染模型:
This is actually a plausible geometry term for a heightfield microsurface (which is what the Blinn-Phong normal distribution function corresponds to, since it is zero for all backfacing microfacets). is equal to 1 when l = n and v = n, which is correct for a heightfield (no microfacets are occluded from the direction of the macrosurface normal). It goes to 0 for either glancing view angles or glancing light angles, which again is correct (the probability of a microfacet being occluded by other microfacets increases with viewing angle).
Until now, several terms from microfacet specular BRDF are all explained, leaving us this rendering model:
前面的系数是因为。
把diffuse加回去,就得到:
The factor is because of .
Add the diffuse term back, that is:
这就是本系列文章最终推出的基于物理的渲染模型。对比原先游戏中使用的模型,计算量增加非常少,但不但保证了保能量,还能让美术更容易调整参数。
未来的工作
一个没有解决的问题是specular的power很高,而且非常光滑的表面。这样的材料对于渲染湿的表面这样的东西很重要。但是,精确光源的近似在这种情况下会挂掉,结果就是产生subpixel大小的极端强烈的高光,很不自然。实际上这种情况需要的是反射出光源的形状,这需要快到适合在游戏中使用的面光源近似。
另一个问题是有很多中geometry项,有没有能比提供更好的视觉效果,同时开销很小的函数?候选之一是Kelemen et. al提出的,是对Cook-Torrance的geometry项的近似,但计算开销很小:
The most import equation of this serial is shown in my last article. This one is the last article in this serial. I’ll propose some future works, and will present some rendering results of physically-based model here.
Future works
A unsolved problem occurs in the context of very smooth surfaces with high specular powers. Such materials are important to model e.g., wet surfaces. However, the punctual light approximation breaks down in this case, yielding extremely intense highlights of subpixel size that are unrealistic and alias badly. What we would like to see is a sharp reflection of the shape of the light source, which requires some kind of area light approximation which is fast enough to use in games.
Another problem is that there are a variety of geometry terms in the literature. Do any of them provide a visual improvement over the “cheaper-than-free” implicit geometry function that is worth the extra cost? One candidate is the geometry factor proposed by Kelemen et. al. This is an approximation to the Cook-Torrance geometry factor but it is far cheaper to compute:
其中的在Fresnel项中就有,可以重复利用。这个很接近完整的Cook-Torrance的geometry项除掉校正因子。
第三个问题是diffuse项的Fresnel。在现实中,没有“不带specular的表面”,这样的材质实际上值在0.03-0.06之间,而且的值非常小(0.1-2.0之间)。在很斜的角度下,即使最粗糙的表面也有感觉得到的specular。缺乏这种效果是另一种游戏看起来不真实的原因。
note that also exists in any case for Fresnel. It’s a close approximation to the full Cook-Torrance geometry factor divided by the foreshortening terms.
The third problem is the Fresnel of diffuse term. It should be noted that there is no such thing as “a surface without specular”. In reality such materials have values around 0.03-0.06, and very low values of (around 0.1-2.0). At glancing angles, even the most “matte” surfaces have noticeable specular appearance; the lack of this effect is another reason why so many game environments appear unrealistic.
渲染一个红色塑料球。下面一排图用的specular项是归一化过的系数渲染的,用的(差不多是塑料的值)。上一排图不带归一化系数,用的值选成让最左的两张图看着一样。可以看出下一排的高光随着变小而变亮,这是正确的现象——出射光越小就越集中。上一排里,高光变小的时候亮度不变,所以有能量损失,表面反射率也下降了。图片来自Real-Time Rendering, 3rd edition。
更多来自Crytek的图片:
Rendered images of a red plastic sphere. The bottom row of images was rendered with a normalization factor applied to the specular term, using (an appropriate value for plastic). The top row of images was rendered without a normalization factor, using a value of chosen so that the two leftmost images match. It can be seen that in the bottom row, the highlight grows much brighter as it gets narrower, which is the correct behavior-the outgoing light is concentrated in a narrower cone. In the top row, the highlight remains equally bright as it narrows, so there is a loss of energy and surface reflectance appears to decrease. (image from “Real-Time Rendering, 3rd edition”)
More pictures from Crytek:
本系列到此结束。谢谢大家的支持,祝大家新年快乐!