im trying to make in-game screenshot with viewport frames but i have a problem so since viewport frames are very laggy i thought of only cloning items that are seen by the camera instead of cloning the whole map but i have a problem since my function only checks if the parts each point is seen by camera so,if i were to take a photo while looking down for some reason the baseplate doesnt show up since baseplates each points isnt seen by the camera so i need help on fixing it i tries all kinds of stuff and it didnt work.
Here is the code:
local ignore = {"Camera","Terrain","Script","LocalScript","Attachment"}
local function OnScreen(camera, part)
-- 1. get all eight points in 3D space for the part
local pos, front, right, up = part.Position, part.CFrame.LookVector, part.CFrame.RightVector, part.CFrame.UpVector
local hx, hy, hz = part.Size.X / 2, part.Size.Y / 2, part.Size.Z / 2
local points = {
pos - front * hz - right * hx - up * hy,
pos - front * hz - right * hx + up * hy,
pos - front * hz + right * hx - up * hy,
pos - front * hz + right * hx + up * hy,
pos + front * hz - right * hx - up * hy,
pos + front * hz - right * hx + up * hy,
pos + front * hz + right * hx - up * hy,
pos + front * hz + right * hx + up * hy,
}
-- 2. compute AABB of the part's bounds on screen
local minX, maxX, minY, maxY = math.huge, -math.huge, math.huge, -math.huge
for i = 1, 8 do
local screenPoint = camera:WorldToViewportPoint(points[i])
local x, y = screenPoint.X, screenPoint.Y
if screenPoint.Z > 0 then
if x < minX then
minX = x
end
if x > maxX then
maxX = x
end
if y < minY then
minY = y
end
if y > maxY then
maxY = y
end
end
end
-- 3. AABB collision with screen rect
local w, h = camera.ViewportSize.X, camera.ViewportSize.Y
return minX < w and maxX > 0 and minY < h and maxY > 0
end
script.Parent.MouseButton1Click:Connect(function()
local s = script.Sky:Clone()
s.Parent = script.Parent.Parent.Frame
local vpf = s.ViewportFrame
local cam = workspace.CurrentCamera:Clone()
cam.Parent = vpf
vpf.CurrentCamera = cam
local scene = Instance.new("Model")
scene.Name = "Scene"
scene.Parent = vpf
for i,v in pairs(workspace:GetDescendants()) do
if (v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("UnionOperation")) and v.Name ~= game.Players.LocalPlayer.Name then
if not table.find(ignore,v.ClassName) and v.Transparency ~= 1 then
local touch = OnScreen(cam,v)
if touch then
v:Clone().Parent = scene
end
end
end
if v.Name == game.Players.LocalPlayer.Name then
local mod = Instance.new("Model")
mod.Name = v.Name
mod.Parent = vpf
for i,v in pairs(v:GetChildren()) do
v:Clone().Parent = mod
end
mod:FindFirstChild("Humanoid").DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
end
end
end)
here is the game file if you need it:
cam.rbxl (56.4 KB)
thanks for any help you can give!