简体   繁体   中英

UE4 create texture from C++

I want to transform an ingame camera view to a permanent texture. For this, I managed to transform the rendertarget into an Texture2d. Now if I save this Texture2D in an array of Textures, all the Textures are identical because they all refer to the same pointer and I struggle at solving this. I assume that I have to create a new texture object, but well... I haven't manage to yet...

I hope you can help me.

Here's my code and the according blueprint.

ScreenShotToTexture.h

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "ScreenShotToTexture.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UScreenShotToTexture : public UActorComponent
{
    GENERATED_BODY()

public: 
    // Sets default values for this component's properties
    UScreenShotToTexture();

protected:
    // Called when the game starts
    virtual void BeginPlay() override;    

public: 
    // Called every frame
    virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

public:
    UPROPERTY(EditAnywhere)
    UTextureRenderTarget2D* RenderTarget;

    UPROPERTY(EditAnywhere)
    UTexture2D* Picture;

    UFUNCTION(BlueprintCallable) //so visible in BluePrints
    UTexture2D* TakePic();
};

ScrenShotToTexture.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "ScreenShotToTexture.h"
#include "Engine/TextureRenderTarget2D.h"
#include "Components/SceneCaptureComponent2D.h"
#include "UObject/ObjectMacros.h"

// Sets default values for this component's properties
UScreenShotToTexture::UScreenShotToTexture()
{
    // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
    // off to improve performance if you don't need them.
    PrimaryComponentTick.bCanEverTick = true;

    // ...
}


// Called when the game starts
void UScreenShotToTexture::BeginPlay()
{
    Super::BeginPlay();

    // ...
    
}


// Called every frame
void UScreenShotToTexture::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    // ...
}

UTexture2D* UScreenShotToTexture::TakePic()
{
    if(RenderTarget == nullptr)
    {
        UE_LOG(LogTemp, Error, TEXT("No RenderTarget set."));
        return nullptr;
    }

    Picture = RenderTarget->ConstructTexture2D(this,"Test", EObjectFlags::RF_NoFlags, CTF_Default) ;

    return Picture;
}

在此处输入图片说明

Found the answer here: https://answers.unrealengine.com/questions/193827/how-to-get-texture-pixels-using-utexturerendertarg.html

void UScreenShotToTexture::CreateTexture()
    UTextureRenderTarget2D* TextureRenderTarget;
     // Creates Texture2D to store TextureRenderTarget content
     UTexture2D *Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX, TextureRenderTarget->SizeY, PF_B8G8R8A8);
     #if WITH_EDITORONLY_DATA
     Texture->MipGenSettings = TMGS_NoMipmaps;
     #endif
     Texture->SRGB = TextureRenderTarget->SRGB;
     
     // 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM