UE4 从 C++ 创建纹理
Posted
技术标签:
【中文标题】UE4 从 C++ 创建纹理【英文标题】:UE4 create texture from C++ 【发布时间】: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;
【问题讨论】:
【参考方案1】:在这里找到答案: https://answers.unrealengine.com/questions/193827/how-to-get-texture-pixels-using-utexturerendertarg.html
void UScreenShotToTexture::CreateTexture()
UTextureRenderTarget2D* TextureRenderTarget;
// Creates Texture2D to store TextureRenderTarget content
UTexture2D *Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX, TextureRenderTarget->SizeY, PF_B8G8R8A8);
#if WITH_EDITORONLY_DATA
Texture->MipGenSettings = TMGS_NoMipmaps;
#endif
Texture->SRGB = TextureRenderTarget->SRGB;
// Read the pixels from the RenderTarget and store them in a FColor array
TArray<FColor> SurfData;
FRenderTarget *RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
RenderTarget->ReadPixels(SurfData);
// Lock and copies the data between the textures
void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
const int32 TextureDataSize = SurfData.Num() * 4;
FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
Texture->PlatformData->Mips[0].BulkData.Unlock();
// Apply Texture changes to GPU memory
Texture->UpdateResource();
【讨论】:
以上是关于UE4 从 C++ 创建纹理的主要内容,如果未能解决你的问题,请参考以下文章