Greedy mesher not working in 2d, only 1d

Im trying to make a greedy mesher that merges floor tiles.

The issue is that it isnt working, i got no idea why but it isnt working.

This is what im using to test it on

And this is the result

Each colored section is a different separated chunk. As you see its just working in 1d lines for some reason. And i don’t know why

This is the code. Im putting it like this as to not take up the entire page by default
function module.MergeParts(Parts, PartSize)
	local SortedParts = {}
	
	local MinX = math.huge
	local MinZ = math.huge
	
	local MaxX = -math.huge
	local MaxZ = -math.huge
	
	for i,v in pairs(Parts) do
		SortedParts[v.Position.X] = SortedParts[v.Position.X] or {}
		SortedParts[v.Position.X][v.Position.Z]=v
		
		MinX = math.min(MinX,v.Position.X)
		MinZ = math.min(MinZ,v.Position.Z)
		
		MaxX = math.max(MaxX,v.Position.X)
		MaxZ = math.max(MaxZ,v.Position.Z)
		
		v.s.t.Text = ""
	end	
	
	local Chunks = {}
	
	local CurrentParts = {}
	
	local OriginalLineChange = nil
	
	for x = MinX,MaxX,PartSize do -- the issue seems to be here inside of this nested for loop
		for z = MinZ,MaxZ,PartSize do
			if (not SortedParts[x][z] or (OriginalLineChange and z == OriginalLineChange.Z)) then	
				
				if not OriginalLineChange then--OriginalLineChange is my way of making chunks, if a line ends, it saves the z position it ended at and goes to a new line. If that line then also ends at the same z it skips to the next line, and so on until it meets a line that ends earlier. And at that point it saves all the previous lines as 1 chunk.
				
					OriginalLineChange = Vector3.new(x,0,z)
					if SortedParts[x][z-PartSize] then
						SortedParts[x][z-PartSize].s.t.Text = "NewLine"
					end				
				else
					if z == OriginalLineChange.Z then -- the new line ends at the same z as the previous line did, so it can just skip to the next line.

						break
					else 
						local Removed = {}
						for i,v in pairs(CurrentParts) do -- here i remove any parts on this new line because it met a problem on that line
							if v.Position.X == x then
								Removed[#Removed+1]=v 			
								CurrentParts[i]=nil
							end
						end
						if #Removed > 0 then
							Removed[#Removed].s.t.Text = "rChunkEnd, "..#Removed
							Chunks[#Chunks+1]=Removed
						end	
						OriginalLineChange = Vector3.new(x,0,z)
					end
				end	
				
		
				
				if CurrentParts[#CurrentParts] and CurrentParts[#CurrentParts].s.t.Text == "" then
					CurrentParts[#CurrentParts].s.t.Text = "ChunkEnd"
				end	
				
				Chunks[#Chunks+1]=CurrentParts
				CurrentParts = {}
			else
				CurrentParts[#CurrentParts+1]=SortedParts[x][z]
			end
		end
	end
	
	Chunks[#Chunks+1]=CurrentParts
	

	
	for i,v in pairs(Chunks) do
		for n,v in pairs(v) do
			v.Color = Color3.fromRGB(i*10, 200-(i*20), (255-i*5)) -- giving a unique color to each chunk
		end
	end
end

I’ve tried searching everywhere on the forums, reading basically everything i can find about greedy meshers and following tutorials like this one. None of them has worked for me.

So any help would be very useful

3 Likes