Unreal Engine4 使用c++创建贴图

一般我们将贴图资源导入引擎的时候,引擎会自己抽取贴图文件的数据然后创建一个UTexture的Object合一个Uasset。但是有时候我们想自己写工具的话需要自己实现这套创建机制。

Unreal Engine4 使用c++创建贴图_第1张图片

可以在引擎的UTextureFactory找到我们导入引擎时的逻辑代码。可以看到不同的图片格式,不同的像素深度创建的像素格式合贴图格式都不一样。

Unreal Engine4 使用c++创建贴图_第2张图片

要使用代码创建贴图资源我们第一部是先指定一个创建贴图的地方

// Build a package name to use for mesh
	FString MeshName;
	FString PackageName;

	if (InPackageName.IsEmpty())
	{
		FString NewNameSuggestion = FString(TEXT("StaticMesh"));
		FString PackageNameSuggestion = FString(TEXT("/Game/Meshes/")) + NewNameSuggestion;
		FString Name;
		FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked("AssetTools");
		AssetToolsModule.Get().CreateUniqueAssetName(PackageNameSuggestion, TEXT(""), PackageNameSuggestion, Name);

		TSharedPtr PickAssetPathWidget =
			SNew(SDlgPickAssetPath)
			.Title(LOCTEXT("ConvertToStaticMeshPickName", "Choose New StaticMesh Location"))
			.DefaultAssetPath(FText::FromString(PackageNameSuggestion));

		if (PickAssetPathWidget->ShowModal() == EAppReturnType::Ok)
		{
			// Get the full name of where we want to create the mesh asset.
			PackageName = PickAssetPathWidget->GetFullAssetPath().ToString();
			MeshName = FPackageName::GetLongPackageAssetName(PackageName);

			// Check if the user inputed a valid asset name, if they did not, give it the generated default name
			if (MeshName.IsEmpty())
			{
				// Use the defaults that were already generated.
				PackageName = PackageNameSuggestion;
				MeshName = *Name;
			}
		}
	}
	else
	{
		PackageName = InPackageName;
		MeshName = *FPackageName::GetLongPackageAssetName(PackageName);
	}

	// Build a package name to use for material
	FString NewMergeOneMaterialName = MeshName + TEXT("_MergeMat");
	FString NewMergeOneMaterialPackageName = PackageName + TEXT("_MergeMat");

	//Build a package name to use for texture
	FString NewMergedTextureName = MeshName + TEXT("_MergeTex");
	FString NewMergedTexturePackageName = PackageName + TEXT("_MergeTex");

有了这个路径之后我们New一个Package

UPackage* TexturePackage = CreatePackage(NULL, *NewMergedTexturePackageName);

然后New一个Object,与此同时把刚才New的哪个Texture Object塞到这个Package里

UTexture2D* MergedTexture = NewObject(TexturePackage, *NewMergedTextureName, RF_Public | RF_Standalone);

接下来需要初始化这个Texture

使用

Source.Init(DestTextureSizeX, DestTextureSizeY, 1, 1, TSF_BGRA8);

Texture的Source中的Init方法初始化它即可,注意格式。

你可能感兴趣的:(游戏开发;,虚幻4)