Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Metallic shader slider

Discussion in 'Shaders' started by Harrison_Hough, Mar 1, 2017.

  1. Harrison_Hough

    Harrison_Hough

    Joined:
    Dec 3, 2015
    Posts:
    43
    Hey Guys,

    I'm just wondering what the correct way is to calculate the Metallic and Smoothness with a slider value. Like how they do it in the Standard Metallic shader. Here is how I currently have it.

    Code (CSharp):
    1. void surf (Input IN, inout SurfaceOutputStandard o) {
    2.             // Albedo comes from a texture tinted by color
    3.             fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    4.             o.Albedo = albedo.rgb;
    5.            
    6.            
    7.             //get spec values
    8.             fixed4 metal = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    9.             //o.Metallic = metal.r;
    10.             o.Metallic = metal.r * _Metallic;
    11.             //o.Metallic = metal.r;
    12.             o.Smoothness = metal.a * _Glossiness;
    13.             //might need to change later on to use alpha (gloss channel in Spec map)
    14.             //o.Smoothnes = spec.a * glossiness
    15.             //o.Smoothness = lerp(o.smoothness, spec.a, glossiness)
    16.            
    17.  
    18.             o.Alpha = albedo.a;
    19.  
    20.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
    21.  
    22.             //o.Occlusion = tex2D(_OcclusionMap, IN.uv_MainTex) * _OcclusionStrength;
    23.             //try lerp approach
    24.             o.Occlusion = tex2D(_OcclusionMap, IN.uv_MainTex) * _OcclusionStrength;
    25. }
    The issue with the current way I have it seems to be that when both smoothness and Metallic sliders are at 0 the mesh seems to render with a slightly washed out white look.
    Here's a pic of what I mean, the hair on the left is the metallic with smoothness and metalness set to 0.
    The hair on the right is the same but with specular workflow with smoothness at 0 and spec color to black (0,0,0).
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,278
    Because:
    Metallic 0.0 != Specular 0, 0, 0
    Metallic 0.0 == Specular 56, 56, 56
    Similarly:
    Metallic 1.0 != Specular 255, 255, 255
    Metallic 1.0 == Specular 250, 250, 250 * Albedo

    If you want black specular you must use the standard specular model. Now internally the standard (metallic) shader uses the standard specular, it just uses the Metallic value to calculate the specular color. You can use the same functions to convert the Metallic value into a specular in a surface shader, and additionally lerp the specular color to black if the smoothness is 0.0 (I've lerped to black over the last 0.1 of the smoothness range in some of the stuff I've worked on).