RenderTexture in unity shows how to have a transparent channel in the picture

Two cameras are prepared here, one is used to display the ui interface, and the other is used to display the camera that needs to use rt, as follows:
insert image description here
We change the Canvas mode to camera and give it to UICamera, and set the culling mask to ui:
insert image description here
Next, in modelCamera, set cullingMask points to the layer defined by myself, and I use the Model layer here. Set Clear Flags to Solid Color solid color mode, and set the value of a to 0.
  Explain why the depth only mode is not used here:
  in this mode, only the depth information of the previous frame will be cleared. It will cover the opaque object in the current frame to the previous frame, which will result in that if the object you need for rt has motion, there will be multiple, a series of motion trajectories presented on rt.

Then hang the created rt on the camera:
insert image description here
in order to realize the transparent channel, after trying to set the color type in rt to float type with transparent channel:
insert image description here
(as for why using int or other types with transparent channel cannot achieve background transparency , not particularly clear)
Create a rawimage in ui to connect to rt:
insert image description hereIf you need to convert the rt with a transparent channel to texture2D in the code, you need to use ARGB32 or RGBAFloat:

		m_screenTure = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
        RenderTexture.active = rt;
        m_screenTure.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);

Guess you like

Origin blog.csdn.net/weixin_47819574/article/details/127886783