Plane triangulation editablemesh bug

I have the following code to try and triangulate a grid:

for _, vertice in vertexes do
    if vertice % 2 <= 0 then continue end

    local bPoint = vertice + 1
    local cPoint = (TILE_SIZE / TILE_RESOLUTION) + 1

    EditableMesh:AddTriangle(vertice, bPoint, cPoint)
 end

Where b is the next point, and c is the point diagonal to (vertice or a).
It skips over every 2nd vertice to avoid duplicate triangles from being created

The grid is setup like the following:

In theory this should work fine and create a half filled plane, yet, when ran, it produces this abomination:

If anyone could help me I would be infinitely grateful :blush:

for x = 1,#grid do
		for y = 1,#grid[x] do
			if x == #grid then continue end
			if y == #grid[x] then continue end

			-- First triangle
			local vertex = grid[x][y]
			local vertex2 = grid[x+1][y]
			local vertex3 = grid[x+1][y+1]
			mesh:AddTriangle(vertex, vertex3, vertex2)
			
			

			-- Second triangle

			vertex2 = grid[x][y+1]
			mesh:AddTriangle(vertex3, vertex, vertex2)

		end
	end

My old script might help you, the grid is a 2d array that contains the vertices

1 Like

I tried rewriting this in a multitude of ways yet no triangles are produced

What I found is that the triangles from any set points (i.e: 1, 6, 5) produce a triangle exceeding the meshpart like so:

Here’s my code (maybe I messed up vertex placement?) for anyone else wondering

function OceanController:GenerateGrid()
    local grid = {}

    for x = 1, GRID_SIZE, TILE_SIZE do
        for y = 1, GRID_SIZE, TILE_SIZE do
            local cframe = CFrame.new(x, OCEAN_HEIGHT, y)

            local MeshPart = Instance.new("MeshPart")
            MeshPart.Parent = TILE_FOLDER
            MeshPart.CFrame = cframe
            MeshPart.Size = Vector3.new(TILE_SIZE, 0.1, TILE_SIZE)
            MeshPart.Anchored = true

            local EditableMesh = Instance.new("EditableMesh")
            EditableMesh.Parent = MeshPart

            table.insert(grid, {
                MeshPart = MeshPart,
                EditableMesh = EditableMesh
            })

            local vertexes = {}

            for vertexX = 1, TILE_SIZE, TILE_SIZE / TILE_RESOLUTION do
                vertexes[vertexX] = {}

                for vertexY = 1, TILE_SIZE, TILE_SIZE / TILE_RESOLUTION do
                    local relativeCFrame = CFrame.new(vertexX, 0, vertexY)
                    local worldPosition = MeshPart.CFrame:ToWorldSpace(relativeCFrame)
                    local vertex = EditableMesh:AddVertex(relativeCFrame.Position)

                    Gizmo:DrawArrow(worldPosition.Position, worldPosition.Position + Vector3.yAxis * 3, Color3.new(1, 0, 0))
                    vertexes[vertexX][vertexY] = vertex
                end
            end

            EditableMesh:AddTriangle(1, 6, 5)
        end
    end

    return grid
end