From what I have seen regarding my current problem regarding incorrect bounding box coords being written out, I need to get the OBB values since both my camera and object rotate. Is there a way of doing this in Unity? I haven’t had luck finding anything yet.
I can’t help with “your current problem” because you haven’t detailed it, but you can generate an OBB from an AABB. Take the 8 corner points of the AABB from the Collider or Renderer’s bounds with your object’s rotation set to identity, then rotate your object and use TransformPoint() on those 8 corner points.
Thanks for your reply! This sounds exactly what I was needing. I am saving snapshots taken and then need to write out bounding box coords to JSON which designate to an AI training model, where the object is in the snapshot. The problem I was dealing with was that the bounding boxes were AABB values and when the products were rotating, the bounding boxes were a bit off.
Going to go try to implement your solution. Again, thank you!
Note that you won’t necessarily get a minimal bounding box with this technique.
Can you elaborate @kru ? So my end desire is to have image snapshots of my shelf and their products and then accurate bounding boxes as close to the products as possible. After my first implementation, which you can see a screenshot of below, you can see 2 issues. The first being that as the products rotated and the camera around the shelf rotated, my bounding boxes were off. This is due to the AABB issue and it being axis aligned. So I first need to solve that problem. The second problem is that I “Really” need the boxes to be as tight to the products without overlap. However because I am working in a 3D space, the bounding box is aware of the full 3D model and not what is just within the 2d snapshot. So after I get this OBB issue resolved, then I need to work on trying to reduce the bounding box coordinates to only contain the individual products on the shelves themselves.
This is a bit of a strange project for Unity, but the end goal is to create synthetic data snapshots with labels to be fed into a Deep Learning model. So if you have a recommended method that will provide me tighter OBB’s I’m all ears. Anyway, that is what I’m trying to achieve here.

I don’t see why the OBB wouldn’t have the same (relative) dimensions of the AABB. Renderer bounds is large enough to contain the entire object while collider is set by you, so it can be any scale relative to the visual object that you want.
Sorry @GroZZleR … I don’t quite get what you are saying here. But the problem I have at hand currently is that when I projected my bounds to 2d to then write out to JSON, if the products on the shelf were rotated the bounds values were off. You can see this perfectly from the top right product and its associated red bounding box. The bounding box was perfect as long as I didn’t rotate the products on the shelf. I later found out that this was due to bounds being Axis aligned and I rather needed an OBB which is more expensive, but for my case I don’t need to worry too much about frame rate as I am just randomly rotating everything in 3D to then take a snapshot of after. I have shared a screenshot of what I currently have and what I am trying to get to.
This all said, I believe an OBB from reading is what I need. I really am not fully sure, but from what I have seen across forums, it sounds like what I am needing in the end. And the JSON coords, x, y, width and height that are written out are the 2D space projected coordinates.
Thanks for your time!

@GroZZleR I just wanted to thank you again for your help on this! I finally got what you had recommended working via the box collider. The final working logic in my case is below as well should anyone else run into a similar need.
public Rect RectFromObj(GameObject go)
{
BoxCollider bxcol = go.GetComponent<BoxCollider>();
var extentPoints = new Vector2[]
{
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(-bxcol.size.x, -bxcol.size.y, -bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(bxcol.size.x, -bxcol.size.y, -bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(bxcol.size.x, -bxcol.size.y, bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(-bxcol.size.x, -bxcol.size.y, bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(-bxcol.size.x, bxcol.size.y, -bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(bxcol.size.x, bxcol.size.y, -bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(bxcol.size.x, bxcol.size.y, bxcol.size.z) * 0.5f)),
WorldToGUIPoint(transform.TransformPoint(bxcol.center + new Vector3(-bxcol.size.x, bxcol.size.y, bxcol.size.z) * 0.5f))
};
Vector2 min = extentPoints[0];
Vector2 max = extentPoints[0];
foreach (Vector2 v in extentPoints)
{
min = Vector2.Min(min, v);
max = Vector2.Max(max, v);
}
return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
}
I welcome our future cereal-box-recognizing computer overlords!
