Search Unity

Is it possible to add an outline using standard surface shader?

Discussion in 'Shaders' started by Zaine7673, Feb 22, 2019.

  1. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    So I've only started messing around with shaders and have managed to write the shader below to do a few things like blend two textures, add emission etc. - it works :)

    I've been looking around for some info on if/how i may be able to add an outline using the same shader. I've come across a few examples but they all seem to be using emission to do this and it does not give me the effect I desire. I'm looking for a black outline i can adjust the width of using a slider.

    is this at all possible with a surface shader or only with a vertex shader?

    (I'm using a surface shader because I need it to react to lighting)

    Go gentle if I've made any noob errors :p

    Code (CSharp):
    1. Shader "Dreemlabs/TextureBlendByUV" {
    2.         Properties {
    3.             _Color ("Color", Color) = (1,1,1,1)
    4.             _Blend ("Texture Blend", Range(0,1)) = 0.0
    5.             _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.             _MainTex2 ("Albedo 2 (RGB)", 2D) = "white" {}
    7.            
    8.             _Emissive("Emission", 2D) = "black" {}
    9.             _EmissiveColor("Emission Color", Color) = (1,1,1,1)
    10.             _EmissiveIntensity("Emission Intensity", Range(0,1) ) = 1
    11.  
    12.             _Glossiness ("Smoothness", Range(0,1)) = 0.5
    13.             _Metallic ("Metallic", Range(0,1)) = 0.0
    14.         }
    15.  
    16.         SubShader {
    17.             Tags { "RenderType"="Opaque" }
    18.             LOD 200
    19.        
    20.             CGPROGRAM
    21.                 // Physically based Standard lighting model, and enable shadows on all light types
    22.                 #pragma surface surf Standard fullforwardshadows
    23.        
    24.                 // Use shader model 3.0 target, to get nicer looking lighting
    25.                 #pragma target 3.0
    26.        
    27.                 sampler2D _MainTex;
    28.                 sampler2D _MainTex2;
    29.  
    30.                 sampler2D _Emissive;
    31.        
    32.                 struct Input {
    33.                     float2 uv_MainTex;
    34.                     float2 uv2_MainTex2;
    35.  
    36.                     float2 uv_Emissive;
    37.                 };
    38.        
    39.                 half _Blend;
    40.                 half _Glossiness;
    41.                 half _Metallic;
    42.                 fixed4 _Color;
    43.  
    44.                 float4 _EmissiveColor;
    45.                 float _EmissiveIntensity;
    46.        
    47.                 void surf (Input IN, inout SurfaceOutputStandard o) {
    48.                     fixed4 c = lerp (tex2D (_MainTex, IN.uv_MainTex), tex2D (_MainTex2, IN.uv2_MainTex2), _Blend) * tex2D (_MainTex, IN.uv_MainTex);
    49.                     o.Albedo = c.rgb;
    50.                     o.Alpha = tex2D (_MainTex, IN.uv_MainTex);
    51.  
    52.                     float4 Tex2D1=tex2D(_Emissive,(IN.uv_Emissive.xyxy).xy);
    53.                     float4 Multiply0=Tex2D1 * _Color.xyxy;
    54.                     float4 Multiply2=Multiply0 * _EmissiveIntensity.xxxx  * _EmissiveColor ;
    55.                     o.Emission = Multiply2;
    56.  
    57.                     o.Metallic = _Metallic;
    58.                     o.Smoothness = _Glossiness;
    59.                 }
    60.             ENDCG
    61.         }
    62.         FallBack "Diffuse"
    63.     }