Recently I was working on something that required fitting an arbitrary model into an arbitrary viewport frame. I looked around saw this was a pretty common question, so I figured I’d write up a module
The above links to a class that can be used in two ways.
The first is to calculate a fixed minimum distance that’s guaranteed to contain the model regardless of the camera orientation or viewport size.
This is useful as it’s a low-cost one-time calculation assuming you’re not constantly adding/removing/changing the model or viewport frame.
The second is to calculate a camera cframe that best fits the model given an orientation. This will mean that the model will always be touching at least one edge of the viewport frame.
However, it’s important to note that this calculation is based off of the model’s point cloud meaning meshes, csg, and/or any other base part that makes it difficult to get a proper point cloud may cause this calculation to seem inaccurate.
For example compare happy home vs piano in the above video
Hope you find this useful and enjoy!
Here’s the placefile I used to record the above videos:
Fit viewport frame.rbxl (2.9 MB)