Shader Notes

A collection of shader examples used in Unity3D

Anistropic



Simulates the directionality of grooves in a surface. Like brushed metals.



Shader "Custom/AnistropicShader"
{
    Properties
    {
        _MainTint ("Diffuse Tint", Color) = (1,1,1,1) 
        _MainTex ("Base (RGB)", 2D) = "white" {} 
        _SpecularColor ("Specular Color", Color) = (1,1,1,1) 
        _Specular ("Specular Amount", Range(0,1)) = 0.5 
        _SpecPower ("Specular Power", Range(0,1)) = 0.5 
        _AnisoDir ("Anisotropic Direction", 2D) = "" {} 
        _AnisoOffset ("Anisotropic Offset", Range(-1,1)) = -0.2 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Anisotropic 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0



        struct Input
        {
            float2 uv_MainTex;
            float2 uv_AnisoDir; 
        };

        struct SurfaceAnisoOutput 
        { 
            fixed3 Albedo; 
            fixed3 Normal; 
            fixed3 Emission; 
            fixed3 AnisoDirection; 
            half Specular; 
            fixed Gloss; 
            fixed Alpha; 
        };

        sampler2D _MainTex; 
        sampler2D _AnisoDir; 
        float4 _MainTint; 
        float4 _SpecularColor; 
        float _AnisoOffset; 
        float _Specular; 
        float _SpecPower; 


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceAnisoOutput o)
{ 
            half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint; 
            float3 anisoTex = UnpackNormal(tex2D(_AnisoDir, 
                IN.uv_AnisoDir)); 
    
            o.AnisoDirection = anisoTex; 
            o.Specular = _Specular; 
            o.Gloss = _SpecPower; 
            o.Albedo = c.rgb; 
            o.Alpha = c.a; 
        } 

    fixed4 LightingAnisotropic(SurfaceAnisoOutput s, fixed3 
            lightDir, half3 viewDir, fixed atten) 
        { 
            fixed3 halfVector = normalize(normalize(lightDir) + 
                normalize(viewDir)); 
            float NdotL = saturate(dot(s.Normal, lightDir)); 
    
            fixed HdotA = dot(normalize(s.Normal + s.AnisoDirection), 
                halfVector);  float aniso = max(0, sin(radians((HdotA + _AnisoOffset) * 
                180)));  float spec = saturate(pow(aniso, s.Gloss * 128) * 
                s.Specular); 
    
            fixed4 c; 
            c.rgb = ((s.Albedo * _LightColor0.rgb * NdotL) + 
                (_LightColor0.rgb * _SpecularColor.rgb * spec)) * 
                atten; 
            c.a = s.Alpha; 
            return c; 
        } 

        ENDCG
    }
    FallBack "Diffuse"
}
    

Blinn Phong



More efficient way of calculating and estimating specularity. Uses less code to achieve almost he same effect.



Shader "Custom/BlinnPhongShader"
{
    Properties
    {
        _MainTint ("Diffuse Tint", Color) = (1,1,1,1) 
        _MainTex ("Base (RGB)", 2D) = "white" {} 
        _SpecularColor ("Specular Color", Color) = (1,1,1,1) 
        _SpecPower ("Specular Power", Range(0.1,60)) = 3 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf CustomBlinnPhong 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0


        struct Input
        {
            float2 uv_MainTex;
        };

        sampler2D _MainTex; 
        float4 _MainTint; 
        float4 _SpecularColor; 
        float _SpecPower; 

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        fixed4 LightingCustomBlinnPhong (SurfaceOutput s, 
                                            fixed3  lightDir, 
                                            half3 viewDir, 
                                            fixed atten) 
        { 
            float NdotL = max(0,dot(s.Normal, lightDir)); 
    
            float3 halfVector = normalize(lightDir + viewDir); 
            float NdotH = max(0, dot(s.Normal, halfVector)); 
            float spec = pow(NdotH, _SpecPower) * _SpecularColor; 
    
            float4 color; 
            color.rgb = (s.Albedo * _LightColor0.rgb * NdotL) + 
                        (_LightColor0.rgb * _SpecularColor.rgb * spec) * atten; 
            color.a = s.Alpha; 
            return color; 
        } 

        void surf (Input IN, inout SurfaceOutput o) 
        {
            half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainTint;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }


        ENDCG
    }
    FallBack "Diffuse"
}
                

Cutout Mat



Cutout Rendering Mode All of the pixels in the Albedo map iwth an alph value equal to or less than Alpha Cutoff will be hidden Does not allow back of geometry to be seen. For that create custom shader with back geometry not culled.



Uses Standard Shader
                

FadeMat



Material that Fade Rendering Mode Alpha transparency for the Albedo Good for fading objects out completely



Uses Standard Shader 
                

Holograph



Semi transparent Uses Lambertian reflectance as its lighting so use SurfaceOutput intead of SurfaceOutputStandard Rim effect slider allows to change amount Initiator work by showing only the silhouette of an object The use of _DotProduct plays an important role int he way reflections are calculated.



Shader "Custom/Holograph"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _DotProduct("Rim effect", Range(-1,1)) = 0.25 
    }
    SubShader
    {
        Tags 
        { 
            "Queue" = "Transparent" 
            "IgnoreProjector" = "True" 
            "RenderType" = "Transparent" 
        } 

        LOD 200


        // if you want back to appear
        // Cull Off
        // Cull Back

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        // #pragma surface surf Lambert alpha:fade nolighting 
        #pragma surface surf Lambert alpha:fade

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex; 
            float3 worldNormal; 
            float3 viewDir; 
        };


        fixed4 _Color;

        float _DotProduct; 

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf(Input IN, inout SurfaceOutput o) 
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;

            float border = 1 - (abs(dot(IN.viewDir, 
                IN.worldNormal))); 
            float alpha = (border * (1 - _DotProduct) + _DotProduct); 
            o.Alpha = c.a * alpha; 
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Mirror Mat



Creates a mirror effect Reflection probe Opaque rendering mode Realtime Refresh Mode Every frame Culling mask everything



Uses Standard Shader
                

Normal Extrusion



Modulates extrusion vertex modifier different extrusion amounts for each a way to add variation to models create skinny or chubby version of soliders



Shader "Custom/NormalExtrusion"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _Amount ("Extrusion Amount", Range(-.0001, .0001)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard vertex:vert

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        float _Amount;


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void vert (inout appdata_full v) 
        {
            v.vertex.xyz += v.normal * _Amount;
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Normal Map Shader


Often called "Bump" map. Can show the bumps on a surface. To show variations in surface elevation. This features intensity sliders.



                    Shader "Custom/NormalShader"
{
    Properties
    {
        _MainTint ("Diffuse Tine", Color) = (0,1,0,1)
        _NormalTex ("Normal Map", 2D) = "bump" {}
        _NormalMapIntensity("Normal intensity", Range(0,3)) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

      

        struct Input
        {
            float2 uv_NormalTex;
        };

        sampler2D _NormalTex;
        float4 _MainTint;
        float _NormalMapIntensity;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {

 
            o.Albedo = _MainTint;
            float3 normalMap = UnpackNormal(tex2D(_NormalTex, IN.uv_NormalTex));

            normalMap.x *= _NormalMapIntensity;
            normalMap.y *= _NormalMapIntensity;



            // o.Normal = normalMap.rgb;
            o.Normal = normalize(normalMap.rgb);
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                   
                    
            

Phong



Most basic and preformance friendly specular type is the phone specular effect. calculation of the light direciton reflecting off the surface compared to the user's view direction.



Shader "Custom/Phong"
{
    Properties
    {
        _MainTint ("Diffuse Tint", Color) = (1,1,1,1) 
        _MainTex ("Base (RGB)", 2D) = "white" {} 
        _SpecularColor ("Specular Color", Color) = (1,1,1,1) 
        _SpecPower ("Specular Power", Range(0,30)) = 1 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf Phong

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        struct Input
        {
            float2 uv_MainTex;
        };

        float4 _SpecularColor; 
        sampler2D _MainTex; 
        float4 _MainTint; 
        float _SpecPower; 

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        fixed4 LightingPhong (SurfaceOutput s, fixed3 lightDir, 
                                half3 viewDir, fixed atten) 
        { 
            // Reflection 
            float NdotL = dot(s.Normal, lightDir); 
            float3 reflectionVector = normalize(2.0 * s.Normal * 
                NdotL - lightDir); 
    
            // Specular 
            float spec = pow(max(0, dot(reflectionVector, viewDir)), 
                _SpecPower); 
            float3 finalSpec = _SpecularColor.rgb * spec; 
    
            // Final effect 
            fixed4 c; 
            c.rgb = (s.Albedo * _LightColor0.rgb * max(0,NdotL) * 
                atten) + (_LightColor0.rgb * finalSpec); 
            c.a = s.Alpha; 
            return c; 
        } 

        void surf (Input IN, inout SurfaceOutput o) 
        {
            half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainTint;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

        ENDCG
    }
    FallBack "Diffuse"
}
                

Radius Shader



Technique for creating a circle o your terrain. When used with a script, the circle can move along with a character and display even on arbitary complex objects. The terrain needs to have the material changed so that the custom shader can be applied.



Shader "Custom/MyRadiusShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}

        _Center("Center", Vector) = (0,0,0,0)
        _Radius("Radius", Float) = 0.5
        _RadiusColor("Radius Color", Color) = (1,0,0,1)
        _RadiusWidth("Radius Width", Float) = 2
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex; // The UV of the terrain texture 
            float3 worldPos;   // The in-world position 
        };

        float3 _Center; 
        float _Radius; 
        fixed4 _RadiusColor; 
        float _RadiusWidth; 

        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Get the distance between the center of the 
            // place we wish to draw from and the input's 
            // world position
            float d = distance(_Center, IN.worldPos);

            // If the distance is larger than the radius and
            // it is less than our radius + width change the color
            if ((d > _Radius) && (d < (_Radius + _RadiusWidth)))
            {
                o.Albedo = _RadiusColor;
            }
            // Otherwise, use the normal color
            else
            {
                o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
            }
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

ScrollingUVs


For scrolling textures over the surface of an object. Waterfalls rivers and lava. Really, were manipulating the UV's so we are faking the effect of texture moving. Includes parameters for x and y scroll speeds.



Shader "Custom/ScrollingUVs"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _ScrollXSpeed ("X Scroll Speed", Range(0,10)) = 2
        _ScrollYSpeed ("Y Scroll Speed", Range(0,10)) = 2
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0


        fixed _ScrollXSpeed;
        fixed _ScrollYSpeed;
        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };


        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            			// Create a separate variable to store our UVs  
			 // before we pass them to the tex2D() function 
			 fixed2 scrolledUV = IN.uv_MainTex; 

			 // Create variables that store the individual x and y  
			 // components for the UV's scaled by time 
			 fixed xScrollValue = _ScrollXSpeed * _Time; 
			 fixed yScrollValue = _ScrollYSpeed * _Time; 

             			 // Apply the final UV offset 
			 scrolledUV += fixed2(xScrollValue, yScrollValue); 
       
			 // Apply textures and tint
			 half4 c = tex2D (_MainTex, scrolledUV); 
			 o.Albedo = c.rgb * _Color; 
			 o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                   

Simple Lambert



Considered non-photorealistic, but used in low-poly games as they produce a neat contract between faces. Very efficient.



Shader "Custom/SimpleLambert"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf SimpleLambert 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf(Input IN, inout SurfaceOutput o) { 
            o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; 
        } 


        // Allows us to use the SimpleLambert lighting mode
        half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, 
                                        half atten) 
        { 
            // First calculate the dot product of the light direction and the 
            // surface's normal
            half NdotL = dot(s.Normal, lightDir); 

            // Next, set what color should be returned
            half4 color; 

            color.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten); 
            color.a = s.Alpha; 

            // Return the calculated color
            return color; 
        } 



        ENDCG
    }
    FallBack "Diffuse"
}
                

Simple Vertex Color



Extract the vertext color and display it on the surface



Shader "Custom/SimpleVertexColor"
{
    Properties
    {
        _MainTint("Global Color Tint", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert vertex:vert

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

                float4 _MainTint;

        struct Input
        {
            float2 uv_MainTex;
            float4 vertColor;
        };

        void vert(inout appdata_full v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input,o);
            o.vertColor = v.color;
        }

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Albedo = IN.vertColor.rgb * _MainTint.rgb;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Simple Difffuse



Diffuse Shader for object that have a uniform color and smooth surface, but not smooth enough to shine in reflected light. It's sort of a matte material but beest represented with a Diffuse Shader. It's like a standard surface shader, but remove any texture information.



Shader "CookbookShaders/SimpleDiffuse"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        // sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };


        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
    
            o.Albedo = _Color.rgb;

        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Standard Diffuse



Same as the Unity Standard Surface Shader. This is a Surface Shader based on physically-based rendering (PBR). This type of shader simulates how light physically behaves when itting objects.




Shader "CookbookShaders/StandardDiffuse"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
               

Standard Color



Just a simple shader for color. No other calculations, so very performant.



Shader "CookbookShaders/StandardColor"
{
    Properties
    {
        _Color ("Color", Color) = (20,12,13,0.2)
        // _AmbientColor ("Ambient Color", Color) = (1,1,1,1)
        // _MySliderValue ("This is a Slider", Range(0,10)) = 2.5
        // _Glossiness ("Smoothness", Range(0,1)) = 1
        // _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        // sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        // half _Glossiness;
        // half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            // fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            fixed4 c = _Color;

            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            // o.Metallic = _Metallic;
            // o.Smoothness = _Glossiness;
            // o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Textured Shader



Maps 2D image to a 3D model using its UV models and textures



Shader "Custom/TexturedShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                    
                

Toon Shader



The Toon Shader Effect sometiems called Celluloid CEL shading. Give illusion that graphcis are being hand-drawn Uses a ramp map to remap the lamgbertiqn light intensity to another value Using a rmap map wityhout a gradient forces the lighting to be rendered in steps.



Shader "Custom/ToonShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" 
        _RampTex ("Ramp", 2D) = "white" {} 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf Toon

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _RampTex; 

        struct Input
        {
            float2 uv_MainTex;
        };


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf(Input IN, inout SurfaceOutput o) { 
            o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; 
        } 


        fixed4 LightingToon (SurfaceOutput s, fixed3 lightDir, fixed atten) 
        { 

            // First calculate the dot product of the light direction and the 
            // surface's normal
            half NdotL = dot(s.Normal, lightDir); 
            
            // Remap NdotL to the value on the ramp map
            NdotL = tex2D(_RampTex, fixed2(NdotL, 0.5)); 
    
            // Next, set what color should be returned
            half4 color; 

            color.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten ); 
            color.a = s.Alpha; 

            // Return the calculated color
            return color; 
        } 

        ENDCG
    }
    FallBack "Diffuse"
}
                

Trans Shader



Trans shader can be used when some parts are transparent. A PNG image can be used since it supports the alpha channel. The same image can be used to create a greyscale image Used on stained glass



Shader "Custom/TransShader"
{
Properties
{
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader
{
    Tags
    {
        "Queue" = "Transparent"
        "IgnoreProjector" = "True"
        "RenderType" = "Transparent"
    }
    LOD 200

    // Do not show back
    Cull Back

    CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard alpha:fade 

    // Use shader model 3.0 target, to get nicer looking lighting
    #pragma target 3.0

        sampler2D _MainTex;

    struct Input
    {
        float2 uv_MainTex;
    };

    fixed4 _Color;

    // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    // #pragma instancing_options assumeuniformscaling
    UNITY_INSTANCING_BUFFER_START(Props)
        // put more per-instance properties here
    UNITY_INSTANCING_BUFFER_END(Props)

    void surf (Input IN, inout SurfaceOutputStandard o)
    {
        // Albedo comes from a texture tinted by color
        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
        o.Albedo = c.rgb;
        o.Alpha = c.a;
    }
    ENDCG
}
FallBack "Diffuse"
}
            

TransparentMat



Material that uses Standard Shader Transparent Rendering Mode Alpha transparency for the Albedo Good for windows, bottles, gems



Uses Standard Shader
                

Vertex Animation



Writer shader to expect a vertex color and displayed on the surface of the model. Modify the positions of each vertex on a match with a sin way. Planes have a lot of verts Uses the same function that is built into the CG language. And uses the built-in vertex parameters the surface shaders give us.



Shader "Custom/VertexAnimation"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _tintAmount ("Tint Amount", Range(0,1)) = 0.5
        _ColorA ("Color A", Color) = (1,1,1,1)
        _ColorB ("Color B", Color) = (1,1,1,1)
        _Speed ("Wave Speed", Range(0.1, 80)) = 5
        _Frequency ("Wave Frequency", Range(0, 5)) = 2
        _Amplitude ("Wave Amplitude", Range(-1, 1)) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Lambert vertex:vert

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        float4 _ColorA;
        float4 _ColorB;
        float _tintAmount;
        float _Speed;
        float _Frequency;
        float _Amplitude;
        float _OffsetVal;

        struct Input
        {
            float2 uv_MainTex;
            float3 vertColor;
        };

        void vert(inout appdata_full v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input,o);
            float time = _Time * _Speed;
            float waveValueA = sin(time + v.vertex.x * _Frequency) * _Amplitude;

            v.vertex.xyz = float3(v.vertex.x, v.vertex.y + waveValueA, v.vertex.z);
            v.normal = normalize(float3(v.normal.x + waveValueA, v.normal.y, v.normal.z));
            o.vertColor = float3(waveValueA,waveValueA,waveValueA);
        }

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            float3 tintColor = lerp(_ColorA, _ColorB, IN.vertColor).rgb; 
            o.Albedo = c.rgb * (tintColor * _tintAmount);
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Vertex Extrusion Map



Builds on NormalExtrusion An extra texture can be used to indicate the amount of extrusion. Allows more control over which parts are raised or lowered.



Shader "Custom/VertexExtrusionMapShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _ExtrusionTex("Extrusion map", 2D) = "white" {}
    _Amount("Extrusion Amount", Range(-0.0001,0.0001)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
#pragma surface surf Standard vertex:vert
    struct Input {
        float2 uv_MainTex;
    };

    float _Amount;
    sampler2D _ExtrusionTex;
    void vert(inout appdata_full v) {
        float4 tex = tex2Dlod (_ExtrusionTex, float4(v.texcoord.xy,0,0));
        float extrusion = tex.r * 2 - 1;
        v.vertex.xyz += v.normal * _Amount * extrusion;
    }
    sampler2D _MainTex;
    void surf(Input IN, inout SurfaceOutputStandard o) {

        float4 tex = tex2D(_ExtrusionTex, IN.uv_MainTex);
        float extrusion = abs(tex.r * 2 - 1);

        o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
        o.Albedo = lerp(o.Albedo.rgb, float3(0, 0,0), extrusion * _Amount / 0.0001 *1.1);
    }
    ENDCG
    }
        Fallback "Diffuse"
}
                

Volumetric Explosion



Instead of particles, these are evolving 3D objects.



Shader "Custom/MyVolumetricExplosion"
{
    Properties
    {
        _RampTex("Color Ramp", 2D) = "white" {}
        _RampOffset("Ramp offset", Range(-0.5,0.5))= 0
        
        _NoiseTex("Noise Texture", 2D) = "gray" {}
        _Period("Period", Range(0,1)) = 0.5
        
        _Amount("_Amount", Range(0, 1.0)) = 0.1
        _ClipRange("ClipRange", Range(0,1)) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert vertex:vert nolightmap

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        struct Input 
        {
            float2 uv_NoiseTex;
        };

        sampler2D _RampTex;
        half _RampOffset;

        sampler2D _NoiseTex;
        float _Period;

        half _Amount;
        half _ClipRange;

void vert(inout appdata_full v) 
        {
            float3 disp = tex2Dlod(_NoiseTex, float4(v.texcoord.xy,0,0));
            float time = sin(_Time[3] *_Period + disp.r*10);
            v.vertex.xyz += v.normal * disp.r * _Amount * time;
        }


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {
            float3 noise = tex2D(_NoiseTex, IN.uv_NoiseTex);

            float n = saturate(noise.r + _RampOffset);

            clip(_ClipRange - n);

            half4 c = tex2D(_RampTex, float2(n,0.5));

            o.Albedo = c.rgb;
            o.Emission = c.rgb*c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
                

Multiply Shader



Vertex and Fragment Shader Technique Multiply Material



Shader "Custom/MyMultiplyShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,0,0,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
                Pass 
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            half4 _Color;
            sampler2D _MainTex;
            struct vertInput 
            {
                float4 pos : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct vertOutput 
            {
                float4 pos : SV_POSITION;
                float2 texcoord : TEXCOORD0;
            };

            vertOutput vert(vertInput input) 
            {
                vertOutput o;
                o.pos = UnityObjectToClipPos(input.pos);
                o.texcoord = input.texcoord;
                return o;
            }

            half4 frag(vertOutput output) : COLOR
            {
                half4 mainColour = tex2D(_MainTex, output.texcoord);
                return mainColour * _Color;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}
                

Window Shader



Most glasses are not perfect this is for distortions or deformations uses Vertex and Fragment Shader with GrabPass Grabs screen and creates a distortion based ona normal map



Shader "Custom/MyWindowShader"
{
    Properties
    {
        _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
        _Colour("Colour", Color) = (1,1,1,1)
        _BumpMap("Noise text", 2D) = "bump" {}
        _Magnitude("Magnitude", Range(0,1)) = 0.05
    }
    SubShader
    {
            Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }

        //GrabPass{ "_GrabTexture" }	// For a shared texture
        GrabPass{  }					// For a new pass every time
        
        Pass 
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            sampler2D _GrabTexture;

            sampler2D _MainTex;
            fixed4 _Colour;

            sampler2D _BumpMap;
            float _Magnitude;

            struct vertInput 
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct vertOutput 
            {
                float4 vertex : POSITION;
                float4 uvgrab : TEXCOORD1;
                float2 texcoord : TEXCOORD0;
            };

            // Vertex function
            vertOutput vert(vertInput v) 
            {
                vertOutput o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uvgrab = ComputeGrabScreenPos(o.vertex);
                o.texcoord = v.texcoord;
                return o;
            }

            // Fragment function
            half4 frag(vertOutput i) : COLOR 
            {
                half4 mainColour = tex2D(_MainTex, i.texcoord);
                half4 bump = tex2D(_BumpMap, i.texcoord);
                half2 distortion = UnpackNormal(bump).rg;

                i.uvgrab.xy += distortion * _Magnitude;

                fixed4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
                return col * mainColour * _Colour;
            }
            ENDCG
        }

    }
    FallBack "Diffuse"
}