【问题标题】:How to downsample a not-power-of-2 texture in UnrealEngine?如何在 UnrealEngine 中对非 2 次幂纹理进行下采样?
【发布时间】:2019-05-14 06:16:26
【问题描述】:

我正在渲染视口,分辨率为 1920x1080 乘以过采样值,例如 4。现在我需要从渲染的分辨率 7680x4320 下采样回 1920x1080。

虚幻引擎中是否有我可以使用的功能?或者任何可以很好地处理这个问题的库(仅限 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 中的任何正 int 值。

【问题讨论】:

    标签: c++ image unreal-engine4


    【解决方案1】:

    我有一个建议。这不是很直接,但它不涉及图像过滤的编写或库的导入。

    1. 使用节点 TextureObject->TextureSample-> 连接到 Emissive。

    2. 使用您在函数中开始使用的纹理在材质的材质实例动态上填充纹理对象。

    3. 使用“绘制材质到渲染目标”功能将材质实例动态绘制到使用您的目标分辨率预设的渲染目标上。

    【讨论】:

    • 谢谢,但即使我从 2048 到 512,“Draw Material to Render Target”也没有给出好的下采样结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多