Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Anisotropic Highlight Surface Shader

Discussion in 'Shaders' started by Farfarer, Jun 24, 2011.

  1. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Shader code for anisotropic highlights. Good for brushed metal or hair highlights.

    Requires an anisotropic direction texture like these (for more information on painting them, see the bottom of this article);



    Gives results like this;


    Can be masked between anisotropic and blinn spec via the specular map's blue channel.

    Specular level (specular map's red channel) and gloss amount (green channel) affects both anisotropic and blinn specular highlights.

    Code (csharp):
    1.  
    2. Shader "Bumped Anisotropic Specular" {
    3.       Properties {
    4.           _Color ("Main Color", Color) = (1,1,1,1)
    5.           _MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
    6.           _SpecularTex ("Specular (R) Gloss (G) Anisotropic Mask (B)", 2D) = "gray" {}
    7.           _BumpMap ("Normal (Normal)", 2D) = "bump" {}
    8.           _AnisoTex ("Anisotropic Direction (RGB)", 2D) = "bump" {}
    9.           _AnisoOffset ("Anisotropic Highlight Offset", Range(-1,1)) = -0.2
    10.           _Cutoff ("Alpha Cut-Off Threshold", Range(0,1)) = 0.5
    11.       }
    12.  
    13.       SubShader{
    14.           Tags { "RenderType" = "Opaque" }
    15.      
    16.           CGPROGRAM
    17.          
    18.               struct SurfaceOutputAniso {
    19.                   fixed3 Albedo;
    20.                   fixed3 Normal;
    21.                   fixed4 AnisoDir;
    22.                   fixed3 Emission;
    23.                   half Specular;
    24.                   fixed Gloss;
    25.                   fixed Alpha;
    26.               };
    27.  
    28.               float _AnisoOffset, _Cutoff;
    29.               inline fixed4 LightingAniso (SurfaceOutputAniso s, fixed3 lightDir, fixed3 viewDir, fixed atten)
    30.               {
    31.                  fixed3 h = normalize(normalize(lightDir) + normalize(viewDir));
    32.                  float NdotL = saturate(dot(s.Normal, lightDir));
    33.  
    34.                  fixed HdotA = dot(normalize(s.Normal + s.AnisoDir.rgb), h);
    35.                  float aniso = max(0, sin(radians((HdotA + _AnisoOffset) * 180)));
    36.  
    37.                  float spec = saturate(dot(s.Normal, h));
    38.                  spec = saturate(pow(lerp(spec, aniso, s.AnisoDir.a), s.Gloss * 128) * s.Specular);
    39.  
    40.                  fixed4 c;
    41.                  c.rgb = ((s.Albedo * _LightColor0.rgb * NdotL) + (_LightColor0.rgb * spec)) * (atten * 2);
    42.                  c.a = 1;
    43.                  clip(s.Alpha - _Cutoff);
    44.                  return c;
    45.               }
    46.  
    47.               #pragma surface surf Aniso
    48.               #pragma target 3.0
    49.              
    50.               struct Input
    51.               {
    52.                   float2 uv_MainTex;
    53.                   float2 uv_AnisoTex;
    54.               };
    55.              
    56.               sampler2D _MainTex, _SpecularTex, _BumpMap, _AnisoTex;
    57.                  
    58.               void surf (Input IN, inout SurfaceOutputAniso o)
    59.               {
    60.                  fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
    61.                   o.Albedo = albedo.rgb;
    62.                   o.Alpha = albedo.a;
    63.                   o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    64.                   fixed3 spec = tex2D(_SpecularTex, IN.uv_MainTex).rgb;
    65.                   o.Specular = spec.r;
    66.                   o.Gloss = spec.g;
    67.                   o.AnisoDir = fixed4(tex2D(_AnisoTex, IN.uv_AnisoTex).rgb, spec.b);
    68.               }
    69.           ENDCG
    70.       }
    71.       FallBack "Transparent/Cutout/VertexLit"
    72. }
    73.  
     
    Last edited: Jul 8, 2011
    petey likes this.
  2. garyhaus

    garyhaus

    Joined:
    Dec 16, 2006
    Posts:
    601
    Looks great! Thank you for sharing your knowledge!!!
     
  3. Dr-Game

    Dr-Game

    Joined:
    Mar 12, 2015
    Posts:
    161
    HI~How can I make a Vertical AnisoDir map?? I want to use for hair