// Copyright Epic Games, Inc. All Rights Reserved. #include "DatasmithFacadeMaterial.h" #include "DatasmithFacadeKeyValueProperty.h" #include "DatasmithFacadeScene.h" #include "DatasmithFacadeUEPbrMaterial.h" #include "DatasmithFacadeDecal.h" #include "DatasmithDefinitions.h" #include "DatasmithUtils.h" #include "Misc/Paths.h" FDatasmithFacadeBaseMaterial::FDatasmithFacadeBaseMaterial( const TSharedRef& BaseMaterialElement ) : FDatasmithFacadeElement( BaseMaterialElement ) { } FDatasmithFacadeBaseMaterial::EDatasmithMaterialType FDatasmithFacadeBaseMaterial::GetDatasmithMaterialType() const { return GetDatasmithMaterialType(GetDatasmithBaseMaterial()); } FDatasmithFacadeBaseMaterial::EDatasmithMaterialType FDatasmithFacadeBaseMaterial::GetDatasmithMaterialType( const TSharedRef& InMaterial ) { if (InMaterial->IsA( EDatasmithElementType::UEPbrMaterial )) { return EDatasmithMaterialType::UEPbrMaterial; } else if (InMaterial->IsA(EDatasmithElementType::MaterialInstance)) { return EDatasmithMaterialType::MaterialInstance; } else if (InMaterial->IsA(EDatasmithElementType::DecalMaterial)) { return EDatasmithMaterialType::DecalMaterial; } return EDatasmithMaterialType::Unsupported; } TSharedRef FDatasmithFacadeBaseMaterial::GetDatasmithBaseMaterial() const { return StaticCastSharedRef( InternalDatasmithElement ); } FDatasmithFacadeBaseMaterial* FDatasmithFacadeBaseMaterial::GetNewFacadeBaseMaterialFromSharedPtr( const TSharedPtr& InMaterial ) { if (InMaterial) { TSharedRef MaterialRef = InMaterial.ToSharedRef(); EDatasmithMaterialType MaterialType = GetDatasmithMaterialType(MaterialRef); switch (MaterialType) { case EDatasmithMaterialType::UEPbrMaterial: return new FDatasmithFacadeUEPbrMaterial(StaticCastSharedRef(MaterialRef)); case EDatasmithMaterialType::MaterialInstance: return new FDatasmithFacadeMaterialInstance(StaticCastSharedRef(MaterialRef)); case EDatasmithMaterialType::DecalMaterial: return new FDatasmithFacadeDecalMaterial(StaticCastSharedRef(MaterialRef)); case EDatasmithMaterialType::Unsupported: default: return nullptr; } } return nullptr; } FDatasmithFacadeMaterialInstance::FDatasmithFacadeMaterialInstance(const TCHAR* InElementName) : FDatasmithFacadeBaseMaterial( FDatasmithSceneFactory::CreateMaterialInstance(InElementName)) { TSharedPtr MaterialInstance = GetDatasmithMaterialInstance(); MaterialInstance->SetMaterialType(EDatasmithReferenceMaterialType::Opaque); } FDatasmithFacadeMaterialInstance::FDatasmithFacadeMaterialInstance(const TSharedRef& InMaterialRef) : FDatasmithFacadeBaseMaterial(InMaterialRef) {} FDatasmithFacadeMaterialInstance::EMaterialInstanceType FDatasmithFacadeMaterialInstance::GetMaterialType() const { return static_cast(GetDatasmithMaterialInstance()->GetMaterialType()); } void FDatasmithFacadeMaterialInstance::SetMaterialType( EMaterialInstanceType InMaterialInstanceType ) { GetDatasmithMaterialInstance()->SetMaterialType(static_cast(InMaterialInstanceType)); } FDatasmithFacadeMaterialInstance::EMaterialInstanceQuality FDatasmithFacadeMaterialInstance::GetQuality() const { return static_cast(GetDatasmithMaterialInstance()->GetQuality()); } void FDatasmithFacadeMaterialInstance::SetQuality( EMaterialInstanceQuality InQuality ) { GetDatasmithMaterialInstance()->SetQuality(static_cast(InQuality)); } const TCHAR* FDatasmithFacadeMaterialInstance::GetCustomMaterialPathName() const { return GetDatasmithMaterialInstance()->GetCustomMaterialPathName(); } void FDatasmithFacadeMaterialInstance::SetCustomMaterialPathName( const TCHAR* InPathName ) { GetDatasmithMaterialInstance()->SetCustomMaterialPathName(InPathName); } void FDatasmithFacadeMaterialInstance::AddColor( const TCHAR* InPropertyName, unsigned char InR, unsigned char InG, unsigned char InB, unsigned char InA ) { // Convert the sRGBA color to a Datasmith linear color. FLinearColor LinearColor(FColor(InR, InG, InB, InA)); // Add the Datasmith material linear color property. AddColor(InPropertyName, LinearColor.R, LinearColor.G, LinearColor.B, LinearColor.A); } void FDatasmithFacadeMaterialInstance::AddColor( const TCHAR* InPropertyName, float InR, float InG, float InB, float InA ) { FLinearColor LinearColor(InR, InG, InB, InA); // Create a new Datasmith material color property. TSharedPtr MaterialPropertyPtr = FDatasmithSceneFactory::CreateKeyValueProperty(InPropertyName); MaterialPropertyPtr->SetPropertyType(EDatasmithKeyValuePropertyType::Color); MaterialPropertyPtr->SetValue(*LinearColor.ToString()); // Add the new property to the Datasmith material properties. GetDatasmithMaterialInstance()->AddProperty(MaterialPropertyPtr); } void FDatasmithFacadeMaterialInstance::AddTexture( const TCHAR* InPropertyName, const FDatasmithFacadeTexture* InTexture ) { if (InTexture) { // Create a new Datasmith material texture property. TSharedPtr MaterialPropertyPtr = FDatasmithSceneFactory::CreateKeyValueProperty(InPropertyName); MaterialPropertyPtr->SetPropertyType(EDatasmithKeyValuePropertyType::Texture); MaterialPropertyPtr->SetValue(InTexture->GetName()); // Add the new property to the Datasmith material properties. GetDatasmithMaterialInstance()->AddProperty(MaterialPropertyPtr); } } void FDatasmithFacadeMaterialInstance::AddString( const TCHAR* InPropertyName, const TCHAR* InPropertyValue ) { if (!FString(InPropertyValue).IsEmpty()) { // Create a new Datasmith material string property. TSharedPtr MaterialPropertyPtr = FDatasmithSceneFactory::CreateKeyValueProperty(InPropertyName); MaterialPropertyPtr->SetPropertyType(EDatasmithKeyValuePropertyType::String); MaterialPropertyPtr->SetValue(InPropertyValue); // Add the new property to the array of Datasmith material properties. GetDatasmithMaterialInstance()->AddProperty(MaterialPropertyPtr); } } void FDatasmithFacadeMaterialInstance::AddFloat( const TCHAR* InPropertyName, float InPropertyValue ) { // Create a new Datasmith material float property. TSharedPtr MaterialPropertyPtr = FDatasmithSceneFactory::CreateKeyValueProperty(InPropertyName); MaterialPropertyPtr->SetPropertyType(EDatasmithKeyValuePropertyType::Float); MaterialPropertyPtr->SetValue(*FString::Printf(TEXT("%f"), InPropertyValue)); // Add the new property to the Datasmith material properties. GetDatasmithMaterialInstance()->AddProperty(MaterialPropertyPtr); } void FDatasmithFacadeMaterialInstance::AddBoolean( const TCHAR* InPropertyName, bool bInPropertyValue ) { // Create a new Datasmith material boolean property. TSharedPtr MaterialPropertyPtr = FDatasmithSceneFactory::CreateKeyValueProperty(InPropertyName); MaterialPropertyPtr->SetPropertyType(EDatasmithKeyValuePropertyType::Bool); MaterialPropertyPtr->SetValue(bInPropertyValue ? TEXT("True") : TEXT("False")); // Add the new property to the Datasmith material properties. GetDatasmithMaterialInstance()->AddProperty(MaterialPropertyPtr); } int32 FDatasmithFacadeMaterialInstance::GetPropertiesCount() const { return GetDatasmithMaterialInstance()->GetPropertiesCount(); } FDatasmithFacadeKeyValueProperty* FDatasmithFacadeMaterialInstance::GetNewProperty( int32 PropertyIndex ) const { if (const TSharedPtr& Property = GetDatasmithMaterialInstance()->GetProperty(PropertyIndex)) { return new FDatasmithFacadeKeyValueProperty(Property.ToSharedRef()); } else { return nullptr; } } FDatasmithFacadeKeyValueProperty* FDatasmithFacadeMaterialInstance::GetNewPropertyByName( const TCHAR* PropertyName ) const { if (const TSharedPtr& Property = GetDatasmithMaterialInstance()->GetPropertyByName(PropertyName)) { return new FDatasmithFacadeKeyValueProperty(Property.ToSharedRef()); } else { return nullptr; } } TSharedRef FDatasmithFacadeMaterialInstance::GetDatasmithMaterialInstance() const { return StaticCastSharedRef( InternalDatasmithElement ); }