原文鏈接:https://docs.unity3d.com/Manual/SL-GrabPass.html
? ? ? ? GrabPass是一個特殊的pass類型仙辟,它可以抓取屏幕上顯示的內(nèi)容然后將這個對象繪制到一個紋理中。此紋理可用于后面的pass中以實現(xiàn)更進(jìn)一步的圖像效果。
語法
? ? ? ? GrabPass在subshader內(nèi)部,他有兩種存在形式:
? ? ? ? ·只通過GrabPass{}抓取屏幕內(nèi)容到一張紋理上。這個紋理可以在后面的pass中通過“_GrabTexture”這個名字訪問到。請注意,這種形式的GrabPass會為每個使用它的對象進(jìn)行一次耗時的屏幕抓取工作沦寂。
? ? ? ? ·GrabPass{"TextureName"}抓取當(dāng)前的屏幕內(nèi)容到一張紋理中,但是每幀只會為使用給定紋理名字的的第一個對象執(zhí)行一次淘衙。這個紋理可以在后面的pass中通過給定的名字訪問到传藏。當(dāng)你在場景中有多個對象使用GrabPass時,這是一個更有效率的方法彤守。
? ? ? ? 另外毯侦,GrabPass可以使用Name和Tags命令。
例子
? ? ? ? 這是一個低效的方式來反轉(zhuǎn)先要渲染的顏色:
Shader "GrabPassInvert"
{
? ? SubShader
? ? {
? ? ? ? // Draw ourselves 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
? ? ? ? }
? ? }
}
? ? ? ? 這個shader有兩個pass具垫,第一個pass抓取渲染時在對象后面要顯示的內(nèi)容侈离,第二個執(zhí)行顏色反轉(zhuǎn)。請注意同樣的效果可以通過使用一個反轉(zhuǎn)融合模式更有效率的實現(xiàn)筝蚕。