Hello,
I am fairly new to Unreal Engine 5, and I am working on making a dynamic material that changes on a value I calculate called Refelxivity.
Here is what I added in the .cpp file
void AActor::AMaterialInstanceDynamicChange()
{
PrimaryActorTick.bCanEverTick = true;
MeshComponent= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent=MeshComponent;
}
void AActor::BeginPlay()
{
Super::BeginPlay();
M_Reflexivity = MeshComponent->GetMaterial(0);
ReflexivityMID = UMaterialInstanceDynamic::Create(M_Reflexivity, this);
MeshComponent->SetMaterial(0, ReflexivityMID);
}
void AActor::CalculateIntensity(...)
{
TRACE_CPUPROFILER_EVENT_SCOPE(ParallelFor); // 10ms instead of 30ms
ParallelFor(Locations.Num(), [&](int32 i)
{
Intensities[i] = ...
float Reflexivity = -0.2130 + 0.0698 * Intensities[i]; //Lambertian Target Reflectance of a Material at an angle of 0 deg
ReflexivityMID->SetScalarParameterValue(TEXT("Reflexivity"),Reflexivity);
UE_LOG(LogTemp, Warning, TEXT("Reflexivity = %f"),Reflexivity);
Reflexivities.Add(Reflexivity);
});
}
And in the .h file
UMaterialInterface* M_Reflexivity;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
I then connect the “Reflexivity” scalar parameter to the emissive colour output of Material Editor.
But I see no difference in the actual material when I simulate this. Please help me out here. Thanks!