From 8dc892b9fafccc31315db3b8bdcc83cd5249a828 Mon Sep 17 00:00:00 2001 From: Filip Iliescu Date: Tue, 29 Apr 2025 15:40:38 -0700 Subject: [PATCH] Scaffold render target library --- .../Private/LRRenderUtilLibrary.cpp | 87 +++++++++++++++++++ .../LuckyWorldV2/Public/LRRenderUtilLibrary.h | 25 ++++++ 2 files changed, 112 insertions(+) create mode 100644 Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp create mode 100644 Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h diff --git a/Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp b/Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp new file mode 100644 index 00000000..6db149f5 --- /dev/null +++ b/Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp @@ -0,0 +1,87 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LRRenderUtilLibrary.h" + +#include "Components/SceneCaptureComponent2D.h" +#include "Engine/TextureRenderTarget2D.h" +#include "Kismet/KismetRenderingLibrary.h" + +void ULRRenderUtilLibrary::SetupSceneCaptureForMesh(USceneCaptureComponent2D* SceneCapture, UStaticMeshComponent* MeshComp) +{ + if (!SceneCapture || !MeshComp) + { + return; + } + + // Set the scene capture to focus on the mesh + SceneCapture->ShowOnlyComponent(MeshComp); + + SceneCapture->CaptureSource = ESceneCaptureSource::SCS_BaseColor; + SceneCapture->bCaptureEveryFrame = false; + SceneCapture->bCaptureOnMovement = false; + SceneCapture->FOVAngle = 90.f; + + // Set a solid background color (black by default) + SceneCapture->CompositeMode = ESceneCaptureCompositeMode::SCCM_Overwrite; + SceneCapture->TextureTarget->ClearColor = FLinearColor::Transparent; + + // Adjust the transform to frame the mesh + FVector MeshBounds = MeshComp->Bounds.BoxExtent; + FVector MeshOrigin = MeshComp->Bounds.Origin; + + // Calculate the camera distance + float CameraDistance = MeshBounds.Size() * 1.1f; // Adjust multiplier as needed + + // Calculate the camera location + FVector CameraLocation = MeshOrigin + FVector(CameraDistance, 0.f, 0.f); // Default to capturing from the +X axis + + // Calculate the camera rotation + FRotator CameraRotation = (MeshOrigin - CameraLocation).Rotation(); + + SceneCapture->SetWorldLocationAndRotation(CameraLocation, CameraRotation); // Adjust rotation as needed +} + +UTextureRenderTarget2D* ULRRenderUtilLibrary::CaptureMeshToRenderTarget(UStaticMeshComponent* MeshComp, const FVector2D& ImageSize, UObject* WorldContextObject) +{ + if (!MeshComp || !WorldContextObject) + { + return nullptr; + } + + // Create a render target + UTextureRenderTarget2D* RenderTarget = UKismetRenderingLibrary::CreateRenderTarget2D( + WorldContextObject, ImageSize.X, ImageSize.Y, ETextureRenderTargetFormat::RTF_RGBA8); + + if (!RenderTarget) + { + return nullptr; + } + + // Create a temporary actor to hold the scene capture component + AActor* TempActor = WorldContextObject->GetWorld()->SpawnActor(); + USceneCaptureComponent2D* SceneCapture = NewObject(TempActor); + SceneCapture->AttachToComponent(TempActor->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform); + SceneCapture->TextureTarget = RenderTarget; + + // Setup the scene capture + SetupSceneCaptureForMesh(SceneCapture, MeshComp); + + UKismetRenderingLibrary::ClearRenderTarget2D(WorldContextObject, SceneCapture->TextureTarget, FLinearColor::Transparent); + + // Disable post-processing effects + SceneCapture->PostProcessSettings.bOverride_AutoExposureMethod = true; + SceneCapture->PostProcessSettings.AutoExposureMethod = EAutoExposureMethod::AEM_Manual; + SceneCapture->PostProcessSettings.bOverride_AutoExposureBias = true; + SceneCapture->PostProcessSettings.AutoExposureBias = 1.0f; + SceneCapture->PostProcessSettings.bOverride_BloomIntensity = true; + SceneCapture->PostProcessSettings.BloomIntensity = 0.0f; + + // Trigger a one-time capture + SceneCapture->CaptureScene(); + + // Clean up the temporary actor + TempActor->Destroy(); + + return RenderTarget; +} diff --git a/Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h b/Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h new file mode 100644 index 00000000..459c7ac9 --- /dev/null +++ b/Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h @@ -0,0 +1,25 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Kismet/BlueprintFunctionLibrary.h" +#include "LRRenderUtilLibrary.generated.h" + +/** + * + */ +UCLASS() +class LUCKYWORLDV2_API ULRRenderUtilLibrary : public UBlueprintFunctionLibrary +{ + GENERATED_BODY() + +public: + + UFUNCTION(BlueprintCallable, Category = "Render Utilities") + UTextureRenderTarget2D* CaptureMeshToRenderTarget(UStaticMeshComponent* MeshComp, const FVector2D& ImageSize, UObject* WorldContextObject); + +private: + void SetupSceneCaptureForMesh(USceneCaptureComponent2D* SceneCapture, UStaticMeshComponent* MeshComp); + +};