Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I calculate the FOV of a camera based on the size of an item in a viewport?

Asked by
7z99 203 Moderation Voter
3 years ago

Hello!

I am looking to dynamically change this viewport's FOV but the problem is that I am not sure how I would go about getting the math for it?

My viewport's current FOV is 45ยบ and it works fine for large accessories, but for small accessories, they're basically invisible. Any ideas? Tiny accessory: https://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/1/3/c/13ccc40c1ba70dd56943f448f0d2eec60a57a2ad.png Large accessory: https://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/0/4/6/0460481a24140a41ee9033bbf9dbe17cfd8bae12.png

Also should add that in my script, I do get the bounding box of the accessory to render it properly in the viewport, however I am unsure as to how I would approach decreasing/increasing the field of view of the camera accurately based on the size of the accessory.

Script that adds the parts to the viewport and welds them to a rotating part which works fine:

    local newObject = Instance.new('Model')
    local boundingBox = Instance.new('Part')
    for i,v in pairs(selectedObject.Value:GetDescendants()) do
        if v:IsA('BasePart') then
            local clonedObject = v:Clone()
            clonedObject.Parent = newObject
            clonedObject.Anchored = false
        end
    end
    local cframe, size = newObject:GetBoundingBox()
    boundingBox.CFrame = cframe
    boundingBox.Size = size
    boundingBox.Parent = newObject
    boundingBox.Transparency = 1
    boundingBox.Anchored = false
    boundingBox.CanCollide = false
    newObject.PrimaryPart = boundingBox
    for i,v in pairs(newObject:GetChildren()) do
        if v:IsA('BasePart') then
            local weldConstraint = Instance.new('WeldConstraint')
            weldConstraint.Parent = v
            weldConstraint.Part0 = boundingBox
            weldConstraint.Part1 = v
        end
    end
    newObject:SetPrimaryPartCFrame(weldPart.CFrame)
    local weldConstraint = Instance.new('WeldConstraint')
    weldConstraint.Parent = newObject
    weldConstraint.Part0 = weldPart
    weldConstraint.Part1 = boundingBox
    boundingBox.CFrame = weldPart.CFrame
    newObject.Parent = worldModel
    oldObject = newObject

Camera's position is 0,5,10 and the orientation in degrees is -26.569999694824, 0, 0

I've also just tried :WorldToScreenPoint and :WorldToViewportPoint and they ended up being inaccurate, no idea how to proceed. Any help would be greatly appreciated.

Answer this question