Fake 3D Objects Using ViewportFrames

I’m trying to use viewport frames to turn 3d items into 2d space and still keeping their orientation and position as if it was 3d. I’ve got most of it complete, but there’s one problem that I don’t know how to fix: the block isn’t centred in the viewport so it doesn’t line up with the 3d object.

The way to fix this is if I can have the 2d object always located in the middle of the viewport frame. The problem with this system is that it uses the cameras view so it won’t be in the centre of the screen unless you’re looking directly at it.

I’m not sure how to adjust it so that it’s in the centre of the viewport so I came here for assistance. This is what I’ve done so far:

Here is the code:

local itemModel = game.ReplicatedStorage.Assets.Loot_Model:FindFirstChild(item)

if itemModel then
	local viewport = script.Parent.ViewportFrame
	
	itemModel = itemModel:Clone()
	itemModel.CFrame = cframe
	itemModel.Parent = viewport
	
	itemModel = itemModel:Clone()
	itemModel.CFrame = cframe
	itemModel.Parent = Workspace
	
	local cam = Instance.new("Camera")
	viewport.CurrentCamera = cam
	cam.Parent = viewport
	
	RunService.RenderStepped:Connect(function()
		
		local worldPoint = cframe.Position
		local vector, onScreen = CAMERA:WorldToScreenPoint(worldPoint)
		
		local screenPoint = Vector2.new(vector.X, vector.Y)
		local depth = vector.Z
		
		cam.CFrame = CAMERA.CFrame
		viewport.Position = UDim2.new(0, screenPoint.X, 0, screenPoint.Y)
	end)
	
end
1 Like

Really not quite sure what you’re attempting here.
This is what I’m gathering:
You want to have the object look like it is in the 3D world, when it is just a VPF.

In that case, it’s very simple.

VPF.Size = UDim2.new(1,0,1,0) -- fullscreen
VPF.CurrentCamera = workspace.CurrentCamera

Then, any objects you place in the VPF will render as if they are in the world, since the VPF is basically just your camera in GUI form.

6 Likes

Thanks, I made this way more complicated than it needed to be

1 Like

Yup! I often do that. That’s why we have these forums, so we can have people check our work!

1 Like