How to create simple editable mesh? After new update Instance.new(‘EditableMesh’) was removed how to do this now?
EditableMesh are the Object class now, so you cannot create it using the Instance.new() constructor. The new API requires you to use AssetService:CreateEditableMesh()
or AssetService:CreateEditableMeshAsync()
. To render them, you have to use the new constructor Content.fromObject()
, which takes in an EditableMesh or EditableImage (currently) and returns Content, which can be used in tandem with AssetService:CreateMeshPartAsync()
.
Heres a code example:
local AssetService = game:GetService("AssetService")
local editableMesh = AssetService:CreateEditableMesh() --Returns an empty EditableMesh object
--Create 3 vertices using EditableMesh:AddVertex()
local vId0 = editableMesh:AddVertex(Vector3.new(0, 1, 1))
local vId1 = editableMesh:AddVertex(Vector3.new(-1, 0, 0))
local vId2 = editableMesh:AddVertex(Vector3.new(0, 0, 0))
--Create a triangle from the vertexIds
local tri = editableMesh:AddTriangle(vId0, vId1, vId2)
local meshContent = Content.fromObject(editableMesh) --Returns the Content of the EditableMesh
local mesh = AssetService:CreateMeshPartAsync(meshContent, nil) --Use meshContent to create a new MeshPart from the EditableMesh with default options
mesh.Parent = workspace
Note that with AssetService:CreateMeshPartAsync() you can use other Content, such as from a URI string, using Content.fromURI()
. The second parameter of AssetService:CreateMeshPartAsync() takes in a Dictionary of options regarding the precision of the mesh.
Content | Documentation
AssetService | Documentation
EditableMesh | Documentation
Asset URI | Documentation
Thank you. But there is another problem. Why meshpart is always cube no matter what vertex size are used?
If you are referring to the bounding box of the MeshPart, EditableMesh allows vertices and triangles to exist outside of the MeshPart’s extents, regardless of the meshes size. The vertices and triangles will still originate from the MeshPart’s CFrame, but it seems to cause bugs with culling (either frustum or occlusion) making triangles sometimes not propertly render on MeshPart’s whos vertices and/or triangles are outside the MeshPart’s bounding box
Basically, whenever you change the mesh content (by editing EditableMesh), you have to calculate the minimum region for all vertices, by looping through all of them, and incrementally updating the max range in all 3 axes.
local boxSize = Vector3.zero
for vertexIndex, vertexId in editableMesh:GetVertices() do
boxSize = boxSize:Max(editableMesh:GetPosition(vertexId))
end
meshPart.Size = boxSize --Minimum size required for all vertices to remain in the MeshPart's bounding box
Roblox says they’ll add a method for this soon (calculating the minimum size), so hopefully they do
still block is visible instead of mesh
Hmm. Perhaps try the alternative method using MeshPart:ApplyMesh()
local myMeshPart = Instance.new("MeshPart", workspace)
local newMeshPart = AssetService:CreateMeshPartAsync(editableMeshContent)
myMeshPart:ApplyMesh(newMeshPart)
Still not working. Assigning CreateMeshpartAsync removing collisions also.
local AssetService = game:GetService("AssetService")
local editableMesh = AssetService:CreateEditableMesh() --Returns an empty EditableMesh object
--Create 3 vertices using EditableMesh:AddVertex()
local vId0 = editableMesh:AddVertex(Vector3.new(11, 0, 10))
local vId1 = editableMesh:AddVertex(Vector3.new(-11, 0, -10))
local vId2 = editableMesh:AddVertex(Vector3.new(11, 0, 0))
--Create a triangle from the vertexIds
local tri = editableMesh:AddTriangle(vId0, vId1, vId2)
local meshContent = Content.fromObject(editableMesh) --Returns the Content of the EditableMesh
--Use meshContent to create a new MeshPart from the EditableMesh with default options
local mesh = AssetService:CreateMeshPartAsync(meshContent)
mesh.Anchored=true
mesh.Parent = workspace
local boxSize = Vector3.zero
for vertexIndex, vertexId in editableMesh:GetVertices() do
boxSize = boxSize:Max(editableMesh:GetPosition(vertexId))
end
mesh.Size = boxSize
Hmm. Im not sure why it doesn’t render properly. Does anything appear in the output window?
Nope. Everything is clear. Does it works to you?
Yeah. With a bit of buggy rendering, but the selector seems to detect the collision properly.
You might want to put in a bug report, if this keeps continuing.