8 Connectivity Edge detection help plsls :C

So basically this script detects if a pixel is on the edge of a part by comparing the depth and surface normals to surrounding pixels and then maps it onto another grid using two tables as the y and x axis

It comes after a ray casting script that returns the table “Grid” and before a function that places frames

Currently just this part of the script runs for about 0.35 seconds detecting a 209 x 289 area of pixels (using a frame das why weird area)

Any help with optimizing it would be appreciated!

local EdgeMap = {} --Table for the next function to read
	
	for i = 1, #Grid do --Reads each row for detected part
		local EdgeRows = {}
		for iv = 1, #Grid[i] do -- Reads each coloumn
			
			local Detected = false
			
			if Grid[i][iv][1] ~= nil then -- Checking if the point is valid for the scan
				
				-- Variables for the point detected
				local Normal = Grid[i][iv][1]
				local Depth = Grid[i][iv][2]
				local Part = Grid[i][iv][3]
				
				local Count = 0 -- Starts counting nearby cells to check if it's an edge
				
				for uwu = 0, EdgeThickness * 2 + 1, 1 do -- Loops to detect a range of cells around it
					for owo = 0, EdgeThickness * 2 + 1, 1 do
						
						if Grid[i+uwu - EdgeThickness] then -- Checking if it's legal
							if Grid[i+ uwu - EdgeThickness][iv + owo - EdgeThickness] then -- Age verification
								if Grid[i+ uwu - EdgeThickness][iv + owo - EdgeThickness][1] ~= nil then -- Human verification
									
									-- Checks if the furries are not too similar to the current cell's stuff like it's surface normals
									if math.deg(math.acos(Normal:Dot(Grid[i+uwu - EdgeThickness][iv + owo - EdgeThickness][1]))) >= 90 or -- see surface normal cool
										Grid[i+uwu - EdgeThickness][iv + owo - EdgeThickness][2] - Depth >= 10 -- you're mom
									then
										Count = Count + 1
									elseif Grid[i+uwu - EdgeThickness][iv + owo - EdgeThickness][3] ~= Part and Grid[i+uwu - EdgeThickness][iv + owo - EdgeThickness][2] - Depth >= 5 then -- your*
										Count = Count + 2 -- specifically for edges so guarantees a pixel on the edge will get colored
									end
								end
							end
						end
					end
				end
				if Count >= 2 then -- If there are enough cells make it valid for shading
					Detected = true
				end
			end
			EdgeRows[#EdgeRows+1] = Detected -- Putting in data
		end
		EdgeMap[#EdgeMap+1] = EdgeRows
	end -- Close book arrivederci

uwu

1 Like