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

Bug generateVisualContent + background-color + rgba/opacity seems to render twice

Discussion in 'UI Toolkit' started by tomsseisums, Jul 31, 2023.

  1. tomsseisums

    tomsseisums

    Joined:
    Oct 8, 2012
    Posts:
    37
    Attempting to implement an element with skewed shape using generateVisualContent, everything worked fine, until I tried to use alpha for color.

    My code:
    Code (CSharp):
    1. private void OnGenerateVisualContent(MeshGenerationContext mgc)
    2. {
    3.     var painter = mgc.painter2D;
    4.     var slant = _slanted ? 5f : 0f;
    5.    
    6.     painter.fillColor = resolvedStyle.backgroundColor;
    7.  
    8.     var topLeft     = new Vector2(paddingRect.x, paddingRect.y);
    9.     var topRight    = new Vector2(paddingRect.x + paddingRect.width + slant, paddingRect.y);
    10.     var bottomRight = new Vector2(paddingRect.x + paddingRect.width, paddingRect.y + paddingRect.height);
    11.     var bottomLeft = new Vector2(paddingRect.x - slant, paddingRect.y + paddingRect.height);
    12.    
    13.     painter.BeginPath();
    14.     painter.MoveTo(topLeft);
    15.     painter.LineTo(topRight);
    16.     painter.LineTo(bottomRight);
    17.     painter.LineTo(bottomLeft);
    18.     painter.ClosePath();
    19.     painter.Fill();
    20. }
    Generates this kind of button, when using no transparency.
    upload_2023-8-1_1-5-23.png

    But, if I am to use transparency on the element, like opacity or rgba for background color, this is what's getting rendered:
    upload_2023-8-1_1-7-19.png

    As can be seen, the square background seems to render alongside generateVisualContent resulting in an overlay effect and color mismatch.

    Is this expected?
     
  2. tomsseisums

    tomsseisums

    Joined:
    Oct 8, 2012
    Posts:
    37
    Changing my code to only render "adornments":
    Code (CSharp):
    1. private void OnGenerateVisualContent(MeshGenerationContext mgc)
    2. {
    3.     var painter = mgc.painter2D;
    4.     var slant = _slanted ? 5f : 0f;
    5.    
    6.     painter.fillColor = resolvedStyle.backgroundColor;
    7.    
    8.     var topLeft     = new Vector2(paddingRect.x, paddingRect.y);
    9.     var topRight    = new Vector2(paddingRect.x + paddingRect.width, paddingRect.y);
    10.     var topRightSkew    = new Vector2(paddingRect.x + paddingRect.width + slant, paddingRect.y);
    11.     var bottomRight = new Vector2(paddingRect.x + paddingRect.width, paddingRect.y + paddingRect.height);
    12.     var bottomLeft  = new Vector2(paddingRect.x, paddingRect.y + paddingRect.height);
    13.     var bottomLeftSkew = new Vector2(paddingRect.x - slant, paddingRect.y + paddingRect.height);
    14.    
    15.     // Left slant.
    16.     painter.BeginPath();
    17.     painter.MoveTo(topLeft);
    18.     painter.LineTo(bottomLeft);
    19.     painter.LineTo(bottomLeftSkew);
    20.     painter.ClosePath();
    21.     painter.Fill();
    22.    
    23.     // Right slant.
    24.     painter.BeginPath();
    25.     painter.MoveTo(topRight);
    26.     painter.LineTo(topRightSkew);
    27.     painter.LineTo(bottomRight);
    28.     painter.ClosePath();
    29.     painter.Fill();
    30. }
    Produces the expected outcome:
    upload_2023-8-1_1-16-42.png
     
  3. tomsseisums

    tomsseisums

    Joined:
    Oct 8, 2012
    Posts:
    37
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    This is correct. The default geometry cannot be removed at the moment. You could use custom properties for your custom element. This way, the default properties could remain fully transparent.