Skip to content

Instantly share code, notes, and snippets.

@IneonInoodle
Created January 20, 2020 10:19
Show Gist options
  • Save IneonInoodle/ca87a5f0ccd6a175491df283753b33b0 to your computer and use it in GitHub Desktop.
Save IneonInoodle/ca87a5f0ccd6a175491df283753b33b0 to your computer and use it in GitHub Desktop.
One Triangle Case
void UUnderWaterMeshGenerator::AddTrianglesOneAboveWater(TArray<FVertexData> vertexData)
{
//H is always at position 0
FVector H = vertexData[0].globalVertexPos;
//Left of H is M
//Right of H is L
//Find the index of M
int M_index = vertexData[0].index - 1;
if (M_index < 0)
{
M_index = 2;
}
//We also need the heights to water
float h_H = vertexData[0].distance;
float h_M = 0.0f;
float h_L = 0.0f;
FVector M = FVector::ZeroVector;
FVector L = FVector::ZeroVector;
//This means M is at position 1 in the List
if (vertexData[1].index == M_index)
{
M = vertexData[1].globalVertexPos;
L = vertexData[2].globalVertexPos;
h_M = vertexData[1].distance;
h_L = vertexData[2].distance;
}
else
{
M = vertexData[2].globalVertexPos;
L = vertexData[1].globalVertexPos;
h_M = vertexData[2].distance;
h_L = vertexData[1].distance;
}
//Now we can calculate where we should cut the triangle to form 2 new triangles
//because the resulting area will always form a square
//Point I_M
FVector MH = H - M;
float t_M = -h_M / (h_H - h_M);
FVector MI_M = t_M * MH;
FVector I_M = MI_M + M;
//Point I_L
FVector LH = H - L;
float t_L = -h_L / (h_H - h_L);
FVector LI_L = t_L * LH;
FVector I_L = LI_L + L;
//Save the data, such as normal, area, etc
//2 triangles below the water
//Save the triangle in reverse order (unreal counter clockwise for some dumb reason)
UnderWaterTriangleData.Add(FTriangleData(I_L, I_M, M));
UnderWaterTriangleData.Add(FTriangleData(L, I_L, M));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment