Real-Time Rendering——5.5.1 Blending Order 混合顺序

To make an object appear transparent, it is rendered on top of the existing scene with an alpha of less than 1.0. Each pixel covered by the object will receive a resulting RGBα (also called RGBA) from the pixel shader. Blending this fragment’s value with the original pixel color is usually done using the over operator, as follows:

为了使对象看起来透明,它以小于1.0的alpha渲染在现有场景的顶部。对象覆盖的每个像素将从像素着色器接收结果RGBα(也称为RGBA)。通常使用over运算符将该片段的值与原始像素颜色混合,如下所示:

where cs is the color of the transparent object (called the source), αs is the object’s alpha, cd is the pixel color before blending (called the destination), and co is the resulting color due to placing the transparent object over the existing scene. In the case of the rendering pipeline sending in cs and αs, the pixel’s original color cd gets replaced by the result co. If the incoming RGBα is, in fact, opaque (αs = 1.0), the equation simplifies to the full replacement of the pixel’s color by the object’s color. 

其中Cs是透明对象的颜色(称为源),αs是对象的alpha,cd是混合前的像素颜色(称为目标),co是由于将透明对象放置在现有场景上而产生的颜色。在渲染管道发送cs和αs的情况下,像素的原始颜色cd被结果co替换。如果传入的RGBα实际上是不透明的(αs = 1.0),则等式简化为用对象的颜色完全替换像素的颜色。

Example: Blending. A red semitransparent object is rendered onto a blue background.Say that at some pixel the RGB shade of the object is (0.9, 0.2, 0.1), the background is (0.1, 0.1, 0.9), and the object’s opacity is set at 0.6. The blend of these two colors is then

例如:混合。红色半透明对象呈现在蓝色背景上。假设在某个像素处,对象的RGB阴影为(0.9,0.2,0.1),背景为(0.1,0.1,0.9),对象的不透明度设置为0.6。这两种颜色的混合就是

0.6(0.9, 0.2, 0.1) + (1 − 0.6)(0.1, 0.1, 0.9),which gives a color of (0.58, 0.16, 0.42).

0.6(0.9, 0.2, 0.1) + (1 − 0.6)(0.1, 0.1, 0.9), 这给出了(0.58,0.16,0.42)的颜色。

The over operator gives a semitransparent look to the object being rendered.Transparency done this way works, in the sense that we perceive something as transparent whenever the objects behind can be seen through it. Using over simulates the real-world effect of a gauzy fabric. The view of the objects behind the fabric are partially obscured—the fabric’s threads are opaque. In practice, loose fabric has an alpha coverage that varies with angle. Our point here is that alpha simulates how much the material covers the pixel.

over操作符为正在渲染的对象提供半透明的外观。以这种方式实现的透明是有效的,在这个意义上,无论什么时候透过它可以看到后面的物体,我们都会感觉到它是透明的。使用over模拟薄纱织物的真实效果。织物后面的对象视图被部分遮挡,织物的线是不透明的。实际上,松散的织物具有随角度变化的alpha覆盖率。我们这里的重点是alpha模拟了材质覆盖像素的程度。

The over operator is less convincing simulating other transparent effects, most notably viewing through colored glass or plastic. A red filter held in front of a blue object in the real world usually makes the blue object look dark, as this object reflects little light that can pass through the red filter. See Figure 5.32. When over is used for blending, the result is a portion of the red and the blue added together. It would be better to multiply the two colors together, as well as adding in any reflection off the transparent object itself. This type of physical transmittance is discussed in Sections 14.5.1 and 14.5.2.

over操作符在模拟其他透明效果时不太令人信服,最明显的是透过有色玻璃或塑料观看。在现实世界中,将红色滤镜放在蓝色物体前面通常会使蓝色物体看起来很暗,因为这个物体反射的光很少,无法穿过红色滤镜。参见图5.32。当over用于混合时,结果是红色和蓝色的一部分相加在一起。最好将两种颜色相乘,并加入透明物体本身的反射。第14.5.1节和第14.5.2节讨论了这种物理透射率。

Figure 5.32. A red gauzy square of fabric and a red plastic filter, giving different transparency effects.Note how the shadows also differ. (Photograph courtesy of Morgan McGuire.) 

图5.32。一个红色薄纱正方形的织物和一个红色塑料过滤器,产生不同的透明效果。 注意阴影也有所不同。(照片由摩根·麦奎尔提供。)

Of the basic blend stage operators, over is the one commonly used for a transparency effect. Another operation that sees some use is additive blending,where pixel values are simply summed. That is,

在基本的混合阶段操作符中,over是常用于透明效果的操作符。另一个有用的操作是加法混合,像素值被简单地相加。也就是说,

This blending mode can work well for glowing effects such as lightning or sparks that do not attenuate the pixels behind but instead only brighten them. However, this mode does not look correct for transparency, as the opaque surfaces do not appear filtered. For several layered semitransparent surfaces, such as smoke or fire,additive blending has the effect of saturating the colors of the phenomenon. 

这种混合模式可以很好地处理发光效果,如闪电或火花,这些效果不会减弱后面的像素,而只会使它们变亮。但是,这种模式看起来不适合透明度,因为不透明的曲面看起来没有经过过滤。对于几个分层的半透明表面,如烟或火,加法混合具有使现象的颜色饱和的效果。

To render transparent objects properly, we need to draw them after the opaque objects. This is done by rendering all opaque objects first with blending off, then rendering the transparent objects with over turned on. In theory we could always have over on, since an opaque alpha of 1.0 would give the source color and hide the destination color, but doing so is more expensive, for no real gain.

为了正确渲染透明物体,我们需要在不透明物体之后绘制它们。这是通过首先在关闭混合的情况下渲染所有不透明对象,然后在打开over的情况下渲染透明对象来实现的。理论上,我们可以一直使用,因为1.0的不透明alpha会给出源颜色并隐藏目标颜色,但是这样做代价更大,而且没有真正的好处。

A limitation of the z-buffer is that only one object is stored per pixel. If several transparent objects overlap the same pixel, the z-buffer alone cannot hold and later resolve the effect of all the visible objects. When using over the transparent surfaces at any given pixel generally need to be rendered in back-to-front order. Not doing so can give incorrect perceptual cues. One way to achieve this ordering is to sort individual objects by, say, the distance of their centroids along the view direction. This rough sorting can work reasonably well, but has a number of problems under various circumstances. First, the order is just an approximation, so objects classified as more distant may be in front of objects considered nearer. Objects that interpenetrate are impossible to resolve on a per-mesh basis for all view angles, short of breaking each mesh into separate pieces. See the left image in Figure 5.33 for an example. Even a single mesh with concavities can exhibit sorting problems for view directions where it overlaps itself on the screen.

z-buffer的限制是每个像素只存储一个对象。如果几个透明对象重叠在同一个像素上,单独的z缓冲区无法保存并在以后解析所有可见对象的效果。当在任何给定像素的透明表面上使用时,通常需要以从后到前的顺序进行渲染。不这样做可能会给出不正确的感知提示。实现这种排序的一种方法是按单个对象的质心沿视图方向的距离进行排序。这种粗略的排序可以相当好地工作,但是在各种情况下有许多问题。首先,这个顺序只是一个近似值,因此被归类为较远的物体可能在被认为较近的物体之前。除了将每个网格分成单独的部分之外,不可能在每个网格的基础上为所有视角解析相互穿透的对象。例子见图5.33中的左图。即使是带有凹面的单个网格,在屏幕上重叠时,也会出现视图方向的排序问题。

Figure 5.33. On the left the model is rendered with transparency using the z-buffer. Rendering the mesh in an arbitrary order creates serious errors. On the right, depth peeling provides the correct appearance, at the cost of additional passes. (Images courtesy of NVIDIA Corporation.) 

图5.33。在左侧,模型使用z缓冲区进行透明渲染。以任意顺序渲染网格会产生严重的错误。在右侧,深度剥皮提供了正确的外观,但代价是增加了passes。(图片由英伟达公司提供。)

Nonetheless, because of its simplicity and speed, as well as needing no additional memory or special GPU support, performing a rough sort for transparency is still commonly used. If implemented, it is usually best to turn off z-depth replacement when performing transparency. That is, the z-buffer is still tested normally, but surviving surfaces do not change the z-depth stored; the closest opaque surface’s depth is left intact. In this way, all transparent objects will at least appear in some form,versus suddenly appearing or disappearing when a camera rotation changes the sort order. Other techniques can also help improve the appearance, such as drawing each transparent mesh twice as you go, first rendering backfaces and then frontfaces

尽管如此,由于其简单性和速度,以及不需要额外的内存或特殊的GPU支持,执行透明的粗略排序仍然是常用的。如果实现,通常最好在执行透明时关闭z深度替换。也就是说,z缓冲区仍然正常测试,但是幸存的表面不会改变存储的z深度;最接近的不透明表面的深度保持不变。这样,所有透明物体至少会以某种形式出现,而不是在相机旋转改变排序顺序时突然出现或消失。其他技术也有助于改善外观,例如在进行过程中对每个透明网格绘制两次,首先渲染背面,然后渲染正面

The over equation can also be modified so that blending front to back gives the same result. This blending mode is called the under operator:

也可以修改上面的等式,使得从前到后混合给出 同样的结果。这种混合模式称为under操作符:

Note that under requires the destination to maintain an alpha value, which over does not. In other words, the destination—the closer transparent surface being blended under—is not opaque and so needs to have an alpha value. The under formulation is like over, but with source and destination swapped. Also, notice that the formula for computing alpha is order-independent, in that the source and destination alphas can be swapped, with the same final alpha being the result. 

请注意,under要求目的地保持一个alpha值,而over则不要求。换句话说,目标(正在混合的更接近的透明曲面)是透明的,因此需要有alpha值。under公式类似于over,但是源和目的交换了。另外,请注意,计算alpha的公式是与顺序无关的,因为源alpha和目标alpha可以交换,最终结果是相同的alpha。

The equation for alpha comes from considering the fragment’s alphas as coverages.Porter and Duff note that since we do not know the shape of the coverage area for either fragment, we assume that each fragment covers the other in proportion to its alpha. For example, if αs = 0.7, the pixel is somehow divided into two areas, with 0.7 covered by the source fragment and 0.3 not. Barring any other knowledge, the destination fragment covering, say, αd = 0.6 will be proportionally overlapped by the source fragment. This formula has a geometric interpretation, shown in Figure 5.34.

alpha的等式来自于将片段的alpha视为覆盖范围。Porter and Duff注意到,由于我们不知道任何一个片段的覆盖区域的形状,我们假设每个片段覆盖另一个片段与其alpha成比例。例如,如果αs = 0.7,像素以某种方式分为两个区域,其中0.7被源片段覆盖,0.3没有被源片段覆盖。排除任何其他知识,覆盖例如αd = 0.6的目的片段将按比例被源片段重叠。这个公式有一个几何解释,如图5.34所示。

Figure 5.34. A pixel and two fragments, s and d. By aligning the two fragments along different axes, each fragment covers a proportional amount of the other, i.e., they are uncorrelated. The area covered by the two fragments is equivalent to the under output alpha value αs − αsαd + αd. This translates to adding the two areas, then subtracting the area where they overlap. 

图5.34。一个像素和两个片段,s和d。通过沿不同的轴对准两个片段,每个片段覆盖另一个片段的比例量,即它们是不相关的。两个片段覆盖的面积等于under 输出alpha值 αs − αsαd + αd,这相当于将两个面积相加,然后减去它们重叠的面积。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子糖莫莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值