我正在使用 1920x1080 之类的分辨率乘以 4 这样的过采样值来渲染视口(viewport)。现在我需要将渲染分辨率从 7680x4320 下采样回到 1920x1080。
Unreal 中是否有我可以使用的函数?或者任何可以很好地处理这个问题的库(仅限Windows)? 或者我自己写这个的正确方法是什么?
我们尝试实现下采样,但它仅在 SnapshotScale 为 2 时有效,当它高于 2 时,它似乎对图像质量没有影响。
UTexture2D* AAVESnapShotManager::DownsampleTexture(UTexture2D* Texture)
{
UTexture2D* Result = UTexture2D::CreateTransient(RenderSettings.imageWidth, RenderSettings.imageHeight, PF_B8G8R8A8);
void* TextureDataVoid = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY);
void* ResultDataVoid = Result->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FColor* TextureData = (FColor*)TextureDataVoid;
FColor* ResultData = (FColor*)ResultDataVoid;
int32 WindowSize = RenderSettings.resolutionScale / 2;
for (int x = 0; x < Result->GetSizeX(); ++x)
{
for (int y = 0; y < Result->GetSizeY(); ++y)
{
const uint32 ResultIndex = y * Result->GetSizeX() + x;
uint32_t R = 0, G = 0, B = 0, A = 0;
int32 Samples = 0;
for (int32 dx = -WindowSize; dx < WindowSize; ++dx)
{
for (int32 dy = -WindowSize; dy < WindowSize; ++dy)
{
int32 PosX = (x * RenderSettings.resolutionScale + dx);
int32 PosY = (y * RenderSettings.resolutionScale + dy);
if (PosX < 0 || PosX >= Texture->GetSizeX() || PosY < 0 || PosY >= Texture->GetSizeY())
{
continue;
}
size_t TextureIndex = PosY * Texture->GetSizeX() + PosX;
FColor& Color = TextureData[TextureIndex];
R += Color.R;
G += Color.G;
B += Color.B;
A += Color.A;
++Samples;
}
}
ResultData[ResultIndex] = FColor(R / Samples, G / Samples, B / Samples, A / Samples);
}
}
Texture->PlatformData->Mips[0].BulkData.Unlock();
Result->PlatformData->Mips[0].BulkData.Unlock();
Result->UpdateResource();
return Result;
}
我期望高质量的过采样纹理输出,使用 SnapshotScale 中的任何正整数值。
最佳答案
我有一个建议。它不是很直接,但它不涉及图像过滤的编写或库的导入。
使用节点 TextureObject->TextureSample-> 连接到 Emissive 制作一个不发光的 Material 。
使用您在函数中开始使用的纹理来填充 Material 的 Material 实例动态上的纹理对象。
使用“将 Material 绘制到渲染目标”功能将 Material 实例动态绘制到使用您的目标分辨率预设的渲染目标。
关于c++ - 如何在 UnrealEngine 中对非 2 的幂纹理进行下采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124079/