一.概念
渲染目標紋理:GPU允許把整個三圍場景渲染到一個中間緩沖中
多重渲染目標:GPU允許把場景同時渲染到多個渲染目標紋理中,不再需要為每個渲染目標紋理單獨渲染完整的場景。
Unity為渲染目標紋理定義了一個專門的紋理類型----渲染紋理。
Unity使用渲染紋理的方式通常有兩種:
A.在 Project 下右鍵創(chuàng)建RenderTexture;
2.在屏幕后處理時使用GrabPass命令或者OnRenderImage函數(shù)來獲取當前屏幕圖像。(OnRenderImage 函數(shù)是我們實現(xiàn)屏幕特效的核心方法之一)
二. 鏡子Mirror
原理:使用一個渲染紋理作為輸入屬性,并把該渲染紋理在水平方向上進行翻轉(zhuǎn)后直接顯示到物體上。
完整shader代碼:
Shader "Unity Shaders Book/Chapter 10/Mirror" {
Properties {
_MainTex ("Main Tex", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
struct a2v {
float4 vertex : POSITION;
float3 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
// Mirror needs to filp x
o.uv.x = 1 - o.uv.x;
return o;
}
fixed4 frag(v2f i) : SV_Target {
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
FallBack Off
}
實現(xiàn)的效果如下:

三. Glass
原理: GrabPass 是一種特殊的 Pass ,它可以抓取屏幕中要將對象繪制到紋理中的內(nèi)容,而且抓取到的紋理可以在其他 Pass 中使用。
它有兩種使用方法:
A.GrabPass {},這種方法抓取時,后續(xù)的 Pass 可以通過 _GrabTexture 來訪問屏幕圖像,要注意的是,對于為一個使用它的物體,Unity 都會為其單獨進行一次抓取操作。這樣每個物體都可以得到不同的屏幕圖像,這取決于這個物體的渲染順序及當前屏幕的緩沖顏色。這樣會造成不小的性能消耗。
B.GrabPass { “TextureName” } ,指定一張紋理,抓取的屏幕圖像會存儲到這張紋理中,而后續(xù)的 Pass 可以訪問這張紋理來訪問屏幕圖像。這種方法抓取屏幕時,Unity 只會在每一幀為第一個使用這張紋理的物體執(zhí)行一次抓取屏幕的操作。所以,如果場景中有復(fù)數(shù)個物體使用了這張紋理,那么它們得到的屏幕圖像其實是一樣的,且為第一個使用這張紋理的物體得到的屏幕圖像。
注意:GrabPass 通常用于渲染透明物體,需要將物體的渲染隊列設(shè)置為透明隊列,即"Queue"="Transparent"。
方法:先使用一張法線紋理修改模型的法線信息,然后使用反射通過一個cubemap模擬玻璃的反射,而在反射折射時,則使用了GrabPass獲取玻璃后面的屏幕圖像,并使用切線空間下的法線對屏幕紋理坐標偏移后,再對屏幕圖像進行采樣來模擬近似的折射效果。
完整shader代碼如下:
Shader "Unity Shaders Book/Chapter 10/Glass Refraction" {
Properties {
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_Cubemap ("Environment Cubemap", Cube) = "_Skybox" {}
_Distortion ("Distortion", Range(0, 100)) = 10
_RefractAmount ("Refract Amount", Range(0.0, 1.0)) = 1.0
}
SubShader {
// We must be transparent, so other objects are drawn before this one.
Tags { "Queue"="Transparent" "RenderType"="Opaque" }
// This pass grabs the screen behind the object into a texture.
// We can access the result in the next pass as _RefractionTex
GrabPass { "_RefractionTex" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
samplerCUBE _Cubemap;
float _Distortion;
fixed _RefractAmount;
sampler2D _RefractionTex;
float4 _RefractionTex_TexelSize;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float4 scrPos : TEXCOORD0;
float4 uv : TEXCOORD1;
float4 TtoW0 : TEXCOORD2;
float4 TtoW1 : TEXCOORD3;
float4 TtoW2 : TEXCOORD4;
};
v2f vert (a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.scrPos = ComputeGrabScreenPos(o.pos);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
fixed4 frag (v2f i) : SV_Target {
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
// Get the normal in tangent space
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
// Compute the offset in tangent space
float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;
// Convert the normal to world space
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
fixed3 reflDir = reflect(-worldViewDir, bump);
fixed4 texColor = tex2D(_MainTex, i.uv.xy);
fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb;
fixed3 finalColor = reflCol * (1 - _RefractAmount) + refrCol * _RefractAmount;
return fixed4(finalColor, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
要點:_Distortion 表示光線折射時的扭曲程度,_RefractAmount 為 0 時,只含反射效果,_RefractAmount 為 1 時,只含折射效果;
指定一個紋理 _RefractionTex;
_RefractionTex_TexelSize 表示紋理的紋素大小,對屏幕圖像采樣時使用;
利用 Unity 內(nèi)置函數(shù) ComputeGrabScreenPos 得到對應(yīng)抓取屏幕圖像的采樣坐標;
效果如下圖:

四. 渲染紋理vs.GrabPass
GrabPass優(yōu)點:實現(xiàn)簡單
GrabPass缺點:效率低,且獲取到的圖像分辨率和顯示屏幕一樣,意味著高分辨率可能有嚴重的帶寬影響。需要cpu直接讀取后備緩沖(back buffer)的數(shù)據(jù),破壞了CPU和GPU間的并行性,會更耗時。
渲染紋理優(yōu)點:效率高,可以自定義渲染紋理大小,且可以控制相機的渲染層來減少二次渲染時的場景大小,且可通過相機自行開啟關(guān)閉。
渲染紋理缺點:操作繁瑣。