I read somwhere in the comments of a ue5 tut that “Heightfield MinMax textures seem to be the heightmap encoded in DXT1 RG containing the differences (MinMax) B Height encoded with DXT1 artifacts.” Don’t know if that is true, though tried to read height values from the said texture at a given location , the values are returning null/zero! Here’s the piece of code:
#include "HeightValueFromTexture.h"
#include "Engine/Texture2D.h"
#include "RenderUtils.h"
float UHeightValueFromTexture::UHeightValueFromTexture::GetHeightValueFromTexture(UTexture2D* Texture, FVector WorldLocation, FVector2D WorldSizeXY)
{
if (!Texture)
{
return 0.0f;
}
int32 Width = Texture->GetSizeX();
int32 Height = Texture->GetSizeY();
// Convert world location to UV space
float U = FMath::Clamp((WorldLocation.X / WorldSizeXY.X), 0.0f, 1.0f);
float V = FMath::Clamp((WorldLocation.Y / WorldSizeXY.Y), 0.0f, 1.0f);
int32 X = FMath::Clamp<int32>(FMath::RoundToInt(U * Width), 0, Width - 1);
int32 Y = FMath::Clamp<int32>(FMath::RoundToInt(V * Height), 0, Height - 1);
FColor* FormattedImageData = static_cast<FColor*>(Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY));
if (!FormattedImageData)
{
return 0.0f;
}
FColor PixelColor = FormattedImageData[X + Y * Width];
float MinHeight = PixelColor.R / 255.0f;
float MaxHeight = PixelColor.G / 255.0f;
float NormalizedHeightValue = PixelColor.B / 255.0f;
Texture->PlatformData->Mips[0].BulkData.Unlock();
// Calculate the height range
float HeightRange = MaxHeight - MinHeight;
// Convert the normalized height value to world units using the height range
float WorldHeightValue = MinHeight + (NormalizedHeightValue * HeightRange);
return WorldHeightValue;
}