Lag Spikes from Mesh Vertex Updates in Ocean Simulation

Hi everyone

I’m working on a custom ocean system that updates mesh vertices every frame based on wave data The mesh is large with many vertices and I update vertex positions dynamically using editableMesh:SetPosition


Here’s a simplified snippet of my update loop:

local positions = table.create(batchSize)
local currentBatch = 1

RunService.Heartbeat:ConnectParallel(function(dt)
	currentTime += dt
	if currentTime - lastUpdate >= updateRate then
		lastUpdate = currentTime

		local ti = tick()
		local offset2 = (currentBatch - 1) * batchSize * bytesPerVertex

		task.desynchronize()

		for i = 1, batchSize do
			
			local t = TableVertex[i]
			local x, z, face1, face2, color = t[1], t[2], t[3], t[4], t[5]
						
			local transform = data:Sample(ti, Vector2.new(x, z))
			positions[i] = Vector3.new(x + transform.X, transform.Y, z + transform.Z)

			offset2 += bytesPerVertex
		end

		task.synchronize()

		for i = 1, batchSize do
			editableMesh:SetPosition(vertices[i], positions[i])
		end

	end
end)

image


What I’ve tried so far:

  • Running this code on Heartbeat:ConnectParallel
  • Using task.desynchronize() / task.synchronize() to try and spread workload
  • Throttling updates by increasing updateRate

My questions are:

  • How do you recommend optimizing or spreading this workload to reduce microprofile spikes?
  • Is it normal to see noticeable lag when updating large numbers of vertices with SetPosition?
  • Would breaking the updates into smaller batches and spreading them across multiple frames be the right approach? If yes, any suggestions on how best to implement that?
  • Any general best practices for handling dynamic mesh updates at this scale in Roblox?

Thanks in advance!

i found the way :relaxed:
it’s wasn’t that hard It was the reason for my own system of equations Movement ocean