forked from LuckyRobots/LuckyWorldV2
Scaffold render target library
This commit is contained in:
parent
22e2b0a419
commit
8dc892b9fa
87
Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp
Normal file
87
Source/LuckyWorldV2/Private/LRRenderUtilLibrary.cpp
Normal file
@ -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<AActor>();
|
||||
USceneCaptureComponent2D* SceneCapture = NewObject<USceneCaptureComponent2D>(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;
|
||||
}
|
25
Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h
Normal file
25
Source/LuckyWorldV2/Public/LRRenderUtilLibrary.h
Normal file
@ -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);
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user