[toc] 什么是遮挡半透 就是当一个角色或者物体跑到树或者遮挡物后面的时候,需要半透显示 主要思路是,两个 Pass, 一个 Pass 负责渲染被遮挡部分,另一个 Pass 负责渲染正常的模型 负责渲染被遮挡部分的 Pass 用 ZTest 中的 Greater 就可以了

# 菲涅尔方程

菲涅尔,就目前搜到的资料来看,菲涅尔是两种不同折射率物体边界的现象。 根据效果来看,或许可以理解为边缘光?公式如下: Fresnel=(1saturate(vecncdotvecv)widthcdotBrightness)Fresnel=(1-saturate(\\vec{n} \\cdot \\vec{v})^{width} \\cdot Brightness)

# 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Shader "Custome/BlockOff"
{
Properties
{
[Header(The Blocked Part)]
[Space(10)]
_Color("X-Ray Color",Color) = (0,1,1,1)
_Width("X-Ray Width",Range(1,2)) = 1
_Brightness("X-Ray Brightness",Range(0,2)) = 1

[Header(The Normal Part)]
[Space(10)]
_Albedo("Albedo",2D) = "white" {}
[NoScaleOffset]_Specular("Specular(RGB-A)",2D) = "block"{}
[NoScaleOffset]_Normal("Normal",2D) = "bump" {}
[NoScaleOffset]_AO("AO",2D) = "white"{}
}
SubShader
{
Tags { "Queue"="Transparent" }

//------The Blocked Part----------
Pass
{
ZTest Greater
ZWrite Off

Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f {
float4 vertexPos : SV_POSITION;
float3 viewDir : TEXCOORD0;
float3 worldNor : TEXCOORD1;
};

fixed4 _Color;
fixed _Width;
half _Brightness;

v2f vert(appdata_base v) {
v2f o;
o.vertexPos = UnityObjectToClipPos(v.vertex);
o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
o.worldNor = UnityObjectToWorldNormal(v.normal);

return o;
}

float4 frag(v2f i) :SV_Target{
//Fresnel算法
half NDotV = saturate(dot(i.worldNor,i.viewDir));
NDotV = pow(1 - NDotV, _Width) * _Brightness;

fixed4 color;
color.rgb = _Color.rgb;
color.a = NDotV;

return color;
}
ENDCG
}
//-----------Regular Layer---------

CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows

struct Input {
float2 uv_Albedo;
};

sampler2D _Albedo;
sampler2D _Specular;
sampler2D _Normal;
sampler2D _AO;

void surf(Input IN, inout SurfaceOutputStandardSpecular o) {
fixed4 c = tex2D(_Albedo, IN.uv_Albedo);
o.Albedo = c.rgb;

fixed4 specular = tex2D(_Specular, IN.uv_Albedo);
o.Specular = specular.rgb;
o.Smoothness = specular.a;

o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Albedo));
o.Occlusion = tex2D(_AO, IN.uv_Albedo);
}

ENDCG
}
}