Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need help with RayTracing. how can i reflect rays?

Today i created a RayTracing System The Shader i made is not fully functional and it have some problems with it.

This is the image: https://ibb.co/TPJcT4t

shadow is working so good but if u imagine the sun by shadow u can see the object lights are bad

This is the code: if the code is important to see reply here and ask on reply text

Thanks

1 answer

local RS = game:GetService("RunService")
local L = game:GetService("Lighting")

local _CACHE = script.Parent:WaitForChild("_CACHE")

local _PIXEL_SIZE = 5
local _SKIP = 10
local _SHADOW_OPACITY = .5
local _SUN_BRIGHTNESS = .3
local _RAY_LENGTH = 500
local _SUN_DIRECTION = L:GetSunDirection()
local _VIEWPORT_SIZE = _CACHE.AbsoluteSize

local _CN
local _CP

function Render()
	_CN = nil
	_CP = nil
	
	for _, _V in pairs(_CACHE:GetChildren()) do
		_V:Destroy()
	end
	
	for _X = 0, _VIEWPORT_SIZE.X / _PIXEL_SIZE do
		for _Y = 0, _VIEWPORT_SIZE.Y / _PIXEL_SIZE do
			local _SCREEN_RAY = workspace.CurrentCamera:ScreenPointToRay(_X * _PIXEL_SIZE, _Y * _PIXEL_SIZE)
			
			local _RAY = Ray.new(_SCREEN_RAY.Origin, _SCREEN_RAY.Direction * _RAY_LENGTH)
			
			_CP = _SCREEN_RAY.Origin
			_CN = _SCREEN_RAY.Direction
			
			local _HIT, _POSITION, _NORMAL = workspace:FindPartOnRay(_RAY)
			
			local Frame = Instance.new("Frame", _CACHE)
			Frame.Size = UDim2.new(0, _PIXEL_SIZE, 0, _PIXEL_SIZE)
			Frame.Position = UDim2.new(0, _X * _PIXEL_SIZE, 0, _Y * _PIXEL_SIZE)
			Frame.BorderSizePixel = 0
			
			if _HIT then
				Frame.BackgroundColor3 = _HIT.Color
				
				local _RAY_SHADOW = Ray.new(_POSITION, _SUN_DIRECTION * _RAY_LENGTH)
				local _H_SHADOW, _POS_SHADOW, _NOR_SHADOW = workspace:FindPartOnRay(_RAY_SHADOW)
				
				if _H_SHADOW then
					Frame.BackgroundColor3 = Color3.new(Frame.BackgroundColor3.R - _SHADOW_OPACITY, Frame.BackgroundColor3.G - _SHADOW_OPACITY, Frame.BackgroundColor3.B - _SHADOW_OPACITY)
				else
					--Frame.BackgroundColor3 = Color3.new(Frame.BackgroundColor3.R+ _SUN_BRIGHTNESS, Frame.BackgroundColor3.G+ _SUN_BRIGHTNESS, Frame.BackgroundColor3.B + _SUN_BRIGHTNESS)
				end
				
				local OldPos = _CP
				_CP = _POSITION
				
				_CN = (_CN - (2 * _CN:Dot(_NORMAL) * _NORMAL))
				
				local _RAY_REFLECT = Ray.new(_CP, (_CN * (4 - (_POSITION - OldPos).Magnitude)) * _RAY_LENGTH)
				local _H, _POS, _NOR = workspace:FindPartOnRay(_RAY_REFLECT)
				
				if not _H then
					Frame.BackgroundColor3 = Color3.new(Frame.BackgroundColor3.R - .1, Frame.BackgroundColor3.G - .1, Frame.BackgroundColor3.B - .1)
				else
					if _H_SHADOW then
						Frame.BackgroundColor3 = Color3.new(Frame.BackgroundColor3.R - _SHADOW_OPACITY, Frame.BackgroundColor3.G - _SHADOW_OPACITY, Frame.BackgroundColor3.B - _SHADOW_OPACITY)
					else
						Frame.BackgroundColor3 = Color3.new(Frame.BackgroundColor3.R + .1, Frame.BackgroundColor3.G + .1, Frame.BackgroundColor3.B + .1)
					end
				end
			else
				Frame:Destroy()
			end
			
			local Chance = math.random(0,10)
			
			if Chance == 5 then
				wait()
			elseif Chance == 2 then
				wait()
			elseif Chance == 8 then
				wait()
			end
		end
		wait()
	end
end

workspace.blocker.ClickDetector.MouseClick:Connect(function()
	Render()
end)

--[[workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
	Render()
end)]]

1 comment

0

bruh

This discussion is preserved as it was. New answers, comments, and votes are closed.