- Home /
Pixel values of a plane (RectTransform)?
Here is a really simple video demo of tracking the top left pixel coordinates of a plane: https://streamable.com/xq5f2e
In my case, I have the top left corner of the screen to be origin (0,0); The values that are being output are the values that show far away the top left corner of the plane is from the top left corner of the game's screen. It seems though, after turning away from the plane after a certain angle, the coordinates of the top left corner get flipped and go from being negative to positive, whereas they should continue to be negative.
Here is the simple code to track the pixel values of the top left corner:
private void FixedUpdate()
{
DisplayWorldCorners();
}
void DisplayWorldCorners()
{
rt = GetComponent<RectTransform>();
Vector3[] v = new Vector3[4];
rt.GetWorldCorners(v);
Vector2 topLeft = Camera.main.WorldToScreenPoint(v[1]);
Vector2 topRight = Camera.main.WorldToScreenPoint(v[2]);
Vector2 bottomRight = Camera.main.WorldToScreenPoint(v[3]);
Vector2 bottomLeft = Camera.main.WorldToScreenPoint(v[0]);
topLeft.y = (Screen.height - topLeft.y);
topRight.y = (Screen.height - topRight.y);
bottomRight.y = (Screen.height - bottomRight.y);
bottomLeft.y = (Screen.height - bottomLeft.y);
text.text = "Top Left Coordinates: " + topLeft.ToString();
Debug.Log("Top Left Coordinates: " + topLeft);
}
Any know why the values drastically change from negative to positive? It seems like the values almost get inverted.
Could the point on the plane be behind the camera at this point? That could possibly be throwing some odd readings
Yeah, it is behind the camera which causes the value to approach a really large negative number and it eventually leads to an over flow (and makes it positive). Not sure how I could handle that and prevent the over flow for the top left corner's pixel value.
You can either do a check that the point is actually on the screen, or you can clamp the position to only valid coordinates, so that if it goes off screen, it'll treat it as if it's on the edge of the screen.
Your answer
Follow this Question
Related Questions
copying a 2d/3d plane/slab from one part of world and displaying it in another? 0 Answers
copy camera view to 2d panel and pixelate it 0 Answers
I need help with a model 0 Answers
How to get pixeldata from graphics.drawmesh? 0 Answers
Setting a cube to be exactly size of intersecting camera view plane 1 Answer