从HeightMap生成的球形地形具有间隙

时间:2019-07-19 18:55:02

标签: c++ unreal-engine4 heightmap

我用DynamicLOD创建了二十面体。这很好。现在我想使用高度图添加地形。首先,我尝试使用GrayScale HeightMap。但是结果只是随机位移。比起我尝试下面的黑白图像,我得到了: enter image description here

为了进行测试,我使用了黑白图像。 enter image description here

如您所见,没有数据显示差距。 首先,我以为我在读取图像时有一些错误。但是,当我将内存中的数据写回到文件中时,我会有相同的图像。

这是我阅读图片的方式:

TArray<uint8> TempData;
if (!FFileHelper::LoadFileToArray(TempData, TEXT("../../../../../UnrealProjects/LODTest/Content/Materials/nocompression.png") , FILEREAD_Silent))
{
    UE_LOG(LogTemp, Warning, TEXT("Could not load File"));
}
else
{
    IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>("ImageWrapper");
    TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

    if (!ImageWrapper->SetCompressed(TempData.GetData(), TempData.Num()))
    {
        UE_LOG(LogTemp, Warning, TEXT("Compress Error"));
    }

    if (ImageWrapper->GetFormat() != ERGBFormat::Gray)
    {
        UE_LOG(LogTemp, Warning, TEXT("Not GrayScale"));
    }
    if (ImageWrapper->GetBitDepth() != 16)
    {
        UE_LOG(LogTemp, Warning, TEXT("Not 16Bit"));
    }

    const TArray<uint8>* RawData = nullptr;

    if (!ImageWrapper->GetRaw(ERGBFormat::Gray, 16, RawData))
    {
        UE_LOG(LogTemp, Warning, TEXT("Could not get Raw Data"));
    }
    else
    {

        heightValues.SetNum(ImageWrapper->GetWidth() * ImageWrapper->GetHeight());
        FMemory::Memcpy(heightValues.GetData(), RawData->GetData(), ImageWrapper->GetWidth() * ImageWrapper->GetHeight() * 2);
        UE_LOG(LogTemp, Warning, TEXT("Success"));
    }
}

这里是我如何读取值:(点是UV坐标)

uint16 heightValue = heightMap[(point.X * imageWidth) * (point.Y * imageHeight)];

float value = heightValue / 65535.f;

return value;

我将网格导出到Blender,并在那里使用HeightMap,没有任何问题。 现在我没了主意:(

编辑: 正如Azarus所说,我获得身高值的方法是错误的。现在我用这个:

int32 locX = UKismetMathLibrary::MapRangeClamped(point.X, 0.f, 1.f, 0.f, imageWidth);
int32 locY = UKismetMathLibrary::MapRangeClamped(point.Y, 0.f, 1.f, 0.f, imageHeight);

uint16 heightValue = heightMap[imageWidth * locY + locX];

1 个答案:

答案 0 :(得分:1)

您的身高采样似乎是错误的:

heightMap[(point.X * imageWidth) * (point.Y * imageHeight)]

这真的是基础数学。

  • 如果X为0且Y为1怎么办?
  • 如果X为1而Y为0会怎样?

答案是一样的,就是问题所在 1*0=00*1=0

虽然您显然想要其他输入。

假设您已正确读取图像,然后将2D坐标压缩为1D,则以下公式应该起作用:

将2D阵列坐标压缩为1D:

Index=(X*RowSize)+Y

将此应用于您的代码:

heightMap[(point.X * imageWidth) + point.Y]