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
0
Question by Domnakus · Aug 20, 2015 at 07:50 AM · physicsgraphics

GetPixel() returns only one value

Hi,

i'm trying to get color codes out of an texture which has a few different colored areas. Here's my code:

 public class ColorRef : MonoBehaviour {
 
 Color pixelColor;
 
 public Camera cam;
 
 void Start() {
 
         cam = GetComponent<Camera>();
     }
     void Update() {
         if (!Input.GetMouseButton(0))
             return;
         
         RaycastHit hit;
         if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
             return;
         
         Renderer rend = hit.transform.GetComponent<Renderer>();
         MeshCollider meshCollider = hit.collider as MeshCollider;
         if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
             return;
 
         Texture2D tex = rend.material.mainTexture as Texture2D;
         Vector2 pixelUV = hit.textureCoord;
 
         pixelColor = tex.GetPixel ((int)hit.textureCoord.x,(int)hit.textureCoord.y);
         print(pixelColor.ToHexStringRGB());
     
     }
 }


But every time I click on the texture my class returns the same color. And every time it's a color that is not used in the texture. It's imported as advanced and read/write is enabled. What's going wrong ?

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 Domnakus · Aug 20, 2015 at 07:45 AM 0
Share

i've formated it with the button

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nesis · Aug 20, 2015 at 08:33 AM

Texture coordinates are in the range of 0,0 to 1,1. Casting your texture coordinates to int means you'll only ever get one of the corners of the texture.

Don't cast to int and you should be fine. To fix that, change:

pixelColor = tex.GetPixel ((int)hit.textureCoord.x,(int)hit.textureCoord.y);

To

pixelColor = tex.GetPixel (hit.textureCoord.x,hit.textureCoord.y);

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 troien · Aug 20, 2015 at 09:13 AM

Looking at the documentation of RaycastHit.textureCoord you can see that the returned values range between 0 and 1.

To get the correct pixel position, you probably should multiply the x by the width and the y by the height like they do in the example.

 Texture2D tex = rend.material.mainTexture as Texture2D;
 Vector2 pixelUV = hit.textureCoord;
 pixelUV.x *= tex.width;
 pixelUV.y *= tex.height;
 
 pixelColor = tex.GetPixel((int)pixelUV.x,(int)pixelUV.y);

Also, please note this sentence in the docs. Printing hit.textureCoord might help debug this futher to see if this applies to your case.

This can be used for 3D texture painting or drawing bullet marks. If the collider is no mesh collider, zero Vector2 will be returned.

Comment
Add comment · Show 5 · 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 Domnakus · Aug 20, 2015 at 02:48 PM 0
Share

I tried this out and the problems still exists. It returns the right texture coords, but the returned color is always the color from the botom left corner even if i click on the top right corner. Is it a problem with my texture or still something with the raycast ?

avatar image troien · Aug 20, 2015 at 06:21 PM 0
Share

strange...

Things I can think of that might be wrong.

  • Are you sure you are hitting the correct mesh? (The mesh wih the texture on it)

You could check this by adding a log like this:

 Debug.Log(hit.transform.name, hit.transform);

And see if the name is correct and that the correct GameObject gets highlighted in the hierarchy

If so, also check if this highlighted GameObject has a Renderer Component.

If so, check if the material used for this renderer contains the texture you are trying to read pixels from (If the renderer has more then one material here, I believe this code should always use the first material of the list)

On a sidenote, what is the size in pixels of your texture? (width and height) and what is the value of pixelUV right before you call tex.GetPixel, both when you click bottom left and top right?

avatar image Domnakus · Aug 20, 2015 at 07:05 PM 0
Share

First, there is only one mesh so no other mesh could be hitted. $$anonymous$$y texture is 128x64 and the pixelUV values from the the bottom left are (0.0|0.0)before muliplying and (0.2|0.1)after and from the top right (1.0|1.0)and (119.7|59.7). And the returned color is always the color from 0|0.

avatar image troien · Aug 20, 2015 at 07:59 PM 0
Share

Really strange... :p

The only thing I can think of now is that the texture isn't the texture you expect. But given what you have said so far I doubt that..

And you are using pixelUV ins$$anonymous$$d of hit.textureCoord as the parameters to GetPixel like in my example right?

If that is all the case, then I think I can't really help you further... As it should work :p

avatar image Domnakus · Aug 20, 2015 at 08:04 PM 0
Share

Ok i I did not see the changes in the arguments of GetPixel. No it#s working as intended. Thank you for help :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Accessing a parent's other children 1 Answer

Grass is very high 1 Answer

Colliding 2 completely flat quads with physics? 5 Answers

Animating physics to be run in the background 0 Answers

C# vs UnityScript 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