I now tried to assign a simple material to my mesh, but somehow it isn’t rendered. If I check the size of Component->Materials inside the FPrimitiveSceneProxy* UGeneratedMeshComponent::CreateSceneProxy() function, it is 0, although I added the Material to the array before. This is the constructor of my Actor:
mesh = PCIP.CreateDefaultSubobject<UGeneratedMeshComponent>(this, TEXT("MeshComp"));
TArray<FGeneratedMeshTriangle> points;
FGeneratedMeshTriangle tri;
tri.Vertex0.Position = FVector(0, 0, 0);
tri.Vertex1.Position = FVector(100, 0, 100);
tri.Vertex2.Position = FVector(0, 0, 100);
//points.Add(tri);
tri.Vertex0.Position = FVector(100, 0, 100);
tri.Vertex1.Position = FVector(0, 0, 0);
tri.Vertex2.Position = FVector(100, 0, 0);
points.Add(tri);
mesh->SetGeneratedMeshTriangles(points);
static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/TestMaterial.TestMaterial'"));
UMaterial* Material = MatFinder.Object;
UMaterialInstanceDynamic* DynMat1 = UMaterialInstanceDynamic::Create(Material, this);
DynMat1->SetVectorParameterValue("Color", FLinearColor(1.f, 0.f, 0.f));
UE_LOG(LogTemp, Warning, TEXT("Terrain Before: %d"), mesh->Materials.Num());
mesh->SetMaterial(0, DynMat1);
if (DynMat1 == NULL)
{
UE_LOG(LogTemp, Warning, TEXT(" DMI NULL"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT(" DMI NOT NULL"));
}
UE_LOG(LogTemp, Warning, TEXT("Terrain: %d"), mesh->Materials.Num());
RootComponent = mesh;
Here the log says:
[]
LogTemp:Warning: Terrain Before: 0
LogTemp:Warning: DMI NOT NULL
LogTemp:Warning: Terrain: 1
[/]
And in the scene proxy:
UE_LOG(LogTemp, Warning, TEXT("GMC: %d"), Component->Materials.Num());
Material = Component->GetMaterial(0);
if (Material == NULL)
{
UE_LOG(LogTemp, Warning, TEXT("NULL"));
Material = UMaterial::GetDefaultMaterial(MD_Surface);
}
else UE_LOG(LogTemp, Warning, TEXT("NOT NULL"));
and it says:
[]
LogTemp:Warning: Ret: 0
LogTemp:Warning: Ret: 0
LogTemp:Warning: GMC: 0
LogTemp:Warning: NULL
LogTemp:Warning: Ret: 0
[/]
“Ret: 0” comes from here:
int32 UGeneratedMeshComponent::GetNumMaterials() const
{
UE_LOG(LogTemp, Warning, TEXT("Ret: %d"), Materials.Num());
return 1;
}
If I change this to “return Materials.Num()” the triangle isn’t rendered at all, since it then always returns 0…
Sorry for the much code, but do you have any idea how to fix this issue?
Thanks,
Taces