UE4使用二维数组动态生成UTexture2D

6 篇文章 0 订阅

需要使用二维高度数据动态加载为灰度图,经过查找,有两个经典案例参考

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;
	
}

效果如下请添加图片描述

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值