5
\$\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\$
  • \$\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\$ – John Hamilton Nov 13 '17 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\$ – Nathan Friend Nov 13 '17 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_ Jul 20 '18 at 20:23
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\$
1
\$\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\$
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\$
  • \$\begingroup\$ This answer would be better if it described what to do with this shader to solve OP's problem. \$\endgroup\$ – DMGregory Oct 5 '18 at 11:39
  • \$\begingroup\$ True, added info : use with RawImage \$\endgroup\$ – karmington Oct 7 '18 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.