How to create a cube using EditableMesh?

Sooo the EditableMesh API got updated so much I literally have no idea how to use it. I want to start from scratch again, but I don’t even know how to create a cube, let alone a triangle using editablemesh smh.

Can someone provide some sample code that can make one? I’d like to go from there

I also replied to your question on the announcement post, but wanted to reply here as well in case others find this question.

Here’s a very simple script that creates an EditableMesh cube with sharp edges.

-- LocalScript
local AssetService = game:GetService("AssetService")

-- Given 4 vertex ids, adds a new normal and 2 triangles, making a sharp quad
local function addSharpQuad(emesh, vid0, vid1, vid2, vid3)
	-- AddTriangle creates a merged normal per vertex by default.
	-- For the sharp cube, we override the default normals with 
	-- 6 normals - a new normal to use for each side of the cube
	local nid = emesh:AddNormal() 

	local fid1 = emesh:AddTriangle(vid0, vid1, vid2)
	emesh:SetFaceNormals(fid1, {nid, nid, nid})

	local fid2 = emesh:AddTriangle(vid0, vid2, vid3)
	emesh:SetFaceNormals(fid2, {nid, nid, nid})
end

-- Makes a cube with creased edges between the sides by using normal ids
local function makeSharpCube_splitNormals()
	local emesh = AssetService:CreateEditableMesh() -- creates an empty EditableMesh of unbounded size 

	local v1 = emesh:AddVertex(Vector3.new(0, 0, 0))
	local v2 = emesh:AddVertex(Vector3.new(1, 0, 0))
	local v3 = emesh:AddVertex(Vector3.new(0, 1, 0))
	local v4 = emesh:AddVertex(Vector3.new(1, 1, 0))
	local v5 = emesh:AddVertex(Vector3.new(0, 0, 1))
	local v6 = emesh:AddVertex(Vector3.new(1, 0, 1))
	local v7 = emesh:AddVertex(Vector3.new(0, 1, 1))
	local v8 = emesh:AddVertex(Vector3.new(1, 1, 1))

	addSharpQuad(emesh, v5, v6, v8, v7) -- front
	addSharpQuad(emesh, v1, v3, v4, v2) -- back
	addSharpQuad(emesh, v1, v5, v7, v3) -- left
	addSharpQuad(emesh, v2, v4, v8, v6) -- right
	addSharpQuad(emesh, v1, v2, v6, v5) -- bottom
	addSharpQuad(emesh, v3, v7, v8, v4) -- top

	-- Because we override all of the default normals, we can remove them
	emesh:RemoveUnused()
	return emesh
end

-- Create the cube and preview MeshPart
local emesh = makeSharpCube_splitNormals()
local previewMesh = AssetService:CreateMeshPartAsync(Content.fromObject(emesh))
previewMesh.Anchored = true
previewMesh.Position = Vector3.new(0, 5, 0)
previewMesh.Size = Vector3.new(5,5,5)
-- Add the preview MeshPart to workspace
previewMesh.Parent = workspace
2 Likes