Hi there, I’m coming from unity so it’s possible that I am simply missing something simple, as a checkbox.
In my setup I have 4 materials, applied to three meshes. I create dynamic instances, and assign color. Two of those material instances, both belonging to mesh A are behaving consistently for client and server. Two others, belonging to mesh B and mesh C have a variety of strange behavior:
I am unable to assign them properly, after creation using :
BaseMaterial = BipedBody->GetMaterial(0);
BaseMaterialInstance = UMaterialInstanceDynamic::Create(BaseMaterial, this);
BipedBody->SetMaterial(0, BaseMaterialInstance);
the parent material in created material instance is null. If I create them using:
BaseMaterialInstance = BipedBody->CreateAndSetMaterialInstanceDynamicFromMaterial(0, BipedBody->GetMaterial(0));
They are fine, for the local player, and If I create them with:
BipedBody->SetMaterial(0, nullptr);
BaseMaterial = BipedBody->GetMaterial(0);
BaseMaterialInstance = UMaterialInstanceDynamic::Create(BaseMaterial, this);
BipedBody->SetMaterial(0, BaseMaterialInstance);
They are also fine.
Also, I replicate the colors for all of these materials. I do not think my replication logic is wrong, as I am able to get it to work correctly on the two materials from mesh A, but the two problematic, from mesh B and C, are just very strange - they are correct on Listen Server, consistent with clients, client A only sees itself in the correct color, and client B only sees itself and client A in correct color, but not the listen server. However, the mesh A have all correct colors assigned on all clients and server.
Since material shows the correct color always in some case, I don’t think it’s the fault of the material setup, and since the code is the same for all, I don’t think it’s a code issue? That would leave the mesh, but that seems strange?
Full Implementation, and I call GenerateRandomColors in BeginPlay:
void ABipedPawn::GenerateRandomColors()
{
if (!IsLocallyControlled())return;
ServerGenerateRandomColors();
}
void ABipedPawn::ServerGenerateRandomColors_Implementation()
{
float r = FMath::RandRange(0, 100) / 100.f;
float g = FMath::RandRange(0, 100) / 100.f;
float b = FMath::RandRange(0, 100) / 100.f;
float Hr = FMath::RandRange(0, 100) / 100.f;
float Hg = FMath::RandRange(0, 100) / 100.f;
float Hb = b * 1.3f;
ReplicatedColors.Main = FLinearColor(r, g, b);
ReplicatedColors.Highlight = FLinearColor(Hr, Hg, Hb);
CreateMaterialInstances();
ApplyColors();
}
void ABipedPawn::OnRep_ColorChange()
{
CreateMaterialInstances();
if (BaseFeatherMaterialInstance == nullptr || BaseMaterialInstance == nullptr || WingPlaneMaterialInstance == nullptr || WingFeatherMaterialInstance == nullptr)
{
return;
}
ApplyColors();
}
void ABipedPawn::ApplyColors()
{
FVector4 HCol = FVector4(ReplicatedColors.Highlight.R, ReplicatedColors.Highlight.G, ReplicatedColors.Highlight.B, 1.0f);
BaseFeatherMaterialInstance->SetVectorParameterValue(FName("HighlightColor"), HCol);
BaseMaterialInstance->SetVectorParameterValue(FName("HighlightColor"), HCol);
WingPlaneMaterialInstance->SetVectorParameterValue(FName("HighlightColor"), HCol);
WingFeatherMaterialInstance->SetVectorParameterValue(FName("HighlightColor"), HCol);
FVector4 MCol = FVector4(ReplicatedColors.Main.R, ReplicatedColors.Main.G, ReplicatedColors.Main.B, 1.0f);
BaseFeatherMaterialInstance->SetVectorParameterValue(FName("BaseColor"), MCol);
BaseMaterialInstance->SetVectorParameterValue(FName("BaseColor"), MCol);
WingPlaneMaterialInstance->SetVectorParameterValue(FName("BaseColor"), MCol);
WingFeatherMaterialInstance->SetVectorParameterValue(FName("BaseColor"), MCol);
}
void ABipedPawn::CreateMaterialInstances()
{
if (WingFeatherMaterialInstance == nullptr)
{
WingFeatherMaterialInstance = BipedWings->CreateAndSetMaterialInstanceDynamicFromMaterial(0, BipedWings->GetMaterial(0));
}
if (WingPlaneMaterialInstance == nullptr)
{
WingPlaneMaterialInstance = BipedWings->CreateAndSetMaterialInstanceDynamicFromMaterial(1, BipedWings->GetMaterial(1));
}
if (BaseMaterialInstance == nullptr)
{
BaseMaterialInstance = BipedBody->CreateAndSetMaterialInstanceDynamicFromMaterial(0, BipedBody->GetMaterial(0));
}
if (BaseFeatherMaterialInstance == nullptr)
{
BaseFeatherMaterialInstance = BipedFur->CreateAndSetMaterialInstanceDynamicFromMaterial(0, BipedFur->GetMaterial(0));
}
}