Guest User

CustomScreenCapture

a guest
Jun 4th, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. ScreenCapture.h:
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6.  
  7. #include "GameFramework/Actor.h"
  8. #include "Classes/Camera/CameraComponent.h"
  9. #include "Classes/Components/SceneCaptureComponent2D.h"
  10. #include "Classes/Engine/TextureRenderTarget2D.h"
  11. #include "CustomScreenCapture.generated.h"
  12.  
  13. UCLASS()
  14. class RTW_SIMULATION_API ACustomScreenCapture : public AActor
  15. {
  16.     GENERATED_BODY()
  17.  
  18. public:
  19.     // Sets default values for this actor's properties
  20.     ACustomScreenCapture();
  21.  
  22. protected:
  23.     // Called when the game starts or when spawned
  24.     virtual void BeginPlay() override;
  25.  
  26.     // Create the image in texture
  27.     void FillTexture();
  28.  
  29.     // Textures need to be power of 2
  30.     // Texture has to be a square
  31.     uint32_t internResolution;
  32.  
  33.     UTextureRenderTarget2D* renderTarget;
  34.     class USceneCaptureComponent2D* sceneCapture;
  35.     class UCameraComponent* OurCamera;
  36.  
  37. public:
  38.     // Called every frame
  39.     virtual void Tick(float DeltaTime) override;
  40.     UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "32", ClampMax = "4096", UIMin = "32", UIMax = "4096"))
  41.         uint32 resolutionX;
  42.  
  43.     UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "32", ClampMax = "4096", UIMin = "32", UIMax = "4096"))
  44.         uint32 resolutionY;
  45.  
  46.     UPROPERTY(EditAnywhere, Category = "Output Information", meta = (ClampMin = "20.0", ClampMax = "179.9", UIMin = "20.0", UIMax = "179.9"))
  47.         float field_of_view;
  48. };
  49.  
  50.  
  51. CustomScreenCapture.cpp:
  52.  
  53. #include "CustomScreenCapture.h"
  54.  
  55. // Sets default values
  56. ACustomScreenCapture::ACustomScreenCapture()
  57.     : resolutionX(1024)
  58.     , resolutionY(1024)
  59.     , field_of_view(90.0f)
  60. {
  61.     PrimaryActorTick.bCanEverTick = true;
  62.     PrimaryActorTick.TickGroup = TG_PostUpdateWork;
  63.     RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  64.     OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewportCamera"));
  65.     OurCamera->SetupAttachment(RootComponent);
  66. }
  67.  
  68. void ACustomScreenCapture::BeginPlay()
  69. {
  70.     Super::BeginPlay();
  71.  
  72.     // Resolution has to be a power of 2. This code finds the lowest RxR resolution which has equal or more pixel than set
  73.     uint32_t higher = std::max(resolutionX, resolutionY);
  74.  
  75.     higher--;
  76.     higher |= higher >> 1;
  77.     higher |= higher >> 2;
  78.     higher |= higher >> 4;
  79.     higher |= higher >> 8;
  80.     higher |= higher >> 16;
  81.     higher++;
  82.  
  83.     internResolution = higher;
  84.  
  85.     OurCamera->FieldOfView = field_of_view;
  86.     renderTarget = NewObject<UTextureRenderTarget2D>();
  87.     renderTarget->InitCustomFormat(internResolution, internResolution, EPixelFormat::PF_B8G8R8A8, true);
  88.  
  89.     renderTarget->UpdateResourceImmediate();
  90.  
  91.     sceneCapture = NewObject<USceneCaptureComponent2D>();
  92.     sceneCapture->CaptureSource = SCS_FinalColorLDR;
  93.  
  94.     sceneCapture->TextureTarget = renderTarget;
  95.     sceneCapture->SetupAttachment(OurCamera);
  96.     sceneCapture->bCaptureEveryFrame = true;
  97. }
  98.  
  99. // Called every frame
  100. void ACustomScreenCapture::Tick(float DeltaTime)
  101. {
  102.     Super::Tick(DeltaTime);
  103.    
  104.     FillTexture();
  105. }
  106.  
  107. void ACustomScreenCapture::FillTexture()
  108. {
  109.     sceneCapture->TextureTarget = renderTarget;
  110.     auto RenderTargetResource = renderTarget->GameThread_GetRenderTargetResource();
  111.  
  112.     if (RenderTargetResource)
  113.     {
  114.         TArray<FColor> buffer8;
  115.         RenderTargetResource->ReadPixels(buffer8);
  116.        
  117.         UE_LOG(LogTemp, Warning, TEXT("Fill Texture.. This line is for break point reasons"));
  118.     }
  119. }
Add Comment
Please, Sign In to add comment