Texture to texture2D以及texture2D像素反转

    private void SaveRenderTextureToPNG(Texture inputTex, string file)
    {
        RenderTexture temp = RenderTexture.GetTemporary(inputTex.width, inputTex.height, 0, RenderTextureFormat.ARGB32);
        Graphics.Blit(inputTex, temp);
        Texture2D tex2D = GetRTPixels(temp);
        RenderTexture.ReleaseTemporary(temp);
        File.WriteAllBytes(file, tex2D.EncodeToPNG());
    }

    private Texture2D GetRTPixels(RenderTexture rt)
    {
        RenderTexture currentActiveRT = RenderTexture.active;
        RenderTexture.active = rt;
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
        RenderTexture.active = currentActiveRT;
        return tex;
    }

 

            Color[] colors = tex.GetPixels(capx, capy, capwidth, capheight);
            System.Array.Reverse(colors, 0, colors.Length);


            Texture2D t = new Texture2D(capwidth, capheight, TextureFormat.RGB24, true);
            //Debug.Log("Pixel size:" + capx + " " + capy + " " + capwidth + " " + capheight);
            //t.ReadPixels(new Rect(capx, capy, capwidth, capheight), 0, 0, false);

            //for (int i = 0; i < capwidth; i++)
            //{
            //    for(int j=0;j<capheight;j++)
            //    {
            //        Color temp = t.GetPixel(i, j);
            //        t.SetPixel(i,j,)
            //    }
            //}

            t.SetPixels(colors);
            t.Apply();

 

posted @ 2018-01-26 12:53  81192  阅读(2390)  评论(0编辑  收藏  举报