我想将一个ingame摄影机视图转换为一个永久纹理。为此,我设法将rendertarget转换为Texture2d。现在,如果我将这个Texture2D保存在一个Textures数组中,所有的Textures都是相同的,因为它们都指向同一个指针,我很难解决这个问题。我假设我必须创建一个新的纹理对象,但很好。我还没想好..
我希望你能帮助我。
这是我的代码和相应的蓝图。
ScreenShotToTexture.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "ScreenShotToTexture.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UScreenShotToTexture : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UScreenShotToTexture();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
public:
UPROPERTY(EditAnywhere)
UTextureRenderTarget2D* RenderTarget;
UPROPERTY(EditAnywhere)
UTexture2D* Picture;
UFUNCTION(BlueprintCallable) //so visible in BluePrints
UTexture2D* TakePic();
};
ScrenShotToTexture.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "ScreenShotToTexture.h"
#include "Engine/TextureRenderTarget2D.h"
#include "Components/SceneCaptureComponent2D.h"
#include "UObject/ObjectMacros.h"
// Sets default values for this component's properties
UScreenShotToTexture::UScreenShotToTexture()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UScreenShotToTexture::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UScreenShotToTexture::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
UTexture2D* UScreenShotToTexture::TakePic()
{
if(RenderTarget == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("No RenderTarget set."));
return nullptr;
}
Picture = RenderTarget->ConstructTexture2D(this,"Test", EObjectFlags::RF_NoFlags, CTF_Default) ;
return Picture;
}
1条答案
按热度按时间eeq64g8w1#
在这里找到了答案:https://answers.unrealengine.com/questions/193827/how-to-get-texture-pixels-using-utexturerendertarg.html