UE4 从TextureRenderTargetCube获取六个面的图片(C++实现)
时间: 2024-03-23 21:40:07 浏览: 231
图片处理软件(C++)
4星 · 用户满意度95%
要从TextureRenderTargetCube获取六个面的图片,可以通过以下步骤实现:
1. 创建一个RenderTargetCube,用于渲染。
```cpp
UTextureRenderTargetCube* RenderTarget = NewObject<UTextureRenderTargetCube>();
RenderTarget->InitAutoFormat(512);
```
2. 渲染六个面,将结果存储到RenderTarget中。
```cpp
FVector Location(0.f, 0.f, 0.f);
FRotator Rotation(0.f, 0.f, 0.f);
FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(
RenderTarget->GetSizeXY(), FEngineShowFlags(ESFIM_Game))
.SetRealtimeUpdate(true));
FSceneView* View = CalculateView(Location, Rotation, ViewFamily);
View->ViewMatrices.ApplyPreViewTranslation(-Location);
View->ViewMatrices.SetProjectionMatrix(FMatrix::Identity);
View->ViewState.SetViewRect(FIntRect(0, 0, RenderTarget->GetSurfaceWidth(), RenderTarget->GetSurfaceHeight()));
View->Render(ViewFamily);
```
3. 从RenderTarget中获取六个面的图片。
```cpp
UTexture2D* TextureArray[6];
for (int i = 0; i < 6; i++)
{
TextureArray[i] = NewObject<UTexture2D>();
TextureArray[i]->Source.Init(TextureSize, TextureSize, 1, 1, ETextureSourceFormat::TSF_RGBA16F);
TextureArray[i]->UpdateResource();
ENQUEUE_RENDER_COMMAND(CaptureCommand)(
[TextureArray, RenderTarget, i](FRHICommandListImmediate& RHICmdList)
{
check(IsInRenderingThread());
FTextureRenderTargetResource* RenderTargetResource = RenderTarget->GameThread_GetRenderTargetResource();
FReadSurfaceDataFlags ReadPixelFlags(RCM_UNorm, CubeFaceToIndex((ECubeFace)i));
ReadPixelFlags.SetLinearToGamma(true);
RHICmdList.ReadSurfaceData(
RenderTargetResource->GetRenderTargetTexture(),
FIntRect(0, 0, TextureSize, TextureSize),
TextureArray[i]->Source.Lock(LOCK_READ_WRITE),
ReadPixelFlags
);
TextureArray[i]->Source.Unlock();
TextureArray[i]->UpdateResource();
});
FlushRenderingCommands();
}
```
其中,CubeFaceToIndex函数用于将Cube Face转换为数组下标。
```cpp
int CubeFaceToIndex(ECubeFace CubeFace)
{
switch (CubeFace)
{
case CubeFace_PosX:
return 0;
case CubeFace_NegX:
return 1;
case CubeFace_PosY:
return 2;
case CubeFace_NegY:
return 3;
case CubeFace_PosZ:
return 4;
case CubeFace_NegZ:
return 5;
default:
check(false);
return -1;
}
}
```
这样就可以从TextureRenderTargetCube获取六个面的图片了。
阅读全文