# How can I make my Camera's RenderTexture output have a transparent background?

I'm writing a 3D game using Unity 2017.1.0b1. I have a disabled Camera that takes a "snapshot" of the current scene and stores it in a RenderTexture when a button is clicked. I then display the RenderTexture in a RawImage within my scene.

This works, except that the output from the Camera into the RenderTexture doesn't take into account the alpha value of the Camera's clear color.

Here's a snapshot of how my disabled Camera is configured. Notice the Background property has an alpha value of 0:

Here's a snapshot of what the RenderTexture (displayed using a RawImage) looks like in the game. The blue square is the RenderTexture:

How can I make the Camera render into the RenderTexture with a transparent background? I didn't expect the blue background to show up in the RenderTexture since its alpha value is 0.

• What do you mean when you say "transparent background". A background wouldn't be a background if it was transparent. Do you just mean it should be black? – John Hamilton Nov 13 '17 at 6:33
• By "transparent background", I mean that any pixels that are rendered with the Camera's clear color would be rendered into the RenderTexture with an alpha value of 0. So in this case, I would expect the main Camera's clear color (the pale yellow) to show through the RawImage (which displays the RenderTexture). – Nathan Friend Nov 13 '17 at 20:11
• If this helps, i used Background Color, and the color was RGBA (255, 255, 255, 0). That worked for me. – Thaun_ Jul 20 '18 at 20:23

I had the same problem and realized that my RenderTexture had a depth of 24, so no alpha. Here's a piece a code that to allows me to take a picture of a 3d model to use it as a sprite, with no background:

public Camera Camera3D;
public int ImageSize;
public Sprite Result;

private RenderTexture mRenderTexture;

public void SetTarget(Transform target)
{
mRenderTexture = new RenderTexture(ImageSize, ImageSize, 32);
Camera3D.targetTexture = mRenderTexture;
transform.SetParent(target, false);
Camera3D.Render();
transform.SetParent(null, false);
}

private void OnPostRender()
{
if(mRenderTexture != null)
{
RenderTexture.active = mRenderTexture;
var virtualPhoto = new Texture2D(ImageSize, ImageSize, TextureFormat.RGBA32, false);
virtualPhoto.ReadPixels(new Rect(0, 0, ImageSize, ImageSize), 0, 0);
virtualPhoto.Apply();

RenderTexture.active = null;
Camera3D.targetTexture = null;

Result = Sprite.Create(virtualPhoto, new Rect(Vector2.zero, new Vector2(ImageSize, ImageSize)), Vector2.zero);

mRenderTexture = null;
}
}


If you want to layer cameras on top of each other and use the lower layered camera as the "background" to the other cameras, you need to set the Clear Flags of the higher layer cameras to Depth Only.

In your case, your background camera should be at a lower depth (ex. -1) than the camera in the provided screenshot (which is 0). Your camera in the screenshot should have its clear flags set to depth only and your background camera should have solid colour as its clear flags variable.

Relevant Unity manuals here and here.

User called 'byrne' posted this OverlayBlend shader. Use it for the RawImage material.

Shader "Custom/OverlayBlend" {
Properties {
_MainTex ("Texture1 (RGB)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
}

Category {
Tags {"RenderType"="Opaque" "Queue"="Transparent"}
Lighting Off
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "Texcoord", Texcoord
}

//screen 1 - (1 - a) * (1 - b)

Pass {
ZWrite Off
Cull Off
Blend One One
BlendOp Sub
SetTexture [_MainTex] {
constantColor (1,1,1,1)
Combine constant
}
}

Pass {
ZWrite Off
Cull Off
Blend Zero OneMinusSrcColor
SetTexture [_MainTex] {
constantColor [_Color]
Combine texture * constant
}
}
Pass {
ZWrite Off
Cull Off
Blend One One
BlendOp Sub
SetTexture [_MainTex] {
constantColor [_Color]
}
SetTexture [_MainTex] {
constantColor (1,1,1,1)
Combine constant, previous
}
}
}
}


}

• This answer would be better if it described what to do with this shader to solve OP's problem. – DMGregory Oct 5 '18 at 11:39
• True, added info : use with RawImage – karmington Oct 7 '18 at 6:55

Obviously this is only 2 years late, but you can just change the settings to allow transparency, after rigging the camera properly like everyone has already said, by changing the colour settings from whatever it is now, to R32G32B32A32, because I think it needs to be 32 bit to allow transparency potentially. I was capturing stuff for a render texture, but I had to change the shape of what I was capturing, but stuff from my game was getting in the spaces that the shape didn't take up, so I just did this and it worked.

Again, sorry this is late but if someone comes on this thread, they can see it I guess ¯_(ツ)_/¯

• Didn't work for me. – Iamsodarncool Mar 12 at 7:12