Get GUI 2d bounding box?

I was wondering if there was a way to make a GUI turn into a 2d bounding box for an object.

Uh, it’s pretty self explanatory.
Any help would be appreciated!

Edit:
So I have recently made public RNMU,
so I decided to add it as a function on it.
(The lag is roblox recording not my pc.)

Finding the cross sectional shape of a rectangular prism through math is mega complicated and I would definitely not take that approach like I normally would want to

I would recommend using a whole screen viewport frame with an invisible background which is bound to the currentcamera with a transparent version of the target object put into the viewport frame to give this effect

You could use Camera:WorldToScreenPoint(Vector3 point) and compute the screen bounding box using the corners of the object’s world bounding box.

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint

With a part you can fetch the corners using just Part.CFrame and Part.Size, but with a model you could get the equivalent values with Model:GetBoundingBox() which also gives a box CFrame and size.

https://developer.roblox.com/en-us/api-reference/function/Model/GetBoundingBox

The corners can be fetched using the object’s right, up, and forward vectors that describe which direction right, up, and forward are relative to the object. An individual corner would look something like:

corner = cframe.Position
		+/- 0.5 * size.X * cframe.RightVector
		+/- 0.5 * size.Y * cframe.UpVector
		+/- 0.5 * size.Z * cframe.LookVector

For corners on the left side you would use - on RightVector, and + for right side.
For corners on the bottom you would use - on UpVector and + for top side.
For corners on the back you would use - on LookVector and + for the front.

The easiest would be to compute all 8 corners and then convert them to screen space using Camera:WorldToScreenPoint(corner). Once you have each corner in screen space you can use math.min and math.max to compute the screen space bounding box. Compute the min on each axis to get the UI object position, and max - min for the size.

Edit: Lemme know if you have any questions about any parts of my explanation. It’s a little rough so I can clarify if anything is poorly worded.

3 Likes

Hm,
now that I think about it,
I could probably use my RNMU module to get the corners
( RNMU Inserter - Roblox if anyone is interested ),
then get the highest corner like I did for the 3d version but with the function.
Thanks.

1 Like