欢迎您光临本站,秉承服务宗旨,履行"站长"责任,销售只是起点,服务永无止境!

UE4虚拟纹理-(RuntimeVirtualTexture)转化为Texture

作者 : qwe789 本文共4798个字,预计阅读时间需要12分钟 发布时间: 2023-01-19 共2.09K人阅读
广告
也想出现在这里? 联系我们

UE4虚拟纹理-(RuntimeVirtualTexture)转化为Texture

一。展示效果

1.如你所见把RuntimeVirtualTexture转化为了Texture

二。原理解释

1.中心思想 把虚拟纹理渲染的图片数据拿到 用图片数据创建Texture然后保存 是不是很简单咋们开始把 嘿嘿

2.画图讲解

(1)URuntimeVirtualTextureComponent里面存了UVirtualTextureBuilder里面存了UVirtualTexture2D ,UVirtualTexture2D是UTexture2D的子类

(2)拿到UTexture2D的照片信息

(3)创建照片 储存照片

三。代码

4.26 版本可用

.h 头文件

UFUNCTION(BlueprintCallable, Category = "Pricture Tool")
		static bool CreateTexture2DAssetAsVirtual(URuntimeVirtualTextureComponent* InComponent, FString TextureName, FString TexturePath);

.Cpp 内容实现

bool UStaticBlueprintLibrary::CreateTexture2DAssetAsVirtual(URuntimeVirtualTextureComponent* InComponent, FString TextureName, FString TexturePath)
{
	TArray64<uint8> RawData1;
	int32 TextureWidth;
	int32 TextureHeight;
	FTextureSource VirtualSource;

	UVirtualTextureBuilder* VirtualTextureBuilder = InComponent->GetStreamingTexture();
	if (VirtualTextureBuilder)
	{
		if (VirtualTextureBuilder->Texture)
		{
			VirtualSource = VirtualTextureBuilder->Texture->Source;
		}
		
	}

	VirtualSource.GetMipData(RawData1, 0);
	TextureWidth = VirtualSource.GetSizeX();
	TextureHeight = VirtualSource.GetSizeY();

	FString PackageName = TEXT("/Game/");
	PackageName = PackageName + TexturePath;
	PackageName += TextureName;
	UPackage* Package = CreatePackage(NULL, *PackageName);
	Package->FullyLoad();

	UTexture2D* NewTexture = NewObject<UTexture2D>(Package, *TextureName, RF_Public | RF_Standalone | RF_MarkAsRootSet);

	NewTexture->AddToRoot();				// This line prevents garbage collection of the texture
	NewTexture->PlatformData = new FTexturePlatformData();	// Then we initialize the PlatformData
	NewTexture->PlatformData->SizeX = TextureWidth;
	NewTexture->PlatformData->SizeY = TextureHeight;
	NewTexture->PlatformData->SetNumSlices(1);
	NewTexture->PlatformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;


	// Allocate first mipmap.
	FTexture2DMipMap* Mip = new(NewTexture->PlatformData->Mips) FTexture2DMipMap();
	Mip->SizeX = TextureWidth;
	Mip->SizeY = TextureHeight;

	// Lock the texture so it can be modified
	Mip->BulkData.Lock(LOCK_READ_WRITE);
	uint8* TextureData = (uint8*)Mip->BulkData.Realloc(RawData1.Num());
	FMemory::Memcpy(TextureData, RawData1.GetData(), RawData1.Num());
	Mip->BulkData.Unlock();

	NewTexture->MipGenSettings = TMGS_NoMipmaps;
	NewTexture->Source.Init(TextureWidth, TextureHeight, 1, 1, ETextureSourceFormat::TSF_BGRA8, RawData1.GetData());

	NewTexture->UpdateResource();
	Package->MarkPackageDirty();
	FAssetRegistryModule::AssetCreated(NewTexture);

	FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
	bool bSaved = UPackage::SavePackage(Package, NewTexture, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);

	return true;
}

4.24代码可用 4.24的存储结构有点变化 我相信你能看懂

.h 头文件

	UFUNCTION(BlueprintCallable, Category = "Pricture Tool")
		static bool CreateTexture2DAssetAsVirtual(URuntimeVirtualTextureComponent* InComponent, FString TextureName);

.Cpp 内容实现

bool UStaticBlueprintLibrary::CreateTexture2DAssetAsVirtual(URuntimeVirtualTextureComponent* InComponent, FString TextureName)
{
	TArray64<uint8> RawData1;
	int32 TextureWidth;
	int32 TextureHeight;
	FTextureSource VirtualSource;
	URuntimeVirtualTexture* VirtualTextureBuilder = InComponent->GetVirtualTexture();
	if (VirtualTextureBuilder)
	{
		if (VirtualTextureBuilder)
		{
			VirtualSource = VirtualTextureBuilder->StreamingTexture->Source;
		}
		
	}

	VirtualSource.GetMipData(RawData1, 0);
	TextureWidth = VirtualSource.GetSizeX();
	TextureHeight = VirtualSource.GetSizeY();

	FString PackageName = TEXT("/Game/ProceduralTextures/");
	PackageName += TextureName;
	UPackage* Package = CreatePackage(NULL, *PackageName);
	Package->FullyLoad();

	UTexture2D* NewTexture = NewObject<UTexture2D>(Package, *TextureName, RF_Public | RF_Standalone | RF_MarkAsRootSet);

	NewTexture->AddToRoot();				// This line prevents garbage collection of the texture
	NewTexture->PlatformData = new FTexturePlatformData();	// Then we initialize the PlatformData
	NewTexture->PlatformData->SizeX = TextureWidth;
	NewTexture->PlatformData->SizeY = TextureHeight;
	NewTexture->PlatformData->SetNumSlices(1);
	NewTexture->PlatformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;


	// Allocate first mipmap.
	FTexture2DMipMap* Mip = new(NewTexture->PlatformData->Mips) FTexture2DMipMap();
	Mip->SizeX = TextureWidth;
	Mip->SizeY = TextureHeight;

	// Lock the texture so it can be modified
	Mip->BulkData.Lock(LOCK_READ_WRITE);
	uint8* TextureData = (uint8*)Mip->BulkData.Realloc(RawData1.Num());
	FMemory::Memcpy(TextureData, RawData1.GetData(), RawData1.Num());
	Mip->BulkData.Unlock();

	NewTexture->MipGenSettings = TMGS_NoMipmaps;
	NewTexture->Source.Init(TextureWidth, TextureHeight, 1, 1, ETextureSourceFormat::TSF_BGRA8, RawData1.GetData());

	NewTexture->UpdateResource();
	Package->MarkPackageDirty();
	FAssetRegistryModule::AssetCreated(NewTexture);

	FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
	bool bSaved = UPackage::SavePackage(Package, NewTexture, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);

	return true;
}

版权声明:
本站所有文章、资源等一切内容,皆为在本站的注册网友所发布、上传、提供,如您发现任何内容侵犯了您的合法权益, 请与我们联系 ,我们将第一时间进行清理。iiiue.com 旨在为广大虚幻引擎爱好者提供技术交流学习、知识技术变现平台。
永久域名 iii ue .com 本站投稿能赚取收益变现提现,请一定要牢记账号密码!
Ue资源站 iiiue.com » UE4虚拟纹理-(RuntimeVirtualTexture)转化为Texture
广告
也想出现在这里? 联系我们

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有内容皆为网友发布,资源版权均属于原作者所有,如需商用请联系原作者获取授权许可,若由于商用引起版权纠纷,一切责任均由使用者承担。如您发现某些内容侵犯了您的合法权益, 请与我们联系 ,我们将在第一时间核实并清理。
下载的资源解压密码是多少?
任何资源的解压密码均由发布者提供,一般情况下都在资源的发布页面或者资源文件夹,请仔细检查,如您发现发布页面没有提供解压密码,请您试着在评论处留言联系作者
点击查看解压软件和密码说明
Tips:除了密码说明内的解压密码外,通常情况下,电脑在安装好RAR或360解压缩软件,双击打开压缩包,包内注释的网址也可以试试是否为解压密码哦
资源能否免费获取
本站用户可参与站内的一系列活动获取积分,资源皆可免费获取,若想快速获取可加入本站永久VIP钻石会员,荣耀身份,全站资源免费获取, 点此查看详情

发表回复

广告
也想出现在这里? 联系我们
开通终身VIP 永久VIP折扣活动火热进行中,享全站资源免费下载特权!