grabpass 功能可以方便的截取当前屏幕画面
可以很方便的实现半透明 / 折射 / 扭曲 等效果
Shader "Unlit/Grab"
{
Properties
{
_MainColor ("MainColor", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("BumpMap", 2D) = "white" {}
_SpecularColor ("SpecularColor", Color) = (1,1,1,1)
_Gloss ("Gloss", float) = 1
_Fresnel ("Fresnel", float) = 1
_Distortion ("Distortion", Range(-1, 1)) = 1
_Opacity ("Opaticy", Range(0, 1)) = 1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent"}
LOD 100
GrabPass { "_SceneTex" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 grabPos : TEXCOORD1;
float4 TtoW0 : TEXCOORD2;
float4 TtoW1 : TEXCOORD3;
float4 TtoW2 : TEXCOORD4;
};
float4 _MainColor;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
sampler2D _SceneTex;
float4 _SpecularColor;
float _Gloss;
float _Distortion;
float _Fresnel;
float _Opacity;
v2f vert (a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.grabPos = ComputeGrabScreenPos(o.pos);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldTangent = UnityObjectToWorldDir(v.tangent);
float3 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;
}
float4 frag (v2f i) : SV_Target
{
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
float3 N = normalize(float3(i.TtoW0.z, i.TtoW1.z, i.TtoW2.z));
float3 V = normalize(UnityWorldSpaceViewDir(worldPos));
float3 L = _WorldSpaceLightPos0.xyz;
float3 bumpMap = UnpackNormal(tex2D(_BumpMap, i.uv));
bumpMap.xy *= _Distortion;
bumpMap.z = sqrt(1.0 - saturate(dot(bumpMap.xy, bumpMap.xy)));
float3 bumpNormal = normalize(half3(dot(i.TtoW0.xyz, bumpMap), dot(i.TtoW1.xyz, bumpMap), dot(i.TtoW2.xyz, bumpMap)));
float2 sceneUV = (i.grabPos.xy / i.grabPos.w);
sceneUV += bumpMap.rg;
fixed4 sceneColor = tex2D(_SceneTex, sceneUV).rgb;
float NdotL = saturate(dot(bumpNormal, L));
float3 H = normalize(V + L);
float HdotL = saturate(dot(bumpNormal, H));
float NdotV = saturate(dot(bumpNormal, V));
float3 specular = _SpecularColor.rgb * pow(HdotL, _Gloss);
float fresnel = _Fresnel + (1 - _Fresnel) * pow(1 - NdotV, 5);
float4 mainColor = tex2D(_MainTex, i.uv) * _MainColor;
float3 emissiveColor = mainColor.rgb * fresnel + specular;
float3 finalColor = lerp(sceneColor, emissiveColor, _Opacity);
return float4(finalColor, 1);
}
ENDCG
}
}
}
虽然使用起来很方便,但是效率不高
还有一点就是在 urp 渲染管线下不能使用
学习了下可以替换的方式
最简单的是直接使用 shadergraph 中的 scenecolor 节点,就可以获得类似的效果,不过 scenecolor 节点是在渲染半透明物体之前,所以不能渲染半透明物体
还可以使用 renderer feature,主要参考这个地址 renderer feature(里面有很详细的代码和截图,所以这里就省略了)