【发布时间】:2021-12-17 20:35:36
【问题描述】:
我想将游戏中的相机视图转换为永久纹理。 为此,我设法将渲染目标转换为 Texture2d。 现在,如果我将此 Texture2D 保存在纹理数组中,所有纹理都是相同的,因为它们都引用相同的指针,我很难解决这个问题。我假设我必须创建一个新的纹理对象,但是...我还没有设法...
我希望你能帮助我。
这是我的代码和相应的蓝图。
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;
}
【问题讨论】:
标签: c++ textures unreal-engine4 texture2d