GrabPass 是一个创建特殊类型通道的命令,该通道将帧缓冲区的内容抓取到纹理中。在后续通道中即可使用此纹理,从而执行基于图像的高级效果。
此命令会显著增加 CPU 和 GPU 帧时间。除了快速原型制作之外,您通常应该避免使用此命令,并尝试通过其他方式实现您的效果。如果您确实使用了此命令,尽量减少屏幕抓取操作的次数;方法是减少您对该命令的使用,或者使用将屏幕抓取到命名纹理的签名(如果适用)。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
GrabPass | 是 | 否 | 否 | 否 |
在 SubShader 代码块中使用此命令。
GrabPass 仅适用于帧缓冲区。您不能使用此命令来获取其他渲染目标、深度缓冲区等的内容。
请注意,这两种不同的签名具有不同的功能和不同的性能影响。使用命名纹理可以显著减少屏幕抓取操作,从而减少此资源密集型命令的影响。
签名 | 功能 |
---|---|
GrabPass { } |
将帧缓冲区内容抓取到一个纹理中,使您可以在同一个子着色器中的后续通道中使用该纹理。 使用 _GrabTexture 名称引用该纹理。 当您使用此签名时,Unity 每次渲染包含此命令的批处理时都会抓取屏幕。这意味着 Unity 可以每帧多次抓取屏幕:每批次一次。 |
GrabPass { "ExampleTextureName" } |
将帧缓冲区内容抓取到一个纹理中,使您可以在同一帧的后续通道中跨多个子着色器访问该纹理。 使用给定名称引用该纹理。 当您使用此签名时,Unity 会在渲染批处理的帧中第一次抓取屏幕,该批处理包含具有给定纹理名称的此命令。 |
内置渲染管线的这个示例演示了使用 GrabPass
来反转渲染目标的颜色。请注意,这不是实现此效果的有效方法,仅用于演示 GrabPass 的使用;使用反转混合模式可以更有效地实现相同的效果。
Shader "GrabPassInvert"
{
SubShader
{
// 在所有不透明几何体之后绘制
Tags { "Queue" = "Transparent" }
// 将对象后面的屏幕抓取到 _BackgroundTexture
中 GrabPass
{
"_BackgroundTexture"
}
// 使用上面生成的纹理渲染对象,并反转颜色
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;
// 使用 UnityCG.cginc 中的 UnityObjectToClipPos 计算
// 顶点的裁剪空间
o.pos = UnityObjectToClipPos(v.vertex);
// 使用 UnityCG.cginc 中的 ComputeGrabScreenPos 函数
// 获得正确的纹理坐标
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
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.