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

Resolved Jagged edges when modifying Alpha in Fragment Shader

Discussion in 'Shaders' started by slushieboy99, Apr 7, 2022.

  1. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Hello,
    I'm trying to write a shader which highlights edges based on their sharpness. It's working for a good portion of my model, but I'm getting a lot of very sharp edges. The way my code works is I mapped values from 0 to 1 per vertex to a uv channel. This 0 to 1 value determines the alpha of the vertex in the fragment shader.

    Code (CSharp):
    1. Shader "Unlit/EdgeHighlight"
    2. {
    3.     SubShader
    4.     {
    5.         Blend SrcAlpha OneMinusSrcAlpha
    6.         Pass
    7.         {
    8.             CGPROGRAM
    9.             #pragma vertex vert
    10.             #pragma fragment frag
    11.             // include file that contains UnityObjectToWorldNormal helper function
    12.             #include "UnityCG.cginc"
    13.      
    14.             float intensity = 1.0f;
    15.  
    16.             struct v2f {
    17.                 // we'll output world space normal as one of regular ("texcoord") interpolators
    18.                 float2 highlight : TEXCOORD4;
    19.                 half3 worldNormal : TEXCOORD0;
    20.                 float4 pos : SV_POSITION;
    21.             };
    22.  
    23.             // vertex shader: takes object space normal as input too
    24.             v2f vert (float4 vertex : POSITION, float3 normal : NORMAL, float2 hl : TEXCOORD4)
    25.             {
    26.                 v2f o;
    27.                 o.pos = UnityObjectToClipPos(vertex);
    28.                 // UnityCG.cginc file contains function to transform
    29.                 // normal from object to world space, use that
    30.                 o.worldNormal = UnityObjectToWorldNormal(normal);
    31.                 o.highlight = hl;
    32.                 return o;
    33.             }
    34.      
    35.             fixed4 frag (v2f i) : SV_Target
    36.             {
    37.                 fixed4 c = 0;
    38.                 // normal is a 3D vector with xyz components; in -1..1
    39.                 // range. To display it as color, bring the range into 0..1
    40.                 // and put into red, green, blue components
    41.                 c.rgb = i.worldNormal*0.5+0.5;
    42.          
    43.                 //Had a problem with blotting, must be an error somewhere I won't fix lol
    44.                 if (i.highlight.x < 1.0f && i.highlight.x > 0.0f){
    45.                     c.a = i.highlight.x * 5;
    46.                 }
    47.                 else{
    48.                     c.a = 0;
    49.                 }
    50.                 return c;
    51.             }
    52.             ENDCG
    53.         }
    54.     }
    55. }
    This, however results in the jagged edges seen. For reference, the difference between alpha values on two of these points where jagged edges occur are 0.084 - 0.037 = 0.047. Many of the jagged edges occur on similar ranges. Does anyone know how to fix this? Thanks!

    Capture.PNG
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    It may be a fault with how you're encoding your values into that UV channel. Keep in mind that not only do hard-edged normals on a mesh create a vertex split, but so do splits in the UV map. So there may be an issue with how you're calculating what is a "hard edge" when writing your values to the UV channel. (though that looks like a pretty bad UV cut if it is one.. but i've seen some wild stuff...)

    Also "
    //Had a problem with blotting, must be an error somewhere I won't fix lol
    ", given you know there is a fault in how the highlight values are calculated already, that would probably be a good place to start.
     
    slushieboy99 likes this.
  3. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Hmmm, I know there isn't an edge split here, and most of the areas with an edge split look quite nice. Do you know what would parameterize a split in the UV map? I find that most of these hard edges are coming from differences of more than 1/2 in alpha value. My script basically runs through each vertex, then runs through the triangles and finds connected vertices and calculates the angle between them. I am getting a lot of errors of vertex indices not being found in the triangle list which is peculiar to me.
     
  4. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Actually upon further inspection, these hard seams seem to go all the way up the model, leading to me believing it's somehow a uv cut. These aren't the seams I made in blender though so I'm not sure how they would have gotten there, but I'm going to take a closer look at the model and its seams.
     
  5. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    This actually did end up being an error with the way uvs were imported. Thanks for the help @Invertex
     
    Invertex likes this.