I need to make a seamless viewport grid on a bunch of surfaceguis. I have the grid already, but I do not understand the math behind making it seamless.
What I have:
What I need:
Code
function class:Generate()
local frustrum = self.Frustrum
local depth = 2048
local d = depth
local bounds,sizeFactor,factors = frustrum:GetSizeAtDistance(depth)
local max = math.max(bounds.X,bounds.Y)
local amt = math.ceil(max/depth)
local res = amt
local size = math.max(bounds.X,bounds.Y)/res
local sizeX = size
local sizeY = size
local yOffset = size/2
local countX = math.ceil((bounds.X/sizeX))
local countY = math.ceil((bounds.Y/sizeY))
local startX = (-factors.X) - (sizeX/2)
local startY = (-factors.Y) - (sizeY/2)
local posZ = -depth
for x=1, countX do
for y=1, countY do
local posX = startX + (x * sizeX)
local posY = startY + (y * sizeY)
local cframe = CFrame.new(
posX,
posY,
posZ
)
local part = script.Part:Clone()
part.Size = Vector3.new(size,size,0)
part.CFrame = cframe
part.Parent = self.Holder
part.Transparency = 1
table.insert(self.Parts,part)
local gui = Instance.new("SurfaceGui",(if isServer then game.StarterGui else game.Players.LocalPlayer.PlayerGui))
gui.Name = "FakeSky"
gui.Adornee = part
gui.Face = Enum.NormalId.Back
gui.LightInfluence = 0
local cam = Instance.new("Camera")
cam:SetAttribute("Offset",CFrame.new(posX/res,posY/res,0))
local sky = Instance.new("ViewportFrame",gui)
sky.Size = UDim2.fromScale(1,1)
sky.CurrentCamera = cam
sky.BorderSizePixel = 0
sky.LightDirection = Vector3.zero
sky.LightColor = Color3.new(0,0,0)
sky.Ambient = Color3.new(1,1,1)
local projector = script.Skybox:Clone()
projector.Parent = sky
projector.Size = Vector3.new(2048,2048,2048)
cam.Parent = sky
table.insert(self.Skies,sky)
end
end
end
function class:SetTexture()
end
function class:Update()
local frustrum = self.Frustrum
local camera = self.Camera :: Camera
frustrum:Update(camera,2048)
local cf = camera:GetRenderCFrame()
self.Holder.Parent = camera
self.Holder:PivotTo(cf)
for i,sky in pairs(self.Skies) do
sky.Skybox.Position = cf.Position
sky.Camera.CFrame = cf * sky.Camera:GetAttribute("Offset")
end
end