UE通过读取外部数据创建体积纹理
·
一、从txt读取体积数据
std::vector ULoadFileToData::ReadFileToData()
{
std::ifstream file(“C://Users//20329//Desktop//VolumeQT//untitled//volumedata1.txt”);
if (!file) {
// UE_LOG()
std::vector<float> temp;
return temp;
}
std::vector<float> values;
std::string line;
int rows = 0;
while (std::getline(file, line)) {
std::istringstream iss(line);
while (iss) {
std::string token;//world
if (!std::getline(iss, token, ',')) break;
if (rows > 0)//x,y,z,t 不要
values.push_back(std::stof(token));
}
rows++;
}
file.close();
return values;
}
二、解析体积数据,并将数据注入体积纹理
#include “Engine/TextureRenderTarget2D.h”
#include “Engine/VolumeTexture.h”
#include “Engine/World.h”
#include <Engine/TextureRenderTargetVolume.h>
void ULoadFileToData::LoadDataToVolume()
{
std::vector<float> values = ReadFileToData();//原始数据
TArray<FColor>Mydata;
std::vector<float>test;
const ETextureSourceFormat format = ETextureSourceFormat::TSF_BGRA8;
int Dimensions[3] = {251,376,26};
//归一化
//for (int i = 0; i < values.size(); i)
//{
// values[i] = values[i] / Dimensions[0];//x
// values[i + 1] = values[i + 1] / Dimensions[1];//y
// values[i + 2] = (values[i + 2] + 100) / 100;//z
// values[i + 3] = values[i + 3];//t
// i=i + 4;
//}
int Dimxy = Dimensions[0] * Dimensions[1];
//转化成颜色FColor
for (int z = 0; z < Dimensions[2]; z++)
{
for (int j = 0; j < Dimensions[1]; j++)
{
for (int i = 0; i < Dimensions[0]; i++)
{
//找到对应原始数据的映射关系
float tt = values[z * Dimxy * 4 + j * Dimensions[0] * 4 + i * 4 + 3];//0-33度
//颜色映射
FColor tempc;
if (30 <= tt &&tt< 33) {
tempc = FColor(255,0,0,255);
}
else if (25<=tt&& tt <30)
{
tempc = FColor(250, 29, 29, 255);
}
else if (20 <= tt && tt < 25)
{
tempc = FColor(250, 188, 30, 255);
}
else if (10<= tt &&tt< 20)
{
tempc = FColor(60, 50, 0, 255);
}
else if (tt==0.0)
{
tempc = FColor(0,0,0,0);
}
else
{
tempc = FColor(100,0,100,100);
//int s = sizeof(tempc);4
}
Mydata.Push(tempc);
}
}
}
const FString Path = "/Game"; //路径
const FString Name = "VolumeTex2"; //名字
//包的完整路径:
const FString PackageName = Path + TEXT("/") + Name;
//创建包:
UPackage* pacakge = CreatePackage(NULL, *PackageName);
pacakge->FullyLoad();
int PixelByteSize = GPixelFormats[EPixelFormat::PF_B8G8R8A8].BlockBytes;
//uint8* LoadArray = new uint8[(long long)Dimensions[0] * Dimensions[1] * Dimensions[2] * PixelByteSize];//bug 此处还得处理//Dimensions.X * Dimensions.Y * Dimensions.Z * BytesPerVoxel;
UVolumeTexture* VolumeTexture = nullptr;
//NewObject<UTexture2D>(pacakge, FName(*Name), RF_Public | RF_Standalone);
VolumeTexture = NewObject<UVolumeTexture>((UObject*)pacakge, FName(*Name), RF_Public | RF_Standalone| RF_MarkAsRootSet);
// Prevent garbage collection of the texture
VolumeTexture->AddToRoot();
///设置细节
VolumeTexture->PlatformData = new FTexturePlatformData();
VolumeTexture->PlatformData->SizeX = Dimensions[0];
VolumeTexture->PlatformData->SizeY = Dimensions[1];
VolumeTexture->PlatformData->SetNumSlices(Dimensions[2]);
VolumeTexture->PlatformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;// no sure
VolumeTexture->CompressionNone = true;
VolumeTexture->Source.Init(Dimensions[0], Dimensions[1], Dimensions[2], 1, ETextureSourceFormat::TSF_BGRA8);
//数据指针
uint32* BufferAddress = (uint32*)VolumeTexture->Source.LockMip(0);
//数据应有的尺寸(应该和 MyData 一致)
const int32 BufferSize = VolumeTexture->Source.CalcMipSize(0);
FMemory::Memcpy(BufferAddress, Mydata.GetData(), BufferSize);
//数据拷贝结束
VolumeTexture->Source.UnlockMip(0);
//给包标脏
VolumeTexture->MarkPackageDirty();
//结束修改
VolumeTexture->PostEditChange();
}
注意: VolumeTexture->CompressionNone = true;//不压缩纹理
数据格式:

结果


DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)