I have a sea mesh that I’m animating with Gerstner waves but doing these calculations every frame for every vertex causes a lot of lag, so I searched how to reduce it and found this post by @apenzijncoolenleuk1, where he explains you can just “spread” the calculations for every X frames and interpolate between them.
My result after trying to code it was this:
local seaBones = {}
local originPositions = {}
local nextPositions = {}
local lastPositions = {}
local frame = 0
local vertexPos
runService.Heartbeat:Connect(function()
frame += 1
if frame > 25 then -- (every 25 frames) do...
print("25")
frame = 1
for _, bone in pairs(seaBones) do
vertexPos = originPositions[bone]
vertexPos += -- Gerstner wave offset (one of this line for each wave)
lastPositions[bone] = bone.Position
nextPositions[bone] = vertexPos
end
end
if typeof(p) == "Vector3" then
for _, bone in pairs(seaBones) do
bone.Position = lastPositions[bone]:Lerp(nextPositions[bone], frame/25)
end
end
end)
It worked in optimizing my original code, there is zero to no lag now, however, when executing this code there is a main visual bug:
Notice that the middle of the lerps are perfectly smooth, they’re not snapping, however, when changing the target, where to lerp to, the nextPositions[bone]
(every 25 frames) the lerp freezes for some time, then goes back to lerping smoothly. (I don’t know if that’s the issue, it’s just a guess)
Before answering:
-
The main issue is the “freezing” of the vertices, which just happens sometimes, you can see if you pay attention to the movement of the mesh in the video
-
Tweens are not an option because as previously explained in the post I linked, they cause a lot of lag
-
I know that there are many other optimization techniques for sea meshes, I plan to implement them after I get this one working.
-
Please read the entire topic and watch the video entirely
-
Help is appreciated and if any additional information is needed please ask me