I have an Icosahedron subdivided and with LOD. Now i am trying to add a dynamic Material. The Problem is that i need the Normals for that.
I use Unreal Engine 4, when i use the build in Function to calculate Normals i get strange Artifacts in the Area between different LOD Levels.
So i made my own calculation with this Code:
TArray<FVector> normals;
normals.Init(FVector(-1.f, -1.f, -1.f), geoData.GeoData.Num());
int32 triangleCount = geoData.Triangles.Num() / 3;
for (int32 i = 0; i < triangleCount; i++)
{
int32 normalTriangleIndex = i * 3;
int32 triangleIndexA = geoData.Triangles[normalTriangleIndex];
int32 triangleIndexB = geoData.Triangles[normalTriangleIndex + 1];
int32 triangleIndexC = geoData.Triangles[normalTriangleIndex + 2];
FVector pointA = geoData.GeoData[triangleIndexA];
FVector pointB = geoData.GeoData[triangleIndexB];
FVector pointC = geoData.GeoData[triangleIndexC];
FVector sideAB = pointB - pointA;
FVector sideBC = pointC - pointB;
FVector nNormal;
nNormal = FVector::CrossProduct(sideAB, sideBC);
nNormal = nNormal / nNormal.Size();
nNormal = nNormal.GetSafeNormal();
normals[triangleIndexA] = -nNormal;
normals[triangleIndexB] = -nNormal;
normals[triangleIndexC] = -nNormal;
}
Seems the calculation is correct. when i skip the calculation of morphing in my shader those artifacts disappear... Maybe the Triangles are to close to each other while morphing them and this causes this artifacts?
This is how morphing looks like. (Source)
I solved it by skipping the first morph value. Now morphing starts from the 2nd picture as you can see on the screenshot.
float morphFac(float dist, int lev)
{
float low = input_distanceLUT[lev - 1];
float high = input_distanceLUT[lev];
float delta = high - low;
float a = (dist - low) / delta;
float morphRange = 0.5f;
return 1.0f - clamp(a / morphRange, 0.1f, 1.0f);
}
Just a tiny change in the shader. clamp(a / morphRange, 0.1f, 1.0f); 0.1f was earlyer 0.0f