I am trying to write a script that takes in a list of parameters as an input and outputs a rendered image so far I have this code:

```
local RenderScale = 1
local Camera = {
Pos = Vector3.new(0,0,0),
Rot = Vector3.new(0,0,0),
Fov = 90,
Range = 50
}
local Objects = {
{
Type = "Sphere",
Pos = Vector3.new(0,0,-5),
Radius = 2,
Color = Color3.new(0.843137, 0.235294, 0.294118)
}
}
local function CreatePixel(PosX,PosY,Color)
local pixel = script.Pixel:Clone()
pixel.Parent = script.Parent.Parent.Pixels
pixel.BackgroundColor3 = Color
pixel.Position = UDim2.new(0,PosX/RenderScale,0,PosY/RenderScale)
pixel.Size = UDim2.new(0,1/RenderScale,0,1/RenderScale)
end
local function SphereIntersect(c,r,d)
local m = Camera.Pos - c
local b = m:Dot(d)
local c = 1 - (r * r)
if c > 0 and b > 0 then
return false
end
local discriminant = b * b - c
if discriminant < 0 then
return false
end
local t = math.clamp(-b - math.sqrt(discriminant),0,math.huge)
local q = Camera.Pos + Vector3.new(t,t,t) * Vector3.new(discriminant,discriminant,discriminant)
return q
end
local function Render()
local ResolutionX = game.Players.LocalPlayer:GetMouse().ViewSizeX * RenderScale
local ResolutionY = game.Players.LocalPlayer:GetMouse().ViewSizeY * 0.95 * RenderScale
local DegreeIncrement = Camera.Fov/ResolutionX/RenderScale
for Y = 1,ResolutionY*RenderScale,1 do
print(Y.." / "..ResolutionY*RenderScale)
wait()
for X = 1,ResolutionX*RenderScale,1 do
local ClosestPos = Camera.Pos+Vector3.new(Camera.Range,Camera.Range,Camera.Range)
local ClosestDist = Camera.Range
local Closest = nil
for o = 1,#Objects,1 do
if Objects[o].Type == "Sphere" then
result = SphereIntersect(Objects[o].Pos,Objects[o].Radius,Vector3.new(math.sin((Camera.Rot.Y+X*DegreeIncrement)*math.pi/180),math.tan((Camera.Rot.X+Y*DegreeIncrement)*math.pi/180),math.cos((Camera.Rot.Y+X*DegreeIncrement)*math.pi/180)))
end
if result ~= false then
if (Camera.Pos-result).Magnitude < ClosestDist then
ClosestPos = result
ClosestDist = (Camera.Pos-result).Magnitude
Closest = Objects[o]
end
end
end
if Closest ~= nil then
CreatePixel(X,Y,Closest.Color)
end
end
end
end
script.Parent.Render.MouseButton1Down:Connect(function()
print("Clearing screen")
script.Parent.Parent.Pixels:ClearAllChildren()
print("Cleared")
print("Rendering in 3 seconds")
wait(1)
print("Rendering in 2 seconds")
wait(1)
print("Rendering in 1 seconds")
wait(1)
print("RENDERING")
Render()
end)
script.Parent.Turn.MouseButton1Down:Connect(function()
Camera.Rot = Camera.Rot + Vector3.new(0,90,0)
end)
```

But it just makes the entire screen red and when it calculates the distance between the ray intersect point and camera it outputs 0 every time instead of displaying a sphere. I suspect there is something wrong with the SphereIntersect() math, but since it already took me an hour to find some math that somewhat understand how to implement, I have no idea how to debug this.