Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Extend Standard Shader

Discussion in 'Unity 5 Pre-order Beta' started by Bravo_cr, Feb 12, 2015.

  1. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    Hi,

    I just want to extend the standard shader with, for example a blend between two textures for the albedo. I know how to do that in the "old" surface shaders and also in CG, but I have no idea where to start with the new standard shader. I have downloaded the built-in shaders and have taken a look at all the includes, but I haven't I can't figure how to make this work. Because there is not documentation yet, could anyone post a quick example of a standard modified shader, so I could start learning form that?
     
  2. cakeslice

    cakeslice

    Joined:
    Oct 18, 2014
    Posts:
    197
    Basically the Standard shader but with metallic, smoothness, ao and emission in one texture:

    Code (CSharp):
    1. Shader "Custom/Standard"
    2. {
    3.     Properties
    4.     {
    5.         _Albedo ("Albedo (RGB), Alpha (A)", 2D) = "white" {}
    6.         [NoScaleOffset]
    7.         _Metallic ("Metallic (R), Occlusion (G), Emission (B), Smoothness (A)", 2D) = "black" {}
    8.         [NoScaleOffset]
    9.         _Normal ("Normal (RGB)", 2D) = "bump" {}
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags
    15.         {
    16.             "Queue" = "Geometry"
    17.             "RenderType" = "Opaque"
    18.         }
    19.      
    20.         CGINCLUDE
    21.         #define _GLOSSYENV 1
    22.         ENDCG
    23.      
    24.         CGPROGRAM
    25.         #pragma target 3.0
    26.         #include "UnityPBSLighting.cginc"
    27.         #pragma surface surf Standard
    28.         #pragma exclude_renderers gles
    29.  
    30.         struct Input
    31.         {
    32.             float2 uv_Albedo;
    33.         };
    34.  
    35.         sampler2D _Albedo;
    36.         sampler2D _Normal;
    37.         sampler2D _Metallic;
    38.  
    39.         void surf (Input IN, inout SurfaceOutputStandard o)
    40.         {
    41.             fixed4 albedo = tex2D(_Albedo, IN.uv_Albedo);
    42.             fixed4 metallic = tex2D(_Metallic, IN.uv_Albedo);
    43.             fixed3 normal = UnpackScaleNormal(tex2D(_Normal, IN.uv_Albedo), 1);
    44.      
    45.             o.Albedo = albedo.rgb;
    46.             o.Alpha = albedo.a;
    47.             o.Normal = normal;
    48.             o.Smoothness = metallic.a;
    49.             o.Occlusion = metallic.g;
    50.             o.Emission = metallic.b;
    51.             o.Metallic = metallic.r;
    52.         }
    53.         ENDCG
    54.     }
    55.  
    56.     FallBack "Diffuse"
    57. }
     
    Psyco92 likes this.
  3. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    Many thanks cakeslice. It was pretty easy at the end... I have notice that there is a really small difference between the Standard (RC1) and this shader in the roughness representation (reflections shows slightly different) but anyway I can start growing from here. Many many thanks!
     
  4. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    Hi,
    I keep investigating the new standard shader and have seen some interesting things. When you made a surface shader using "#pragma surface surf Standard" you get the proper lighting, but not all the performance/quality tweaks that the Standard shader does. As an example the shader posted above by cakeslice does work perfect but has worst performance on iPad 3 than the normal Standard, and thats because the standard shader when running on iPad 3, iPad2 ... runs with per vertex specular and other simplifications. That's great, but because is so transparent for us I don't know how this is happening and how to replicate when using our custom surface shaders. Checking the shader includes I see keywords all over that place (SHADER_API_MOBILE , UNITY_BRDF_FBS NRDF2_Unity_PBS ...) which I'm sure that this shows the path, but I don't see where this are being changed by platform. I will keep searching and post my progress.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Agreed, its a problem also on desktop when you need approximations ie low detail levels.
     
  6. lilymontoute

    lilymontoute

    Joined:
    Feb 8, 2011
    Posts:
    1,181
    SHADER_API_MOBILE and anything using SM2.0 is where you want to look. From what I understand a number of features are switched off/lowered with certain hardware, ranging from per-vertex specular to turning off dithered shadows.

    I recommend taking some time to sit through and read through the Standard Shader and its includes to get a good picture of what its doing, especially with mobile optimizations. Hopefully some of those features (scaling down for lower hardware) will make it into surface shaders as well.