Drawing Anti-Aliased cube to RenderTexture gives edge artifacts

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:

Shader "Unlit Color Only" {
    Properties {
        _Color ("Color", Color) = (0,0,0,0)
    }
   
    SubShader {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
        Blend One OneMinusSrcAlpha
        Color [_Color]
        Pass {}
    }
}

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?

It looks like RT is low res. What’s the resolution you’re using for rendering on render texture?

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 Likes

Thank you so much! I can’t believe it’s that simple and I spent more than a day on it without results!