Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pablolopez280797 · Nov 24, 2020 at 03:24 PM · rendertextureblackreadpixels

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges