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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
| Shader "Custom/Marschner" { Properties { _DiffuseColor("Hair Diffuse Color",Color)= (1,1,1,1) _MainTex("Main Tex",2D)= "white" {} _AlphaTex("Alpha Tex",2D) = "white" {} _AoTex("Ao Tex",2D) = "white" {} _RoughnessTex("Roughness Tex",2D)="white" {} _EmissionTex("Emission Tex",2D)="white" {} _Metallic("Metallic Tex",2D)="white" {} _NormalMap("Nromal Map",2D) = "white" {} _BumpScale("Hair Bump Scale",Range(0,10))=0.2 _Shift("Specular Shift Scale",Range(0,1))=0.04 } SubShader { Tags { "RenderType" = "Opaque"} LOD 200 Cull off Pass{ Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#include "UnityCG.cginc" #include "Lighting.cginc" #include "UnityPBSLighting.cginc" #include "AutoLight.cginc"
#pragma vertex vert #pragma fragment frag
#pragma multi_compile_fwdbase
struct appdata{ float4 vertex :POSITION; float2 uv : TEXCOORD0; float3 normal :NORMAL; float4 texcoord1 : TEXCOORD1; float4 tangent : TANGENT; };
struct v2f{ float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float3 normal : TEXCOORD1; float3 bitangent :TEXCOORD2; float3 worldPos : TEXCOORD3; float4 TtoW0 : TEXCOORD4; float4 TtoW1 : TEXCOORD5; float4 TtoW2 : TEXCOORD6;
UNITY_SHADOW_COORDS(7) };
struct SurfaceOutputHair{ half3 Albedo; half3 Normal; half Roughness; half AO; half Alpha; half3 Emission; half Metallic; half Shift; };
float4 _DiffuseColor; sampler2D _MainTex; sampler2D _AlphaTex; sampler2D _AoTex; sampler2D _RoughnessTex; sampler2D _EmissionTex; sampler2D _MetallicTex; sampler2D _NormalMap;
fixed _Shift; fixed _BumpScale;
#define PI 3.1415926 #define SQRT2PI 2.50663
v2f vert(appdata v){ v2f o; UNITY_INITIALIZE_OUTPUT(v2f, o); o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; float3 worldPos=mul(unity_ObjectToWorld, v.vertex).xyz; float3 worldNormal=UnityObjectToWorldNormal(v.normal); float3 worldTangent=UnityWorldToObjectDir(v.tangent).xyz; float3 worldBitangent=cross(worldTangent,worldNormal)*v.tangent.w; o.worldPos = worldPos; o.normal=UnityObjectToWorldNormal(v.normal); o.bitangent =cross(v.normal,v.tangent.xyz)*v.tangent.w*unity_WorldTransformParams.w; o.TtoW0 = float4(worldTangent.x,worldBitangent.x,worldNormal.x,worldPos.x); o.TtoW1 = float4(worldTangent.y,worldBitangent.y,worldNormal.y,worldPos.y); o.TtoW2 = float4(worldTangent.z,worldBitangent.z,worldNormal.z,worldPos.z); UNITY_TRANSFER_LIGHTING(o,v.texcoord1.xy); return o; }
float3 getNormal(float4 TtoW0,float4 TtoW1,float4 TtoW2,float2 uv){ float4 packedNormal = tex2D(_NormalMap,uv); float3 tangentNormal=UnpackNormal(packedNormal); tangentNormal.xy*=_BumpScale; float3 worldNormal = normalize(float3(dot(TtoW0.xyz,tangentNormal),dot(TtoW1.xyz,tangentNormal),dot(TtoW2.xyz,tangentNormal))); return worldNormal;
}
inline float square(float x){ return x*x; }
float Hair_g(float B,float Theta){ return exp(-0.5 * square(Theta) / (B * B)) / (SQRT2PI * B); }
inline float3 SpecularFresnel(float F0, float x) { return F0 + (1.0f - F0) * pow(1 - x, 5); }
float3 HairSpecularMarschner(SurfaceOutputHair sh,float3 N,float3 V,float3 L,float Shadow,float Backlit,float Area){ float3 S = 0; const float dotVL = dot(V,L); const float SinThetaL = clamp(dot(N,L),-1.f,1.f); const float SinThetaV = clamp(dot(N,V),-1.f,1.f); float CosThetaL = sqrt(max(0,1-SinThetaL*SinThetaL)); float CosThetaV = sqrt(max(0,1-SinThetaV*SinThetaV)); float CosThetaD = sqrt((1 + CosThetaL * CosThetaV + SinThetaL * SinThetaV) / 2.0);
const float3 Lp = L-SinThetaL*N; const float3 Vp = V-SinThetaV*N; const float CosPhi = dot(Lp,Vp)*rsqrt(dot(Lp,Lp)*dot(Vp,Vp) + 1e-4); const float CosHalfPhi = sqrt(saturate(0.5+0.5*CosPhi));
float n=1.55; float n_prime = 1.19/CosThetaD+0.36*CosThetaD;
float Shift=sh.Shift; float Alpha[] = { -Shift*2, Shift, Shift*4 };
float B[]={ Area+square(sh.Roughness), Area+square(sh.Roughness)/2, Area+square(sh.Roughness)*2 };
float F0=square((1-n)/(1+n));
float3 Tp; float Mp,Np,A,f,Fp,h,a;
Mp=Hair_g(B[0],SinThetaL+SinThetaV-Alpha[0]); A=SpecularFresnel(F0,sqrt(saturate(0.5 + 0.5 * dotVL))); Np=0.25*CosHalfPhi*A; S+=Mp*Np*lerp(1,Backlit,saturate(-dotVL));
Mp=Hair_g(B[1],SinThetaL+SinThetaV-Alpha[1]); a=1/n_prime; h=CosHalfPhi*(1+a*(0.6 - 0.8 * CosPhi));
f=SpecularFresnel(F0,CosThetaD*sqrt(saturate(1 - h*h))); Fp=square(1 - f);
Tp = pow(sh.Albedo, 0.5 * sqrt(1 - square((h * a))) / CosThetaD);
Np=exp(-3.65*CosPhi-3.98); S+=Mp*Np*Fp*Tp*Backlit;
Mp=Hair_g(B[2],SinThetaL+SinThetaV-Alpha[2]); f=SpecularFresnel(F0,CosThetaD*0.5f); Fp=square(1 - f)*f;
Tp=pow(sh.Albedo,0.8/CosThetaD); Np=exp(17*CosPhi-16.78); S+=Mp*Np*Fp*Tp; return S; }
float3 HairDiffuseKajiya(SurfaceOutputHair sh,float3 N,float3 V,float3 L,float Shadow,float Backlit,float Area){ float3 S = 0; float KajiyaDiffuse = 1-abs(dot(N,L));
float3 FakeNormal = normalize(V-N*dot(V,N)); N=FakeNormal;
float Wrap=1; float dotNL=saturate((dot(N,L)+Wrap)/square(1+Wrap)); float DiffuseScatter=( (1 / PI) * lerp(dotNL, KajiyaDiffuse, 0.33))*sh.Metallic; float Luma=Luminance(sh.Albedo); float3 ScatterTint=pow(sh.Albedo/Luma,1 - Shadow); S=sqrt(sh.Albedo)*DiffuseScatter*ScatterTint;
return S; }
float3 HairShading(SurfaceOutputHair sh,float3 N,float3 V,float3 L,float Shadow,float Backlit,float Area){ float3 S= float3(0,0,0); S=HairSpecularMarschner(sh,N,V,L,Shadow,Backlit,Area); S+=HairDiffuseKajiya(sh,N,V,L,Shadow,Backlit,Area); S=-min(-S,0.0); return S; }
float3 HairBxDF(SurfaceOutputHair sh,float3 N,float3 V,float3 L,float Shadow,float Backlit,float Area){ return HairShading(sh,N,V,L,Shadow,Backlit,Area); }
inline void LightingHair_GI(SurfaceOutputHair sh,UnityGIInput giInput,inout UnityGI gi){ gi=UnityGlobalIllumination(giInput,sh.AO,sh.Normal); }
inline fixed4 LightingHair(SurfaceOutputHair sh,float3 viewDir,UnityGI gi){ fixed4 c = fixed4(0,0,0,sh.Alpha); c.rgb=gi.light.color*HairBxDF(sh,sh.Normal,viewDir,gi.light.dir,0.1f,0.5f,0.0f); float3 reflect=normalize(viewDir-sh.Normal*dot(sh.Normal,viewDir)); c.rgb+=gi.indirect.diffuse*6.28f*HairBxDF(sh,sh.Normal,viewDir,reflect,0.5f,0.0f,0.5f);
return c; }
fixed4 frag(v2f i) : SV_TARGET{ float3 l=normalize(UnityWorldSpaceLightDir(i.worldPos)); float3 v=normalize(UnityWorldSpaceViewDir(i.worldPos)); float3 n=getNormal(i.TtoW0,i.TtoW1,i.TtoW2,i.uv);
float3 albedo = tex2D(_MainTex,i.uv); float ao =tex2D(_AoTex,i.uv); float alpha=tex2D(_AlphaTex,i.uv); float roughness=tex2D(_RoughnessTex,i.uv); float metallic=tex2D(_MetallicTex,i.uv); float3 emission=tex2D(_EmissionTex,i.uv);
SurfaceOutputHair sh; UNITY_INITIALIZE_OUTPUT(SurfaceOutputHair, sh); sh.Normal=n; sh.AO=ao; sh.Albedo = fixed4(albedo*_DiffuseColor,alpha); sh.Roughness=roughness; sh.Emission=emission; sh.Alpha=alpha; sh.Metallic=metallic; sh.Shift=_Shift;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos)
UnityGI gi; UNITY_INITIALIZE_OUTPUT(UnityGI, gi); gi.indirect.diffuse=0; gi.indirect.specular=0; gi.light.color = _LightColor0.rgb; gi.light.dir=l;
UnityGIInput giInput; UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput); giInput.light=gi.light; giInput.worldPos=i.worldPos; giInput.worldViewDir=v; giInput.atten=atten; giInput.probeHDR[0]=unity_SpecCube0_HDR; giInput.probeHDR[1]=unity_SpecCube1_HDR;
LightingHair_GI(sh,giInput,gi); fixed4 col = LightingHair(sh,v,gi); return col; }
ENDCG } } FallBack "Diffuse" }
|