Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by edcx · Nov 15, 2016 at 08:59 AM · texturematerialtexture2d

Reading pixel data from material.mainTexture returns grey color

Hello everyone,

I am trying to get texture2d from a material, which I play a movie on it by using a plug-in. I create a texture2D and assigned materials.mainTexture component to it. I can draw that texture screen using GUI.DrawTexture but when I try to Encode it to PNG file or when I read colors using GetPixels I got (0.804,0.804,0.804,0.804).

Do you have an idea about this issue? I found out some problems related to textures appearing grey when texture data is lost but I couldn't solve my problem.

I don't understand that why I can draw texture correctly on screen but when I try to retrieve its content it is grey.

Is it because I convert Texture to Texture2D? I do this because I can't read pixels from Texture.

     void Update () {
         if (_mat == null) return;
         tex = _mat.mainTexture as Texture2D;
 
         if (tex == null) return;
         for (int i = 0; i < 50; i++)
         {
             Debug.Log(tex.GetPixel(100, 100 + i)); // Prints (RGBA(0.804, 0.804, 0.804, 0.804))
         }
         
         count++;
 
     }
 
     void OnGUI() {
         GUI.DrawTexture(new Rect(0, 0, 200, 200), tex); // Draws texture correctly so its content is not grey
        
     }



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

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by BBIT-SOLUTIONS · Jan 01, 2017 at 08:20 AM

Hey,

I had exactly the same problem.

It seems not to be possible to directly convert from Texture to Texture2D. You can solve the problem by first converting the Texture into RenderTexture using Graphics.Blit() and then save it as Texture2D.

Now you can call GetPixel() or encode it as PNG or JPG and it works.

             Texture mainTexture = renderer.material.mainTexture;
             Texture2D texture2D = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);
 
             RenderTexture currentRT = RenderTexture.active;
 
             RenderTexture renderTexture = new RenderTexture(mainTexture.width, mainTexture.height, 32);
             Graphics.Blit(mainTexture, renderTexture);
 
             RenderTexture.active = renderTexture;
             texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
             texture2D.Apply();
 
             Color[] pixels = texture2D.GetPixels();
 
             RenderTexture.active = currentRT;
Comment
Add comment · Show 3 · 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 edcx · Mar 09, 2017 at 07:11 AM 0
Share

Well, the operation looks a bit slow but this indeed gives me the texture. Thanks for your answer.

avatar image enigmatic · Jun 24, 2019 at 05:27 AM 0
Share

Found this after struggling for hours with this simple task.

This works but any idea on improvising this so that it can be done every frame? $$anonymous$$ulti-threading does not make sense as I am getting new frame every update.

[Upvoted]

avatar image Icaro-DLima · Dec 27, 2019 at 11:29 PM 1
Share

Remember to use renderTexture.Release(), I was having a giant memory leak problem because of that.

avatar image
0

Answer by luisjesusmv · Apr 28, 2017 at 06:16 PM

I using a Texture to Texture2D casting

example

Texture2D t = (Texture2D)GetComponent().material.mainTexture;

Debug.Log(t.GetPixel(100, 100));

Comment
Add comment · Show 3 · 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 Bunny83 · Apr 28, 2017 at 08:37 PM 0
Share

You can only cast it to a Texture2D when the texture that is used by the material is a Texture2D. A cast can't "convert" a texture.

avatar image luisjesusmv · Apr 29, 2017 at 06:37 PM 0
Share

is use this method playing a realtime video provided by the vuforia plugin rendered on a plane 3d object and works fine

the object hierarchy is ARCamera-Camera-BackgroundPlane

attaching the script above described on the background plane

avatar image EdwinChua · May 12, 2017 at 07:09 AM 0
Share

I used this method, also with Vuforia. Works fine on Desktop, but breaks when deployed in Android.

Applying @BBIT-SOLUTIONS solution worked well.

avatar image
0

Answer by romi-fauzi · Dec 26, 2018 at 01:55 AM

I've found a shorter way, so hopefully this is useful for someone who stumble the same issue, apparently its because Graphics.CopyTexture uses GPU, and GetPixels uses CPU, and this is where the synchronization issues are happening. a way to work around that is to set the RenderTexture active, like code below:

 RenderTexture.active = renderTexture;
 Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
 tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
 tex.Apply();

Source link: https://forum.unity.com/threads/graphics-copytexture-then-getpixels.482601/

Comment
Add comment · 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

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

11 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

Related Questions

Set image as material 0 Answers

from texture2D to cubemap 1 Answer

Material not updating on Image 2 Answers

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers

Offset detail texture in c# 1 Answer


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