diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/OpenDrive/OpenDriveGenerator.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/OpenDrive/OpenDriveGenerator.cpp index a5ae4354e..8ee89021f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/OpenDrive/OpenDriveGenerator.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/OpenDrive/OpenDriveGenerator.cpp @@ -113,9 +113,9 @@ void AOpenDriveGenerator::GenerateRoadMesh() const auto Indexes = MeshData.GetIndexes(); TArray Triangles; TArray TriIndices; - FTriIndices Triangle; for (auto i = 0u; i < Indexes.size(); i += 3) { + FTriIndices Triangle; // "-1" since mesh indexes in Unreal starts from index 0. Triangles.Add(Indexes[i] - 1); // Since Unreal's coords are left handed, invert the last 2 indices. @@ -128,15 +128,35 @@ void AOpenDriveGenerator::GenerateRoadMesh() TriIndices.Add(Triangle); } + TArray Normals; + Normals.Init(FVector::UpVector, Vertices.Num()); + + for (const auto &Triangle : TriIndices) { + FVector Normal; + const FVector U = Vertices[Triangle.v1] - Vertices[Triangle.v0]; + const FVector V = Vertices[Triangle.v2] - Vertices[Triangle.v0]; + Normal.X = (U.Y * V.Z) - (U.Z * V.Y); + Normal.Y = (U.Z * V.X) - (U.X * V.Z); + Normal.Z = (U.X * V.Y) - (U.Y * V.X); + Normal = -Normal; + Normal = Normal.GetSafeNormal(.0001f); + if (Normal != FVector::ZeroVector) + { + Normals[Triangle.v0] = Normal; + Normals[Triangle.v1] = Normal; + Normals[Triangle.v2] = Normal; + } + } + RoadMesh->CreateMeshSection_LinearColor( 0, Vertices, Triangles, - TArray(), - TArray(), - TArray(), - TArray(), - true); + Normals, // Normals + TArray(), // UV0 + TArray(), // VertexColor + TArray(), // Tangents + true); // Create collision // Build collision data FTriMeshCollisionData CollisitonData;