Hey, I’m trying to draw a healthbar with UCanvas onto a texture by using UCanvasRenderTarget2D. When I draw directly on the AHUD canvas, everthing goes well. However, if I render to texture, and then draw the texture onto my AHUD canvas, the blending modes seem to get all messed up. I’ve tried different blending modes all around, as well as fidling with the material blending mode (it’s translucent). I have no idea what is wrong and how to fix this.
This is how I create the render target on startup.
void UHealthBar::SetWorld(UWorld* NewWorld)
{
this->RenderTarget = UCanvasRenderTarget2D::CreateCanvasRenderTarget2D(
NewWorld,
UCanvasRenderTarget2D::StaticClass(),
256,
64
);
this->RenderTarget->OnCanvasRenderTargetUpdate.AddDynamic(this, &UHealthBar::Draw);
this->RenderTarget->ClearColor = FLinearColor::White;
this->RenderTarget->UpdateResource();
}
This function calls for a redraw when the health has changed.
void UHealthBar::SetHealth(const float NewHealth)
{
this->Health = FMath::Clamp<float>(NewHealth, 0, 100);
if (this->RenderTarget)
this->RenderTarget->UpdateResource();
}
This function will actually draw the healthbar on a given canvas.
void UHealthBar::Draw(UCanvas* Canvas, int32 Width, int32 Height)
{
// Back
FCanvasTileItem BackItem(
FVector2D(500.0f, 500.0f),
this->HealthBack->GetRenderProxy(false),
FVector2D(Width, Height)
);
Canvas->DrawItem(BackItem);
// Front
FCanvasTileItem FrontItem(
FVector2D(0.0f, 0.0f),
this->HealthFront->GetRenderProxy(false),
FVector2D(Width, Height)
);
Canvas->DrawItem(FrontItem);
}
This is the result if I draw directly onto the given AHUD canvas, which renders correctly:
void ASupraballHUD::DrawHealthBar()
{
// Draw healthbar.
this->HealthBar->Draw(this->Canvas, 200, 50);
}
http://puu.sh/ig2Yh/474c4c34ca.jpg
This is the result if I draw the render target texture on the given AHUD canvas, which is wrong:
void ASupraballHUD::DrawHealthBar()
{
// Draw healthbar.
if (this->HealthBar->RenderTarget != nullptr)
{
float Width = this->HealthBar->RenderTarget->GetSurfaceWidth();
float Height = this->HealthBar->RenderTarget->GetSurfaceHeight();
Canvas->DrawTile(
this->HealthBar->RenderTarget,
0.0f, Canvas->SizeY - Height,
Width, Height,
0.0f, 0.0f,
Width, Height
);
}
}
http://puu.sh/ig2Uz/938371d19a.jpg
The two materials in question: