- Home /
GetPixel is not working.
Hi,
I've create a draw3d texture using jpg file but getpixel() is not working perfectly.
Here is my script
Vector2 pickpos = Event.current.mousePosition;
int aaa = (int)(pickpos.x);
int bbb = (int)(pickpos.y);
Color col = colorPicker.GetPixel(aaa, bbb);
GameObject gdata = GameObject.Find("gDomino");
gdata.renderer.material.color = col;
It's not fetching pixel of related color.I show different color
thanks for support
What is 'colorPicker'? Without knowing what that is, I'm afraid I can't help you.
Assu$$anonymous$$g that colorPicker is a texture of some kind, keep in $$anonymous$$d that 'Event.current.mousePosition' is in screen coordinates, not in whatever texture coordinates you would probably need for GetPixel to work. $$anonymous$$ake sure you are transfor$$anonymous$$g the coordinates properly.
How do i transform coordinates will you pls give an example?
yes i can change color but the color is different it's not same as i have selected.
I mean, the screen coordinates go from '0, 0' in one corner, to 'width, height' in the other, wheras texture coordinates go from '0, 0' to 'resolution, resolution'. Textures are almost always square, whereas screens almost never are, and unless the texture in question is perfectly covering the entire screen, you need to skew and clip the coordinates so that they map properly onto the texture.
Answer by Kryptos · May 10, 2012 at 03:00 PM
Do you know that JPG is a lossy compression algorithm? You should use a lossless compressed image such as PNG. And then set the texture image type to true color (not compressed).
I've change as per you suggestion it's take some effect i think i need to take get local coordination not screen coordination .Here is my full code.can you give me suggestion that how can i get it.
Rect irect = new Rect(0, 0, 300, 194);
this.colorPicker = (Texture2D)Resources.Load("sp3");
GUI.DrawTexture(irect, this.colorPicker);
if (irect.Contains(Event.current.mousePosition))
{
if (Input.Get$$anonymous$$ouseButton(0))
{
Vector2 pickpos = Event.current.mousePosition;
int aaa = (int)(pickpos.x);
int bbb = (int)(pickpos.y);
Debug.Log(pickpos.x + "x" + pickpos.y + " " + aaa + "/" + bbb);
Color col = colorPicker.GetPixel(aaa-10, bbb-7);
GameObject gdata = GameObject.Find("gDo$$anonymous$$o");
gdata.renderer.material.color = col;
//Debug.Log(col);
}
}
Answer by Bunny83 · May 10, 2012 at 03:47 PM
You probably just need to invert the y-coordinate since a texture is build up from bottom to top:
Vector2 pickpos = Event.current.mousePosition;
int aaa = (int)( pickpos.x - irect.x);
int bbb = (int)(irect.height - (pickpos.y - irect.y));
I've also subtracted the pivot of your rect just in case you move the picker somewhere else on the screen.