-- Private


function ViewportWindow:_createVPF(name, zindex)
	local vpf = Instance.new("ViewportFrame")
	vpf.LightColor = Color3.new(0, 0, 0)
	vpf.Size = UDim2.new(1, 0, 1, 0)
	vpf.Position = UDim2.new(0, 0, 0, 0)
	vpf.AnchorPoint = Vector2.new(0, 0)
	vpf.BackgroundTransparency = 1
	vpf.LightDirection = -Lighting:GetSunDirection()
	vpf.Ambient = Lighting.Ambient
	vpf.Name = name
	vpf.ZIndex = zindex
	vpf.CurrentCamera = self.camera
	vpf.Parent = self.surfaceGui

	return vpf
end

function ViewportWindow:_refreshVisibility(cameraCFrame, surfaceCFrame, surfaceSize, marginOfError)
	local aabbCF, aabbSize = Helpers.getAABB({
		surfaceCFrame * Vector3.new(-surfaceSize.X / 2 + marginOfError, surfaceSize.Y / 2 - marginOfError, -marginOfError),
		surfaceCFrame * Vector3.new(surfaceSize.X / 2 - marginOfError, surfaceSize.Y / 2 - marginOfError, -marginOfError),
		surfaceCFrame * Vector3.new(surfaceSize.X / 2 - marginOfError, -surfaceSize.Y / 2 + marginOfError, -marginOfError),
		surfaceCFrame* Vector3.new(-surfaceSize.X / 2 + marginOfError, -surfaceSize.Y / 2 + marginOfError, -marginOfError),
		cameraCFrame.Position,
	})
	
	-- region3 is more performant tmk over newer API which allows rotation (which we don't need)
	-- local overlap = self.worldRoot:GetPartBoundsInBox(aabbCF, aabbSize)
	local overlap = self.worldRoot:FindPartsInRegion3(Region3.new(
		aabbCF.Position + aabbSize / 2,
		aabbCF.Position - aabbSize / 2
	), nil, math.huge)
	
	local overlapped = {}
	for _, part in pairs(overlap) do
		part.LocalTransparencyModifier = 1
		overlapped[part] = true
	end
	
	for _, instance in pairs(self.worldRoot:GetDescendants()) do
		if instance:IsA("BasePart") and not overlapped[instance] then
			instance.LocalTransparencyModifier = 0
		end
	end
end
