Search Unity

How to make Outline Shader draw by Z-Order?

Discussion in 'Shaders' started by dohaiha930, May 7, 2018.

  1. dohaiha930

    dohaiha930

    Joined:
    Mar 27, 2018
    Posts:
    55
    Hi, i use Quick Outline from asset store to draw my 3d gameobject outline, it work, but have some problem, when 2 gameobject collapse each other, collapse area don't draw outline. I want outline draw by Z-order of gameoject, how can i achieve this?

    Shader Code:

    Code (CSharp):
    1. Shader "Custom/Outline Fill" {
    2.   Properties {
    3.     [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
    4.  
    5.     _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
    6.     _OutlineWidth("Outline Width", Range(0, 10)) = 2
    7.   }
    8.  
    9.   SubShader {
    10.     Tags {
    11.       "Queue" = "Transparent+110"
    12.       "RenderType" = "Transparent"
    13.       "DisableBatching" = "True"
    14.     }
    15.  
    16.     Pass {
    17.       Name "Fill"
    18.       Cull Off
    19.       ZTest [_ZTest]
    20.       ZWrite Off
    21.       Blend SrcAlpha OneMinusSrcAlpha
    22.       ColorMask RGB
    23.  
    24.       Stencil {
    25.         Ref 1
    26.         Comp NotEqual
    27.         Pass Replace
    28.       }
    29.  
    30.       CGPROGRAM
    31.       #include "UnityCG.cginc"
    32.  
    33.       #pragma vertex vert
    34.       #pragma fragment frag
    35.  
    36.       struct appdata {
    37.         float4 vertex : POSITION;
    38.         float3 smoothNormal : TEXCOORD3;
    39.         UNITY_VERTEX_INPUT_INSTANCE_ID
    40.       };
    41.  
    42.       struct v2f {
    43.         float4 position : SV_POSITION;
    44.         fixed4 color : COLOR;
    45.         UNITY_VERTEX_OUTPUT_STEREO
    46.       };
    47.  
    48.       uniform float _OutlineWidth;
    49.       uniform fixed4 _OutlineColor;
    50.  
    51.       v2f vert(appdata input) {
    52.         v2f output;
    53.  
    54.         UNITY_SETUP_INSTANCE_ID(input);
    55.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    56.  
    57.         float3 viewPosition = UnityObjectToViewPos(input.vertex);
    58.         float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, input.smoothNormal));
    59.  
    60.         output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * _OutlineWidth / 1000.0);
    61.         output.color = _OutlineColor;
    62.  
    63.         return output;
    64.       }
    65.  
    66.       fixed4 frag(v2f input) : SV_Target {
    67.         return input.color;
    68.       }
    69.       ENDCG
    70.     }
    71.   }
    72. }
    73.  
    cube-2.PNG
    Normal Outline

    cube-1.PNG
    When 2 cube collapse

    cube-13.png
    What i want to achieve, outline by Zorder
     
  2. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Remove the stencil bits, and make sure they're in different Z layers
     
  3. dohaiha930

    dohaiha930

    Joined:
    Mar 27, 2018
    Posts:
    55
    So i just remove this ?

    Code (CSharp):
    1. Stencil {
    2.         Ref 1
    3.         Comp NotEqual
    4.         Pass Replace
    5.       }
    I 'm beginner of shader learning so please forgive about stupid question. :D
     
  4. dohaiha930

    dohaiha930

    Joined:
    Mar 27, 2018
    Posts:
    55

    Attached Files:

  5. Mikekan13

    Mikekan13

    Joined:
    May 30, 2015
    Posts:
    90
    Last edited: May 27, 2019
  6. Daniel_Darko

    Daniel_Darko

    Joined:
    May 18, 2017
    Posts:
    2
    Did you ever find out? Trying to know what he changed.
     
  7. Walidkila

    Walidkila

    Joined:
    Jun 5, 2021
    Posts:
    1