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
| Shader "test/NPR07_hair" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color("Color",Color) = (1,1,1,1)
_Ramp ("Ramp", 2D) = "white" {} _Bump ("Normal", 2D) = "white" {} _HairLightRamp ("HairLightRamp", 2D) = "white" {} _LightMapMask ("LightMapMask", 2D) = "white" {}
[Space(10)][Header(xxxxxxxxxxxxxxxx)] _Specular("Specular",Color) = (1,1,1,1) _SpecularScale("SpecularScale", Range(0, 5)) = 1
[Space(10)][Header(xxxxxxxxxxxxxxxx)] _MainSpecularSmooth("MainHairSpecularSmooth", Range(-10, 100)) = 1 _FuSpecularSmooth("FuHairSpecularSmooth", Range(-10, 100)) = 1 _MainSpecularOff("MainHairSpecularOff", Range(-10, 10)) = 1 _FuSpecularOff("FuHairSpecularOff", Range(-10, 10)) = 1
[Space(10)][Header(xxxxxxxxxxxxxxxx)] _RimPower("RimPower", Range(0.2, 10)) = 1 } SubShader { Tags { "RenderType"="Opaque" } LOD 100
Pass { Tags { "LightMode"="ForwardBase" } Cull off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" #include "UnityShaderVariables.cginc" fixed4 _Color; sampler2D _MainTex; float4 _MainTex_ST;
sampler2D _Bump; float4 _Bump_ST;
sampler2D _Ramp;
fixed4 _Specular; fixed _SpecularScale;
fixed _MainSpecularSmooth; fixed _FuSpecularSmooth; float _MainSpecularOff; float _FuSpecularOff;
sampler2D _HairLightRamp; float4 _HairLightRamp_ST;
float _RimPower;
sampler2D _LightMapMask; struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; float4 tangent : TANGENT; }; struct v2f { float4 pos : POSITION; float2 uv : TEXCOORD0; float3 worldNormal : TEXCOORD1; float3 worldPos : TEXCOORD2; SHADOW_COORDS(3) float3 tangent : TEXCOORD4; float2 hairLightUV:TEXCOORD5; float2 uv_Bump : TEXCOORD6; float3 normal : TEXCOORD7; }; v2f vert (a2v v) { v2f o; o.pos = UnityObjectToClipPos( v.vertex); o.normal = v.normal; o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); o.hairLightUV = TRANSFORM_TEX(v.texcoord, _HairLightRamp); o.uv_Bump = TRANSFORM_TEX(v.texcoord, _Bump); o.worldNormal = UnityObjectToWorldNormal(v.normal); o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; TRANSFER_SHADOW(o);
half4 p_tangent = mul(unity_ObjectToWorld, v.tangent); o.tangent = normalize(p_tangent).xyz; o.tangent = cross(o.tangent, o.worldNormal); return o; } float3 ShiftTangent(float3 T, float3 N, float shift) { float3 shiftedT = T + (shift * N); return normalize(shiftedT); }
float StrandSpecular(float3 T, float3 V, float3 L, float exponent) { float3 halfDir = normalize(L + V); float dotTH = dot(T, halfDir); float sinTH = max(0.01,sqrt(1 - pow(dotTH, 2))); float dirAtten = smoothstep(-1,0, dotTH); return dirAtten * pow(sinTH, exponent); } float3 LightMapColor(fixed3 worldLightDir,fixed3 worldNormalDir, fixed2 uv) { float LdotN = max(0, dot(worldLightDir, worldNormalDir)); float3 lightColor = LdotN * tex2D(_LightMapMask, uv); return lightColor; } float4 frag(v2f i) : SV_Target { fixed3 worldNormal = normalize(i.worldNormal); fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir); fixed4 c = tex2D (_MainTex, i.uv); fixed3 albedo = c.rgb * _Color.rgb; fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); fixed diff = dot(worldNormal, worldLightDir); diff = (diff * 0.5 + 0.5) * atten; fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, diff)).rgb; float3 speTex = tex2D(_HairLightRamp, i.hairLightUV); float3 Ts = ShiftTangent(i.tangent, worldNormal, _MainSpecularOff * speTex); float3 Tf = ShiftTangent(i.tangent, worldNormal, _FuSpecularOff * speTex); float specMain = StrandSpecular(Ts, worldViewDir, worldLightDir, _MainSpecularSmooth); float specFu = StrandSpecular(Tf, worldViewDir, worldLightDir, _FuSpecularSmooth); float specFinal = specMain;
specFinal *= _SpecularScale ; fixed3 specular = _Specular.rgb * specFinal * atten; half rim = 1.0 - saturate(dot(worldViewDir, worldNormal)); rim = pow(rim, _RimPower); fixed3 ao = LightMapColor(worldLightDir, worldNormal,i.uv).rgb; return fixed4((ambient + diffuse + specular + rim) , 1.0 ); } ENDCG } } FallBack "Diffuse" }
|