需要使用二维高度数据动态加载为灰度图,经过查找,有两个经典案例参考
1.从本地读取图片,显示为UTexture2D:
https://zhuanlan.zhihu.com/p/373649289
2.将二维数组显示为UTexture2D并保存为Asset到硬盘
https://isaratech.com/save-a-procedurally-generated-texture-as-a-new-asset/
亲测两个代码可以直接使用。
经过对比与修改,得到从二维数据生成UTexture2D的代码。
UTexture2D * UMyBlueprintFunctionLibrary::CreateTextureFromArray()
{
int32 TextureHeight = 1024;
int32 TextureWidth = 1024;
UTexture2D* TheTexture2D = UTexture2D::CreateTransient(TextureHeight, TextureWidth, PF_B8G8R8A8);
uint8* Pixels = new uint8[TextureWidth * TextureHeight * 4];
for (int32 y = 0; y < TextureHeight; y++)
{
for (int32 x = 0; x < TextureWidth; x++)
{
int32 curPixelIndex = ((y * TextureWidth) + x);
int32 color = x;
while (color > 255)
color -= 255;
Pixels[4 * curPixelIndex] = color;
Pixels[4 * curPixelIndex + 1] = color;
Pixels[4 * curPixelIndex + 2] = color;
Pixels[4 * curPixelIndex + 3] = 255;
}
}
void* TextureData = TheTexture2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, Pixels, sizeof(uint8) * TextureHeight * TextureWidth * 4);
TheTexture2D->PlatformData->Mips[0].BulkData.Unlock();//TheTexture2D->PlatformData = new FTexturePlatformData(); // Then we initialize the PlatformData
TheTexture2D->UpdateResource();
return TheTexture2D;
}
UTexture2D * UMyBlueprintFunctionLibrary::CreateTextureFromArray_Vertical()
{
int32 TextureHeight = 1024;
int32 TextureWidth = 1024;
UTexture2D* TheTexture2D = UTexture2D::CreateTransient(TextureHeight, TextureWidth, PF_B8G8R8A8);
uint8* Pixels = new uint8[TextureWidth * TextureHeight * 4];
for (int32 y = 0; y < TextureHeight; y++)
{
for (int32 x = 0; x < TextureWidth; x++)
{
int32 curPixelIndex = ((y * TextureWidth) + x);
//根据y的值进行颜色渐变
int32 color = y;
while (color > 255)
color -= 255;
Pixels[4 * curPixelIndex] = color;
Pixels[4 * curPixelIndex + 1] = color;
Pixels[4 * curPixelIndex + 2] = color;
Pixels[4 * curPixelIndex + 3] = 255;
}
}
void* TextureData = TheTexture2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, Pixels, sizeof(uint8) * TextureHeight * TextureWidth * 4);
TheTexture2D->PlatformData->Mips[0].BulkData.Unlock();//TheTexture2D->PlatformData = new FTexturePlatformData(); // Then we initialize the PlatformData
TheTexture2D->UpdateResource();
return TheTexture2D;
}
在蓝图中调用
效果图
根据存储DEM高程数据的全局变量Lanscape[N][N],可以得到灰度图
extern int16 Landscape[N][N];//存储高度数据
UTexture2D * UMyBlueprintFunctionLibrary::CreateTextureFromArray()
{
UE_LOG(LogTemp, Warning, TEXT("CreateTexture Start!"));
int32 MaxHeight = Landscape[0][0];
int32 MinHeight = Landscape[0][0];
UE_LOG(LogTemp, Warning, TEXT("landscape[0][0]:%d"), Landscape[0][0]);
for (int32 i = 0; i < 721; ++i)
{
for (int32 j = 0; j < 721; ++j)
{
if (MaxHeight < Landscape[i][j])
MaxHeight = Landscape[i][j];
if (MinHeight > Landscape[i][j])
MinHeight = Landscape[i][j];
}
}
UE_LOG(LogTemp, Warning, TEXT("MaxHeight:%d,MinHeight:%d"), MaxHeight, MinHeight);
int32 TextureHeight = 722;
int32 TextureWidth = 722;
uint8* Pixels = new uint8[TextureWidth * TextureHeight * 4];
for (int32 y = 0; y < TextureHeight; y++)
{
for (int32 x = 0; x < TextureWidth; x++)
{
if (x >= 721 || y >= 721)
{
int32 curPixelIndex = ((y * TextureWidth) + x);
Pixels[4 * curPixelIndex] = 255;
Pixels[4 * curPixelIndex + 1] = 255;
Pixels[4 * curPixelIndex + 2] = 255;
Pixels[4 * curPixelIndex + 3] = 200;
}
else
{
int32 h = Landscape[y][x];
int32 curPixelIndex = ((y * TextureWidth) + x);
float color = (((float)h - MinHeight) / (MaxHeight - MinHeight)) * 150+50;
Pixels[4 * curPixelIndex] = color;
Pixels[4 * curPixelIndex + 1] = color;
Pixels[4 * curPixelIndex + 2] = color;
Pixels[4 * curPixelIndex + 3] = 255;
}
}
}
UTexture2D* TheTexture2D = UTexture2D::CreateTransient(TextureHeight, TextureWidth, PF_B8G8R8A8);
void* TextureData = TheTexture2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, Pixels, sizeof(uint8) * TextureHeight * TextureWidth * 4);
TheTexture2D->PlatformData->Mips[0].BulkData.Unlock();//TheTexture2D->PlatformData = new FTexturePlatformData(); // Then we initialize the PlatformData
TheTexture2D->UpdateResource();
ifTextureNeedToUpdate = false;
return TheTexture2D;
}
效果如下