Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ScreenCapture.h:
- #pragma once
- #include "CoreMinimal.h"
- #include "GameFramework/Actor.h"
- #include "Classes/Camera/CameraComponent.h"
- #include "Classes/Components/SceneCaptureComponent2D.h"
- #include "Classes/Engine/TextureRenderTarget2D.h"
- #include "CustomScreenCapture.generated.h"
- UCLASS()
- class RTW_SIMULATION_API ACustomScreenCapture : public AActor
- {
- GENERATED_BODY()
- public:
- // Sets default values for this actor's properties
- ACustomScreenCapture();
- protected:
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
- // Create the image in texture
- void FillTexture();
- // Textures need to be power of 2
- // Texture has to be a square
- uint32_t internResolution;
- UTextureRenderTarget2D* renderTarget;
- class USceneCaptureComponent2D* sceneCapture;
- class UCameraComponent* OurCamera;
- public:
- // Called every frame
- virtual void Tick(float DeltaTime) override;
- UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "32", ClampMax = "4096", UIMin = "32", UIMax = "4096"))
- uint32 resolutionX;
- UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "32", ClampMax = "4096", UIMin = "32", UIMax = "4096"))
- uint32 resolutionY;
- UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "20.0", ClampMax = "179.9", UIMin = "20.0", UIMax = "179.9"))
- float field_of_view;
- };
- CustomScreenCapture.cpp:
- #include "CustomScreenCapture.h"
- // Sets default values
- ACustomScreenCapture::ACustomScreenCapture()
- : resolutionX(1024)
- , resolutionY(1024)
- , field_of_view(90.0f)
- {
- PrimaryActorTick.bCanEverTick = true;
- PrimaryActorTick.TickGroup = TG_PostUpdateWork;
- RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
- OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewportCamera"));
- OurCamera->SetupAttachment(RootComponent);
- }
- void ACustomScreenCapture::BeginPlay()
- {
- Super::BeginPlay();
- // Resolution has to be a power of 2. This code finds the lowest RxR resolution which has equal or more pixel than set
- uint32_t higher = std::max(resolutionX, resolutionY);
- higher--;
- higher |= higher >> 1;
- higher |= higher >> 2;
- higher |= higher >> 4;
- higher |= higher >> 8;
- higher |= higher >> 16;
- higher++;
- internResolution = higher;
- OurCamera->FieldOfView = field_of_view;
- renderTarget = NewObject<UTextureRenderTarget2D>();
- renderTarget->InitCustomFormat(internResolution, internResolution, EPixelFormat::PF_B8G8R8A8, true);
- renderTarget->UpdateResourceImmediate();
- sceneCapture = NewObject<USceneCaptureComponent2D>();
- sceneCapture->CaptureSource = SCS_FinalColorLDR;
- sceneCapture->TextureTarget = renderTarget;
- sceneCapture->SetupAttachment(OurCamera);
- sceneCapture->bCaptureEveryFrame = true;
- }
- // Called every frame
- void ACustomScreenCapture::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- FillTexture();
- }
- void ACustomScreenCapture::FillTexture()
- {
- sceneCapture->TextureTarget = renderTarget;
- auto RenderTargetResource = renderTarget->GameThread_GetRenderTargetResource();
- if (RenderTargetResource)
- {
- TArray<FColor> buffer8;
- RenderTargetResource->ReadPixels(buffer8);
- UE_LOG(LogTemp, Warning, TEXT("Fill Texture.. This line is for break point reasons"));
- }
- }
Add Comment
Please, Sign In to add comment