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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
Shader "Character/Hair" { Properties { _MainTex ("MainTex", 2D) = "white" {} _Normal("Normal",2D) = "bump" {} _DiffuseRamp ("DiffuseRamp", 2D) = "Black" {} _Envmap ("Envmap", Cube) = "Black" {} _ShiftRamp("ShiftRamp",2D)="Black"{}
[Header(Tint Base)] _TintBase ("Tint Base", COLOR) = (0,0,0,1) [Header(RampLayer1)] _RampLayerOffset1 ("RampLayerOffset1", Range(-0.5,0.5)) = 0.5 _TintLayer1 ("Tint_Layer1", COLOR) = (0,0,0,1) [Header(RampLayer2)] _RampLayerOffset2 ("RampLayerOffset2", Range(-0.5,0.5)) = 0.5 _RampLayerSoftness2 ("RampLayerSoftness2", Range(0,1)) = 0.5 __TintLayer2 ("Tint Layer2", COLOR) = (0,0,0,1) [Header(RampLayer3)] _RampLayerOffset3 ("RampLayerOffset3", Range(-0.5,0.5)) = 0.5 _RampLayerSoftness3 ("RampLayerSoftness3", Range(0,1)) = 0.5 __TintLayer3 ("Tint Layer3", COLOR) = (0,0,0,1) [Header(Specular1)] _SpecularColor1 ("Specular Color1", COLOR) = (0,0,0,1) _SpecularIntensity1 ("SpecularIntensity1", Float) = 1 _SpecularSmooth1 ("Specular Smooth1", Range(0,1)) = 1 _ShiftOffset1 ("Shift Offset1", Float) = 0 [Header(Specular2)] _SpecularColor2 ("Specular Color2", COLOR) = (0,0,0,1) _SpecularIntensity2 ("SpecularIntensity2", Float) = 1 _SpecularSmooth2 ("Specular Smooth2", Range(0,1)) = 1 _ShiftOffset2 ("Shift Offset2", Float) = 0
[Header(Rim)] _RimMin("RimMin", Range(-2,2)) = 0.5 _RimMax("RimMax", Range(-2,2)) = 0.5 _Roughness("Roughness", Float) = 0 _EnvIntensity("Env Intensity", Float) = 0
[Header(Outline)] _Outline ("Outline", Float) = 0.6 _OutlineColor ("OutlineColor", COLOR) = (0,0,0,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Name "Skin" Tags { "LightMode" = "ForwardBase" } Stencil { Ref 1 Comp NotEqual Pass Keep Fail Keep }
CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #pragma multi_compile_fog #include "AutoLight.cginc" #include "UnityCG.cginc"
struct appdata { float4 vertex : POSITION; float2 texcoord0 : TEXCOORD0; float4 normal :NORMAL; float4 tangent:TANGENT; float4 VertexColor:COLOR; };
struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0;
float3 normalDir: TEXCOORD1; float3 tangentDir: TEXCOORD2; float3 binormalDir: TEXCOORD3; float4 VertexColor: TEXCOORD4; float3 pos_world: TEXCOORD5; LIGHTING_COORDS(6,7) };
sampler2D _MainTex; sampler2D _Normal; sampler2D _DiffuseRamp; samplerCUBE _Envmap; sampler2D _ShiftRamp; float4 _ShiftRamp_ST;
float4 _LightColor0;
float4 _MainTex_ST; float4 _TintBase; float4 __TintLayer1; float _RampLayerOffset1;
float4 __TintLayer2; float _RampLayerOffset2; float _RampLayerSoftness2; float4 __TintLayer3; float _RampLayerOffset3; float _RampLayerSoftness3;
float _ShiftOffset1; float _SpecularIntensity1; float4 _SpecularColor1; float _SpecularSmooth1; float _ShiftOffset2; float _SpecularIntensity2; float4 _SpecularColor2; float _SpecularSmooth2;
float _RimMax; float _RimMin; float _Roughness; float _EnvIntensity; float4 _Envmap_HDR;
v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord0; o.normalDir=normalize( mul(unity_ObjectToWorld,v.normal).xyz); o.tangentDir=normalize( mul(unity_ObjectToWorld,v.tangent).xyz); o.pos_world=normalize( mul(unity_ObjectToWorld,v.vertex).xyz); o.binormalDir=normalize( cross(o.normalDir,o.tangentDir)*v.tangent.w); o.VertexColor = v.VertexColor; TRANSFER_VERTEX_TO_FRAGMENT(o); return o; }
half StrandSpecular(half3 T, half3 V, half3 L, half exponent) { half3 H = normalize(L + V); half dotTH = dot(T, H); half sinTH = sqrt(1 - dotTH * dotTH); half dirAtten = smoothstep(-1.0, 0.0, dotTH); return dirAtten * pow(sinTH, exponent); }
half4 frag (v2f i) : SV_Target { half atten = LIGHT_ATTENUATION (i); half3 base_col = tex2D(_MainTex, i.uv).xyz; half3 normal_world = normalize(i.normalDir); half3 tangent_world= normalize(i.tangentDir); half3 binormal_world= normalize(i.binormalDir); half3 normal_data = UnpackNormal(tex2D(_Normal,i.uv)); half3x3 TBN = half3x3 (tangent_world,binormal_world,normal_world); normal_world = mul(normal_data,TBN); half3 ViewDir = normalize(_WorldSpaceCameraPos.xyz-i.pos_world); half3 lightDir = normalize(_WorldSpaceLightPos0.xyz); half NdotL = max(0,dot(normal_world,lightDir)); half half_lambert = (NdotL+1.0)*0.5;
half3 tint_Base_color1 = base_col;
half2 uv_ramp1 = half2(half_lambert+_RampLayerOffset1,0.5); half toon_diffuse1 = tex2D(_DiffuseRamp,uv_ramp1).r; half3 tint_color1 = lerp(half3(1,1,1),__TintLayer1,toon_diffuse1*__TintLayer1.a); half2 uv_ramp2 = half2(half_lambert+_RampLayerOffset2,1-i.VertexColor.g+_RampLayerSoftness2); half toon_diffuse2 = tex2D(_DiffuseRamp,uv_ramp2).g; half3 tint_color2 = lerp(half3(1,1,1),__TintLayer2,toon_diffuse2*__TintLayer2.a); half2 uv_ramp3 = half2(half_lambert+_RampLayerOffset3,1-i.VertexColor.b+_RampLayerSoftness3); half toon_diffuse3 = tex2D(_DiffuseRamp,uv_ramp3).b; half3 tint_color3 = lerp(half3(1,1,1),__TintLayer3,toon_diffuse3*__TintLayer3.a); half3 final_diffuse = tint_Base_color1 * tint_color1 *tint_color2 *tint_color3 ; half2 uv_shift = i.uv * _ShiftRamp_ST.xy + _ShiftRamp_ST.zw; half3 shift_col = tex2D(_ShiftRamp,uv_shift); half3 binormal_world1 = normalize( binormal_world + (shift_col+_ShiftOffset1)* normal_world); half3 binormal_world2 = normalize( binormal_world + (shift_col+_ShiftOffset2)* normal_world); half NdotV = max(0.00001 ,dot(normal_world , ViewDir)); half3 H = normalize(lightDir+ViewDir); half NdotH = dot(normal_world ,H ); half TdotH = dot(tangent_world ,H); half BdotH1 =dot(binormal_world1 ,H)/_SpecularSmooth1; half spec_term1 =exp(-(TdotH*TdotH+BdotH1*BdotH1)/(1.0+NdotH)); half spec_atten1 = saturate(sqrt(max(0.0,half_lambert/NdotV))); half3 specular_col1 = spec_term1* spec_atten1 * _LightColor0.xyz * _SpecularIntensity1 * atten * _SpecularColor1.xyz; half BdotH2 =dot(binormal_world2 ,H)/_SpecularSmooth2; half spec_term2 =exp(-(TdotH*TdotH+BdotH2*BdotH2)/(1.0+NdotH)); half spec_atten2 = saturate(sqrt(max(0.0,half_lambert/NdotV))); half3 specular_col2 = spec_term2* spec_atten2 * _LightColor0.xyz * _SpecularIntensity2 * atten *_SpecularColor1.xyz*shift_col;
half3 final_specular = specular_col1 + specular_col2 ;
half NDL = NdotL>0 ? 1:0; half fresnel = 1- dot(ViewDir,normal_world); half rim = smoothstep(_RimMin,_RimMax,fresnel); rim = rim * NDL;
half3 r = reflect(-ViewDir,normal_world); half roughness = lerp(0,0.95,saturate(_Roughness)); roughness = roughness * (1.7-0.7*roughness ); half mip_level = roughness * 6.0; half4 color_cubemap = texCUBElod(_Envmap,half4(r,mip_level)); half3 color_env = DecodeHDR(color_cubemap,_Envmap_HDR); half3 final_env =color_env * rim * _EnvIntensity ;
half3 final_col = final_diffuse +final_specular +final_env; return half4(final_col,1); } ENDCG } Pass { Name "OUTLINE" Cull Front ZWrite On ColorMask RGB Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; float4 texCoord : TEXCOORD0; float4 vertexColor : COLOR; }; struct v2f { float4 pos : POSITION; float4 color : COLOR; float4 tex : TEXCOORD0; }; sampler2D _MainTex; float _Outline; float4 _OutlineColor; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal); float2 offset = TransformViewToProjection(norm.xy); o.pos.xy += offset * _Outline*0.0001; o.tex = v.texCoord; o.color = v.vertexColor; return o; } fixed4 frag (v2f i) : SV_Target { half4 base_col = tex2D(_MainTex,i.tex); return _OutlineColor*base_col; } ENDCG } } FallBack "Diffuse" }
|