// 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; }