Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ReadPixel returns Black Texture in Editor

Discussion in 'Scripting' started by pablolopez280797, Nov 24, 2020.

  1. pablolopez280797

    pablolopez280797

    Joined:
    Nov 28, 2019
    Posts:
    4
    Hello, I am trying to make a script that saves a capture of a render texture that is in a camera. To do this is momentarily, the gameObject to which I want to make a screenshot. and I call this function:
    Code (CSharp):
    1. public Texture2D ExtractImage()
    2.     {
    3.         if (!RenderCamera || !RenderCamera.targetTexture) return null;
    4.         int width = RenderCamera.targetTexture.width;
    5.         int height = RenderCamera.targetTexture.height;
    6.         Texture2D rendered = new Texture2D(width, height, TextureFormat.RGBA32, false, true);
    7.         rendered.Apply(true);
    8.         RenderTexture.active = RenderCamera.targetTexture;
    9.         rendered.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    10.         rendered.Apply(true);
    11.         Color[] testPixels = rendered.GetPixels();
    12.  
    13.         RenderTexture.active = null;
    14.  
    15.         return rendered;
    16.     }
    Form A Call (Works):
    Code (CSharp):
    1. public class ExportRenderedImageEditor : Editor
    2. {
    3.     private VEGEX_ExportRenderedImage myTarget;
    4.  
    5.     void OnEnable()
    6.     {
    7.         myTarget = (VEGEX_ExportRenderedImage)target;
    8.     }
    9.  
    10.     public override void OnInspectorGUI()
    11.     {
    12.         base.OnInspectorGUI();
    13.  
    14.         if (GUILayout.Button("Export"))
    15.         {
    16.             ExportRenderedTexture();
    17.         }
    18.     }
    19.  
    20.     private void ExportRenderedTexture()
    21.     {
    22.         string path = EditorUtility.SaveFolderPanel("Export Rendered Texture", "Assets", "New RenderedTexture");
    23.         if (string.IsNullOrEmpty(path)) return;
    24.         Texture2D rendered = myTarget.ExtractImage();
    25.         if (!rendered) return;
    26.         byte[] imageBGBytes = rendered.EncodeToPNG();
    27.         string imageName = myTarget.imageName + ".png";
    28.         File.WriteAllBytes(path + "/" + imageName, imageBGBytes);
    29.         AssetDatabase.Refresh();
    30.     }
    31. }
    Call form B (Does not work):
    Code (CSharp):
    1. public static void ExtractBillboardImage(GameObject bill, GameObject render)
    2.         {
    3.             if (!render) return;
    4.             GameObject tempo = Instantiate(bill.transform.parent.gameObject, Vector3.zero, Quaternion.identity);
    5.             GameObject first = tempo.transform.GetChild(0).gameObject;
    6.             int previousLayer = first.layer;
    7.             first.layer = 1;
    8.             GameObject renderInstance = Instantiate(render, Vector3.zero, Quaternion.identity, tempo.transform);
    9.             VEGEX_ExportRenderedImage renderizator = renderInstance.GetComponent<VEGEX_ExportRenderedImage>();
    10.             Bounds treeBounds = first.GetComponent<MeshRenderer>().bounds;
    11.             if (treeBounds.extents.y > treeBounds.extents.x || treeBounds.extents.y > treeBounds.extents.z)
    12.                 renderizator.RenderCamera.orthographicSize = treeBounds.extents.y + 0.05f;
    13.             else
    14.             {
    15.                 if (treeBounds.extents.x > treeBounds.extents.z)
    16.                     renderizator.RenderCamera.orthographicSize = treeBounds.extents.x + 0.05f;
    17.                 else
    18.                     renderizator.RenderCamera.orthographicSize = treeBounds.extents.z + 0.05f;
    19.             }
    20.             renderizator.transform.localPosition = treeBounds.center;
    21.             string path = EditorUtility.SaveFolderPanel("Export Rendered Texture", "Assets", "New RenderedTexture");
    22.             Texture2D billboardTex = renderizator.ExtractImage();
    23.             //billboardTex.Apply();
    24.  
    25.             MeshRenderer billRenderer = bill.GetComponent<MeshRenderer>();
    26.             Material materialCopy = billRenderer.sharedMaterial;
    27.  
    28.  
    29.             if (string.IsNullOrEmpty(path)) return;
    30.             path = FileUtil.GetProjectRelativePath(path);
    31.             if (!billboardTex) return;
    32.             materialCopy.name = bill.transform.parent.name;
    33.             string matp = path + "/" + materialCopy.name + ".mat";
    34.             AssetDatabase.CreateAsset(materialCopy, matp);
    35.             EditorUtility.SetDirty(materialCopy);
    36.             byte[] imageBGBytes = billboardTex.EncodeToPNG();
    37.             //Color[] testPixel = billboardTex.GetPixels();
    38.             string imageName = bill.transform.parent.name + "_Billboard" + ".png";
    39.             if (!AssetDatabase.IsValidFolder(path + "/BillboardTextures"))
    40.                 AssetDatabase.CreateFolder(path, "BillboardTextures");
    41.             File.WriteAllBytes(path + "/BillboardTextures/" + imageName, imageBGBytes);
    42.             materialCopy.SetTexture("_BillboardMap", billboardTex);
    43.             first.layer = previousLayer;
    44.             AssetDatabase.SaveAssets();
    45.             AssetDatabase.Refresh();
    46.             DestroyImmediate(tempo);
    47.         }
    To debug the result of ReadPixels, I save the pixels in an array of Colors, and each and every one of the colors is Color (0,0,0,0), that is, all the pixels of the generated image are black. However, I call this same function through a button of a manobehabiour in the Inspector, and this one does generate the image correctly. I've been reading about this on other forums, and they point out that it may be a Unity bug, as it worked on versions like 2019.2.21. Is this the problem, or something that I have not realized?