Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Outline Shader HDRP

Discussion in 'Shaders' started by Matze_MP, Mar 26, 2019.

  1. Matze_MP

    Matze_MP

    Joined:
    Dec 4, 2018
    Posts:
    22
    Hey,

    i searching for whiles now for an outline shader working with hdrp..

    to be honest i have realy no knowledge with shader programing, and are very happy with the new shadergraph.

    i just read some Topics about Outline Shader and i think i got some Idea how it should work. This is my "simple" definition what i understand. Scale the Vertex of an object from actual view angle to get the Outline.

    In the Shadergraph i just found Vertex Color. i guess it is not implemented yet into the Graph.

    Then i looked into this Asset:

    https://assetstore.unity.com/packages/tools/particles-effects/quick-outline-115488

    i Think it got all what i need but i guess the shader isnt compatible with the HDRP Pipeline here is the Code from the Fill and Mask Shader (due it is free i hope it is no problem to post it here):

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

    Code (CSharp):
    1. //
    2. //  OutlineMask.shader
    3. //  QuickOutline
    4. //
    5. //  Created by Chris Nolet on 2/21/18.
    6. //  Copyright © 2018 Chris Nolet. All rights reserved.
    7. //
    8.  
    9. Shader "Custom/Outline Mask" {
    10.   Properties {
    11.     [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
    12.   }
    13.  
    14.   SubShader {
    15.     Tags {
    16.       "Queue" = "Transparent+100"
    17.       "RenderType" = "Transparent"
    18.     }
    19.  
    20.     Pass {
    21.       Name "Mask"
    22.       Cull Off
    23.       ZTest [_ZTest]
    24.       ZWrite Off
    25.       ColorMask 0
    26.  
    27.       Stencil {
    28.         Ref 1
    29.         Pass Replace
    30.       }
    31.     }
    32.   }
    33. }
    34.  

    Is there any way to get this two shaders working within the new pipeline (changing wording or implementing the right library) Does anybody have an Idea ?

    Or is there some way to get the vertex "displacement" into the Shader Graph Custom Node ?
     
  2. Matze_MP

    Matze_MP

    Joined:
    Dec 4, 2018
    Posts:
    22
    Hi, i was able to creat a sort of Outline Shader in shadergraph:

    upload_2019-3-26_17-24-37.png

    It is doing a bit what i want but the outline is not correct.
    upload_2019-3-26_17-25-2.png


    It is like in this example like the custom one. It depends on the scale of the object and making lines thiner and bigger on not correct scaled Objects.

    https://github.com/Shrimpey/Outlined-Diffuse-Shader-Fixed

    Maybe there is some solution with the custom Node to interpolate it correctly ? Do someone know where the magic happen ^^?
     
  3. Amitloaf

    Amitloaf

    Joined:
    Jan 30, 2012
    Posts:
    83
    I like your example. It just creates a bigger dark version of the object. How do you layer it properly? (How to make an outline instead of a black, bigger version?)