You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
7.2 KiB
C++
152 lines
7.2 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
|
|
#include "MaterialConverterSystemComponent.h"
|
|
|
|
#include <AssetBuilderSDK/AssetBuilderSDK.h>
|
|
#include <AzCore/Math/Color.h>
|
|
#include <AzCore/Serialization/EditContext.h>
|
|
#include <AzCore/Serialization/SerializeContext.h>
|
|
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
|
|
|
|
namespace AZ
|
|
{
|
|
namespace Render
|
|
{
|
|
void MaterialConverterSystemComponent::Reflect(AZ::ReflectContext* context)
|
|
{
|
|
if (auto* serialize = azrtti_cast<SerializeContext*>(context))
|
|
{
|
|
serialize->Class<MaterialConverterSystemComponent, Component>()
|
|
->Version(3)
|
|
->Attribute(Edit::Attributes::SystemComponentTags, AZStd::vector<Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }));
|
|
}
|
|
}
|
|
|
|
void MaterialConverterSystemComponent::Activate()
|
|
{
|
|
RPI::MaterialConverterBus::Handler::BusConnect();
|
|
}
|
|
|
|
void MaterialConverterSystemComponent::Deactivate()
|
|
{
|
|
RPI::MaterialConverterBus::Handler::BusDisconnect();
|
|
}
|
|
|
|
bool MaterialConverterSystemComponent::ConvertMaterial(
|
|
const AZ::SceneAPI::DataTypes::IMaterialData& materialData, RPI::MaterialSourceData& sourceData)
|
|
{
|
|
using namespace AZ::RPI;
|
|
|
|
// The source data for generating material asset
|
|
sourceData.m_materialType = GetMaterialTypePath();
|
|
|
|
auto handleTexture = [&materialData, &sourceData](
|
|
const char* propertyTextureGroup, SceneAPI::DataTypes::IMaterialData::TextureMapType textureType) {
|
|
MaterialSourceData::PropertyMap& properties = sourceData.m_properties[propertyTextureGroup];
|
|
const AZStd::string& texturePath = materialData.GetTexture(textureType);
|
|
|
|
// Check to see if the image asset exists. If not, skip this texture map and just disable it.
|
|
bool assetFound = false;
|
|
if (!texturePath.empty())
|
|
{
|
|
using namespace AzToolsFramework;
|
|
AZ::Data::AssetInfo sourceInfo;
|
|
AZStd::string watchFolder;
|
|
AssetSystemRequestBus::BroadcastResult(
|
|
assetFound, &AssetSystem::AssetSystemRequest::GetSourceInfoBySourcePath, texturePath.c_str(), sourceInfo,
|
|
watchFolder);
|
|
}
|
|
|
|
if (assetFound)
|
|
{
|
|
properties["textureMap"].m_value = texturePath;
|
|
}
|
|
else if (!texturePath.empty())
|
|
{
|
|
AZ_Warning("AtomFeatureCommon", false, "Could not find asset '%s' for '%s'", texturePath.c_str(), propertyTextureGroup);
|
|
}
|
|
};
|
|
|
|
// If PBR material properties aren't in use, fall back to legacy properties. Don't do that if some PBR material properties are set, though.
|
|
bool anyPBRInUse = false;
|
|
|
|
handleTexture("specularF0", SceneAPI::DataTypes::IMaterialData::TextureMapType::Specular);
|
|
handleTexture("normal", SceneAPI::DataTypes::IMaterialData::TextureMapType::Normal);
|
|
AZStd::optional<bool> useColorMap = materialData.GetUseColorMap();
|
|
// If the useColorMap property exists, this is a PBR material and the color should be set to baseColor.
|
|
if (useColorMap.has_value())
|
|
{
|
|
anyPBRInUse = true;
|
|
handleTexture("baseColor", SceneAPI::DataTypes::IMaterialData::TextureMapType::BaseColor);
|
|
}
|
|
else
|
|
{
|
|
// If it doesn't have the useColorMap property, then it's a non-PBR material and the baseColor
|
|
// texture needs to be set to the diffuse texture.
|
|
handleTexture("baseColor", SceneAPI::DataTypes::IMaterialData::TextureMapType::Diffuse);
|
|
}
|
|
|
|
auto toColor = [](const AZ::Vector3& v) { return AZ::Color::CreateFromVector3AndFloat(v, 1.0f); };
|
|
|
|
AZStd::optional<AZ::Vector3> baseColor = materialData.GetBaseColor();
|
|
if (baseColor.has_value())
|
|
{
|
|
anyPBRInUse = true;
|
|
sourceData.m_properties["baseColor"]["color"].m_value = toColor(baseColor.value());
|
|
}
|
|
|
|
sourceData.m_properties["opacity"]["factor"].m_value = materialData.GetOpacity();
|
|
|
|
auto applyOptionalPropertiesFunc = [&sourceData, &anyPBRInUse](const auto& propertyGroup, const auto& propertyName, const auto& propertyOptional)
|
|
{
|
|
// Only set PBR settings if they were specifically set in the scene's data.
|
|
// Otherwise, leave them unset so the data driven default properties are used.
|
|
if (propertyOptional.has_value())
|
|
{
|
|
anyPBRInUse = true;
|
|
sourceData.m_properties[propertyGroup][propertyName].m_value = propertyOptional.value();
|
|
}
|
|
};
|
|
|
|
handleTexture("metallic", SceneAPI::DataTypes::IMaterialData::TextureMapType::Metallic);
|
|
applyOptionalPropertiesFunc("metallic", "factor", materialData.GetMetallicFactor());
|
|
applyOptionalPropertiesFunc("metallic", "useTexture", materialData.GetUseMetallicMap());
|
|
|
|
handleTexture("roughness", SceneAPI::DataTypes::IMaterialData::TextureMapType::Roughness);
|
|
applyOptionalPropertiesFunc("roughness", "factor", materialData.GetRoughnessFactor());
|
|
applyOptionalPropertiesFunc("roughness", "useTexture", materialData.GetUseRoughnessMap());
|
|
|
|
handleTexture("emissive", SceneAPI::DataTypes::IMaterialData::TextureMapType::Emissive);
|
|
sourceData.m_properties["emissive"]["color"].m_value = toColor(materialData.GetEmissiveColor());
|
|
applyOptionalPropertiesFunc("emissive", "intensity", materialData.GetEmissiveIntensity());
|
|
applyOptionalPropertiesFunc("emissive", "useTexture", materialData.GetUseEmissiveMap());
|
|
|
|
handleTexture("ambientOcclusion", SceneAPI::DataTypes::IMaterialData::TextureMapType::AmbientOcclusion);
|
|
applyOptionalPropertiesFunc("ambientOcclusion", "useTexture", materialData.GetUseAOMap());
|
|
|
|
if (!anyPBRInUse)
|
|
{
|
|
// If it doesn't have the useColorMap property, then it's a non-PBR material and the baseColor
|
|
// texture needs to be set to the diffuse color.
|
|
sourceData.m_properties["baseColor"]["color"].m_value = toColor(materialData.GetDiffuseColor());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const char* MaterialConverterSystemComponent::GetMaterialTypePath() const
|
|
{
|
|
return "Materials/Types/StandardPBR.materialtype";
|
|
}
|
|
}
|
|
}
|