Extend Standard Shader

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?

Basically the Standard shader but with metallic, smoothness, ao and emission in one texture:

Shader "Custom/Standard"
{
    Properties
    {
        _Albedo ("Albedo (RGB), Alpha (A)", 2D) = "white" {}
        [NoScaleOffset]
        _Metallic ("Metallic (R), Occlusion (G), Emission (B), Smoothness (A)", 2D) = "black" {}
        [NoScaleOffset]
        _Normal ("Normal (RGB)", 2D) = "bump" {}
    }
  
    SubShader
    {
        Tags
        {
            "Queue" = "Geometry"
            "RenderType" = "Opaque"
        }
      
        CGINCLUDE
        #define _GLOSSYENV 1
        ENDCG
      
        CGPROGRAM
        #pragma target 3.0
        #include "UnityPBSLighting.cginc"
        #pragma surface surf Standard
        #pragma exclude_renderers gles

        struct Input
        {
            float2 uv_Albedo;
        };

        sampler2D _Albedo;
        sampler2D _Normal;
        sampler2D _Metallic;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 albedo = tex2D(_Albedo, IN.uv_Albedo);
            fixed4 metallic = tex2D(_Metallic, IN.uv_Albedo);
            fixed3 normal = UnpackScaleNormal(tex2D(_Normal, IN.uv_Albedo), 1);
      
            o.Albedo = albedo.rgb;
            o.Alpha = albedo.a;
            o.Normal = normal;
            o.Smoothness = metallic.a;
            o.Occlusion = metallic.g;
            o.Emission = metallic.b;
            o.Metallic = metallic.r;
        }
        ENDCG
    }
  
    FallBack "Diffuse"
}
1 Like

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!

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.

Agreed, its a problem also on desktop when you need approximations ie low detail levels.

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.