Version: Unity 6.0 (6000.0)
语言 : 中文
使用内置渲染管线中的内置着色器函数
内置渲染管线中的 HLSL 着色器示例

使用 GrabPass 命令获取当前帧缓冲区

GrabPass 是一个创建特殊类型通道的命令,该通道将帧缓冲区的内容抓取到纹理中。在后续通道中即可使用此纹理,从而执行基于图像的高级效果。

此命令会显著增加 CPU 和 GPU 帧时间。除了快速原型制作之外,通常应该避免使用此命令,并尝试通过其他方式实现效果。如果确实使用了此命令,那么尽量减少屏幕抓取操作的次数;可以通过减少对该命令的使用频率来实现,或者在适用的情况下,使用将屏幕内容抓取到指定名称纹理的签名来达成这一目的。

GrabPass 仅适用于帧缓冲区。您不能使用此命令来获取其他渲染目标、深度缓冲区等内容。

示例

这个内置渲染管线的示例演示了使用 GrabPass 来反转渲染目标的颜色。请注意,这不是实现此效果的有效方法,仅用于演示 GrabPass 的使用;使用反转混合模式可以更有效地实现相同的效果。

Shader "GrabPassInvert"
{
    SubShader
    {
        // Draw after all opaque geometry
        Tags { "Queue" = "Transparent" }

        // Grab the screen behind the object into _BackgroundTexture
        GrabPass
        {
            "_BackgroundTexture"
        }

        // Render the object with the texture generated above, and invert the colors
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct v2f
            {
                float4 grabPos : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            v2f vert(appdata_base v) {
                v2f o;
                // use UnityObjectToClipPos from UnityCG.cginc to calculate 
                // the clip-space of the vertex
                o.pos = UnityObjectToClipPos(v.vertex);

                // use ComputeGrabScreenPos function from UnityCG.cginc
                // to get the correct texture coordinate
                o.grabPos = ComputeGrabScreenPos(o.pos);
                return o;
            }

            sampler2D _BackgroundTexture;

            half4 frag(v2f i) : SV_Target
            {
                half4 bgcolor = tex2Dproj(_BackgroundTexture, i.grabPos);
                return 1 - bgcolor;
            }
            ENDCG
        }

    }
}

其他资源

使用内置渲染管线中的内置着色器函数
内置渲染管线中的 HLSL 着色器示例