I’m making an algorithm that makes an ocean mesh by looking at what points in the grid are under or at the ocean level and creating the contour based on the marching squares algorithm. The problem is that there are holes on concave curves (on convex curves the ocean mesh just goes inside the land mesh, which is fine). This is the code for getting the position of purple vertices from the image:
Vector2 GetLerpedEdgePoint(CellPoint cellPoint, Vector2 gridPos)
{
int corner1Index = cellPoint.AdditionalPos.Corner1Offset;
int corner2Index = cellPoint.AdditionalPos.Corner2Offset;
//get oceanVertData of corner to use the "DistanceToZero" value
OceanVertData Corner1VertData = _corners[corner1Index];
OceanVertData Corner2VertData = _corners[corner2Index];
//lerp between these positions to get the edge point
Vector2 corner1Pos = _gridPosOffsets[corner1Index] + gridPos;
Vector2 corner2Pos = _gridPosOffsets[corner2Index] + gridPos;
//ocean level is interpreted as 0,
//but for the interpolation formula to work, I need to interpret it as 1,
//so I just add one to a and b
float a = Corner1VertData.DistanceToOceanLevel + 1;
float b = Corner2VertData.DistanceToOceanLevel + 1;
return LerpCloseToOne(a , b, corner1Pos, corner2Pos);
}
Vector2 LerpCloseToOne(float valueA, float valueB, Vector2 a, Vector2 b)
{
return Vector2.Lerp(a, b, (1 - valueA) / (valueB - valueA));
}
What should I do to solve this problem? The easy thing is to just increase the ocean level but that feels like a band aid. I could also try to somehow make the edge points be part of the tangent that touches the land curve. What do you guys suggest?
Here’s the link to the GitHub repository, most of the relevant code is in the OceanFace class:
link