Announcement

Collapse
No announcement yet.

attach and change material base color with c++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    attach and change material base color with c++

    Hi all,

    Is this possible attach dynamically (with c++) a material to an actor static mesh, and to dynamically (still with c++) change the base color of this material ?

    #2
    Hi!

    Yep, it is possible. For example in AYourActor::BeginPlay you can do something like this:

    Code:
    if (materialBase)
      dynamicMaterial = UMaterialInstanceDynamic::Create(materialBase, this);
    mesh->SetMaterial(0, dynamicMaterial);
    where

    materialBase - basic material for dynamic instanced material
    dynamicMaterial - dynamic instanced material that you can modify
    mesh - your static mesh

    You can change color via material parameters. Using methods: SetScalarParameterValue, SetVectorParameterValue. For example:

    Code:
    dynamicMaterial->SetVectorParameterValue(TEXT("Color"), FLinearColor::Red);
    [GAME] The Unexpected Quest | [TOOL] Localization UE4

    Comment


      #3
      Thanks a lot

      Comment


        #4
        Hi, Thanks for this but I am still having a problem. I did EXACTLY what you posted and it doesn't work:

        Code:
        void AFloatingActor::BeginPlay()
        {
        Super::BeginPlay();

        auto Cube = FindComponentByClass<UStaticMeshComponent>();
        auto Material = Cube->GetMaterial(0);

        DynamicMatierial = UMaterialInstanceDynamic::Create(Material, this);
        Cube->SetMaterial(0, DynamicMatierial);

        qColor.A = 0.0;
        qColor.R = 120.0;
        qColor.G = 200.0;
        qColor.B = 36.0;
        FLinearColor fColor(100.0, 23.0, 233.0);
        FVector vColor(100.0, 23.0, 233.0);

        DynamicMatierial->SetVectorParameterValue(FName(TEXT("QbColor")), FLinearColor::Red);

        }
        Thanks for any help!

        Comment


          #5
          Make sure that:
          1. the shape has the material applied;
          2. the material has a "QbColor" vector parameter connected to the "Base Color" pin.
          "I have harnessed the shadows that stride from world to world to sow death and madness."

          Comment


            #6
            What does "doesn't work" mean?
            If you step through it in the debugger, what does it do?
            Is any variable NULL?
            Can you step into SetVectorParameterValue() and see what it does?

            Comment


              #7
              Thanks for the quick responses. I noticed the error a little while ago and fixed it - newb here.

              Cheers!

              Comment

              Working...
              X