Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Cg Programming/Unity/Order-Independent Transparency

From Wikibooks, open books for an open world
<Cg Programming |Unity
“Where Have You Bean” by flickr user Ombligotron. The typo in the title refers to the depicted sculpture “Cloud Gate” a.k.a. “The Bean”.

This tutorial coversorder-independent blending.

It continues the discussion inSection “Transparency” and solves some problems of standard transparency. If you haven't read that tutorial, you should read it first.

“84 – Father son” by Ben Newton. An example of double exposure.

Order-Independent Blending

[edit |edit source]

As noted inSection “Transparency”, the result of blending often (in particular for standard alpha blending) depends on the order in which triangles are rendered and therefore results in rendering artifacts if the triangles are not sorted from back to front (which they usually aren't). The term “order-independent transparency” describes various techniques to avoid this problem. One of these techniques is order-independent blending, i.e. the use of a blend equation that does not depend on the order in which triangles are rasterized. There are two basic possibilities: additive blending and multiplicative blending.

Additive Blending

[edit |edit source]

The standard example for additive blending are double exposures as in the images in this section: colors are added such that it is impossible (or at least very hard) to say in which order the photos were taken. Additive blending can be characterized in terms of the blend equation introduced inSection “Transparency”:

float4 result = SrcFactor * fragment_output + DstFactor * pixel_color;

wherefragment_output is the output of the fragment shader, andpixel_color is the color that is already in the framebuffer whileSrcFactor andDstFactor are determined by a line in Unity's ShaderLab syntax:

Blend {code forSrcFactor} {code forDstFactor}

For additive blending, the code forDstFactor has to beOne and the code forSrcFactor must not depend on the pixel color in the framebuffer; i.e., it can beOne,SrcColor,SrcAlpha,OneMinusSrcColor, orOneMinusSrcAlpha.

An example is:

Shader"Cg shader using additive blending"{SubShader{Tags{"Queue"="Transparent"}// draw after all opaque geometry has been drawnPass{CullOff// draw front and back facesZWriteOff// don't write to depth buffer// in order not to occlude other objectsBlendSrcAlphaOne// additive blendingCGPROGRAM#pragma vertex vert#pragma fragment fragfloat4vert(float4vertexPos:POSITION):SV_POSITION{returnUnityObjectToClipPos(vertexPos);}float4frag(void):COLOR{returnfloat4(1.0,0.0,0.0,0.2);}ENDCG}}}

Multiplicative Blending

[edit |edit source]

An example for multiplicative blending in photography is the use of multiple uniform grey filters: the order in which the filters are put onto a camera doesn't matter for the resulting attenuation of the image. In terms of the rasterization of triangles, the image corresponds to the contents of the framebuffer before the triangles are rasterized, while the filters correspond to the triangles.

When specifying multiplicative blending in Unity with the line

Blend {code forSrcFactor} {code forDstFactor}

the code forSrcFactor has to beZero and the code forDstFactor must depend on the fragment color; i.e., it can beSrcColor,SrcAlpha,OneMinusSrcColor, orOneMinusSrcAlpha. A typical example for attenuating the background with the opacity specified by the alpha component of fragments would useOneMinusSrcAlpha for the code forDstFactor:

Shader"Cg shader using multiplicative blending"{SubShader{Tags{"Queue"="Transparent"}// draw after all opaque geometry has been drawnPass{CullOff// draw front and back facesZWriteOff// don't write to depth buffer// in order not to occlude other objectsBlendZeroOneMinusSrcAlpha// multiplicative blending// for attenuation by the fragment's alphaCGPROGRAM#pragma vertex vert#pragma fragment fragfloat4vert(float4vertexPos:POSITION):SV_POSITION{returnUnityObjectToClipPos(vertexPos);}float4frag(void):COLOR{returnfloat4(1.0,0.0,0.0,0.2);}ENDCG}}}

Complete Shader Code

[edit |edit source]

Finally, it makes good sense to combine multiplicative blending for the attenuation of the background and additive blending for the addition of colors of the triangles in one shader by combining the two passes that were presented above. This can be considered an approximation to alpha blending forsmall opacities, i.e.small values of alpha, if one ignores attenuation of colors of the triangle mesh by itself.

Shader"Cg shader using order-independent blending"{SubShader{Tags{"Queue"="Transparent"}// draw after all opaque geometry has been drawnPass{CullOff// draw front and back facesZWriteOff// don't write to depth buffer// in order not to occlude other objectsBlendZeroOneMinusSrcAlpha// multiplicative blending// for attenuation by the fragment's alphaCGPROGRAM#pragma vertex vert#pragma fragment fragfloat4vert(float4vertexPos:POSITION):SV_POSITION{returnUnityObjectToClipPos(vertexPos);}float4frag(void):COLOR{returnfloat4(1.0,0.0,0.0,0.2);}ENDCG}Pass{CullOff// draw front and back facesZWriteOff// don't write to depth buffer// in order not to occlude other objectsBlendSrcAlphaOne// additive blending to add colorsCGPROGRAM#pragma vertex vert#pragma fragment fragfloat4vert(float4vertexPos:POSITION):SV_POSITION{returnUnityObjectToClipPos(vertexPos);}float4frag(void):COLOR{returnfloat4(1.0,0.0,0.0,0.2);}ENDCG}}}

Note that the order of the two passes is important: first the background is attenuated and then colors are added.

Summary

[edit |edit source]

Congratulations, you have reached the end of this tutorial. We have looked at:

  • What order-independent transparency and order-independent blending is.
  • What the two most important kinds of order-independent blending are (additive and multiplicative).
  • How to implement additive and multiplicative blending.
  • How to combine two passes for additive and multiplicative blending for an order-independent approximation to alpha blending.

Further reading

[edit |edit source]

If you still want to know more

  • about the shader code, you should readSection “Transparency”.
  • about another technique for order-independent transparency, namely depth peeling, you could read a technical report by Cass Everitt: “Interactive Order-Independent Transparency”, which is availableonline.

<Cg Programming/Unity

Unless stated otherwise, all example source code on this page is granted to the public domain.
Retrieved from "https://en.wikibooks.org/w/index.php?title=Cg_Programming/Unity/Order-Independent_Transparency&oldid=3684687"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp