How to use EditableMesh?

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)
	local nid = eMesh:AddNormal()  -- This creates a normal ID which is automatically computed

	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 6 sides
local function makeSharpCube()
	local eMesh = AssetService:CreateEditableMesh()

	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

	eMesh:RemoveUnused()
	return eMesh
end

code from the documentation.

As I understood, to use I have to write

game.Workspace.MeshPart.MeshContent = makeSharpCube():GetContext()

But I getting error GetContext is not a valid member of EditableMesh

By the way, while I was trying to figure out how to work with it, I came across a bug. Rather, the error did not correspond to reality.

game.Workspace.MeshPart = makeSharpCube() - error
MeshPart is not a valid member of Workspace "Workspace" I am more than sure that the error must be “different”.

image
Yes, its a server script and yes, meshpart.Parent = Workspace

GetContext isn’t a thing. I don’t remember ever seeing that anywhere.
image

That’s not how it works. The engine thinks you are trying to set a property called MeshPart which isn’t a thing. You are misusing the assignment statement…


I get that the documentation for the new EditableMesh/Image can be rather vague and sparse, I had issues with it too at first. It’s just what happens when you try to use a brand spanking new feature that no one have experience with.

To save as much time as possible, here’s my working example that you can build off from:

--!strict

local ast = game:GetService('AssetService')

local emesh = ast:CreateEditableMesh() --create a new EditableMesh

--for this example, let's make a "wavy" mesh.
--first, we need to figure out the vertices of the mesh.

local points: {[Vector3]: number} = {}
for x = -10, 10 do
    for z = -10, 10 do
        local pos: Vector3 = Vector3.new(x, 0, z)
        --this is essentially a 3D graphing equation.
        --after creating the vertex, we store its ID in a hashmap for future use.
        points[pos] = emesh:AddVertex(pos + Vector3.yAxis*math.cos(x)*math.sin(z))
    end
end

--after making the vertices, we need to connect them to form the triangles.
--we will iterate the points we stored and access their neighbors.
--they won't have neighbors if they are on the edge of the mesh.
for p, id in points do
    local _10: number? = points[p+Vector3.xAxis]
    local _01: number? = points[p+Vector3.zAxis]
    local _11: number? = points[p+Vector3.xAxis+Vector3.zAxis]
    if _10 and _01 and _11 then --only make the triangle if it has all 3 neighbors
        --the order matters; the vertices need to be connected counter-clockwise for the top face to be visible
        emesh:AddTriangle(id, _01, _11)
        emesh:AddTriangle(id, _11, _10)
    end
end

--finally, we can turn the EditableMesh into a Content object which is then
--used as a blueprint for creating a brand new meshpart

local mp = ast:CreateMeshPartAsync(Content.fromObject(emesh))
mp.Anchored = true
mp.CFrame = CFrame.new(0, 10, 0)
mp.Parent = workspace

P.S. There are some similarities with using EditableImages, but the way you actually use it is different:


image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.