Hi,
I am trying to export render target as PNG images from C++. I can see the render target properly on a surface, however, ExportToDisk results in a blank image. Any pointers on debugging this?
Here’s the code to I used to setup the render target and screen capture component.
void AScreenCaptureCameraActor::SetupSceneCaptureComponment() {
UCameraComponent* currentCamera = this->GetCameraComponent();
FMinimalViewInfo minimalViewInfo;
currentCamera->GetCameraView(0, minimalViewInfo); // first argument is delta time.
mSceneCaptureComponent = mSceneCaptureActor->GetCaptureComponent2D();
mSceneCaptureComponent->SetCameraView(minimalViewInfo);
mSceneCaptureComponent->bAlwaysPersistRenderingState = false;
mSceneCaptureComponent->CaptureSource = ESceneCaptureSource::SCS_FinalColorLDR;
mSceneCaptureComponent->bCaptureOnMovement = false;
mSceneCaptureComponent->bCaptureEveryFrame = false;
mSceneCaptureComponent->MaxViewDistanceOverride = -1.0f;
mSceneCaptureComponent->bAutoActivate = true;
mSceneCaptureComponent->DetailMode = EDetailMode::DM_High;
}
void AScreenCaptureCameraActor::CaptureScene() {
FTransform cameraWorldTransform = this->GetCameraComponent()->GetComponentTransform();
mSceneCaptureComponent->SetWorldTransform(cameraWorldTransform);
UTextureRenderTarget2D* renderTarget2D = NewObject<UTextureRenderTarget2D>();
renderTarget2D->ClearColor = FLinearColor::Blue;
renderTarget2D->InitCustomFormat(IMG_WIDTH, IMG_HEIGHT, PF_B8G8R8A8, true);
renderTarget2D->RenderTargetFormat = ETextureRenderTargetFormat::RTF_RGBA8;
UCameraComponent* currentCamera = this->GetCameraComponent();
FMinimalViewInfo minimalViewInfo;
currentCamera->GetCameraView(0, minimalViewInfo); // first argument is delta time.
mSceneCaptureComponent->SetCameraView(minimalViewInfo);
mSceneCaptureComponent->bAlwaysPersistRenderingState = false;
mSceneCaptureComponent->CaptureSource = ESceneCaptureSource::SCS_FinalColorLDR;
mSceneCaptureComponent->TextureTarget = renderTarget2D;
mSceneCaptureComponent->CaptureScene();
this->mScreenNeedsToBeCaptured = true;
}