I think that should do it…
Code to generate the Beckmann lookup texture can be found at the bottom of this page;
http://www.altdevblogaday.com/2011/12/31/skin-shading-in-unity3d/
Roughness slider:
Shader "Custom/CookTorrance" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
_Roughness ("Roughness", Range(0,1)) = 0.5
_BumpMap ("Normal (Normal)", 2D) = "bump" {}
_Beckmann ("Beckmann Lookup (RGB)", 2D) = "gray" {}
_Fresnel ("Fresnel Value", Float) = 0.028
_Cutoff ("Alpha Cut-Off Threshold", Range(0,1)) = 0.5
}
SubShader{
Tags { "Queue" = "Geometry" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
CGPROGRAM
#pragma surface surf CookTorrance exclude_path:prepass nolightmap nodirlightmap fullforwardshadows
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex, _BumpMap, _Beckmann;
float _Fresnel, _Cutoff, _Roughness;
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = albedo.rgb;
o.Alpha = albedo.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
}
inline fixed4 LightingCookTorrance (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
clip ( s.Alpha - _Cutoff );
viewDir = normalize ( viewDir );
lightDir = normalize ( lightDir );
float3 h = normalize ( lightDir + viewDir );
float NdotL_unsat = dot ( s.Normal, lightDir );
float NdotH_unsat = dot ( s.Normal, h );
float NdotL = saturate( NdotL_unsat );
float NdotH = saturate( NdotH_unsat );
float NdotV = saturate( dot ( s.Normal, viewDir ) );
float VdotH = saturate( dot ( viewDir, h ) );
float geo_numerator = 2.0 * NdotH;
float geo_b = ( geo_numerator * NdotV ) / VdotH;
float geo_c = ( geo_numerator * NdotL ) / VdotH;
float geo = min( 1.0f, min( geo_b, geo_c ) );
fixed roughness = tex2D( _Beckmann, float2 ( NdotH_unsat * 0.5 + 0.5, _Roughness ) ).r;
float fresnel = pow( 1.0 - VdotH, 5.0 );
fresnel *= ( 1.0 - _Fresnel );
fresnel += _Fresnel;
float3 spec = float3 ( fresnel * geo * roughness ) / ( NdotV * NdotL );
fixed4 c;
c.rgb = NdotL * ( _LightColor0.rgb * spec + s.Albedo ) * atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Transparent/Cutout/VertexLit"
}
Rougness texture:
Shader "Custom/CookTorrance" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
_Roughness ("Roughness (R)", 2D) = "gray" {}
_BumpMap ("Normal (Normal)", 2D) = "bump" {}
_Beckmann ("Beckmann Lookup (RGB)", 2D) = "gray" {}
_Fresnel ("Fresnel Value", Float) = 0.028
_Cutoff ("Alpha Cut-Off Threshold", Range(0,1)) = 0.5
}
SubShader{
Tags { "Queue" = "Geometry" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
CGPROGRAM
#pragma surface surf CookTorrance exclude_path:prepass nolightmap nodirlightmap fullforwardshadows
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex, _BumpMap, _Beckmann, _Roughness;
float _Fresnel, _Cutoff;
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = albedo.rgb;
o.Alpha = albedo.a;
o.Specular = tex2D(_Roughness, IN.uv_MainTex).r;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
}
inline fixed4 LightingCookTorrance (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
clip ( s.Alpha - _Cutoff );
viewDir = normalize ( viewDir );
lightDir = normalize ( lightDir );
float3 h = normalize ( lightDir + viewDir );
float NdotL_unsat = dot ( s.Normal, lightDir );
float NdotH_unsat = dot ( s.Normal, h );
float NdotL = saturate( NdotL_unsat );
float NdotH = saturate( NdotH_unsat );
float NdotV = saturate( dot ( s.Normal, viewDir ) );
float VdotH = saturate( dot ( viewDir, h ) );
float geo_numerator = 2.0 * NdotH;
float geo_b = ( geo_numerator * NdotV ) / VdotH;
float geo_c = ( geo_numerator * NdotL ) / VdotH;
float geo = min( 1.0f, min( geo_b, geo_c ) );
fixed roughness = tex2D( _Beckmann, float2 ( NdotH_unsat * 0.5 + 0.5, s.Specular ) ).r;
float fresnel = pow( 1.0 - VdotH, 5.0 );
fresnel *= ( 1.0 - _Fresnel );
fresnel += _Fresnel;
float3 spec = float3 ( fresnel * geo * roughness ) / ( NdotV * NdotL );
fixed4 c;
c.rgb = NdotL * ( _LightColor0.rgb * spec + s.Albedo ) * atten;
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Transparent/Cutout/VertexLit"
}