Search Unity

Help with wrapped specular shader!

Discussion in 'Shaders' started by 3r5k3n, Dec 30, 2013.

  1. 3r5k3n

    3r5k3n

    Joined:
    Jun 17, 2013
    Posts:
    17
    I have tweaked a wrapped diffused shader to be a specular one instead but it doesn't work, it just makes a diffuse one instead.
    I'm just recently started with shaders so I have no idea whats wrong and I can't find any prevously asked questions about this but anyway here is the shader I use. Thanks in advance

    Code (csharp):
    1. Shader "Wrapped/Specular Wrapped"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    8.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    9.     }
    10.        
    11.     SubShader
    12.     {
    13.         Tags { "RenderType" = "Opaque" }
    14.         CGPROGRAM
    15.         #pragma surface surf WrapLambert
    16.  
    17.         half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten)
    18.         {
    19.             half NdotL = dot (s.Normal, lightDir);
    20.             half diff = NdotL * 0.5 + 0.5;
    21.             half4 c;
    22.             c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
    23.             c.a = s.Alpha;
    24.             return c;
    25.         }
    26.  
    27.         struct Input
    28.         {
    29.             float2 uv_MainTex;
    30.         };
    31.        
    32.         float4 _Color;
    33.         sampler2D _MainTex;
    34.         half _Shininess;
    35.        
    36.         void surf (Input IN, inout SurfaceOutput o)
    37.         {
    38.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    39.                        
    40.             o.Albedo = tex.rgb * _Color.rgb;
    41.             o.Gloss = tex.a;
    42.             o.Alpha = tex.a * _Color.a;
    43.             o.Specular = _Shininess;
    44.         }
    45.         ENDCG
    46.     }
    47.     FallBack "Specular"
    48. }
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,597
    Change your lighting function with this:

    fixed _LambertWrapAmount
    fixed4 LightingWrapLambert(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) {
    //Diffuse term
    fixed NdotL = dot(s.Normal, lightDir) + _LambertWrapAmount;
    fixed diff = max(NdotL, 0) / (1.0 + _LambertWrapAmount);
    fixed3 diffColor = s.Albedo * _LightColor0.rgb;

    //Specular term
    half3 h = normalize(lightDir + viewDir);
    float NdotH = dot(s.Normal, h);
    float spec = pow(max(NdotH, 0), s.Specular * 128.0) * s.Gloss;
    fixed3 specColor = _SpecColor.rgb * _LightColor0.rgb;

    //Sum
    fixed4 c;
    c.rgb = ((diffColor * diff) + (specColor * spec)) * (atten * 2);
    c.a = s.Alpha + (_LightColor0.a * _SpecColor.a * spec * atten);
    return c;
    }

    also add a new property as:
    _LambertWrapAmount("Lambert Wrapping", Range (0.01, 1)) = 0.5

    EDIT: This is blinn lighting.
     
  3. 3r5k3n

    3r5k3n

    Joined:
    Jun 17, 2013
    Posts:
    17
    That was a little more complicated than I thought it would be to add specular to it, but it works great.
    I got a error first but that was just because of a missing ";" after "fixed _LambertWrapAmount"
    Once again, thank you
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,597
    There is more than one way of doing specular highlights. This is one way of doing it.