• 9、osg的texture转换为虚幻引擎的UTexture2D


    osg::Texture中读取纹理图片,并动态转换为虚幻引擎的UTexture2D,不过这段代码只能转换图片格式为R8G8B8A8的纹理图片,其他格式请根据实际情况进行修改

    UTexture2D* ConvertOsgTexture2UeTexture2D(osg::Texture* texture) {
    	if (texture)
    	{
    		const unsigned int numImages = texture->getNumImages();
    		if (numImages != 0) {
    			for (unsigned int j = 0; j < numImages; j++) {
    				osg::ref_ptr<osg::Image> img = texture->getImage(j);
    				//osgb的纹理坐标的y轴和虚幻引擎的纹理坐标的y轴是相反的
    				img->flipVertical();
    				const int imgWidth = img->s();
    				const int imgHeight = img->t();
    
    				UTexture2D* ueTexture2D = NewObject<UTexture2D>();
    				ueTexture2D->AddToRoot();
    				FTexturePlatformData* platformData = new FTexturePlatformData();
    				ueTexture2D->SetPlatformData(platformData);
    				ueTexture2D->PlatformData->SizeX = imgWidth;
    				ueTexture2D->PlatformData->SizeY = imgHeight;
    				ueTexture2D->PlatformData->SetNumSlices(1);
    				ueTexture2D->PlatformData->PixelFormat = EPixelFormat::PF_R8G8B8A8;
    				//填充texture,创建一个像素数组,然后为每个数组定义颜色。数组大小为width*height*每个像素的字节
    				const int32 numBlocksX = imgWidth / GPixelFormats[EPixelFormat::PF_R8G8B8A8].BlockSizeX;
    				const int32 numBlocksY = imgHeight / GPixelFormats[EPixelFormat::PF_R8G8B8A8].BlockSizeY;
    				const int32 size = numBlocksX * numBlocksY * GPixelFormats[EPixelFormat::PF_R8G8B8A8].BlockBytes;
    
    				//接下来,把颜色数组输入到纹理中去
    				FTexture2DMipMap* mip = new FTexture2DMipMap();
    				mip->SizeX = imgWidth;
    				mip->SizeY = imgHeight;
    				mip->BulkData.Lock(LOCK_READ_WRITE);
    				uint8* pTextureData = static_cast<uint8*>(mip->BulkData.Realloc(size));
    				memset(pTextureData, 255, size);
    				for (int k = 0; k < imgWidth * imgHeight; k++)
    				{
    					FMemory::Memcpy(&pTextureData[k * 4], &img->data()[k * 3], 3 * sizeof(uint8));
    				}
    				mip->BulkData.Unlock();
    				ueTexture2D->PlatformData->Mips.Add(mip);
    				//保存texture
    				ueTexture2D->UpdateResource();
    
    				return ueTexture2D;
    			}
    		}
    	}
    	return nullptr;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    uniapp封装接口
    源码分析:规则引擎提交策略
    Redis五种基本数据类型-Hash
    【能效管理】关于学校预付费水电系统云平台应用分析介绍
    现代卷积网络实战系列2:PyTorch构建训练函数、LeNet网络
    STM32-HAL库09-CAN通讯(loopback模式)
    <老式喜剧>
    面试:聊聊阻塞和非阻塞、协程非阻塞式挂起
    前端开发学习 (一) 搭建Vue基础环境
    深圳市商务局2022年电子商务创新发展扶持计划跨境电商通关监管场所服务奖励项目申报指南
  • 原文地址:https://blog.csdn.net/weixin_41364246/article/details/137959513