ViaCognita
(ViaCognita)
October 15, 2015, 4:39pm
1
I have a material with a Vector Parameter Value to allow me to change its colour. I have also created a Material Instance for this material.
I’m trying to create many actors with this material instance but with different colours but I can’t. I don’t this programmatically. The code is:
At class constructor:
static ConstructorHelpers::FObjectFinder<UMaterialInstance> MatFinder(TEXT("MaterialInstanceConstant'/Game/Geometry/Materials/Star_Inst.Star_Inst'")); // 'Material /Game/Geometry/Materials/Star.Star'
if (MatFinder.Succeeded())
{
StarMaterialInstance = MatFinder.Object;
}
And in PostInitialiteMethod:
void AStarActor::PostInitializeComponents()
{
Super::PostInitializeComponents();
UMaterialInstanceDynamic* matInstance = UMaterialInstanceDynamic::Create(StarMaterialInstance, this);
matInstance->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(255.0, 255.0, 255.0, 1.0));
matInstance->SetVectorParameterValue(FName(TEXT("EmiterColor")), FLinearColor(125.0, 125.0, 125.0, 1.0));
matInstance->SetScalarParameterValue(FName(TEXT("Magnitude")), -1);
SphereVisual->SetMaterial(0, matInstance);
}
But, all the instances have the same colour!!! I have two meshes in the editor, and they also change their colour.
How create many AStarActor instances with different colours using the same Material Instance?
This is my material setup:
Yata
(Yata)
October 15, 2015, 5:18pm
2
You are setting the same color in PostInitializeComponents, that’s why they all have the same color. Try changing the color.
You can generate random color for ex. generate random vector with FMath::VRand and create new FLinearColor using vector values.
matInstance->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(FMath::VRand()));
matInstance->SetVectorParameterValue(FName(TEXT("EmiterColor")), FLinearColor(FMath::VRand()));
will work I think.
ViaCognita
(ViaCognita)
October 15, 2015, 7:02pm
3
I have put two instances of my class in Unreal Engine with a different colour and when I play the game these two instances change their colour.
Yata
(Yata)
October 15, 2015, 7:17pm
4
I am not sure, but I think you should get the reference to a material, not a material instance. And then create dynamic material instance from it.
Static Material Instances are for setting material parameters at design time for use in the editor, i think.
ViaCognita
(ViaCognita)
October 16, 2015, 5:16pm
5
Thanks, but I have tried what you have suggested me and I get the same result. Maybe, I’m using the same Material with all of them and this is why I change the colour in all of them.
Yata
(Yata)
October 17, 2015, 11:16am
6
It is strange. Can you post you material setup?
ViaCognita
(ViaCognita)
October 17, 2015, 5:03pm
7
I don’t know what do you mean with material setup, but I have updated my question with a picture.
ViaCognita
(ViaCognita)
October 18, 2015, 6:40am
8
I have created this git repository if you want to check all the project: https://github.com/ViaCognita/Estrellas
You can also download Estrellas.sdf 7zipped here: Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.
Yata
(Yata)
October 18, 2015, 9:20am
9
Yes, it would be very handy, but please add all the files. )
Yata
(Yata)
October 18, 2015, 1:24pm
10
Hi, ViaCognita
Here what i got
And here is the code for my actors http://pastebin.com/4sx68Tn2
Setting parameters works ok. you can compare it to yours.
What is interesting about your material is some color math which involves multiplication. Check it if it is ok.
ViaCognita
(ViaCognita)
October 18, 2015, 2:42pm
11
Sorry, I have forgotten to push it to Github. You can also download Estrellas.sdf 7zipped here: Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.
ViaCognita
(ViaCognita)
October 19, 2015, 4:22pm
12
Thanks a lot. Your solution works perfectly. Now I have understood the problem.