How to implement positioning block based on orientation and normal in my grid placement system

I want to change variable “check” based on orientation and normal to get boolean, which will cause in positioning block as seen in the example below:



How could I achieve this?

Script:

local UIS = game:GetService("UserInputService")
local GS = game:GetService("GuiService")

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local meshPart = game:GetService("ReplicatedStorage").MeshPart

local ROTATION_Z = Vector3.new(0,0,90)
local ROTATION_X = Vector3.new(90,0,0)

local XYZ = {"X","Y","Z"}
local snapPos, inverse = {},{}

local gridSize = 4

local shadow = meshPart:Clone()
shadow.Size = Vector3.new(8,12,8)
shadow.Parent = workspace

local rayPerms = RaycastParams.new()
rayPerms.FilterDescendantsInstances = {shadow, player.Character}
rayPerms.FilterType = Enum.RaycastFilterType.Exclude

local function updatePos()
	shadow.Position = Vector3.new(unpack(snapPos))+shadow.CFrame:VectorToWorldSpace(shadow.Size):Abs()*0.5*Vector3.new(unpack(inverse))
end

UIS.InputChanged:Connect(function(input)
	if input.UserInputType ~= Enum.UserInputType.MouseMovement then return end
	--raycast
	local mousePos = input.Position
	local unitRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y + GS.TopbarInset.Height)
	local raycast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, rayPerms)

	if not raycast then return end
	local normal = raycast.Normal
	local point = raycast.Position + normal*0.01
	
	for i,dir in XYZ do
		local check = normal[dir] > -1
		inverse[i] = check and 1 or -1 --convert normal into 1 or -1
		snapPos[i] = (check and math.floor or math.ceil)(point[dir] / gridSize) * gridSize --snap
	end
	updatePos()
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		shadow.Orientation += ROTATION_Z
		updatePos()
	elseif input.KeyCode == Enum.KeyCode.T then
		shadow.Orientation += ROTATION_X
		updatePos()
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		local block = shadow:Clone()
		block.Transparency = 0
		block.CanCollide = true
		block.Parent = workspace
	end
end)