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;
}