Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Standard Shader vs (identical?) Standard Surface Shader

Discussion in 'Shaders' started by pixelsplit, Aug 22, 2016.

  1. pixelsplit

    pixelsplit

    Joined:
    Sep 16, 2013
    Posts:
    171
    Hi,

    since we need to get rid of the no-secondary-maps-on-mobile-Restriction we've create a custom Standard Surface Shader.

    However the custom shader (which just sets some basic vars) does not work like intended. First issue, is that the _BumpScale is not applied like in the Standard Shader (a "fine grained" value like 0.26 is the same as if set to 1, but 2 is stronger, 3 etc. - but the value is a float?)
    Code (CSharp):
    1. o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)) * _BumpScale;
    Is this the correct way to apply the BumpScale?

    The second issue is that - even without bump - the detail albedo is much brighter when using the Standard Shader.

    Standard Shader: https://www.dropbox.com/s/1s6fj9r8uwwztu5/Screenshot 2016-08-22 17.22.51.png?dl=0

    Custom Shader: https://www.dropbox.com/s/2ook10wqap3sf27/Screenshot 2016-08-22 17.22.38.png?dl=0

    Here is the full source of the shader (Note that for simplification / performance i use the UV of MainTex for Metallic and Bump):
    Code (CSharp):
    1. Shader "Custom Colorable Object" {
    2.     Properties {
    3.         _Color("Color", Color) = (1,1,1,1)
    4.  
    5.         _MainTex("Albedo", 2D) = "white" {}
    6.  
    7.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    8.  
    9.         _GlossMapScale("Smoothness", Range(0, 1)) = 0
    10.  
    11.         _MetallicFactor("Metallic Factor",Range(0, 1)) = 0
    12.  
    13.         _BumpMap("Normal Map", 2D) = "bump" {}
    14.  
    15.         _BumpScale("Scale", Float) = 1.0
    16.    
    17.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "white" {}
    18.     }
    19.     SubShader {
    20.         Tags { "RenderType"="Opaque" }
    21.         LOD 200
    22.        
    23.         CGPROGRAM
    24.         // Physically based Standard lighting model, and enable shadows on all light types
    25.         #pragma surface surf Standard fullforwardshadows
    26.  
    27.         // Use shader model 3.0 target, to get nicer looking lighting
    28.         #pragma target 3.0
    29.  
    30.         sampler2D _MainTex;
    31.         sampler2D _DetailAlbedoMap;
    32.         sampler2D _BumpMap;
    33.         sampler2D _MetallicGlossMap;
    34.  
    35.         float _MetallicFactor;
    36.         float _BumpScale;
    37.         float _GlossMapScale;
    38.  
    39.         struct Input {
    40.             float2 uv_MainTex;
    41.             //float2 uv_BumpMap;
    42.             float2 uv2_DetailAlbedoMap;
    43.  
    44.         };
    45.  
    46.         fixed4 _Color;
    47.  
    48.         void surf (Input IN, inout SurfaceOutputStandard o) {
    49.             // Albedo comes from a texture tinted by color
    50.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * tex2D(_DetailAlbedoMap, IN.uv2_DetailAlbedoMap) * _Color;
    51.             //fixed4 c = tex2D(_DetailAlbedoMap, IN.uv2_DetailAlbedoMap) * _Color;
    52.             o.Albedo = c.rgb;
    53.             // Bump
    54.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex)) * _BumpScale;
    55.             // Metallic
    56.             fixed4 mc = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    57.             o.Metallic = mc.r * _MetallicFactor;
    58.             o.Smoothness = mc.a * _GlossMapScale;
    59.         }
    60.         ENDCG
    61.     }
    62.     FallBack "Diffuse"
    63. }
    64.  
    I'd appreciate any help on these issues!

    Best regards
    zkw
     
  2. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    No. That's the way to scale bumps:

    Code (csharp):
    1. o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_BumpMap), _BumpScale);
    Also you are correct in saying detail is brighter that's because if _DETAIL_MULX2 is defined (which it is normally) detailalbedo is multiplied by a constant:

    Code (csharp):
    1. detailAlbedo * unity_ColorSpaceDouble.rgb
    Overall you'll need to refer to UnityStandardInput.cginc and probably the rest of the standard shader source as the documentation does a really poor job explaining anything.
     
    pixelsplit likes this.
  3. pixelsplit

    pixelsplit

    Joined:
    Sep 16, 2013
    Posts:
    171
    Perfect! Thank you very much! Just one thing: If i use unity_ColorSpaceDouble.rgb the shader can't compile. I now just use 2 as a constant. Any idea how to get this Unity constant in my shader?
     
  4. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Don't know the value but then again this should compile. Are you sure you don't have a typo?