Search code examples
c++unreal-engine4texture2drendertarget

How do you update a UTextureRenderTarget2D in C++?


I'm referenced @J. Rehbein's ask. How do you update a UTextureRenderTarget2D dynamically in C++?

And use public modules ("Core", "CoreUObject", "Engine", "InputCore", "UMG", "GameplayTasks", "Landscape", "RHI", "RenderCore").

Why printed assertion failed log?

How fix assertion failed problem?

Please, teachers.

void AWorldCreator::BeginPlay()
{
 Super::BeginPlay();
 if (RenderTarget != nullptr) {
   RenderTarget->InitCustomFormat(Width, Height, PF_B8G8R8A8, true);

   auto region = FUpdateTextureRegion2D(0, 0, 0, 0, Width, Height);

   FTexture2DRHIRef TextureRHI = RenderTarget->GameThread_GetRenderTargetResource()->GetRenderTargetTexture();

   ENQUEUE_RENDER_COMMAND(UpdateTextureRegionsData)(
   [=](FRHICommandListImmediate& RHICmdList)
     {
       check(TextureRHI.IsValid());  // Assertion failed line
     });
 }

}


Solution

  • I want change URenderTargetTexture2D's texture. i'm tried how that use 2_DrawMaterial. And i feel hardness from how convert UMaterialInstanceDynamic to UMaterialInterface. So, next tried how that use ENQUEUE_RENDER_COMMAND. Blocked again. So I posted a question. Sorry and thanks everyone for read my awkward(weird?) question.

    ...

    I succeed URenderTargetTexture2D updating without ENQUEUE_RENDER_COMMAND. I converted UMaterialInstanceDynamic to UMaterialInterface thought UStaticMeshComponent. I use UStaticMeshComponent's functions (GetMaterial, SetMaterial).

    UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(Material, GetWorld());
    ...
    StaticMeshComponent->SetMaterial(0, DynamicMaterial);
    ...
    canvas->K2_DrawMaterial(StaticMeshComponent->GetMaterial(0), Position, Size, CoordinatePosition);
    

    Thank you.