Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Why pixels in the second pass are not drawen in a transparent shader?

Discussion in 'Shaders' started by geniusz, Dec 6, 2021.

  1. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    This is a simple outline shader, draw the object first, then draw a bigger one as the outline.

    Code (CSharp):
    1. Shader "Unlit/OutLine"
    2. {
    3.    Properties
    4.    {
    5.        _Color("Color",Color) = (0,0,0,1)
    6.        _Color2("Color 2",Color) = (0,0,0,0)
    7.    }
    8.  
    9.    SubShader
    10.    {
    11.        Tags
    12.        {
    13.            "RenderType" = "Transparent"
    14.            "Queue" = "Transparent"
    15.        }
    16.        Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.        Pass
    19.        {
    20.            Name "Front"
    21.            Cull Back
    22.  
    23.            CGPROGRAM
    24.            #include "UnityCG.cginc"
    25.            #pragma vertex vert
    26.            #pragma fragment frag
    27.  
    28.            uniform float4 _Color;
    29.  
    30.            float4 vert(float3 v : POSITION) : SV_POSITION
    31.            {
    32.                return UnityObjectToClipPos(v);
    33.            }
    34.  
    35.            float4 frag() : COLOR { return _Color; }
    36.            ENDCG
    37.        }
    38.  
    39.        Pass
    40.        {
    41.            Name "Back"
    42.            Cull Front
    43.  
    44.            CGPROGRAM
    45.            #include "UnityCG.cginc"
    46.            #pragma vertex vert
    47.            #pragma fragment frag
    48.  
    49.            uniform float4 _Color2;
    50.            struct vin { float3 vertex : POSITION; float3 normal : NORMAL; };
    51.  
    52.            float4 vert(vin v) : SV_POSITION
    53.            {
    54.                v.vertex.xyz += v.normal * 0.1;
    55.                return UnityObjectToClipPos(v.vertex);
    56.            }
    57.  
    58.            float4 frag() : COLOR { return _Color2; }
    59.            ENDCG
    60.        }
    61.    }
    62. }
    Because this is a transparent shader, so I think z-write is disabled by default and pixels in the second should be drawen and blended with the first pass, but they are not.


    • Left: normal rendering result
    • Right: reversed pass order


    Here is the material settings, the object is green, the outline is red.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    ZWrite is not disabled unless you manually disable it for vertex fragment shaders.
     
    Zack_C likes this.
  3. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    You solved my confusion, thanks a lot.