x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Proper way to convert from UTextureRenderTarget2D into UTexture2D?

Hello, I want to convert a captured screen of game into UTexture2D. Here's what has been done.

I have inherited a custom class AScreenCapture2D from ASceneCapture2D, with member

 UPROPERTY()
         UTexture2D* m_photo;

and added these functions.

 void AScreenCapture2D::SetRenderedTexture()
 {
     USceneCaptureComponent2D* captureComponent = GetCaptureComponent2D();
     UTextureRenderTarget2D* renderTarget = captureComponent->TextureTarget;
     checkf(renderTarget != nullptr, TEXT("renderTarget is null"));
 
     captureComponent->UpdateContent();
 
     m_photo = ConvertRenderTargetToTexture2D(renderTarget);
     checkf(m_photo != nullptr, TEXT("photo is null"));
 }
 
 UTexture2D* AScreenCapture2D::ConvertRenderTargetToTexture2D(UTextureRenderTarget2D* TextureRenderTarget)
 {
     // Creates Texture2D to store TextureRenderTarget content
     UTexture2D *Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX, TextureRenderTarget->SizeY);
 #if WITH_EDITORONLY_DATA
     Texture->MipGenSettings = TMGS_NoMipmaps;
 #endif
     Texture->SRGB = false;
     Texture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
 
     // Read the pixels from the RenderTarget and store them in a FColor array
     TArray<FColor> SurfData;
     FRenderTarget *RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
     RenderTarget->ReadPixels(SurfData);
 
     // Lock and copies the data between the textures
     void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
     const int32 TextureDataSize = SurfData.Num() * 4;
     FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
     Texture->PlatformData->Mips[0].BulkData.Unlock();
     // Apply Texture changes to GPU memory
     Texture->UpdateResource();
 
     return Texture;
 }
 
 UTexture2D* AScreenCapture2D::GetRenderedTexture() const
 {
     checkf(m_photo != nullptr, TEXT("photo is null"));
 
     return m_photo;
 }

The second function I found from one of answers here, and adapted it a little bit.

Now for testing purposes I made a UMG widget with initial white image and tried to render that UTexture2D photo. alt text

The code above works for windows with editor, but fails on android. The white colored background image disappears, and I get a transparent image.

This is the changed settings of UTextureRenderTarget2D alt text

So if this method is a correct way to convert UTextureRenderTarget2D into UTexture2D, then how to fix this issue with android rendering? If not, then what's the proper way for conversion?

P.S: I know about ConstructTexture2D, but it works only with editor.

Product Version: UE 4.13
Tags:
capture0.png (97.7 kB)
capture1.png (29.2 kB)
more ▼

asked May 26 '17 at 01:56 PM in C++ Programming

avatar image

david.noorgames
11 1 4 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question