111 lines
3.8 KiB
C
Raw Normal View History

2025-04-03 15:29:06 +03:00
// Copyright 2025 RLoris
2025-03-18 19:25:25 +01:00
#pragma once
2025-04-03 15:29:06 +03:00
#include "Async/Future.h"
2025-03-18 19:25:25 +01:00
#include "Engine/Texture2D.h"
#include "Engine/World.h"
#include "Kismet/BlueprintAsyncActionBase.h"
2025-04-03 15:29:06 +03:00
#include "Misc/Paths.h"
2025-03-18 19:25:25 +01:00
#include "FileHelperScreenshotAction.generated.h"
2025-04-03 15:29:06 +03:00
namespace ECameraProjectionMode
{
enum Type : int;
}
2025-03-18 19:25:25 +01:00
USTRUCT(BlueprintType)
struct FFileHelperScreenshotActionOptions
{
GENERATED_BODY()
2025-04-03 15:29:06 +03:00
/** Directory path where to store screenshot */
UPROPERTY(BlueprintReadWrite, Category = "FileHelper|Screenshot")
FString DirectoryPath = FPaths::ScreenShotDir();
/** File name without extension or path information, extension will be added internally (.png or .exr) */
UPROPERTY(BlueprintReadWrite, Category = "FileHelper|Screenshot")
2025-03-18 19:25:25 +01:00
FString Filename;
/** Prefix filename with a custom timestamp */
2025-04-03 15:29:06 +03:00
UPROPERTY(BlueprintReadWrite, Category = "FileHelper|Screenshot")
2025-03-18 19:25:25 +01:00
bool bPrefixTimestamp = true;
2025-04-03 15:29:06 +03:00
/** Include the viewport UI in the screenshot, only used when CustomCameraActor is not provided */
UPROPERTY(BlueprintReadWrite, Category = "FileHelper|Screenshot")
2025-03-18 19:25:25 +01:00
bool bShowUI = false;
2025-04-03 15:29:06 +03:00
/**
* Uses this option only if the scene has HDR enabled,
* extension of screenshot file will be exr instead of png,
* if the scene is not using HDR, fallback to png
*/
UPROPERTY(BlueprintReadWrite, Category = "FileHelper|Screenshot")
2025-03-18 19:25:25 +01:00
bool bWithHDR = false;
2025-04-03 15:29:06 +03:00
/**
* Leave this empty for default viewport screenshot,
* if set, a different type of screenshot will be taken from a different perspective,
* options and settings quality may differ from regular screenshot, no UI is shown
*/
2025-03-18 19:25:25 +01:00
UPROPERTY(BlueprintReadWrite, Category = "Screenshot")
TObjectPtr<ACameraActor> CustomCameraActor = nullptr;
};
UCLASS()
class FILEHELPER_API UFileHelperScreenshotAction : public UBlueprintAsyncActionBase
{
GENERATED_BODY()
public:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOutputPin, UTexture2D*, Screenshot, FString, Path);
2025-04-03 15:29:06 +03:00
2025-03-18 19:25:25 +01:00
UPROPERTY(BlueprintAssignable)
FOutputPin Completed;
2025-04-03 15:29:06 +03:00
2025-03-18 19:25:25 +01:00
UPROPERTY(BlueprintAssignable)
FOutputPin Failed;
2025-04-03 15:29:06 +03:00
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", Keywords = "File plugin screenshot save load texture", ToolTip = "Take a screenshot, save and load it"), Category = "FileHelper|Screenshot")
static UFileHelperScreenshotAction* TakeScreenshot(const FFileHelperScreenshotActionOptions& InOptions);
2025-03-18 19:25:25 +01:00
2025-04-03 15:29:06 +03:00
UFUNCTION(BlueprintCallable, meta = (Keywords = "screenshot load texture FileHelper", ToolTip = "Load a screenshot into a texture"), Category = "FileHelper|Screenshot")
2025-03-18 19:25:25 +01:00
static UTexture2D* LoadScreenshot(const FString& InFilePath);
private:
2025-04-03 15:29:06 +03:00
//~ Begin UBlueprintAsyncActionBase
virtual void Activate() override;
//~ End UBlueprintAsyncActionBase
2025-03-18 19:25:25 +01:00
void OnTaskCompleted();
void OnTaskFailed();
2025-04-03 15:29:06 +03:00
2025-03-18 19:25:25 +01:00
void CreateCustomCameraScreenshot();
2025-04-03 15:29:06 +03:00
void CreateViewportScreenshot();
void CreatePlayerPOVScreenshot();
void CreateRenderTargetScreenshot(UWorld* InWorld, float InFOV, ECameraProjectionMode::Type InProjectionMode, float InOrthoWidth, const FVector& InLocation, const FRotator& InRotation, int32 InWidth, int32 InHeight);
void ConvertLinearColorToColorBuffer(const TArray<FLinearColor>& InSourceBuffer, TArray<FColor>& OutDestBuffer);
bool WriteColorBufferToDisk(const TArray<FColor>& InBuffer, int32 InWidth, int32 InHeight, UTexture2D*& OutTexture) const;
TFuture<bool> WriteLinearColorBufferToDiskAsync(TArray<FLinearColor>&& InBuffer, int32 InWidth, int32 InHeight);
TFuture<bool> WriteColorBufferToDiskAsync(TArray<FColor>&& InBuffer, int32 InWidth, int32 InHeight);
2025-03-18 19:25:25 +01:00
void Reset();
2025-04-03 15:29:06 +03:00
/** Final screenshot texture */
2025-03-18 19:25:25 +01:00
UPROPERTY()
TObjectPtr<UTexture2D> ScreenshotTexture;
2025-04-03 15:29:06 +03:00
/** Screenshot options */
2025-03-18 19:25:25 +01:00
UPROPERTY()
FFileHelperScreenshotActionOptions Options;
/** The file path of the new screenshot taken */
2025-04-03 15:29:06 +03:00
UPROPERTY()
2025-03-18 19:25:25 +01:00
FString FilePath;
/** Is this node active */
2025-04-03 15:29:06 +03:00
UPROPERTY()
2025-03-18 19:25:25 +01:00
bool bActive = false;
};