GrabPass は特別のパスタイプで、描画されるときの画面のコンテンツを、テクスチャ内に取得します。このテクスチャは後続プロセスで高度な画像ベースのエフェクトに使用できます。
GrabPass は サブシェーダー の中にあります。二つの形式のいずれかをとります。
GrabPass { }
と指定し、単に現在の画面コンテンツをテクスチャ内に取得します。このテクスチャは後続のパスで _GrabTexture
の名でアクセスすることができます。注意:この形式の Grap Pass は時間がかかる画面の取得処理を、使用する各オブジェクトごとに行います。GrabPass { "TextureName" }
により現在の画面コンテンツをテクスチャ内に取得しますが、フレームごとに同じテクスチャを使用するはじめのオブジェクトにのみ実施します。テクスチャは後続のパスで指定のテクスチャ名でアクセスすることができます。シーンで Grab Pass を使用するオブジェクトが複数ある場合に効率的な方法です。さらに、GrabPass は Name や Tags コマンドを使用することができます。
次の例は、前にレンダリングされたカラーを反転する高負荷な方法です。
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
}
}
}
このシェーダーには 2つのパスがあります。最初のパスはレンダリングのときにオブジェクトにあるすべてを取得し、次にそれを 2つめのパスで適用します。当然、同じエフェクトはより低負荷に反転ブレンドモード を使用して得ることができます。
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.