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

How to anti-alias VertexHelper

Discussion in 'UI Toolkit' started by mattbrandmm, Jun 15, 2022.

  1. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    I am using Vertex Helper components to show pointers from the panels on top of the screen pointing downwards. The position and size of the triangles are all perfect.

    You can see that the edges get pixelated though:

    upload_2022-6-14_14-0-38.png

    Is there a good way to anti-alias the triangles?

    The code I'm using to generate each triangle:
    Code (CSharp):
    1. public class UITriangleRenderer : Graphic
    2. {
    3.     [SerializeField] private Color color;
    4.  
    5.     private List<Vector3> points;
    6.        
    7.     public void SetPoints(List<Vector3> points)
    8.     {
    9.         this.points = points;
    10.         SetVerticesDirty();
    11.     }
    12.        
    13.     protected override void OnPopulateMesh(VertexHelper vh)
    14.     {
    15.         vh.Clear();
    16.            
    17.         vh.AddVert(points[0], color, new Vector2(0f, 0f));
    18.         vh.AddVert(points[1], color, new Vector2(0f, 1f));
    19.         vh.AddVert(points[2], color, new Vector2(1f, 1f));
    20.  
    21.         vh.AddTriangle(0, 1, 2);
    22.     }
    23. }
     
    Midiphony-panda likes this.
  2. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    234
    Very curious if there is an appropriated way to do it

    Otherwise... you can render your PanelSettings into a RenderTexture, which you can apply some anti-aliasing on
     
    mattbrandmm likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    When zooming in you can see that the triangles are indeed anti-aliased. However anti-aliasing can only do so much when the contrast between two colors is extreme as in this case (white vs dark green). Adjusting colors might help or adding a gradient (add a half-tone between dark green and white bands), and avoid sharply pointed triangles that thin out near the end like the one on the left, which generally seems to be at an unfortunate angle too, so changing position might also help. And try a higher anti alias mode (4x or 8x) if possible.
     
    mattbrandmm likes this.
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    Custom geometry does not include the additional vertex data required to perform our smoothing of the edges with transparency. However, you could double your edges to perform an alpha-fade by interpolating the color alpha from 1 to 0. Alternately, 2022.1 will introduce a Vector API that allows to generate aliasing-free geometry.
     
    mattbrandmm and CodeSmile like this.
  5. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    Thanks for the replies everyone!