Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by squarefanatic · Sep 28, 2015 at 05:58 AM · cameratexture2drendertexturereadpixels

Rendering Camera to PNG produces completely grey image.

The following Behaviour is attached to a new GameObject programatically, and when it's done it doesn't output a rendered image from the Camera, rather a completely grey image. What have a I done wrong or overlooked?

 using UnityEngine;
 
 namespace Cartographer
 {
 
     class MapCameraBehaviour : MonoBehaviour
     {
 
         private int size = 1000;
         private Camera camera;
         private RenderTexture renderTexture;
         private Texture2D texture;
         private bool render = false;
         private int renderFrameDelay = 0;
         private bool renderNow = false;
 
         void Log(string message)
         {
             //some logging code
         }
 
         void Awake()
         {
 
             Log("Awake()");
 
             camera = GetComponent<Camera>();
             if (camera == null)
             {
                 camera = gameObject.AddComponent<Camera>();
                 camera.nearClipPlane = 0.1f;
                 camera.farClipPlane = 2000f;
                 camera.renderingPath = RenderingPath.Forward;
                 camera.depth = 100;
                 camera.transform.position = new Vector3(0.0f, 100.0f, 0.0f);
             }
             else
             {
                 Log("Existing camera.");
             }
             if (camera != null)
             {
                 Log("Camera found.");
                 renderTexture = new RenderTexture(size, size, 0);
                 texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
                 camera.targetTexture = renderTexture;
                 RenderTexture.active = renderTexture;
                 render = true;
                 renderFrameDelay = 100;
                 renderNow = false;
             }
             else
             {
                 Log("Camera not found.");
             }
 
         }
 
         void Update()
         {
             if (render)
             {
                 if (renderFrameDelay <= 0)
                 {
                     render = false;
                     renderNow = true;
                     QualitySettings.antiAliasing = 0;
                     camera.Render();
                 }
                 else
                 {
                     renderFrameDelay--;
                 }
             }
         }
 
         void OnPostRender()
         {
             if (renderNow == true)
             {
 
                 renderNow = false;
 
                 Log("OnPostRender()");
 
                 texture.ReadPixels(new Rect(0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
 
                 byte[] bytes = texture.EncodeToPNG();
                 System.IO.File.WriteAllBytes(Application.dataPath + "/texture.png", bytes);
 
                 camera.targetTexture = null;
                 RenderTexture.active = null;
 
                 DestroyImmediate(texture);
                 DestroyImmediate(renderTexture);
 
                 Log("Done.");
 
             }
         }
 
     }
 
 }
Comment
Add comment · Show 1
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
avatar image gshabanov · Jun 29, 2018 at 10:42 PM 0
Share

Were you able to solve your problem I am running into the same issue

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by saschandroid · Sep 29, 2015 at 07:10 AM

Have you tried to apply the texture after reading the pixels?

 texture.ReadPixels(new Rect(0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
 texture.Apply();

Also make sure the texture is Read/Write enabled.

Comment
Add comment · Show 2 · Share
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
avatar image squarefanatic · Oct 09, 2015 at 03:57 AM 0
Share

I have yeah, it made no difference.

Sorry for the late reply. The Unity emails have been going to SPA$$anonymous$$. >_<

avatar image reismoreira_carlosandre squarefanatic · Apr 21 at 03:10 PM 0
Share

I have been facing the same problem... Did you find a solution?

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

34 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

Related Questions

Trying to Take a Snapshot of Only a Portion of the Screen In-Game 0 Answers

RenderTexture to Texture2D very slowly 0 Answers

Get Colors from RenderTexture faster! 1 Answer

Wrong Coloring (Sometimes) when capturing "Screen"-shot 1 Answer

How to sync RenderTextures so that they all render once per frame 0 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