- Home /
Cropping Texture2D using a Rect
Hello,
This is getting me crazy! I have texture2D (which is an image from a camera) and somewhere on my code I do some processing and I come up with a Rect, which should be used to crop te image and select only a portion of it.
To show the entire image I'm using the following (placed on OnGUI function):
Texture2D texture = k.getTexture2D(Mode.RGB);
GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
The texture is showing the RGB image from the camera. As stated, I have a rect and I want to crop the texture i get from the camera. For that I'm doing as follow:
Rect r = getUserRect(skeletonID);
if (r.width != 0 && r.height != 0)
{
MonoBehaviour.print(r);
Texture2D result = new Texture2D((int)r.width, (int)r.height);
result.SetPixels(color.getTexture2D().GetPixels(Mathf.FloorToInt(r.x), Mathf.FloorToInt(r.y), Mathf.FloorToInt(r.width), Mathf.FloorToInt(r.height)));
result.Apply();
}
After this I show the cropped area(inside the OnGUI function):
if (user != null)
{
GUI.DrawTexture(new Rect(0, 0, result.width, result.height), result);
}
This is not working and I don't know why. The cropped area seems to stucked at y = 0, but if I draw onlye the Rect (instead on cropping) the cropped area is fully filled with the rect (to do this I do as follow):
void DrawQuad(Rect position, Color color)
{
Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, color);
texture.Apply();
GUI.skin.box.normal.background = texture;
GUI.Box(position, GUIContent.none);
}
void OnGUI()
{
...
DrawQuad(r, Color.black);
...
}
Any ideias? Thanks!
EDIT: Code overview
void OnGUI()
{
Texture2D texture = k.getTexture2D(Mode.RGB);
GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
Rect r = getUserRect(skeletonID);
if (r.width != 0 && r.height != 0)
{
MonoBehaviour.print(r);
Texture2D result = new Texture2D((int)r.width, (int)r.height);
result.SetPixels(texture.GetPixels(Mathf.FloorToInt(r.x), Mathf.FloorToInt(r.y), Mathf.FloorToInt(r.width), Mathf.FloorToInt(r.height)));
result.Apply();
GUI.DrawTexture(new Rect(0, 0, result.width, result.height), result);
}
}
It's hard to figure out exactly what happens when we can't see the code in getUserRect. :)
But my idea is that this is some confusion between the orientation of texture coordinates and that of GUI coordinates. If getUserRect is an implementation of some lasso-function that allows the user to drag out a rect on screen, then you should beware that the returned coordinates will have their origin in the upper left corner, while GetPixels and SetPixels work from bottom and up. Perhaps this is at least part of the reason it doesn't work at expected?
I substituted the getUserRect with new Rect(320, 240, 320, 240). Since Texture I get from the camera is 640x480, I think I should get the lower right corner of the image, but what I'm getting is the upper right corner of the image. What am I doing wrong?
Replacing that line with these ones: return new Rect(0, 0, 320, 240); //expected lower left, but got upper left return new Rect(320, 240, 320, 240); //expected upper right, but got lower right return new Rect(0, 240, 320, 240); //expected upper left, but got lower left return new Rect(320, 0, 320, 240); //expected lower right, but got upper right
Your answer
Follow this Question
Related Questions
Animation editor - trimming tail 3 Answers
Generate objects in a square? 0 Answers
How to not scale my Texture? 1 Answer
jibjab (insert your own face) implementation in Unity 0 Answers
crop an image from WWW 0 Answers