Search Unity

Add outline to arrow

Discussion in 'General Graphics' started by denali95, Oct 11, 2017.

  1. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I'm trying to add an outline to an arrow 3d model to make its edges stick out more. Any advice?
     
  2. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    This simple outlining shader requires your mesh has all the joints welded otherwise the joints will create gaps for the outline (as can be seen when applied to built-in cube, cylinder or capsule).

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Legacy Shaders/Diffuse outlined" {
    4. Properties {
    5.     _Color ("Main Color", Color) = (1,1,1,1)
    6.     _OutlineColor ("Outline Color", Color) = (1,1,1,1)
    7.     _thickness ("Outline thickness", Range(-1,1)) = .2
    8.     _MainTex ("Base (RGB)", 2D) = "white" {}
    9. }
    10. SubShader {
    11.     Tags { "RenderType"="Opaque" }
    12.     LOD 200
    13.    
    14.     Pass {
    15.         Name "Outliner"
    16.         Cull Front
    17.         CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.            
    21.             fixed4 _OutlineColor;
    22.             half _thickness;
    23.  
    24.             struct appdata {
    25.                 float4 pos : POSITION;
    26.                 fixed4 normal : NORMAL0;
    27.             };
    28.  
    29.             struct v2f {
    30.                 fixed4 pos : SV_POSITION;
    31.             };
    32.            
    33.             v2f vert (appdata v) {
    34.                 v2f o;
    35.                 v.pos.xyz += -v.normal.xyz * _thickness;
    36.                 o.pos = mul(UNITY_MATRIX_MVP, v.pos);
    37.                 return o;
    38.             }
    39.            
    40.             half4 frag ( v2f i ) : COLOR {
    41.                 return _OutlineColor;
    42.             }
    43.         ENDCG
    44.     }
    45.    
    46.     CGPROGRAM
    47.         #pragma surface surf Lambert
    48.        
    49.         sampler2D _MainTex;
    50.         fixed4 _Color;
    51.  
    52.         struct Input {
    53.             float2 uv_MainTex;
    54.         };
    55.  
    56.         void surf (Input IN, inout SurfaceOutput o) {
    57.            
    58.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    59.             o.Albedo = c.rgb;
    60.             o.Alpha = c.a;
    61.         }
    62.     ENDCG
    63.     }
    64.     Fallback "Legacy Shaders/VertexLit"
    65. }
     
    theANMATOR2b likes this.