This is confusing to explain, but I’ll try my best.
I want to display a model through a ViewportFrame, and make it look normal, just like how any other object in the workspace would be rendered. I could use a ViewportFrame in a ScreenGui, set the size to fill the screen and then set the CurrentCamera property to the workspace camera, HOWEVER this ViewportFrame will draw over everything else in the workspace (since it’s a 2d element), which is not what I want.
So, I decided to place a physical viewport in the workspace with a SurfaceGui so that the ViewportFrame won’t render over objects in the workspace.
The grey cube is a Part inside a ViewportFrame which is inside a SurfaceGui, which is adornee’d to a Flat Plane Mesh in workspace. The flat mesh is always rotated to face the camera (basically emulating a billboardgui)
The purple cube is a Part inside the workspace which serves as a frame of reference for what the ViewportFrame cube should look like.
The problem is that the cube inside the viewport doesn’t always match the one in workspace, like the perspective seems off. What maths would I need to use to make the model inside the viewport match the real one in workspace?
The code I have so far, which works but is still off.
RunService.RenderStepped:Connect(function()
local X, Y, Z = Camera.CFrame:ToEulerAnglesXYZ()
-- this is to rotate the plane to constantly face the camera (emulate billboardgui)
Plane.CFrame = CFrame.new(Cube.Position) * CFrame.Angles(X, Y, Z)
Plane.CFrame *= CFrame.Angles(math.pi/2, math.pi/2, 0)
-- calculate the CFrame of the Viewport Camera
local VirtCFrame = VPF.Part.CFrame * CFrame.Angles(X, -Y, Z) * CFrame.new(0,0,-9)
VirtualCam.CFrame = CFrame.lookAt(VirtCFrame.Position, VPF.Part.Position)
end)
Right now I’m just setting the rotation of the viewport camera to the same as the actual workspace camera, and the position is the position of the cube + an arbitrary offset. This obviously isn’t right, but I’m not mathematically knowledgable enough to know how to calculate the correct solution.
This is another problem which is the same as mine, however was left unanswered