Changing Viewport Field of View and Position based on size of model

Basically, I have a folder in ReplicatedStorage that I want to loop through all the models in, and make them viewports. It works, but it doesn’t look very good. Is there a way to make it look better. Doing this requires changing the FieldOfView according to the size of the model, and changing the angle of the camera. How can I do this?

What it looks like now:

Code:

for i, v in pairs(builds:GetChildren()) do
			if v:IsA("Model") then
				local viewport = viewPortFrame:Clone()
				
				viewport.Parent = buildsFrame
				
				viewport.Visible = true
				
				
				local ViewportCamera = Instance.new('Camera') 
				
				ViewportCamera.Parent = viewport
				
				ViewportCamera.CameraType = Enum.CameraType.Scriptable
				
				ViewportCamera.FieldOfView = 10 
				
				viewport.CurrentCamera = ViewportCamera
				

				local model = v:Clone()
				
				model:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
				
				model.Parent = viewport
			end
		end

Have you tried using this: ViewportFrame Model Fitter

1 Like

This system is done automatically, so that wouldn’t work. I am making it so that you can easily add a build into the folder, and it will look perfect.

try using Model:GetExtentsSize() Model | Documentation - Roblox Creator Hub

How would I correlate this to changing the angle of the camera, the FieldOfView, and/or the distance away from the object the camera is?

Also, if you get the chance, why aren’t the models staying within the actual ViewPortFrame?

local focusPart = --// model that you want to add
local size = focusPart:GetExtentsSize()
camera.CFrame = focusPart.WorldPivot * CFrame.new(0,size.Y*1.5,size.Z) * CFrame.Angles(math.rad(-45),0,0)

table

it works on my game properly.

This doesn’t seem to work for me? Am I doing something wrong?

for i, v in ipairs(builds:GetChildren()) do
			if v:IsA("Model") then
				if not v.PrimaryPart then warn(v.Name) return end
				local viewportlabel = viewPortFrame:Clone()
				
				viewportlabel.Parent = buildsFrame
				
				viewportlabel.Visible = true
				
				local viewport = viewportlabel.ViewPort
				viewport.Visible = true
				
				local ViewportCamera = Instance.new('Camera') 
				
				ViewportCamera.Parent = viewport
				
				ViewportCamera.CameraType = Enum.CameraType.Scriptable
				
				ViewportCamera.FieldOfView = 10 
				
				viewport.CurrentCamera = ViewportCamera
				

				local focusPart = v:Clone()
				v.Parent = viewport
				local size = focusPart:GetExtentsSize()
				ViewportCamera.CFrame = focusPart.WorldPivot * CFrame.new(0,size.Y*1.5,size.Z) * CFrame.Angles(math.rad(-45),0,0)
			end
		end

Can anyone else try and solve my issue, I have been stuck on it.

Also, forgot to mention here, the other issue of the model taking up to much is a problem too. The viewportframe is the size of the little section above the read parts. So I don’t know why this is happening, as well as the problem this entire post is about.

sorry for another bump, i just really, really need this fixed!

1 Like