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;//不压缩纹理
数据格式:
在这里插入图片描述

结果

请添加图片描述
请添加图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值