Unreal/CarlaUE4/Plugins/RoadRunnerCarlaIntegration/Source/RoadRunnerCarlaIntegration/Private/RoadRunnerCarlaIntegration.cpp
////////////////////////////////////////////////////////////////////////////////
// Create a material using CARLA's as a base to allow weather effects. Currently
// only replaces the Road and Foliage materials since the other materials were
// missing vertex coloring or texture scaling.
Create a material using CARLA's as a base to allow weather effects. Currently
only replaces the Road and Foliage materials since the other materials were
missing vertex coloring or texture scaling.
Create a material using CARLA ' s as a base to allow weather effects . Currently only replaces the Road and Foliage materials since the other materials were missing vertex coloring or texture scaling .
void CreateCarlaMaterial(FString sourceFilePath, FString packagePath, FRoadRunnerImportUtil::MaterialInfo materialInfo, int metadataVersion)
	{
		FString materialFullName = materialInfo.Name;
		FString basePackageName = FPackageName::GetLongPackagePath(packagePath) / materialFullName;
		basePackageName = PackageTools::SanitizePackageName(basePackageName);
		if (materialInfo.IsDecal.Equals("true"))
			return;
		FText materialSearchError;
		UMaterialInterface* baseMaterial = nullptr;
		FString materialName;
		static FString materialPath = "/Game/Carla/Static/GenericMaterials/Masters/";
		FString segName = materialInfo.SegmentationType;
		if (segName.Equals("Road") || segName.Equals("Gutter") || segName.Equals("Curb") || segName.Equals("Sidewalk"))
		{
			materialName = "MI_WetPavement";
		}
		else if (segName.Equals("Ground")) 
		{
			return; 
		}
		else 
		{
			if (materialFullName.Contains("Leaves")) 
			{
				materialName = "M_Foliage";
			}
			else
			{
				return; 
			}
		}
		float alphaVal = 1.0f; 
		float transparency = 0.0f;
		if (!materialInfo.TransparencyFactor.IsEmpty())
		{
			transparency = FCString::Atof(*(materialInfo.TransparencyFactor));
		}
		baseMaterial = UMaterialImportHelpers::FindExistingMaterialFromSearchLocation(materialName, materialPath, EMaterialSearchLocation::UnderParent, materialSearchError);
		if (!materialSearchError.IsEmpty() || baseMaterial == nullptr)
		{
			UE_LOG(RoadRunnerCarlaIntegration, Error, TEXT("Base material not found: %s"), *(materialSearchError.ToString()));
			return;
		}
		UMaterial* oldMat = LoadObject<UMaterial>(nullptr, *basePackageName);
		if (oldMat != nullptr)
		{
			TArray<UObject*> objectsToForceDelete;
			objectsToForceDelete.Add(oldMat);
			ObjectTools::ForceDeleteObjects(objectsToForceDelete, false);
		}
		UPackage* package = CreatePackage(NULL, *basePackageName);
		auto materialInstanceFactory = NewObject<UMaterialInstanceConstantFactoryNew>();
		materialInstanceFactory->InitialParent = baseMaterial;
		UMaterialInstanceConstant* unrealMaterial = (UMaterialInstanceConstant*)materialInstanceFactory->FactoryCreateNew(UMaterialInstanceConstant::StaticClass(), package, *materialFullName, RF_Standalone | RF_Public, NULL, GWarn);
		if (unrealMaterial == NULL)
		{
			UE_LOG(RoadRunnerCarlaIntegration, Error, TEXT("Material %s could not be created."), *materialInfo.Name);
			return;
		}
		FAssetRegistryModule::AssetCreated(unrealMaterial);
		package->SetDirtyFlag(true);
		if (materialName.Equals("MI_WetPavement"))
		{
			FRoadRunnerImportUtil::SetTextureParameter(unrealMaterial, FName(TEXT("basecolorA")), sourceFilePath, materialInfo.DiffuseMap, packagePath);
			FRoadRunnerImportUtil::SetTextureParameter(unrealMaterial, FName(TEXT("NormalA")), sourceFilePath, materialInfo.NormalMap, packagePath);
			FRoadRunnerImportUtil::SetTextureParameter(unrealMaterial, FName(TEXT("RoughnessA")), sourceFilePath, materialInfo.SpecularMap, packagePath);
			FStaticParameterSet matStaticParams;
			unrealMaterial->GetStaticParameterValues(matStaticParams);
			for (auto& switchParam : matStaticParams.StaticSwitchParameters)
			{
				if (switchParam.ParameterInfo.Name.IsEqual("MultipleTextures"))
				{
					switchParam.bOverride = true;
					switchParam.Value = true;
				}
			}
			unrealMaterial->UpdateStaticPermutation(matStaticParams);
		}
		else if (materialName.Equals("M_Foliage"))
		{
			FRoadRunnerImportUtil::SetTextureParameter(unrealMaterial, FName(TEXT("BaseColor")), sourceFilePath, materialInfo.DiffuseMap, packagePath);
			FRoadRunnerImportUtil::SetTextureParameter(unrealMaterial, FName(TEXT("None")), sourceFilePath, materialInfo.NormalMap, packagePath);
#if ENGINE_MINOR_VERSION > 18
			unrealMaterial->SetVectorParameterValueEditorOnly(FMaterialParameterInfo(FName(TEXT("SubsurfaceValue")), EMaterialParameterAssociation::GlobalParameter), FLinearColor(0.07f, 0.1f, 0.07f, 1));
#else
			material->SetVectorParameterValueEditorOnly(FName(TEXT("SubsurfaceValue")), FLinearColor(0.07f, 0.1f, 0.07f, 1));
#endif
		}
		else
		{
			return;
		}
		unrealMaterial->PreEditChange(NULL);
		unrealMaterial->PostEditChange();
	}
void CreateCarlaMaterial ( FString sourceFilePath , FString packagePath , FRoadRunnerImportUtil :: MaterialInfo materialInfo , int metadataVersion ) { FString materialFullName = materialInfo . Name ; FString basePackageName = FPackageName :: GetLongPackagePath ( packagePath ) / materialFullName ; basePackageName = PackageTools :: SanitizePackageName ( basePackageName ) ; if ( materialInfo . IsDecal . Equals ( " " ) ) return ; FText materialSearchError ; UMaterialInterface * baseMaterial = nullptr ; FString materialName ; static FString materialPath = " " ; FString segName = materialInfo . SegmentationType ; if ( segName . Equals ( " " ) || segName . Equals ( " " ) || segName . Equals ( " " ) || segName . Equals ( " " ) ) { materialName = " " ; } else if ( segName . Equals ( " " ) ) { return ; } else { if ( materialFullName . Contains ( " " ) ) { materialName = " " ; } else { return ; } } float alphaVal = 1.0f ; float transparency = 0.0f ; if ( ! materialInfo . TransparencyFactor . IsEmpty ( ) ) { transparency = FCString :: Atof ( * ( materialInfo . TransparencyFactor ) ) ; } baseMaterial = UMaterialImportHelpers :: FindExistingMaterialFromSearchLocation ( materialName , materialPath , EMaterialSearchLocation :: UnderParent , materialSearchError ) ; if ( ! materialSearchError . IsEmpty ( ) || baseMaterial == nullptr ) { UE_LOG ( RoadRunnerCarlaIntegration , Error , TEXT ( " " ) , * ( materialSearchError . ToString ( ) ) ) ; return ; } UMaterial * oldMat = LoadObject < UMaterial > ( nullptr , * basePackageName ) ; if ( oldMat != nullptr ) { TArray < UObject * > objectsToForceDelete ; objectsToForceDelete . Add ( oldMat ) ; ObjectTools :: ForceDeleteObjects ( objectsToForceDelete , false ) ; } UPackage * package = CreatePackage ( NULL , * basePackageName ) ; auto materialInstanceFactory = NewObject < UMaterialInstanceConstantFactoryNew > ( ) ; materialInstanceFactory -> InitialParent = baseMaterial ; UMaterialInstanceConstant * unrealMaterial = ( UMaterialInstanceConstant * ) materialInstanceFactory -> FactoryCreateNew ( UMaterialInstanceConstant :: StaticClass ( ) , package , * materialFullName , RF_Standalone | RF_Public , NULL , GWarn ) ; if ( unrealMaterial == NULL ) { UE_LOG ( RoadRunnerCarlaIntegration , Error , TEXT ( " " ) , * materialInfo . Name ) ; return ; } FAssetRegistryModule :: AssetCreated ( unrealMaterial ) ; package -> SetDirtyFlag ( true ) ; if ( materialName . Equals ( " " ) ) { FRoadRunnerImportUtil :: SetTextureParameter ( unrealMaterial , FName ( TEXT ( " " ) ) , sourceFilePath , materialInfo . DiffuseMap , packagePath ) ; FRoadRunnerImportUtil :: SetTextureParameter ( unrealMaterial , FName ( TEXT ( " " ) ) , sourceFilePath , materialInfo . NormalMap , packagePath ) ; FRoadRunnerImportUtil :: SetTextureParameter ( unrealMaterial , FName ( TEXT ( " " ) ) , sourceFilePath , materialInfo . SpecularMap , packagePath ) ; FStaticParameterSet matStaticParams ; unrealMaterial -> GetStaticParameterValues ( matStaticParams ) ; for ( auto & switchParam : matStaticParams . StaticSwitchParameters ) { if ( switchParam . ParameterInfo . Name . IsEqual ( " " ) ) { switchParam . bOverride = true ; switchParam . Value = true ; } } unrealMaterial -> UpdateStaticPermutation ( matStaticParams ) ; } else if ( materialName . Equals ( " " ) ) { FRoadRunnerImportUtil :: SetTextureParameter ( unrealMaterial , FName ( TEXT ( " " ) ) , sourceFilePath , materialInfo . DiffuseMap , packagePath ) ; FRoadRunnerImportUtil :: SetTextureParameter ( unrealMaterial , FName ( TEXT ( " " ) ) , sourceFilePath , materialInfo . NormalMap , packagePath ) ; #if ENGINE_MINOR_VERSION > 18 unrealMaterial -> SetVectorParameterValueEditorOnly ( FMaterialParameterInfo ( FName ( TEXT ( " " ) ) , EMaterialParameterAssociation :: GlobalParameter ) , FLinearColor ( 0.07f , 0.1f , 0.07f , 1 ) ) ; #else material -> SetVectorParameterValueEditorOnly ( FName ( TEXT ( " " ) ) , FLinearColor ( 0.07f , 0.1f , 0.07f , 1 ) ) ; #endif } else { return ; } unrealMaterial -> PreEditChange ( NULL ) ; unrealMaterial -> PostEditChange ( ) ; }
Create a material using CARLA's as a base to allow weather effects.
Create a material using CARLA ' s as a base to allow weather effects .
// Always use our materials for decals and markings // Find our base material to instance from // Road and sidewalk // Terrain // Just use RoadRunner materials // Props or foliage // Foliage // Hack to find leaves // Just use RoadRunner materials // Default to opaque // Get the original material package and delete it // Re-create package for new material instance // Create material instance from our base material // Modified: always create a material instance from our base material // Notify the asset registry // Set the dirty flag so this package will get saved later // Set material parameters // Leave out marking due to vertex coloring and fences // Disable multiple textures // Let the material update itself if necessary