Hi,
I am currently trying to implement a photo-feature for my game. I would like to capture an image using the USceneCaptureComponent2D component and get it as a UTexture2D to display in the UI. I have looked through the blueprint-office example and searched the answerhub but I can’t seem to find out how to correctly use USceneCaptureComponent2D and UTextureRenderTarget2D. Whenever I try to construct the UTexture2D using ConstructTexture2D my entire engine crashes due to illegal memory access.
The capture component is part of my character class which is based upon the FPS-example.
My header declarations:
UCLASS(config = Game)
class AKTUCharacter : public ACharacter
{
GENERATED_BODY()
/** First person camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FirstPersonCameraComponent;
/** Scene Capture Component*/
UPROPERTY(VisibleAnywhere, Category = Camera)
class USceneCaptureComponent2D* sceneCapture;
UTexture2D* lastPhoto;
UTextureRenderTarget2D* renderTarget;
public:
AKTUCharacter(const FObjectInitializer& ObjectInitializer);
protected:
void OnPhoto();
}
Constructor assignment:
AKTUCharacter::AKTUCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
/**
FPS-Camera and value initialisation
*/
renderTarget = NewObject<UTextureRenderTarget2D>();
renderTarget->InitAutoFormat(512, 512);
renderTarget->UpdateResourceImmediate();
sceneCapture = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCapture"));
sceneCapture->AttachParent = FirstPersonCameraComponent;
sceneCapture->CaptureSource = SCS_FinalColorLDR;
sceneCapture->TextureTarget = renderTarget;
}
Taking the photo:
void AKTUCharacter::OnPhoto()
{
sceneCapture->FOVAngle = FirstPersonCameraComponent->FieldOfView;
sceneCapture->UpdateContent();
lastPhoto = sceneCapture->TextureTarget->ConstructTexture2D(this, "Photo", EObjectFlags::RF_NoFlags, CTF_DeferCompression); // <- It crashes here
}
Does anyone know what I did wrong?
Thanks in advance,
Todd