Convert RenderTexture to Texture2D

后端 未结 1 762
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 03:58

I need to save a RenderTexture object to a .png file that will then be used as a texture to wrap about a 3D object. My problem is right now I can\'t save a RenderTexture obj

相关标签:
1条回答
  • 2020-12-11 04:38

    Create new Texture2D, use RenderTexture.ReadPixels to read the pixels from RenderTexture into the new Texture2D. Finally, Call Texture2D.Apply(); to apply the changed pixels.

    Texture2D toTexture2D(RenderTexture rTex)
    {
        Texture2D tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
        RenderTexture.active = rTex;
        tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
        tex.Apply();
        return tex;
    }
    

    Usage:

    public RenderTexture tex;
    Texture2D myTexture = toTexture2D(tex);
    

    You can make it an extension method:

    public static class ExtensionMethod
    {
        public static Texture2D toTexture2D(this RenderTexture rTex)
        {
            Texture2D tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
            RenderTexture.active = rTex;
            tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
            tex.Apply();
            return tex;
        }
    }
    

    Usage:

    public RenderTexture tex;
    Texture2D myTexture = tex.toTexture2D();
    
    0 讨论(0)
提交回复
热议问题