forked from ipud2/Unity-Basic-Shader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YuanShen_Hair.shader
219 lines (182 loc) · 7.33 KB
/
YuanShen_Hair.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
Shader "Unlit/YuanShen_Hair"
{
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)
[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;
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));
// ramp*=ramp;
// return ramp.xyzz;
// return ramp;
float3 Diffuse = lerp( _ShadowColor*BaseColor,BaseColor,ramp);
/*==========================Spedular ==========================*/
float3 Specular =0;
Specular = pow(saturate(NH),_SpecularPowerValue * LightMap.r )*_SpecularScale * LightMap.b ;
Specular = saturate(Specular);
// return Specular.xyzz;
float3 Emission = 0;
FinalColor = Diffuse + Specular +Emission;
return float4(FinalColor,1)*1.2;
}
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
}
}
}