--// Rotate viewport

local function setRotationEvent(model, camera)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	
	
	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
	
	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))
	
	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end
	
	local minDist = (maxExtent/2)/tan + diagonal
	if model.Name == "Cam" then
		minDist += 1
	end

	return RunService.RenderStepped:Connect(function(dt)
		local increase = (1 * dt * 60) / 1.5
		currentAngle = currentAngle + increase
		camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, (minDist - 1))
	end)
end
