- Home /
ReadPixel returns Black Texture in Editor. Why?
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:
public Texture2D ExtractImage()
{
if (!RenderCamera || !RenderCamera.targetTexture) return null;
int width = RenderCamera.targetTexture.width;
int height = RenderCamera.targetTexture.height;
Texture2D rendered = new Texture2D(width, height, TextureFormat.RGBA32, false, true);
rendered.Apply(true);
RenderTexture.active = RenderCamera.targetTexture;
rendered.ReadPixels(new Rect(0, 0, width, height), 0, 0);
rendered.Apply(true);
Color[] testPixels = rendered.GetPixels();
RenderTexture.active = null;
return rendered;
}
Method A Call (Works):
public class ExportRenderedImageEditor : Editor
{
private VEGEX_ExportRenderedImage myTarget;
void OnEnable()
{
myTarget = (VEGEX_ExportRenderedImage)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Export"))
{
ExportRenderedTexture();
}
}
private void ExportRenderedTexture()
{
string path = EditorUtility.SaveFolderPanel("Export Rendered Texture", "Assets", "New RenderedTexture");
if (string.IsNullOrEmpty(path)) return;
Texture2D rendered = myTarget.ExtractImage();
if (!rendered) return;
byte[] imageBGBytes = rendered.EncodeToPNG();
string imageName = myTarget.imageName + ".png";
File.WriteAllBytes(path + "/" + imageName, imageBGBytes);
AssetDatabase.Refresh();
}
}
Call form B (Does not work):
public static void ExtractBillboardImage(GameObject bill, GameObject render)
{
if (!render) return;
GameObject tempo = Instantiate(bill.transform.parent.gameObject, Vector3.zero, Quaternion.identity);
GameObject first = tempo.transform.GetChild(0).gameObject;
int previousLayer = first.layer;
first.layer = 1;
GameObject renderInstance = Instantiate(render, Vector3.zero, Quaternion.identity, tempo.transform);
VEGEX_ExportRenderedImage renderizator = renderInstance.GetComponent<VEGEX_ExportRenderedImage>();
Bounds treeBounds = first.GetComponent<MeshRenderer>().bounds;
if (treeBounds.extents.y > treeBounds.extents.x || treeBounds.extents.y > treeBounds.extents.z)
renderizator.RenderCamera.orthographicSize = treeBounds.extents.y + 0.05f;
else
{
if (treeBounds.extents.x > treeBounds.extents.z)
renderizator.RenderCamera.orthographicSize = treeBounds.extents.x + 0.05f;
else
renderizator.RenderCamera.orthographicSize = treeBounds.extents.z + 0.05f;
}
renderizator.transform.localPosition = treeBounds.center;
string path = EditorUtility.SaveFolderPanel("Export Rendered Texture", "Assets", "New RenderedTexture");
Texture2D billboardTex = renderizator.ExtractImage();
//billboardTex.Apply();
MeshRenderer billRenderer = bill.GetComponent<MeshRenderer>();
Material materialCopy = billRenderer.sharedMaterial;
if (string.IsNullOrEmpty(path)) return;
path = FileUtil.GetProjectRelativePath(path);
if (!billboardTex) return;
materialCopy.name = bill.transform.parent.name;
string matp = path + "/" + materialCopy.name + ".mat";
AssetDatabase.CreateAsset(materialCopy, matp);
EditorUtility.SetDirty(materialCopy);
byte[] imageBGBytes = billboardTex.EncodeToPNG();
//Color[] testPixel = billboardTex.GetPixels();
string imageName = bill.transform.parent.name + "_Billboard" + ".png";
if (!AssetDatabase.IsValidFolder(path + "/BillboardTextures"))
AssetDatabase.CreateFolder(path, "BillboardTextures");
File.WriteAllBytes(path + "/BillboardTextures/" + imageName, imageBGBytes);
materialCopy.SetTexture("_BillboardMap", billboardTex);
first.layer = previousLayer;
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
DestroyImmediate(tempo);
}
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613020456im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I copy an ARGBHalf RenderTexture to a Texture2D on Windows? 0 Answers
Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers
RenderTexture turn my model to black, why ? 1 Answer
Unwanted dark edge around RenderTexture 0 Answers
How do I get the raw pixels of a RFloat RenderTexture on the CPU? 5 Answers