Search Unity

Drawing Anti-Aliased cube to RenderTexture gives edge artifacts

Discussion in 'General Graphics' started by DeltaPiSystems, Jul 31, 2019.

  1. DeltaPiSystems

    DeltaPiSystems

    Joined:
    Jul 26, 2019
    Posts:
    30
    I want to draw a cube to a RenderTexture and then display that texture somewhere else (UI primarily)
    This cube needs to be able to support anti-aliasing.

    The issue I'm running into is that the edges of the cube get anti-aliased with the "Clear Flags" color of the camera, leaving a nasty edge artifact when displaying the image on a different color background.

    Exact setup:
    • Camera
      • Clear Flags: Solid Color
      • Background: RGBA 255 255 255 0
      • Target Texture: RenderTexture
    • RenderTexture
      • Anti-aliasing: 8 samples
    • Cube - drawn by camera above
      • Shader: Custom shader (see below)
      • 45° rotation on all axes
      • Color: RGBA 98 112 140 255
    • Plane - outside camera, only visible in editor
      • Material created by dragging the RenderTexture onto the plane
        • Shader: Unlit/Transparant
    Custom shader:
    Code (csharp):
    1.  
    2. Shader "Unlit Color Only" {
    3.     Properties {
    4.         _Color ("Color", Color) = (0,0,0,0)
    5.     }
    6.    
    7.     SubShader {
    8.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    9.         Blend One OneMinusSrcAlpha
    10.         Color [_Color]
    11.         Pass {}
    12.     }
    13. }
    14.  
    The edge artifact result:

    The color of the edge is derived from the clear color of the camera.

    How can I get rid of these edge artifacts while maintaining anti-aliasing transparency?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It looks like RT is low res. What's the resolution you're using for rendering on render texture?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    That should be RGBA 0 0 0 0. Solid black and fully transparent. That alone will get you a dark outline instead of a bright outline. After that you need to be using a premultiplied alpha blend for the object displaying the render texture. You can use the Particles/Alpha Blend Premultiply shader to test with, though that shader does more than you probably need or want.
     
  4. DeltaPiSystems

    DeltaPiSystems

    Joined:
    Jul 26, 2019
    Posts:
    30
    Thank you so much! I can't believe it's that simple and I spent more than a day on it without results!