Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by mitohnesprudel · Dec 04, 2021 at 12:37 PM · texturetexture2drendertexturegetpixel

Texture2D GetPixel returns the same value everywhere

I am trying to calculate the exposed area of a falling Starship. For this, I am trying to use an orthographic camera that looks at and renders to a render texture. I want to count the pixels with the ship on it to calculate the area. I converted the render texture to a Texture2D and I am rendering that to on a RawImage, but when I want to use GetPixel() on any pixel it returns 205 or 0.8039216.

     public Transform Ship;
     public RawImage ri;
     public RenderTexture area;
     private Texture2D area2D;
     void Start()
     {
         transform.position = new Vector3(Ship.position.x, 0f, Ship.position.z);
         transform.LookAt(Ship);
     }
 
     void Update()
     {
         transform.position = new Vector3(Ship.position.x, 0f, Ship.position.z);
         area2D = toTexture2D(area);
         ri.texture = area2D;
         Debug.Log(area2D.GetPixel(128,128).grayscale);
     }
 
     public Texture2D toTexture2D(RenderTexture rTex)
     {
         Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
         Graphics.CopyTexture(rTex, dest);
         return dest;
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Dec 04, 2021 at 02:19 PM

You can not use CopyTexture to read texture data from the GPU. See the last sentence on the CopyTexture page.:

CopyTexture operates on GPU-side data exclusively

As you may know a Texture2D may exist both in normal CPU memory (this is the case when the texture was loaded from disk) and also in GPU memory. Calling Apply will transfer / upload the image data from the CPU / system memory to the GPU. Textures also can only exist on the GPU side. This is the case when the texture marked as "not readable". This would free up the memory on the CPU side.


When you use CopyTexture you only copy the data on the GPU from one texture to the other. However you can not simply "download" the data from the GPU. For this you have to use Texture2D.ReadPixels.

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
avatar image
0

Answer by mitohnesprudel · Dec 04, 2021 at 02:21 PM

This finally works

     public Transform Ship;
     public int widht, height;
     public RenderTexture rt;
     public int area;
     void Start()
     {
         transform.position = new Vector3(Ship.position.x, 0f, Ship.position.z);
         transform.LookAt(Ship);
     }
 
     void Update()
     {
         area = 0;
         transform.position = new Vector3(Ship.position.x, 0f, Ship.position.z);
         RenderTexture.active = rt;
         Texture2D tex = new Texture2D(widht, height);
         tex.ReadPixels(new Rect(0,0, tex.width, tex.height),0, 0);
         tex.Apply();
         for(var x = widht; x > 0; x--)
         {
             for(var y = height; y > 0; y--)
             {
                 if(tex.GetPixel(x,y).grayscale > 0)
                 {
                     area++;
                 }
             }
         }
     }
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

169 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 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

Copy non-rectangle part of one texture to another 2 Answers

How do I initialize a float texture by script? 1 Answer

Using Texture2DArray as RenderTarget and passing data to fragment shader 2 Answers

Unity crashes trying to get pixels and using Debug.Log 0 Answers

GetPixels of RenderTexture 3 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