-
Notifications
You must be signed in to change notification settings - Fork 197
/
YuanShen_Face.shader
237 lines (198 loc) · 8.66 KB
/
YuanShen_Face.shader
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Shader "Unlit/YuanShen_Face"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_LightMap ("_LightMap", 2D) = "white" {}
_ShadowRamp ("_ShadowRamp", 2D) = "white" {}
_ShadowColor("_ShadowColor",Color) = (0.2,0.2,0.2,0.2)
_ShadowRange ("Shadow Range", Range(-1, 1)) = 0.5
_ShadowSmooth("Shadow Smooth", Range(0, 1)) = 0.2
_DiffuseThreshold("_DiffuseThreshold",Float) =0
_SpecularScale("_SpecularScale",Float) =1
_SpecularPowerValue("_SpecularPowerValue",Float) =1
_SpecularColor("_SpecularColor",Color) = (1,1,1,1)
_Outline("Thick of Outline",Float) = 0.01
_Factor("Factor",range(0,1)) = 0.5
_OutColor("OutColor",color) = (0,0,0,0)
_FaceLightmpOffset ("Face Lightmp Offset", Range(-1, 1)) = 0 //用来和 头发 身体的明暗过渡对齐
[KeywordEnum(None,LightMap_R,LightMap_G,LightMap_B,LightMap_A,UV,UV2,VertexColor,BaseColor,BaseColor_A)] _TestMode("_TestMode",Int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" "Queue" = "Geometry"}
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fullforwardshadows
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "UnityGlobalIllumination.cginc"
#include "AutoLight.cginc"
sampler2D _MainTex,_LightMap,_ShadowRamp;
float4 _ShadowColor,_SpecularColor;
int _TestMode;
float _DiffuseThreshold,_SpecularScale,_SpecularPowerValue;
float _ShadowRange,_ShadowSmooth;
float _FaceLightmpOffset;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 tangent :TANGENT;
float3 normal : NORMAL;
float4 vertexColor : Color;
};
struct v2f
{
float4 pos : SV_POSITION; // 必须命名为pos ,因为 TRANSFER_VERTEX_TO_FRAGMENT 是这么命名的,为了正确地获取到Shadow
float2 uv : TEXCOORD0;
float3 tangent : TEXCOORD1;
float3 bitangent : TEXCOORD2;
float3 normal : TEXCOORD3;
float3 worldPosition: TEXCOORD4;
float3 localPosition : TEXCOORD5;
float3 localNormal : TEXCOORD6;
float4 vertexColor : TEXCOORD7;
LIGHTING_COORDS(8,9)
float2 uv2 : TEXCOORD10;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.uv2 = v.uv2;
o.normal = UnityObjectToWorldNormal(v.normal);
o.worldPosition = mul(unity_ObjectToWorld,v.vertex);
o.localPosition = v.vertex.xyz;
o.tangent = UnityObjectToWorldDir(v.tangent);
o.bitangent = cross(o.normal,o.tangent) * v.tangent.w;
o.localNormal = v.normal;
o.vertexColor = v.vertexColor;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
float4 frag (v2f i) : SV_Target
{
//Variable
float3 T = normalize(cross(i.normal ,i.tangent));
float3 N = normalize(i.normal);
float3 B = normalize( cross(N,T));
// float3 B = normalize( i.bitangent);
float3 L = normalize( UnityWorldSpaceLightDir(i.worldPosition.xyz));
float3 V = normalize( UnityWorldSpaceViewDir(i.worldPosition.xyz));
float3 H = normalize(V+L);
float2 uv = i.uv;
uv.y = 1-uv.y; //uv颠倒了,如果在 Csv2Obj阶段已经反转了,这里就不需要再转了
float2 uv2 = i.uv2;
// return float4(uv2,0,0);
float4 vertexColor = i.vertexColor;
// return vertexColor.xyzz;
float HV = dot(H,V);
float NV = dot(N,V);
float NL = dot(N,L);
float NH = dot(N,H);
/*==========================Texture ==========================*/
float3 FinalColor = 0;
float4 BaseColor = tex2D(_MainTex,uv);
float4 LightMap = tex2D(_LightMap,uv);
// return BaseColor.xyzz;
int mode = 1;
if(_TestMode == mode++)
return LightMap.r;
if(_TestMode ==mode++)
return LightMap.g; //阴影 Mask
if(_TestMode ==mode++)
return LightMap.b; //漫反射 Mask
if(_TestMode ==mode++)
return LightMap.a; //漫反射 Mask
if(_TestMode ==mode++)
return float4(uv,0,0); //uv
if(_TestMode ==mode++)
return float4(uv2,0,0); //uv2
if(_TestMode ==mode++)
return vertexColor.xyzz; //vertexColor
if(_TestMode ==mode++)
return BaseColor.xyzz; //BaseColor
if(_TestMode ==mode++)
return BaseColor.a; //BaseColor.a
/*==========================Diffuse ==========================*/
float halfLambert = 0.5*NL+0.5;
float rampValue = smoothstep(0,_ShadowSmooth,halfLambert-_ShadowRange);
float3 ramp = tex2D(_ShadowRamp, float2(saturate(rampValue), 0.5));
float3 faceLightMap = tex2D(_LightMap, float2(uv.x,uv.y));
// return faceLightMap.y;
// RTS rotation transform scal
//float3 _Up = float3(0,1,0); //人物上方向 用代码传进来
//float3 _Front = float3(0,0,-1); //人物前方向 用代码传进来
//float3 Left = cross(_Up,_Front);
//float3 Right = -Left;
//也可以直接从模型的世界矩阵中拿取出 各个方向
//这要求模型在制作的时候得使用正确的朝向: X Y Z 分别是模型的 右 上 前
float4 Front = mul(unity_ObjectToWorld,float4(0,0,1,0));
float4 Right = mul(unity_ObjectToWorld,float4(1,0,0,0));
float4 Up = mul(unity_ObjectToWorld,float4(0,1,0,0));
float3 Left = -Right;
float FL = dot(normalize(Front.xz), normalize(L.xz));
float LL = dot(normalize(Left.xz), normalize(L.xz));
float RL = dot(normalize(Right.xz), normalize(L.xz));
float faceLight = faceLightMap.r + _FaceLightmpOffset ; //用来和 头发 身体的明暗过渡对齐
float faceLightRamp = (FL > 0) * min((faceLight > LL),(1 > faceLight+RL ) ) ;
float3 Diffuse = lerp( _ShadowColor*BaseColor,BaseColor,faceLightRamp);
// float3 Diffuse = lerp( _ShadowColor*BaseColor,BaseColor,ramp) ;
/*==========================Spedular ==========================*/
float3 Specular =0;
// Specular = pow(saturate(NH),_SpecularPowerValue * LightMap.r )*_SpecularScale * LightMap.b ;
float3 Emission = 0; //头发上没有Emission
FinalColor = Diffuse + Specular +Emission;
return float4(FinalColor,1);
}
ENDCG
}
pass
{//处理光照前的pass渲染
Tags{ "LightMode" = "Always" }
Cull Front
ZWrite On
CGPROGRAM
#pragma multi_compile_fog
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Outline;
float _Factor;
fixed4 _OutColor;
struct v2f
{
float4 pos:SV_POSITION;
UNITY_FOG_COORDS(0)
};
v2f vert(appdata_full v)
{
v2f o;
float3 dir = normalize(v.vertex.xyz);
float3 dir2 = v.normal;
float D = dot(dir,dir2);
dir = dir * sign(D);
dir = dir * _Factor + dir2 * (1 - _Factor);
v.vertex.xyz += dir * _Outline*0.001;
o.pos = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
float4 frag(v2f i) :COLOR
{
float4 c = _OutColor;
UNITY_APPLY_FOG(i.fogCoord, c);
return c;
}
ENDCG
}
}
}