- Home /
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 ?
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);
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.
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 ?
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?
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.
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
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
Follow this Question
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