7
\$\begingroup\$

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:

My disabled Camera's configuration in Unity

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

The game view with the non-transparent 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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Nov 13, 2017 at 6:33
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ Commented Nov 13, 2017 at 20:11
  • \$\begingroup\$ If this helps, i used Background Color, and the color was RGBA (255, 255, 255, 0). That worked for me. \$\endgroup\$
    – Thaun_
    Commented Jul 20, 2018 at 20:23

5 Answers 5

2
\$\begingroup\$

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;
    }
}
\$\endgroup\$
2
\$\begingroup\$

For Unity 2019.4 I needed to set one camera as an overlay camera via... "Camera Component" --> "Render Type" --> Overlay. Secondly, I needed to take the other camera and add a stack element in it's Camera Component (at bottom) then select my Overlay Camera to add to the stack.

\$\endgroup\$
2
  • \$\begingroup\$ Seems like this is only possible when using URP instead of the built-in RP for your project. forum.unity.com/threads/… \$\endgroup\$
    – A__
    Commented May 19, 2022 at 1:00
  • \$\begingroup\$ Thanks for the context! \$\endgroup\$
    – mrVentures
    Commented Jun 5, 2022 at 13:29
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

https://forum.unity.com/threads/using-render-textures-on-a-ugui-image-panel-or-button.272332/

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)

    SubShader {
        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
            BlendOp Add
            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
            }
        }
    }
}

}

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This answer would be better if it described what to do with this shader to solve OP's problem. \$\endgroup\$
    – DMGregory
    Commented Oct 5, 2018 at 11:39
  • \$\begingroup\$ True, added info : use with RawImage \$\endgroup\$
    – karmington
    Commented Oct 7, 2018 at 6:55
0
\$\begingroup\$

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 ¯_(ツ)_/¯

\$\endgroup\$
1
  • \$\begingroup\$ Didn't work for me. \$\endgroup\$
    – Jimmy
    Commented Mar 12, 2021 at 7:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .