Search Unity

Why standard shader looks different from my custom surf standard shader?

Discussion in 'Shaders' started by MaxPirat, May 31, 2019.

  1. MaxPirat

    MaxPirat

    Joined:
    Sep 7, 2015
    Posts:
    12
    I'm really confused about why does the Standard Unity shader look so different from my bare-bone shader with Surf Standard. The biggest and maybe the only difference is how Specular Highlights looks, mine is much darker make it almost unusable along with objects with Standard shader in the scene. From my understanding, if I use the same textures and use "surf Standard" it should look pretty much the same. Here is the shader:
    Code (CSharp):
    1. Shader "Custom/MyStandard" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Metallic("Metallic (R=Metallic, A=Smoothness)", 2D) = "white" {}
    6.         _Smoothness("Smoothness", Range(0,1)) = 0.5
    7.         _BumpMap("Bumpmap", 2D) = "bump" {}
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.      
    12.         CGPROGRAM
    13.         #pragma surface surf Standard fullforwardshadows
    14.  
    15.         sampler2D _MainTex;
    16.         sampler2D _Metallic;
    17.         sampler2D _BumpMap;
    18.  
    19.         float _Smoothness;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.             float2 uv_Dirt;
    24.             float3 viewDir;
    25.         };
    26.  
    27.         fixed4 _Color;
    28.  
    29.         void surf (Input IN, inout SurfaceOutputStandard o) {
    30.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    31.             o.Albedo = c.rgb;
    32.  
    33.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    34.  
    35.             fixed4 m = tex2D(_Metallic, IN.uv_MainTex);
    36.             o.Metallic = m.r;
    37.             o.Smoothness =  _Smoothness * m.a;
    38.         }
    39.         ENDCG
    40.     }
    41.     FallBack "Diffuse"
    42. }
    And now comparising to show what I mean (please note that all textures and settings are the same 100%)
    MyStandard.png

    I really need to get this looking the same way (or similar). If it is indeed intended behavior, how can I fake it to make it look more simular?
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    edit: Guess it was the #pragma 3.0. I had tested it initially without texture and it made no difference, but once I setup all the texture channels it did make a difference, bleh.
     
    Last edited: Jun 1, 2019
    MaxPirat likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You're missing one line:
    #pragma target 3.0

    If you don't have that line, Unity shaders default to the equivalent of "target 2.0". Basically it means "support every possible platform", where as target 3.0 means "target modern devices".
    https://docs.unity3d.com/Manual/SL-ShaderCompileTargets.html
    The Standard lighting model uses a simplified, mobile version of the lighting when the shader is targeting 2.0 compared to 3.0 and above.

    This only has an affect on float values, like how the Standard shader uses it, not textures.
     
    MUGIK and MaxPirat like this.
  4. MaxPirat

    MaxPirat

    Joined:
    Sep 7, 2015
    Posts:
    12
    Thanks, bgolus! Adding "#pragma target 3.0" is exactly what I was looking for, now it is 1 to 1. Although, seems wrong that "nicer lighting" gives much brighter results.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Standard using target 2.0 is lambert diffuse with normalized blinn-phong specular.
    Standard using target 3.0 is disney diffuse with ggx specular and additional fresnel effects.

    They're very different. GGX has a much wider specular highlight, so at the same relative smoothness values more of the mesh will be lit up from that. And at 0% smoothness, disney diffuse has a very flat and "soft" diffuse falloff curve meaning more of the surface facing the light will appear lit compared to lambert.

    Lambert & normalized blinn phong is kind of where PBR started off, with it being the first steps toward having the "specular sharpness" (now referred to as glossiness, roughness, or smoothness) be what controls the specular brightness, which is how it works in the real world, rather than having artists arbitrarily tweak them as two different values. Disney diffuse or some other Oren-Nayer like diffuse with GGX is where we are now, and better matches real world measured data than blinn phong, though it's far from perfect.
     
    MUGIK likes this.