So I have a small script that takes screenshots of a cube with a 2d boundingbox drawn around it and then of the same cube, but just the contents of the bounding box.
I do this first by a standard call to Screen
ScreenCapture.CaptureScreenshot(filename)
then I call SnapShotInBoundingBox()
in my code to create a Texture2d from the rect of the bounding box and save that as a screenshot.
The problem I have is the texture when saved seems to shift the image up a few pixels.
public void OnGUI() //draws the boundingbox
{
Bounds b = theCube.GetComponent<Renderer>().bounds;
Camera cam = Camera.main;
if (cam == null) return;
//The object is behind us
if (cam.WorldToScreenPoint(b.center).z < 0) return;
//All 8 vertices of the bounds
pts[0] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[1] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[2] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[3] = cam.WorldToScreenPoint(new Vector3(b.center.x + b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
pts[4] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z + b.extents.z));
pts[5] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y + b.extents.y, b.center.z - b.extents.z));
pts[6] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z + b.extents.z));
pts[7] = cam.WorldToScreenPoint(new Vector3(b.center.x - b.extents.x, b.center.y - b.extents.y, b.center.z - b.extents.z));
//Get them in GUI space
for (int i = 0; i < pts.Length; i++) pts[i].y = Screen.height - pts[i].y;
//Calculate the min and max positions
Vector3 min = pts[0];
Vector3 max = pts[0];
for (int i = 1; i < pts.Length; i++)
{
min = Vector3.Min(min, pts[i]);
max = Vector3.Max(max, pts[i]);
}
//Construct a rect of the min and max positions and apply some margin
r = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
r.xMin -= BoundingBoxMargin;
r.xMax += BoundingBoxMargin;
r.yMin -= BoundingBoxMargin;
r.yMax += BoundingBoxMargin;
//Render the bounding box
GUI.backgroundColor = new Color(0, 0, 0, 0);
GUI.DrawTexture(r, boundingBoxTexture);
}
byte[] SnapShotInBoundingBox()
{
Rect lasso = new Rect(r.x, r.y, r.width+3, r.height);
Debug.Log(lasso);
tex = new Texture2D((int)lasso.width, (int)lasso.height, TextureFormat.RGBA32,false);
tex.ReadPixels(lasso, 0, 0); //only render what's inside the bounding box,including the box itself
tex.Apply();
return tex.EncodeToJPG();
}