I’m trying to calculate the distance the ViewportFrame Camera has to be in order for the Model to be fully in frame.
local cf, size = model:GetBoundingBox()
camera.CFrame = CFrame.lookAt(cf.Position + cf.lookVector * size.Magnitude,cf.Position)
-- here the camera is offset by the Magnitude of the Model
camera.FieldOfView = ?
-- what FieldOfView would make the Model fit perfectly at this distance?
I also want to know the inverse. For example if I want to set the camera’s FieldOfView = 30, what distance from the Model would I have to put the camera so that the Model will fit perfectly (keeping the Model’s size in mind)
What’s the relationship between FieldOfView and Distance to object (including size)?
FOV is the vertical degrees of the cameras sight. It is related to the horizontal degrees through the aspect ratio of the device. Trying to change the FOV to fit an object would probably require a bunch of trig to figure out, so I would recommend looking at this similar post: ViewportFrame Model Fitter . Sorry I can’t give direct help.
I don’t know of a simple way to do this, but I believe that the following should work:
Find the minimum distance from the model’s bounding box to the camera’s position.
Find the distance at which the whole model would fit within the camera’s field of view.
The minimum distance is defined as: d = abs(camera.Position - a) - (model.Size.Magnitude / 2)
Where a is the closest point of the model’s bounding box to the camera. To find this, you need to find the closest point on each axis: a.X = clamp(camera.Position.X, min.X, max.X)
a.Y = clamp(camera.Position.Y, min.Y, max.Y)
a.Z = clamp(camera.Position.Z, min.Z, max.Z)
Where min and max are the minimum and maximum values of the bounding box respectively.
Then, you can find the distance at which the model would fit within the field of view: d = (model.Size.Z * 0.5) / tan(camera.FieldOfView * 0.5)
Where model.Size.Z is the height of the model, and tan is the tangent function.
To make the model fit in the camera’s field of view, set the camera’s position to the minimum distance found above, minus the distance found above.