Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

vertex Outline overlapping

Discussion in 'Shaders' started by son1cman, Feb 13, 2019.

  1. son1cman

    son1cman

    Joined:
    Jan 2, 2015
    Posts:
    15
    I have found this project: https://github.com/chrisnolet/QuickOutline/ which do the trick that i am looking for, the problem is when you overlap between to objects using the same shader it erase the line:

    upload_2019-2-13_4-40-16.png

    Autor respond in unity asset store that: "The outline can be applied per-object with a few tweaks on the shader." but i cant figured out what are those small tweaks.

    This are the 2 shaders doind the trick:

    Outlinemask.shader (Renders the object in front of the inverted vertex)


    Shader "Custom/Outline Mask" {
    Properties {
    [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
    }
    SubShader {
    Tags {
    "Queue" = "Transparent+100"
    "RenderType" = "Transparent"
    }
    Pass {
    Name "Mask"
    Cull Off
    ZTest [_ZTest]
    ZWrite Off
    ColorMask 0
    Stencil {
    Ref 1
    Pass Replace
    }
    }
    }
    }

    OutlineFill.shader (Renders the actual line):

    Shader "Custom/Outline Fill" {
    Properties {
    [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
    _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
    _OutlineWidth("Outline Width", Range(0, 10)) = 2
    }
    SubShader {
    Tags {
    "Queue" = "Transparent+110"
    "RenderType" = "Transparent"
    "DisableBatching" = "True"
    }
    Pass {
    Name "Fill"
    Cull Off
    ZTest [_ZTest]
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    ColorMask RGB
    Stencil {
    Ref 1
    Comp NotEqual
    }
    CGPROGRAM
    #include "UnityCG.cginc"
    #pragma vertex vert
    #pragma fragment frag
    struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float3 smoothNormal : TEXCOORD3;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };
    struct v2f {
    float4 position : SV_POSITION;
    fixed4 color : COLOR;
    UNITY_VERTEX_OUTPUT_STEREO
    };
    uniform fixed4 _OutlineColor;
    uniform float _OutlineWidth;
    v2f vert(appdata input) {
    v2f output;
    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    float3 normal = any(input.smoothNormal) ? input.smoothNormal : input.normal;
    float3 viewPosition = UnityObjectToViewPos(input.vertex);
    float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));
    output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * _OutlineWidth / 1000.0);
    output.color = _OutlineColor;
    return output;
    }
    fixed4 frag(v2f input) : SV_Target {
    return input.color;
    }
    ENDCG
    }
    }
    }
     
  2. son1cman

    son1cman

    Joined:
    Jan 2, 2015
    Posts:
    15