Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TArray<UObject*> FMyPluginModule::ImportTexture(const TArray<FString>& Paths)
- {
- for (FString Path : Paths)
- {
- Path = Path.Replace(TEXT("\\"), TEXT("/"));
- }
- /*FAssetToolsModule& AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
- return AssetToolsModule.Get().ImportAssets(Paths, TEXT("/Game/Imported"));*/
- UAutomatedAssetImportData* importData = NewObject<UAutomatedAssetImportData>();
- importData->bReplaceExisting = true;
- importData->DestinationPath = TEXT("/Game/Imported");
- importData->Filenames = Paths;
- FAssetToolsModule& AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
- TArray<UObject*> importedAssets = AssetToolsModule.Get().ImportAssetsAutomated(importData);
- return importedAssets;
- }
- UMaterial* TryAddMaterial(TMap<FString, UMaterial*>& Materials,const FString Name, UMaterialFactoryNew* MaterialFactory)
- {
- if (!Materials.Contains(Name))
- {
- UPackage* Package = CreatePackage(*(FString::Printf(TEXT("/Game/ImportedMaterial/%s"), *Name)));
- UObject* NewMat = MaterialFactory->FactoryCreateNew(MaterialFactory->SupportedClass, Package, *Name, EObjectFlags::RF_Standalone | EObjectFlags::RF_Public, nullptr, GWarn);
- /*UMaterialInstanceConstantFactoryNew* InstanceFactory = NewObject<UMaterialInstanceConstantFactoryNew>();
- FString InstanceName = Name + TEXT("_Instance");
- UObject* Instance = InstanceFactory->FactoryCreateNew(InstanceFactory->SupportedClass, NewMat, *InstanceName, EObjectFlags::RF_Standalone | EObjectFlags::RF_Public, nullptr, GWarn);
- UMaterialInstanceConstant* InstanceMat = Cast<UMaterialInstanceConstant>(Instance);*/
- UMaterial* Material = Cast<UMaterial>(NewMat);
- Materials.Add(Name, Material);
- FAssetRegistryModule::AssetCreated(Material);
- return Material;
- }
- return Materials[Name];
- }
- bool FMyPluginModule::Exec(UWorld* InWorld, const TCHAR* Cmd, FOutputDevice& Ar)
- {
- if (FParse::Command(&Cmd, TEXT("MakeMaterial")))
- {
- FString Param;
- if (FParse::Value(Cmd, TEXT("Path"), Param))
- {
- TArray<FString> FoundFiles = TArray<FString>();
- FFileManagerGeneric FileManager = FFileManagerGeneric();
- FileManager.FindFilesRecursive(FoundFiles, *Param, TEXT("*.png"), true, true, true);
- FString BaseColor = "_BaseColor";
- FString Normal = "_Normal";
- FString ORM = "_OcclusionRoughnessMetallic";
- FString Emissive = "_Emissive";
- FString FileName;
- TArray<UObject*> Imported = ImportTexture(FoundFiles);
- TMap<FString,UMaterial*> Materials;
- UMaterialFactoryNew* MatFactory = NewObject<UMaterialFactoryNew>();
- for (UObject* Texture : Imported)
- {
- FileName = Texture->GetName();
- //UE_LOG(LogTemp, Warning, TEXT("%s"), *Path);
- if (FileName.Contains(BaseColor))
- {
- FString Name;
- FileName.Split(BaseColor, &Name, nullptr);
- UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
- Material->Modify(); //Start modify material
- UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
- TextureExpression->Texture = Cast<UTexture>(Texture);
- Material->Expressions.Add(TextureExpression);
- Material->BaseColor.Expression = TextureExpression;
- Material->PostEditChange();
- Material->MarkPackageDirty();
- }
- if (FileName.Contains(Normal))
- {
- FString Name;
- FileName.Split(Normal, &Name, nullptr);
- UE_LOG(LogTemp, Error, TEXT("%s"), *Name);
- UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
- Material->Modify(); //Start modify material
- UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
- TextureExpression->SamplerType = EMaterialSamplerType::SAMPLERTYPE_Normal;
- TextureExpression->Texture = Cast<UTexture>(Texture);
- Material->Expressions.Add(TextureExpression);
- Material->Normal.Expression = TextureExpression;
- Material->PostEditChange();
- Material->MarkPackageDirty();
- }
- if (FileName.Contains(ORM))
- {
- UTexture* MatTex = Cast<UTexture>(Texture);
- MatTex->CompressionSettings = TextureCompressionSettings::TC_Default;
- MatTex->SRGB = false;
- //capire come collegare i parametri separati
- FString Name;
- FileName.Split(ORM, &Name, nullptr);
- UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
- Material->Modify(); //Start modify material
- UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
- TextureExpression->Texture = MatTex;
- TextureExpression->SamplerType = EMaterialSamplerType::SAMPLERTYPE_LinearColor;
- TArray<FExpressionOutput> Out = TextureExpression->GetOutputs();
- Material->Expressions.Add(TextureExpression);
- Material->AmbientOcclusion.Connect(1, TextureExpression);
- Material->Roughness.Connect(2, TextureExpression);
- Material->Metallic.Connect(3, TextureExpression);
- Material->PostEditChange();
- Material->MarkPackageDirty();
- }
- if (FileName.Contains(Emissive))
- {
- FString Name;
- FileName.Split(Emissive, &Name, nullptr);
- UMaterial* Material = TryAddMaterial(Materials, Name, MatFactory);
- Material->Modify(); //Start modify material
- UMaterialExpressionTextureSample* TextureExpression = NewObject<UMaterialExpressionTextureSample>(Material);
- TextureExpression->Texture = Cast<UTexture>(Texture);
- Material->Expressions.Add(TextureExpression);
- Material->EmissiveColor.Expression = TextureExpression;
- Material->PostEditChange();
- Material->MarkPackageDirty();
- }
- }
- }
- }
- if (FParse::Command(&Cmd, TEXT("MakeMaterial")))
- {
- MakeMaterialExample();
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement