Search Unity

Toon/Basic Outline with alpha mask texture

Discussion in 'Shaders' started by Sobrtonog, Oct 15, 2019.

  1. Sobrtonog

    Sobrtonog

    Joined:
    Sep 28, 2019
    Posts:
    15
    Hi all, a few of my characters have hair textures that are just flat poly's with alpha masks on them.
    With unity's toon shader, there are black shapes outlining certain parts of the mesh for the mentioned. Is there any way to modify these so I can add a texture image and have parts of the outline masked out?

    I've tried tinkering with it a bit, but I have no expertise on shaders at all and it wound up being even more broken.

    Thanks very much in advance!
    Code (CSharp):
    1. Shader "Toon/Basic Outline"
    2. {
    3.     Properties
    4.     {
    5.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    6.         _Outline ("Outline width", Range (.002, 0.03)) = .005
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.        
    12.         Cull Front
    13.         ZWrite On
    14.         ColorMask RGB
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.        
    17.         Pass
    18.         {
    19.             Name "OUTLINE"
    20.            
    21.             HLSLPROGRAM
    22.             #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
    23.            
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma multi_compile_fog
    27.            
    28.             CBUFFER_START(UnityPerMaterial)
    29.             float _Outline;
    30.             float4 _OutlineColor;
    31.             CBUFFER_END
    32.            
    33.             struct Attributes
    34.             {
    35.                 float4 positionOS : POSITION;
    36.                 float3 normalOS : NORMAL;
    37.             };
    38.        
    39.             struct Varyings
    40.             {
    41.                 float4 positionCS : SV_POSITION;
    42.                 half fogCoord : TEXCOORD0;
    43.                 half4 color : COLOR;
    44.             };
    45.            
    46.             Varyings vert(Attributes input)
    47.             {
    48.                 Varyings output = (Varyings)0;
    49.                
    50.                 input.positionOS.xyz += input.normalOS.xyz * _Outline;
    51.                
    52.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    53.                 output.positionCS = vertexInput.positionCS;
    54.                
    55.                 output.color = _OutlineColor;
    56.                 output.fogCoord = ComputeFogFactor(output.positionCS.z);
    57.                 return output;
    58.             }
    59.            
    60.             half4 frag(Varyings i) : SV_Target
    61.             {
    62.                 i.color.rgb = MixFog(i.color.rgb, i.fogCoord);
    63.                 return i.color;
    64.             }
    65.             ENDHLSL
    66.         }
    67.     }
    68. }
    69.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Not really, no. That style of outline shader relies heavily on the surface geometry and normals. While you can have the outline clip too, it may not show as an outline in the clipped areas. You may be better off not using outlines on those parts of the character, and maybe just paint the outlines into the texture.