MeshPart:ApplyMesh() not working correctly

When trying to update a MeshPart with a EditableMesh via ApplyMesh the result is not correct most of the time


in this video we can see that it does not work twice but on the third play it works

here is the project:
EditableMesh.rbxl (52.3 KB)

and this is the code used in the project

local AssetService = game:GetService("AssetService")
local RunService = game:GetService("RunService")

local options = {CollisionFidelity = Enum.CollisionFidelity.Hull, RenderFidelity = Enum.RenderFidelity.Precise, FluidFidelity = Enum.FluidFidelity.UseCollisionGeometry}

local mesh = AssetService:CreateEditableMesh()
local content = Content.fromObject(mesh)
local vertex1 = mesh:AddVertex(Vector3.new(0, 1, 0))
local vertex2 = mesh:AddVertex(Vector3.new(-2, -1, 0))
local vertex3 = mesh:AddVertex(Vector3.new(2, -1, 0))
local vertex4 = mesh:AddVertex(Vector3.new(0, -1, 2))
local face1 = mesh:AddTriangle(vertex1, vertex3, vertex2)
local face2 = mesh:AddTriangle(vertex1, vertex4, vertex3)
local face3 = mesh:AddTriangle(vertex1, vertex2, vertex4)
local face4 = mesh:AddTriangle(vertex2, vertex3, vertex4)
local normal1 = mesh:AddNormal()
local normal2 = mesh:AddNormal()
local normal3 = mesh:AddNormal()
local normal4 = mesh:AddNormal()
mesh:SetFaceNormals(face1, {normal1, normal1, normal1})
mesh:SetFaceNormals(face2, {normal2, normal2, normal2})
mesh:SetFaceNormals(face3, {normal3, normal3, normal3})
mesh:SetFaceNormals(face4, {normal4, normal4, normal4})

local part = AssetService:CreateMeshPartAsync(content, options)
part.Position = Vector3.new(0, 1, 0)
part.Parent = script.Parent

local attachment = Instance.new("Attachment")
attachment.Visible = true
attachment.Position = mesh:GetPosition(vertex1)
attachment.Parent = part

workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
workspace.CurrentCamera.CameraSubject = part

local timer = 0
while true do
	local s = 2 + math.sin(timer)
	local c = 2 + math.cos(timer)
	mesh:SetPosition(vertex1, Vector3.new(s, c, 0))
	attachment.Position = mesh:GetPosition(vertex1)
	part:ApplyMesh(AssetService:CreateMeshPartAsync(content, options))
	task.wait()
	timer += 0.1
end

if you comment out part:ApplyMesh(AssetService:CreateMeshPartAsync(content, options)) then it will work 100% of the time

1 Like

Hi, thanks for the report.

What’s going on is that there is an implicit scaling that happens when rendering MeshParts.

Each mesh asset has an intrinsic size, and each MeshPart has a size. These might not match, so the mesh is scaled in the scene.

For example, if you have a teapot mesh that was originally 2x2x2, then the MeshPart.MeshSize will be 2x2x2. But in the scene, you might want a larger or smaller teapot, or to scale it differently in x, y, and z. So you can set the MeshPart.Size to a different value.

In general, renderScale = part.Size / part.MeshSize

ApplyMesh does not change the part.Size, but if you call it with a mesh that has a different MeshSize, then the renderScale will change, resulting in a different visual size.

For EditableMesh scenarios, it can be helpful to ensure that the renderScale stays the same, by using code like this:

	local renderScale = part.Size / part.MeshSize 
	part:ApplyMesh(AssetService:CreateMeshPartAsync(content, options))
	part.Size = renderScale * part.MeshSize