Download

DrawText always drawing white

Hi am trying to draw text as a parameter of my Material using the delegate UCanvasRenderTarget2D, but it’s always drawing white.

Here is my material:

And here is my code:


void ALetterCube::BeginPlay()
{
    Super::BeginPlay();
    UWorld* world = GetWorld();
   
     if(world){
     CanvasRenderTarget = Cast<UCanvasRenderTarget2D>(UCanvasRenderTarget2D::CreateCanvasRenderTarget2D(world,          UCanvasRenderTarget2D::StaticClass(), 1024, 1024));
     CanvasRenderTarget->OnCanvasRenderTargetUpdate.AddDynamic(this, &ALetterCube::DrawToCanvasRenderTarget);
     UMaterialInstanceDynamic* MaterialInstance = UMaterialInstanceDynamic::Create(LetterMaterial, CubeMesh);
     MaterialInstance->SetTextureParameterValue(FName("LetterInput"), CanvasRenderTarget);

     CubeMesh->SetMaterial(0, MaterialInstance);
     CubeMesh->SetMaterial(1, MaterialInstance);
     CubeMesh->SetMaterial(2, MaterialInstance);
     CubeMesh->SetMaterial(3, MaterialInstance);
     CubeMesh->SetMaterial(4, MaterialInstance);
     CubeMesh->SetMaterial(5, MaterialInstance);
     CanvasRenderTarget->UpdateResource();
     }

}

void ALetterCube::DrawToCanvasRenderTarget(UCanvas* Canvas, int32 Width, int32 Height)
{
    //Drawing code with UCanvas goes here...
    Canvas->SetDrawColor(FColor::Black);
    Canvas->DrawText(MyFont, FText::FromString(FString::Printf(TEXT("C"))), 0, 0);
}

If I change the color of the material to black (in the Param) I can see the letter C in white.

Any idea why it’s not working? Thanks alot!

I notice that if I connect directly the LetterInput to LetterCube_Mat I can see that when I draw text (if I choose like blue color on canvas) the background is black.
So that’s why when I do

Canvas->SetDrawColor(FColor::Black);

I can’t see anything.
How can we change the background color of the Canvas itself?

Just figured it out, I need to use:

CanvasRenderTarget->ClearColor = FLinearColor::White;

And then I can see what I write in black. Didn’t know about ClearColor :facepalm: