玻璃效果
当我们在shader中定义了一个GrabPass后,unity会把当前屏幕的图像绘制在一张纹理中,以便我们在后续的pass中可以访问它。我们可以使用GrabPass来对物体后面的图像进行更复杂的处理,但需要额外小心物体的渲染队列设置。尽管代码不包含混合指令,但仍需要把渲染队列设置成透明队列。
Shader "myShader/GlassRefraction"
{
Properties
{
_MainTex("Main Tex", 2D) = "White"{} //玻璃的材质纹理
_BumpMap("Normal Map", 2D) = "bump"{} //玻璃的法线纹理
_CubeMap("Environment Cubmap", Cube) = "_Skybox"{} //模拟反射的环境纹理
_Distortion("Distortion", Range(0,100)) = 10 //控制折射图像的扭曲程度
_RefractAmount("Refract Amount", Range(0.0,1.0)) = 1.0 //控制折射度
}
SubShader
{
//把Queue设置为Transparent可以确保物体渲染时,其他所有不透明的物体都被渲染到屏幕上,否则无法透过物体看到
Tags { "Queue" = "Transparent" "RenderType" = "Opaque" }
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);//得到对应被抓取的屏幕图像的采样坐标
//分别计算_MainTex和_BumpMap的采样坐标
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));
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
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;
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"
}
使用GrabPass { "TextureName" }这种形式,可以在后续的pass中使用TextureName来访问屏幕图像,但无论多少物体使用了该命令,每一帧中unity只会执行一次抓取操作,意味着所有的物体都会使用同一张屏幕图像。也可以使用GrabPass { },在后续的pass中使用_GrabTexture来访问屏幕图像,但当场景中有多个物体使用了这样的形式来抓取屏幕时,性能消耗就会过大,但使得每个物体获得不同的屏幕图像。
效果图(一个透明立方体包含了一个球体):
自我注意:在声明和定义变量时将名字写错,造成名字没对应,以至于花了大量的时间来找错误,今后应该更加仔细。