Hi! I’m trying to figure out how to use a UTextureRenderTarget2D with canvas rendering (not a camera render target.) I’ve almost got it working, but the editor crashes (with absolutely no useful info in the Log or the crash reporter - I’ve even got the editor crashing without the error reporter coming up at all, just silently closing.)
Here’s what I’ve got so far… I have no idea if this is correct or not:
Class:
protected:
UPROPERTY()
UTextureRenderTarget2D* tRenderTarget;
FCanvas* oCanvas;
UPROPERTY()
UCanvas* uoCanvas;
Method that creates texture:
FVector2D v2Size = GetSize();
if ( v2Size.X <= 0.f || v2Size.Y <= 0.f )
return;
tRenderTarget = NewObject<UTextureRenderTarget2D>( this );
tRenderTarget->InitAutoFormat( FMath::Floor( v2Size.X ), FMath::Floor( v2Size.Y ) );
tRenderTarget->ClearColor = FLinearColor( 0.f, 0.f, 0.f, 0.f );
// Create canvas to draw on the render target.
// FCanvas, not a UObject
oCanvas = new FCanvas( tRenderTarget->GameThread_GetRenderTargetResource(), NULL, GetWorld() );
// UCanvas UObject.
uoCanvas = NewObject<UCanvas>( this );
uoCanvas->Canvas = oCanvas;
uoCanvas->Init( FMath::Floor( v2Size.X ), FMath::Floor( v2Size.Y ), NULL );
I use the uoCanvas->DrawItem to draw stuff to the render target (causes crash.)
I then use the tRenderTarget object as a texture in an FCanvasTileItem, which I draw to the hud within the DrawHUD() call (causes crash after I press Stop from doing a PIE (where a big green square is drawn on the screen.))
Does anyone have any experience with rendering to render targets with canvas? Would appreciate some tips!