How can I resize a part to match a Frame's size by the distance from the camera?

I need to resize a part to match a Frame’s size by the distance from the camera (of the part) and the UDim2 size of the Frame.

I’m trying to achieve a glassmorphism kind of effect and I only need to resize the part for it to be done. How can I achieve this?


(in this image, the black lines represent the part and the gray lines the frame, on the left is what I have and on the right is the expected output)

Here’s my code:

function module:BindFrameToPart(object: GuiObject, proprieties: {})
	local folder = game.Workspace:FindFirstChild("_RFT.uiparts")
	if not folder then
		folder = Instance.new("Folder", game.Workspace)
		folder.Name = "_RFT.uiparts"
	end
	
	local transparency = proprieties["Transparency"] or 0.98
	local brickColor = proprieties["BrickColor"] or BrickColor.new("Institutional white")
	
	local part = Instance.new("Part", folder)
	part.Name = object.Name
	part.Transparency = transparency
	part.BrickColor = brickColor
	part.Anchored = true
	part.CanCollide = false
	
	game["Run Service"].RenderStepped:Connect(function()
		part.CFrame = game.Workspace.CurrentCamera.CFrame + (game.Workspace.CurrentCamera.CFrame.LookVector * 5)
	end)
end
1 Like

Boosting beacuse of inactivity

well for starters any 3d games camrea is piramid so for what your wanting i would try and read up on Field of view - Wikipedia.

because if my memory serves me right the part should always scale by a constant along the fov lines

for example your camrea should in theory look like
image

And how can I scale the part accordingly?

well first i would suggest getting something like this.
image

to help visualize what your doing

then get a ratio of the sreen size you want for example 1:2

There is a similar post with the solution for it.

well hopefully it helps you out :slight_smile:

I’m going to try this, and I’ll tell you if it works

I modified the code a little, but I still can’t get it to work. Here’s what I changed:

local height = math.tan(math.rad(game.Workspace.CurrentCamera.FieldOfView/2)) * 2  * forwardOffsetDistance
local width = (object.Size.X.Offset / object.Size.Y.Offset) * height
part.Size = Vector3.new(width, height, depth)

Edit: the “object” is located inside another Frame, making it’s size adapt to the other Frame. I believe I need to somehow find the actual size of the object in order to properly size the part.

Edit 2: the Part is way too big, but it has the shape.
image
image

If you make a part that size and then CFrame it so that it’s offset from the camera by forwardOffsetDistance , I believe it should cover the entire screen.

I believe you also need to CFrame it with the forwardOffsetDistance.

This is how I position the Part:

part.CFrame = game.Workspace.CurrentCamera.CFrame + (game.Workspace.CurrentCamera.CFrame.LookVector * forwardOffsetDistance)

Am I doing something wrong here on in the Size part?