You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Make a Raytracing engine following Sebastian Lague Unity’s Raytracer engine logic (https://www.youtube.com/watch?v=Qz0KTGYJtUk)
-
What is the issue? I cant see anything
-
What solutions have you tried so far? ive tried searching on the dev hub for any solutions to this but to no avail
Wierd purple and White pixels appear on my “Screen” which in realty is just a grid of frames
Screen drawer
local ResY = 100
local ResX = 100
local Camera = workspace.CurrentCamera
local Screen = script.Parent
local FakeScreen = Screen:WaitForChild("FakeScreen")
local Tracer = require(script.Tracer)
local PixelArray = {}
local RaycastParam = RaycastParams.new()
RaycastParam.FilterDescendantsInstances = {}
RaycastParam.FilterType = Enum.RaycastFilterType.Exclude
for Y = 0, ResY do
local Column = Instance.new("Frame", FakeScreen)
Column.Name = Y
Column.Size = UDim2.new(1,0,1/ResY,0)
Column.Position = UDim2.new(0,0,Y/ResY,0)
Column.Transparency = 1
Column.BorderSizePixel = 0
PixelArray[Y] = {}
for X = 0, ResX do
local Pixel = Instance.new("Frame", Column)
Pixel.Name = X .. "_" .. Y
Pixel.Size = UDim2.new(1/ResX,0,1,0)
Pixel.Position = UDim2.new(X/ResX,0,0,0)
Pixel.BorderSizePixel = 0
Pixel.Transparency = 0
PixelArray[Y][X] = Pixel
end
end
function Frame()
for _, Y in pairs(PixelArray) do
for z, Pixel in pairs(Y) do
Pixel.BackgroundColor3 = Tracer.Trace(Pixel)
end
end
task.wait()
Frame()
end
Frame()
Tracer Module
local Tracer = {}
local RaycastParam = RaycastParams.new()
RaycastParam.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
RaycastParam.FilterType = Enum.RaycastFilterType.Exclude
local Bounces = 5
local Samples = 5 -- Not in use for now
local function RandomDirection(RNG: number) --, normal: Vector3)
local Randomf = Random.new(RNG)
local Dir = Vector3.new(Randomf:NextNumber(), Randomf:NextNumber(), Randomf:NextNumber())
return Dir --* math.sign(normal:Dot(Dir))
end
function Tracer.Trace(Pixel)
local InLight = Vector3.new(0,0,0)
local Color = Vector3.new(255,255,255)
local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(Pixel.AbsolutePosition.X, Pixel.AbsolutePosition.Y)
local directionVector = screenToWorldRay.Direction * 5000
local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector,RaycastParam)
local OriginRay = workspace.CurrentCamera.CFrame.Position
if raycastResult then
for i = 1, Bounces do
local RayInstance = raycastResult.Instance
local RayDir = (raycastResult.Position - OriginRay).Unit
local DiffusseDir = (raycastResult.Normal + RandomDirection(Pixel.AbsolutePosition.X * 2 + Pixel.AbsolutePosition.Y * 3)).Unit
local ReflectDir = RayDir - (2 * RayDir:Dot(raycastResult.Normal) * raycastResult.Normal)
RayDir = DiffusseDir:Lerp(ReflectDir, RayInstance.Smoothness.Value)
raycastResult = workspace:Raycast(raycastResult.Position, RayDir, RaycastParam)
if raycastResult then
RayInstance = raycastResult.Instance
local EmittedLight = Vector3.new(RayInstance.EmissionColor.Value.R * 255 * RayInstance.EmissionStrenght.Value,RayInstance.EmissionColor.Value.G * 255 * RayInstance.EmissionStrenght.Value,RayInstance.EmissionColor.Value.B * 255 * RayInstance.EmissionStrenght.Value)
InLight += EmittedLight * Color
Color *= Vector3.new(RayInstance.Color.R * 255, RayInstance.Color.G * 255, RayInstance.Color.B * 255)
else
Color = Vector3.new(0,0,0)
break
end
print(InLight)
end
end
return Color3.fromRGB(InLight.X, InLight.Y, InLight.Z)
end
return Tracer
The green ball is supossed to be the light source but only white and purple Pixels appear