Hello,
I’ve been trying to make a mesh system and now I want to make the mesh be able to display the lines connecting the vertices. Basically like a wireframe render or for example like in Blender when viewing the lines of an mesh. Also, I want to connect the lines so that they form triangles so I can later on create the surfaces from those triangles. But I don’t know how I would go about doing that, for now I’ve just been comparing axes and creating lines to the vertices laying where the position is further than the original vertex. For better visualization of what I’m trying to do I attached two screenshots.
Module:
-- @ Mesh
-- // Variables
-- # Constants
local debris = game:GetService("Debris")
-- # Modules
local types = require(script:WaitForChild("Types"))
-- # Tables
local mesh = {}
local vertices = {}
local connections = {}
-- // Functions
mesh.__index = mesh
vertices.__index = vertices
connections.__index = connections
local function removeDuplicates(parts: {})
for _, part in pairs(parts) do
for _, comparePart in pairs(parts) do
if part ~= comparePart and part.Position == comparePart.Position and part.Size == comparePart.Size then
debris:AddItem(part, 0)
end
end
end
end
function mesh.new(center: Vector3, vertices: {})
local data = {}
setmetatable(data, mesh)
data.Vertices = {}
for vertex, offset in pairs(vertices) do
local vertexPosition = center + offset
data.Vertices[vertex] = offset
end
return data
end
function mesh:connections()
local data = {}
setmetatable(data, connections)
data.Parts = {}
local vertices = self.Vertices
local function newConnection(rootVertexPosition: Vector3, endVertexPosition: Vector3)
local connection = Instance.new("Part", game.Workspace)
-- | Customizing
-- # Vectors
connection.CFrame = CFrame.lookAt(rootVertexPosition:Lerp(endVertexPosition, 0.5), endVertexPosition)
connection.Size = types.Connection.Size + Vector3.new(0, 0, (rootVertexPosition - endVertexPosition).Magnitude)
-- # Colors
connection.Material = types.Connection.Material
-- # Enums
connection.BrickColor = types.Connection.BrickColor
-- # Booleans
connection.Anchored = true
-- | Organizing
table.insert(data.Parts, connection)
-- | Returning
return connection
end
local function compareVerticesAxis(axis: string)
for _, vertex in pairs(vertices) do
for _, compareVertex in pairs(vertices) do
if vertex ~= compareVertex and vertex[axis] > compareVertex[axis] then
newConnection(vertex, compareVertex)
end
end
end
end
local function compareVerticesAxes(axis_A: string, axis_B: string)
for _, vertex in pairs(vertices) do
for _, compareVertex in pairs(vertices) do
if vertex ~= compareVertex and vertex[axis_A] > compareVertex[axis_A] and vertex[axis_B] > compareVertex[axis_B] then
newConnection(vertex, compareVertex)
end
end
end
end
local function removeConnectionsInShape(parts: {}, center: Vector3, size: Vector3)
end
-- | Connect all upper vertecies with the bottom vertices ( Compare X Values )
compareVerticesAxis("X")
-- | Connect all right vertices with the left vertices ( Compare Y Values )
compareVerticesAxis("Y")
-- | Connect all front vertices with the back vertices ( Compare Z Values )
compareVerticesAxis("Z")
-- | Cleanup ( Remove duplicates )
removeDuplicates(data.Parts)
-- | Connect all upper front vertices with the bottom back vertices ( Compare Y, Z Values )
compareVerticesAxes("Y", "Z")
-- | Connect all upper right vertices with the bottom left vertices ( Compare Y, X Values )
compareVerticesAxes("Y", "X")
-- | Connect all right front vertices with the left back vertices ( Compare X, Z Values )
compareVerticesAxes("X", "Z")
-- | Cleanup ( Remove lines inside mesh )
return data
end
function mesh:vertices()
local data = {}
setmetatable(data, vertices)
data.Parts = {}
for _, vertexPosition in pairs(self.Vertices) do
local vertex = Instance.new("Part", game.Workspace)
-- | Customizing
-- # Vectors
vertex.Position = vertexPosition
vertex.Size = types.Vertex.Size
-- # Colors
vertex.BrickColor = types.Vertex.BrickColor
-- # Enums
vertex.Material = types.Vertex.Material
-- # Booleans
vertex.Anchored = true
-- | Organizing
table.insert(data.Parts, vertex)
end
return data
end
return mesh
I appreciate any help!