包含此页的版本:
不含此页的版本:
GrabPass 是一种命令,用于创建一种特殊类型的 Pass,用于将帧缓冲区的内容抓取到纹理中。此纹理可用于后续通道,以执行基于图像的高级效果。
此命令可以显着增加 CPU 和 GPU 帧时间。通常,除了快速原型设计之外,您应避免使用此命令,并尝试以其他方式实现效果。如果您确实使用此命令,请尽量减少屏幕抓取作的次数;通过减少此命令的使用,或使用将屏幕抓取到命名纹理(如果适用)的签名。
GrabPass 仅适用于帧缓冲区。您不能使用此命令来抓取其他渲染目标的内容,即深度缓冲区 保存图像中每个像素的 z 值深度的内存存储,其中 z 值是投影平面中每个呈现像素的深度。更多信息
请参阅术语表,依此类推。
此示例适用于内置渲染管线(Render Pipeline) 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表演示使用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
{
HLSLPROGRAM
#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;
}
ENDHLSL
}
}
}
某些 GPU(尤其是 iOS 上基于 PowerVR 的 GPU)允许您通过提供当前片段颜色作为片段的输入来执行某种形式的可编程混合着色器在 GPU 上运行的程序。更多信息
请参阅术语表(参考EXT_shader_framebuffer_fetch在 khronos.org)。此过程有时称为帧缓冲区获取。
为此,请使用inoutcolor 参数。例如:
HLSLPROGRAM
// only compile Shader for platforms that can potentially
// do it (currently gles,gles3,metal)
#pragma only_renderers framebufferfetch
void frag (v2f i, inout half4 ocol : SV_Target)
{
// ocol can be read (current framebuffer color)
// and written into (will change color to that one)
// ...
}
ENDHLSL