I am working on a module that requires a live editablemesh. I have it generated and being manipulated, but it doesn’t update at all.
Generate:
function class:Generate()
local visual = self.MainMesh :: MeshPart
local res = self.Res
local plane = asset:CreateEditableMesh()
self.Plane = plane
local model = visual.Parent :: Model
local size = getSurfaceSize(visual,Enum.NormalId.Top)
local sizeX,sizeZ = size.X/res,size.Z/res
local amtX,amtZ = size.X/sizeX,size.Z/sizeZ
local start = -size/2
local vertices = {}
local grid = {}
for x=1,amtX do
grid[x] = {}
for z=1,amtZ do
local pos = Vector3.new(
x * sizeX,
0,
z * sizeZ
)
local worldPos = visual.Position + Vector3.new(0,0,0) + (start + pos)
local newPos = visual:GetClosestPointOnSurface(worldPos)
local Position = visual.CFrame:PointToObjectSpace(newPos)
--local Position = visual.CFrame:PointToObjectSpace(worldPos)
local vertex = plane:AddVertex(Position)
table.insert(vertices,vertex)
grid[x][z] = vertex
local att = Instance.new("Attachment",visual)
att.Position = Position
att.Visible = true
end
end
triangulateFromVertices(plane,grid)
local mesh = asset:CreateMeshPartAsync(Content.fromObject(plane))
self.Mesh = mesh
self.Visual:ApplyMesh(mesh)
self.Grid = grid
end
Update code:
function repositionAtSurfaceLevel(mesh: EditableMesh, plane: EditableMesh, visual: MeshPart, surfaceLevel: number, res: number, grid: {})
local model = visual.Parent :: Model
local size = getSurfaceSize(visual,Enum.NormalId.Top)
local sizeX,sizeZ = size.X/res,size.Z/res
local amtX,amtZ = size.X/sizeX,size.Z/sizeZ
local start = -size/2
for x,row in grid do
for z,id in row do
local pos = Vector3.new(
x * sizeX,
0,
z * sizeZ
)
local worldPos = visual.Position + Vector3.new(0,0,0) + (start + pos)
local newPos = visual:GetClosestPointOnSurface(worldPos)
local Position = visual.CFrame:PointToObjectSpace(newPos)
--local Position = visual.CFrame:PointToObjectSpace(worldPos)
mesh:SetPosition(id,Position)
local att = Instance.new("Attachment",visual)
att.Position = Position
att.Visible = true
end
end
end
function class:Update()
self.Generated = self.Grid ~= nil
if self.Generated then
local plane = self.Plane :: EditableMesh
local grid = self.Grid :: {}
local part = self.MainMesh :: MeshPart
local res = self.Res :: number
local container = self.Container :: EditableMesh
local visible = self.Visual :: MeshPart
local y = part.Size.Y/2
part:ClearAllChildren()
repositionAtSurfaceLevel(container,plane,part,math.lerp(-y,y,self.FillLevel),res,grid)
else
warn("Mesh not generated!")
end
end