I’ve tried to ask this question on AnswerHub, but to no avail. Therefore, I’m resorting to the forums as my options start depleting.
I have the following mock healthbar class, which should draw a white box for no other reason than to see if something renders at all.
UHealthBar::UHealthBar(const FObjectInitializer& OI)
: Super(OI)
{
SetHealth(100);
}
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::Transparent;
this->RenderTarget->UpdateResource();
}
float UHealthBar::GetHealth() const
{
return this->Health;
}
void UHealthBar::SetHealth(const float NewHealth)
{
this->Health = FMath::Clamp<float>(NewHealth, 0, 100);
if (this->RenderTarget)
this->RenderTarget->UpdateResource();
}
void UHealthBar::Draw(UCanvas* Canvas, int32 Width, int32 Height)
{
FCanvasTileItem TileItem(FVector2D(0, 0), GWhiteTexture, FVector2D(Width, Height), FLinearColor::White);
TileItem.BlendMode = SE_BLEND_Translucent;
Canvas->DrawItem(TileItem);
}
In my AHUD class I then draw the texture on my hud using:
void AMyHUD::DrawHUD()
{
// Draw healthbar.
if (this->HealthBar->RenderTarget != nullptr)
{
float Width = this->HealthBar->RenderTarget->GetSurfaceWidth();
float Height = this->HealthBar->RenderTarget->GetSurfaceHeight();
Canvas->SetDrawColor(FLinearColor::White);
Canvas->DrawTile(
this->HealthBar->RenderTarget,
0.0f, Canvas->SizeY - Height,
Width, Height,
0.0f, 0.0f,
Width, Height, EBlendMode::BLEND_Additive
);
}
}
This seems to work, except that I’m using BLEND_Additive. If I replace this with BLEND_Translucent, which I actually need, nothing gets rendered at all! I am completely clueless on why this happens. How is it possible that something may draw as additive but not as translucent? I’ve checked the texture’s pixelformat, which is set to Float_RGBA, so it can’t be that. I’ve tried different draw colors as well, but none of the combinations I’ve tried worked, and I’ve tried a lot of them. I am completely stumped on this.
And I made a typo in the title which I can’t change